summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2015-05-07 17:09:28 +0200
committerAleš Smodiš <aless@guru.si>2015-08-11 14:26:03 +0200
commit162bb2c59b9b3faa7a161e192c1821602d2ab326 (patch)
treeaf19b73f55939c60bfeec57352e9a087c79617aa
parenta101310e4c0b3e07d9f35ef9b28647709ae2960c (diff)
Improve error & warning reporting
Hope it does not break anything.
-rwxr-xr-xmonkey/monkey.py5
-rw-r--r--prolog/engine.py20
2 files changed, 10 insertions, 15 deletions
diff --git a/monkey/monkey.py b/monkey/monkey.py
index 61ff573..99f5a2a 100755
--- a/monkey/monkey.py
+++ b/monkey/monkey.py
@@ -11,9 +11,8 @@ from .util import PQueue
# Check whether all tests for problem [name] succeed.
def test(name, code):
try:
- reply = prolog.engine.create(
- code=code,
- query='run_tests({})'.format(name))
+ reply, output = prolog.engine.create(
+ code=code, query='run_tests({})'.format(name))
return reply.get('event') == 'success'
except Exception as ex:
return False
diff --git a/prolog/engine.py b/prolog/engine.py
index 96191ae..b3cd805 100644
--- a/prolog/engine.py
+++ b/prolog/engine.py
@@ -17,15 +17,12 @@ def create(code='', query=''):
opts = {'format': 'json-html', 'destroy': True, 'src_text': code}
if query:
opts['ask'] = query
- reply, messages = request('POST', '/pengine/create', body=json.dumps(opts))
-
- if reply.get('event') != 'create' or 'error' in messages:
- raise Exception('\n'.join(messages['error']))
+ reply, output = request('POST', '/pengine/create', body=json.dumps(opts))
# If query was given, the actual reply is nested in create/destroy objects.
if query:
reply = reply['answer']['data']
- return reply
+ return reply, output
def ask(engine, query):
return send(engine, 'ask(({}),[])'.format(query))
@@ -44,14 +41,13 @@ def send(engine, event):
'id': engine,
'event': event,
'format': 'json-html'})
- reply, messages = request('GET', '/pengine/send?' + params)
- return reply
+ return request('GET', '/pengine/send?' + params)
# Return the main reply and pull potential output replies.
address, port = 'localhost', 3030 # TODO put this somewhere sane
def request(method, path, body=None):
headers = {'Content-Type': 'application/json;charset=utf-8'}
- messages = collections.defaultdict(list)
+ messages = []
try:
conn = http.client.HTTPConnection(address, port, timeout=10)
conn.request(method, path, body, headers=headers)
@@ -62,8 +58,7 @@ def request(method, path, body=None):
reply = json.loads(response.read().decode('utf-8'))
if reply.get('event') == 'output':
- msg_type, msg_data = get_message(reply)
- messages[msg_type].append(msg_data)
+ messages.append(get_message(reply))
# Request next reply.
params = urllib.parse.urlencode({
@@ -79,7 +74,8 @@ def request(method, path, body=None):
def get_message(reply):
data = strip_html(reply['data']).strip()
message = ''
- if reply['message'] == 'error':
+ if reply['message'] in ('error', 'warning'):
+ message = reply['message'] + ': '
if 'location' in reply:
loc = reply['location']
message += 'near line ' + str(loc['line'])
@@ -96,7 +92,7 @@ def get_message(reply):
elif reply.get('code') == 'type_error':
match = re.match(r'^.*(Type error: [^\n]*)', data, flags=re.DOTALL)
data = match.group(1)
- message += data
+ message += data
# Replace anonymous variable names with _.
message = re.sub(r'_G[0-9]*', '_', message)