diff --git a/.travis.yml b/.travis.yml index 17c0776..5b7907e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ install: - make dependencies script: + - make lint - make test - make coverage_publish - make coverage_report diff --git a/Makefile b/Makefile index 5469bf1..bafd245 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,11 @@ coverage_publish: coverage_report: pipenv run python -m coverage report --fail-under 90 +lint: + pipenv run pycodestyle stw_potsdam tests + clean: pipenv run python -m coverage erase rm -rf .pytest_cache .cache -.PHONY: dependencies run debug test test_debug coverage_publish coverage_report clean +.PHONY: dependencies run debug test test_debug coverage_publish coverage_report lint clean diff --git a/Pipfile b/Pipfile index f8381f2..19ccb00 100644 --- a/Pipfile +++ b/Pipfile @@ -8,3 +8,5 @@ pytest = "*" coveralls = "*" pytest-cov = "*" httpretty = "*" +pycodestyle = "*" + diff --git a/Pipfile.lock b/Pipfile.lock index 2a878bb..3551593 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "7ba9cecb08807ab00a9a85ce40a9f66e5088b1f51b683c09b7be92ca55792ab3" + "sha256": "7d175b6dd121a91ba3e19ab1d19cf724275d1de5bb682dba5ea0d337fc011ba4" }, "pipfile-spec": 6, "requires": {}, @@ -345,6 +345,14 @@ ], "version": "==1.7.0" }, + "pycodestyle": { + "hashes": [ + "sha256:cbc619d09254895b0d12c2c691e237b2e91e9b2ecf5e84c26b35400f93dcfb83", + "sha256:cbfca99bd594a10f674d0cd97a3d802a1fdef635d4361e1a2658de47ed261e3a" + ], + "index": "pypi", + "version": "==2.4.0" + }, "pycparser": { "hashes": [ "sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3" diff --git a/stw_potsdam/canteen.py b/stw_potsdam/canteen.py index e716e0c..07f85d8 100644 --- a/stw_potsdam/canteen.py +++ b/stw_potsdam/canteen.py @@ -2,4 +2,5 @@ from collections import namedtuple -Canteen = namedtuple('Canteen', ('key', 'name', 'street', 'city', 'id', 'chash')) +Canteen = namedtuple('Canteen', + ('key', 'name', 'street', 'city', 'id', 'chash')) diff --git a/stw_potsdam/canteen_api.py b/stw_potsdam/canteen_api.py index da4be6a..8f97216 100644 --- a/stw_potsdam/canteen_api.py +++ b/stw_potsdam/canteen_api.py @@ -6,7 +6,8 @@ from collections import namedtuple MenuParams = namedtuple('MenuParams', ('canteen_id', 'chash')) -URL = 'https://www.studentenwerk-potsdam.de/essen/unsere-mensen-cafeterien/detailinfos/' +URL = 'https://www.studentenwerk-potsdam.de' + \ + '/essen/unsere-mensen-cafeterien/detailinfos/' def _param_json(it): diff --git a/stw_potsdam/feed.py b/stw_potsdam/feed.py index 443ba46..b83e751 100644 --- a/stw_potsdam/feed.py +++ b/stw_potsdam/feed.py @@ -33,8 +33,10 @@ def _prices(offer): price = offer[api_role] # When no price is set, this can be empty dict - if (isinstance(price, unicode) or isinstance(price, str)) and price.strip(): - result[role] = str(price) # Convert unicode to str for PyOpenMensa -> misses type check + if (isinstance(price, unicode) or isinstance(price, str)) \ + and price.strip(): + # Convert unicode to str for PyOpenMensa -> misses type check + result[role] = str(price) return result @@ -65,6 +67,14 @@ def render_meta(canteen, menu_feed_url): builder.address = canteen.street builder.city = canteen.city - builder.define(name="full", priority="0", url=menu_feed_url, source=None, dayOfWeek="*", dayOfMonth="*", hour="8-18", minute="0", retry="30 1") + builder.define(name='full', + priority='0', + url=menu_feed_url, + source=None, + dayOfWeek='*', + dayOfMonth='*', + hour='8-18', + minute='0', + retry='30 1') return builder.toXMLFeed() diff --git a/stw_potsdam/views.py b/stw_potsdam/views.py index 8da10e7..7aa97a2 100644 --- a/stw_potsdam/views.py +++ b/stw_potsdam/views.py @@ -15,7 +15,7 @@ CACHE_TIMEOUT = 45 * 60 app = Flask(__name__) app.url_map.strict_slashes = False -if 'BASE_URL' in os.environ: # pragma: no cover +if 'BASE_URL' in os.environ: # pragma: no cover base_url = urlparse.urlparse(os.environ.get('BASE_URL')) if base_url.scheme: app.config['PREFERRED_URL_SCHEME'] = base_url.scheme @@ -30,8 +30,8 @@ cache = SimpleCache() def canteen_not_found(config, canteen_name): app.logger.warn('Canteen %s not found', canteen_name) configured = ', '.join("'{}'".format(c) for c in config.keys()) - message = "Canteen '{canteen}' not found, available: {configured}".format(canteen=canteen_name, - configured=configured) + message = "Canteen '{0}' not found, available: {1}".format(canteen_name, + configured) return make_response(message, 404) @@ -62,7 +62,9 @@ def canteen_menu_feed_xml(menu): def canteen_meta_feed_xml(canteen): - menu_feed_url = url_for('canteen_menu_feed', canteen_name=canteen.key, _external=True) + menu_feed_url = url_for('canteen_menu_feed', + canteen_name=canteen.key, + _external=True) xml = feed.render_meta(canteen, menu_feed_url) return _canteen_feed_xml(xml) @@ -95,7 +97,10 @@ def canteen_menu_feed(canteen_name): @app.route('/canteens') def canteen_index(): config = read_canteen_config() - return jsonify({key: url_for('canteen_meta_feed', canteen_name=key, _external=True) for key in config}) + return jsonify({ + key: url_for('canteen_meta_feed', canteen_name=key, _external=True) + for key in config + }) @app.route('/health_check') diff --git a/tests/response_util.py b/tests/response_util.py index fd90948..5e7047c 100644 --- a/tests/response_util.py +++ b/tests/response_util.py @@ -5,9 +5,11 @@ from xml.etree import ElementTree def meal_names(response): """Extract meal names from OpenMensa XML. - By no means below parsing is robust or complete. Below just helps ensuring that the parser indeed returns proper XML - instead of arbitrary responses.""" + By no means below parsing is robust or complete. Below just helps ensuring + that the parser indeed returns proper XML instead of arbitrary responses. + """ root = ElementTree.fromstring(response) namespace = {'om': 'http://openmensa.org/open-mensa-v2'} - nodes = root.findall('om:canteen/om:day/om:category/om:meal/om:name', namespace) + nodes = root.findall('om:canteen/om:day/om:category/om:meal/om:name', + namespace) return [node.text for node in nodes] diff --git a/tests/stub_api.py b/tests/stub_api.py index 9e899dc..1e4fd5e 100644 --- a/tests/stub_api.py +++ b/tests/stub_api.py @@ -21,7 +21,8 @@ def api_online_one_shot(): Subsequent API invocations will return with HTTP status code 500.""" def canned_menu(request, uri, response_headers): - path = os.path.join(os.path.dirname(__file__), 'resources', 'input.json') + path = os.path.join(os.path.dirname(__file__), + 'resources', 'input.json') with open(path) as f: return 200, response_headers, f.read() @@ -30,7 +31,10 @@ def api_online_one_shot(): httpretty.Response(body='invalid', status=500), ] - httpretty.register_uri(httpretty.POST, canteen_api.URL, responses=responses) + httpretty.register_uri(httpretty.POST, + canteen_api.URL, + responses=responses) + httpretty.enable(allow_net_connect=False) yield httpretty httpretty.disable() diff --git a/tests/test_retrieval.py b/tests/test_retrieval.py index 8d2fb9a..b8c82be 100644 --- a/tests/test_retrieval.py +++ b/tests/test_retrieval.py @@ -26,12 +26,14 @@ def is_enabled(): requires_online_api = pytest.mark.skipif( not is_enabled(), - reason='Querying the online API is disabled. Turn on by setting env variable %s.' % ENV_ENABLED + reason='Querying the online API is disabled. ' + 'Turn on by setting env variable %s.' % ENV_ENABLED ) @requires_online_api def test_retrieval(canteen): feed.render_meta(canteen, "/canteens/{}/menu".format(canteen.key)) - menu = download_menu(MenuParams(canteen_id=canteen.id, chash=canteen.chash)) + params = MenuParams(canteen_id=canteen.id, chash=canteen.chash) + menu = download_menu(params) feed.render_menu(menu) diff --git a/tests/test_views.py b/tests/test_views.py index f06d335..421623f 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -24,7 +24,10 @@ def test_index(client): assert canteen.status_code == 200, 'Canteen URL is reachable' -@pytest.mark.parametrize('url', ['/canteens/spam', '/canteens/spam/meta', '/canteens/spam/menu']) +@pytest.mark.parametrize('url', [ + '/canteens/spam', + '/canteens/spam/meta', + '/canteens/spam/menu']) def test_canteen_not_found(client, url): response = client.get(url) assert response.status_code == 404 @@ -56,8 +59,8 @@ def _request_check_meals(client): response = client.get('/canteens/griebnitzsee/menu') assert response.status_code == 200 - meals = meal_names(response.data) - assert meals[0] == u"Gefüllter Germknödel \nmit Vanillesauce und Mohnzucker" + meal = meal_names(response.data)[0] + assert meal == u"Gefüllter Germknödel \nmit Vanillesauce und Mohnzucker" @pytest.fixture