diff --git a/.travis.yml b/.travis.yml index c12794a..2d35087 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: python python: - - "2.7" + - "3.5" install: - pip install -r ./config/requirements.txt script: diff --git a/Dockerfile b/Dockerfile index f191b8d..a3af770 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:2 +FROM python:3.5 WORKDIR /usr/src/app diff --git a/config/requirements.txt b/config/requirements.txt index 32da6ce..6ea2610 100644 --- a/config/requirements.txt +++ b/config/requirements.txt @@ -4,4 +4,4 @@ PyJWT==1.6.1 psycopg2-binary==2.7.5 uri==2.0.0 urllib3==1.24.2 -web.py==0.39 +web.py==0.40-dev1 diff --git a/src/api.py b/src/api.py index 248c42f..a7d08a6 100644 --- a/src/api.py +++ b/src/api.py @@ -139,7 +139,7 @@ def build_parms(filters): raise Error(BADFILTERS, msg="Unknown filter '%s'" % (p)) process = {"work_type": types, "uri_scheme": schemes, "canonical": canoncl} - for key, values in process.items(): + for key, values in list(process.items()): if len(values) > 0: try: andclause, ops = build_clause(key, values) diff --git a/src/aux.py b/src/aux.py index 028c2cf..e988602 100644 --- a/src/aux.py +++ b/src/aux.py @@ -17,7 +17,7 @@ def logger_instance(name): def strtolist(data): - if isinstance(data, basestring) or isinstance(data, dict): + if isinstance(data, str) or isinstance(data, dict): return [data] - elif type(data) is list: + elif isinstance(data, list): return data diff --git a/src/errors.py b/src/errors.py index 41b17d6..1fbfd40 100644 --- a/src/errors.py +++ b/src/errors.py @@ -65,7 +65,10 @@ def __init__(self, level=DEFAULT, msg='', data=[]): httpcode = self.get_code(level) headers = {'Content-Type': 'application/json'} message = self.get_message(level) - params = web.input() if web.input() else web.data() + if web.ctx.env.get('REQUEST_METHOD', '') == 'GET': + params = web.input() + else: + params = web.data().decode('utf-8') output = json.dumps( self.make_output(httpcode, message, msg, params, data)) diff --git a/src/models.py b/src/models.py index 38a6537..80aaf8d 100644 --- a/src/models.py +++ b/src/models.py @@ -1,11 +1,13 @@ +import web import uuid import psycopg2 -from aux import logger_instance +from aux import logger_instance, debug_mode from api import db from uri import URI from errors import Error, FATAL logger = logger_instance(__name__) +web.config.debug = debug_mode() class Work(object): @@ -421,10 +423,8 @@ def results_to_works(results, include_relatives=False): titles = [] # temporary array of work titles uris = [] # temp array of work URIs (strings, used for comparison) uris_fmt = [] # temporary array of work URIs (Identifier objects) - last = len(results) - 1 - i = 0 - for e in results: + for i, e in enumerate(results): if i == 0: # we can't do cur=results[0] outsise--it moves IterBetter's pointer cur = e @@ -449,14 +449,17 @@ def results_to_works(results, include_relatives=False): uris_fmt.append({"uri_scheme": e["uri_scheme"], "uri_value": e["uri_value"], "canonical": e["canonical"]}) - if i == last: - cur["titles"] = titles - cur["URI"] = uris_fmt - work = result_to_work(cur) - work.URI = results_to_identifiers(cur["URI"]) - if include_relatives: - work.load_children() - work.load_parents() - data.append(work.__dict__) - i += 1 + try: + cur["titles"] = titles + cur["URI"] = uris_fmt + work = result_to_work(cur) + work.URI = results_to_identifiers(cur["URI"]) + if include_relatives: + work.load_children() + work.load_parents() + data.append(work.__dict__) + except NameError: + # we need to run the above with the last element of IterBetter, if it + # fails it means that no results were iterated + pass return data diff --git a/src/relationsctrl.py b/src/relationsctrl.py index 444c02f..ca35727 100644 --- a/src/relationsctrl.py +++ b/src/relationsctrl.py @@ -22,9 +22,9 @@ def GET(self, name): @check_token def POST(self, name): """Create a work relation""" - logger.debug("Data: %s" % (web.data())) + logger.debug("Data: %s" % (web.data().decode('utf-8'))) - data = json.loads(web.data()) + data = json.loads(web.data().decode('utf-8')) parent_uuid = data.get('parent_UUID') or data.get('parent_uuid') child_uuid = data.get('child_UUID') or data.get('child_uuid') diff --git a/src/titlesctrl.py b/src/titlesctrl.py index 8b077ef..3d93680 100644 --- a/src/titlesctrl.py +++ b/src/titlesctrl.py @@ -24,9 +24,9 @@ def GET(self, name): @check_token def POST(self, name): """Add titles to an existing work""" - logger.debug("Data: %s" % (web.data())) + logger.debug("Data: %s" % (web.data().decode('utf-8'))) - data = json.loads(web.data()) + data = json.loads(web.data().decode('utf-8')) title = data.get('title') work_id = data.get('UUID') or data.get('uuid') diff --git a/src/translator.py b/src/translator.py index 1f9d69a..7eea732 100644 --- a/src/translator.py +++ b/src/translator.py @@ -1,5 +1,7 @@ import web -import urllib +import urllib.parse +import urllib.error +import urllib.request from aux import logger_instance, debug_mode from api import build_parms, json_response, api_response, check_token from errors import Error, BADPARAMS, NORESULT, NOTALLOWED, \ @@ -41,7 +43,7 @@ def GET(self, name): scheme, value = Identifier.split_uri(uri) assert scheme and value if title: - title = urllib.unquote(title.strip()) + title = urllib.parse.unquote(title.strip()) assert title assert uri or title except BaseException: @@ -60,7 +62,7 @@ def GET(self, name): if not results: raise Error(NORESULT) - return self.process_results(results, strict) + return self.process_results(list(results), strict) def POST(self, name): raise Error(NOTALLOWED) diff --git a/src/urisctrl.py b/src/urisctrl.py index 829d6a3..853bc47 100644 --- a/src/urisctrl.py +++ b/src/urisctrl.py @@ -22,9 +22,9 @@ def GET(self, name): @check_token def POST(self, name): """Add identifiers to an existing work""" - logger.debug("Data: %s" % (web.data())) + logger.debug("Data: %s" % (web.data().decode('utf-8'))) - data = json.loads(web.data()) + data = json.loads(web.data().decode('utf-8')) uri = data.get('URI') or data.get('uri') canonical = data.get('canonical') in (True, "true", "True") work_id = data.get('UUID') or data.get('uuid') diff --git a/src/worksctrl.py b/src/worksctrl.py index c6cb8ce..d4e0124 100644 --- a/src/worksctrl.py +++ b/src/worksctrl.py @@ -57,9 +57,9 @@ def GET(self, name): @check_token def POST(self, name): """Create a work""" - logger.debug("Data: %s" % (web.data())) + logger.debug("Data: %s" % (web.data().decode('utf-8'))) - data = json.loads(web.data()) + data = json.loads(web.data().decode('utf-8')) wtype = data.get('type') title = data.get('title') uri = data.get('URI') or data.get('uri') @@ -78,7 +78,7 @@ def POST(self, name): try: assert WorkType(wtype).exists() except AssertionError: - t = wtype if type(wtype) == str else "" + t = wtype if isinstance(wtype, str) else "" raise Error(BADPARAMS, msg="Unknown work type '%s'" % (t)) for i in uris: