From ae3efa44bad4cf748938a15c514f63d3d5b9b78c Mon Sep 17 00:00:00 2001 From: Edwin Henneken Date: Tue, 12 Jan 2021 16:26:21 +0000 Subject: [PATCH 1/3] stage 1 port to Python3 --- .gitignore | 2 +- graphics_service/app.py | 3 ++- graphics_service/graphics.py | 3 ++- graphics_service/models.py | 2 +- graphics_service/tests/test_endpoint.py | 3 ++- graphics_service/views.py | 3 ++- requirements.txt | 1 + 7 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8da0560..31ca274 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *.py[co] - +*.bak # Packages *.egg *.egg-info diff --git a/graphics_service/app.py b/graphics_service/app.py index 0328587..61029a2 100644 --- a/graphics_service/app.py +++ b/graphics_service/app.py @@ -1,6 +1,7 @@ +from __future__ import absolute_import from flask import Flask from werkzeug.serving import run_simple -from views import Graphics +from .views import Graphics from flask_restful import Api from flask_discoverer import Discoverer from adsmutils import ADSFlask diff --git a/graphics_service/graphics.py b/graphics_service/graphics.py index 31b1c0e..1d67f82 100644 --- a/graphics_service/graphics.py +++ b/graphics_service/graphics.py @@ -3,13 +3,14 @@ @author: ehenneken ''' +from __future__ import absolute_import import sys import os import simplejson as json import random from flask import current_app #from models import AlchemyEncoder, GraphicsModel -from models import get_graphics_record +from .models import get_graphics_record #from sqlalchemy.orm.exc import NoResultFound thumb_link = '' + \ diff --git a/graphics_service/models.py b/graphics_service/models.py index 2cb21e7..23246d4 100644 --- a/graphics_service/models.py +++ b/graphics_service/models.py @@ -59,6 +59,6 @@ def get_graphics_record(bibcode): res = execute_SQL_query(bibcode) except NoResultFound: res = {'Error': 'Unable to get results!', 'Error Info': 'No database entry found for %s' % bibcode} - except Exception, err: + except Exception as err: res = {'Error': 'Unable to get results!', 'Error Info': 'Graphics query failed for %s: %s'%(bibcode, err)} return res diff --git a/graphics_service/tests/test_endpoint.py b/graphics_service/tests/test_endpoint.py index ce39d03..5985a1f 100644 --- a/graphics_service/tests/test_endpoint.py +++ b/graphics_service/tests/test_endpoint.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import os from flask_testing import TestCase @@ -112,7 +113,7 @@ def test_query_no_record(self, mock_execute_SQL_query): r = self.client.get(url) self.assertTrue(r.status_code == 200) expected = {u'Error Info': u'No database entry found for foo', u'Error': u'Unable to get results!'} - print r.json + print(r.json) self.assertTrue(r.json == expected) @mock.patch('graphics_service.models.execute_SQL_query', return_value={'Error': 'error', 'Error Info': 'info'}) diff --git a/graphics_service/views.py b/graphics_service/views.py index df0e067..afc33d4 100644 --- a/graphics_service/views.py +++ b/graphics_service/views.py @@ -1,7 +1,8 @@ +from __future__ import absolute_import from flask import current_app from flask_restful import Resource from flask_discoverer import advertise -from graphics import get_graphics +from .graphics import get_graphics import time class Graphics(Resource): diff --git a/requirements.txt b/requirements.txt index 13821c0..ef60b61 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ git+https://github.com/adsabs/ADSMicroserviceUtils.git@v1.1.9 alembic==0.8.9 +future==0.18.2 psycopg2==2.8.3 simplejson==3.10.0 From 7e4a6b5074370cf4426663481da8567467542cf6 Mon Sep 17 00:00:00 2001 From: Edwin Henneken Date: Tue, 12 Jan 2021 16:37:17 +0000 Subject: [PATCH 2/3] stage 2 port to Python 3 --- dev-requirements.txt | 15 +++++++++------ graphics_service/graphics.py | 3 +-- graphics_service/tests/test_endpoint.py | 5 +++-- graphics_service/tests/test_internals.py | 2 +- graphics_service/tests/test_webservices.py | 7 ++++--- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 379cfe7..fedfa9d 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,7 +1,10 @@ -pytest==2.8.2 -coveralls==1.1.0 -httpretty==0.8.10 -mock==1.3.0 -coverage==4.3.4 -pytest-cov==2.2.0 +coveralls==1.8.2 +coverage==4.5.4 Flask-Testing==0.8.0 +httpretty==0.8.10; python_version < '3.0' +httpretty==1.0.5; python_version > '3.0' +mock==1.3.0 +pytest==2.8.2; python_version < '3.0' +pytest==5.1.0; python_version > '3.0' +pytest-cov==2.2.0; python_version < '3.0' +pytest-cov==2.7.1; python_version > '3.0' diff --git a/graphics_service/graphics.py b/graphics_service/graphics.py index 1d67f82..5698a54 100644 --- a/graphics_service/graphics.py +++ b/graphics_service/graphics.py @@ -37,8 +37,7 @@ def get_graphics(bibcode): source = results.get('source', 'NA') results['ADSlink'] = [] if not eprint: - results['figures'] = filter( - lambda a: a['figure_label'] != None, results['figures']) + results['figures'] = [a for a in results['figures'] if a['figure_label'] != None] display_figure = random.choice(results['figures']) results['pick'] = '' results['number'] = 0 diff --git a/graphics_service/tests/test_endpoint.py b/graphics_service/tests/test_endpoint.py index 5985a1f..421f02e 100644 --- a/graphics_service/tests/test_endpoint.py +++ b/graphics_service/tests/test_endpoint.py @@ -1,4 +1,5 @@ from __future__ import print_function +from builtins import map import sys import os from flask_testing import TestCase @@ -55,9 +56,9 @@ def test_data_model(self): bc = Column(Boolean) jc = Column(postgresql.JSON) dc = Column(DateTime) - cols_expect = map( + cols_expect = list(map( type, [ic.type, sc.type, sc.type, sc.type, bc.type, - jc.type, dc.type]) + jc.type, dc.type])) self.assertEqual([type(c.type) for c in GraphicsModel.__table__.columns], cols_expect) diff --git a/graphics_service/tests/test_internals.py b/graphics_service/tests/test_internals.py index 113e8dd..2767db2 100644 --- a/graphics_service/tests/test_internals.py +++ b/graphics_service/tests/test_internals.py @@ -37,7 +37,7 @@ def test_config_values(self): required = ["GRAPHICS_INCLUDE_ARXIV", "SQLALCHEMY_DATABASE_URI", "DISCOVERER_PUBLISH_ENDPOINT", "DISCOVERER_SELF_PUBLISH"] - missing = [x for x in required if x not in self.app.config.keys()] + missing = [x for x in required if x not in list(self.app.config.keys())] self.assertTrue(len(missing) == 0) def test_json_encoder(self): diff --git a/graphics_service/tests/test_webservices.py b/graphics_service/tests/test_webservices.py index b8a77f1..4e89188 100644 --- a/graphics_service/tests/test_webservices.py +++ b/graphics_service/tests/test_webservices.py @@ -1,3 +1,4 @@ +from past.builtins import basestring import sys import os from flask_testing import TestCase @@ -38,12 +39,12 @@ def test_ResourcesRoute(self): for expected_field, _type in {'scopes': list, 'methods': list, 'description': basestring, - 'rate_limit': list}.iteritems(): + 'rate_limit': list}.items(): # Assert each resource is described has the expected_field - [self.assertIn(expected_field, v) for v in r.json.values()] + [self.assertIn(expected_field, v) for v in list(r.json.values())] # Assert every expected_field has the proper type [self.assertIsInstance(v[expected_field], _type) - for v in r.json.values()] + for v in list(r.json.values())] if __name__ == '__main__': From 7b9ebbc30788ef145c5d89ee774b2042c13d9fc1 Mon Sep 17 00:00:00 2001 From: Edwin Henneken Date: Tue, 19 Jan 2021 08:10:55 -0500 Subject: [PATCH 3/3] PR comments implementation --- .travis.yml | 2 +- graphics_service/tests/test_internals.py | 2 +- graphics_service/tests/test_webservices.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6dd401c..00befa7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: python python: - - "2.7" + - "3.8" install: - "pip install --upgrade setuptools pip" diff --git a/graphics_service/tests/test_internals.py b/graphics_service/tests/test_internals.py index 2767db2..113e8dd 100644 --- a/graphics_service/tests/test_internals.py +++ b/graphics_service/tests/test_internals.py @@ -37,7 +37,7 @@ def test_config_values(self): required = ["GRAPHICS_INCLUDE_ARXIV", "SQLALCHEMY_DATABASE_URI", "DISCOVERER_PUBLISH_ENDPOINT", "DISCOVERER_SELF_PUBLISH"] - missing = [x for x in required if x not in list(self.app.config.keys())] + missing = [x for x in required if x not in self.app.config.keys()] self.assertTrue(len(missing) == 0) def test_json_encoder(self): diff --git a/graphics_service/tests/test_webservices.py b/graphics_service/tests/test_webservices.py index 4e89188..30ae04a 100644 --- a/graphics_service/tests/test_webservices.py +++ b/graphics_service/tests/test_webservices.py @@ -41,10 +41,10 @@ def test_ResourcesRoute(self): 'description': basestring, 'rate_limit': list}.items(): # Assert each resource is described has the expected_field - [self.assertIn(expected_field, v) for v in list(r.json.values())] + [self.assertIn(expected_field, v) for v in r.json.values()] # Assert every expected_field has the proper type [self.assertIsInstance(v[expected_field], _type) - for v in list(r.json.values())] + for v in r.json.values()] if __name__ == '__main__':