From 49b0844d8140a27885c0d47ffe2d1b0c7279380c Mon Sep 17 00:00:00 2001 From: androiddrew Date: Thu, 5 Jul 2018 11:07:20 -0400 Subject: [PATCH] Clean up and refactoring of code and style I didn't do much to the functionality of the code, however I did make style changes that I believe will make the project more readable. --- MANIFEST.in | 1 + cookie_api/app.py | 21 +++++++--------- cookie_api/logger.py | 2 +- cookie_api/models.py | 2 -- cookie_api/resources/cookies.py | 2 +- cookie_api/{static => templates}/index.html | 2 +- cookie_api/util/__init__.py | 3 +-- cookie_api/util/app.py | 28 --------------------- cookie_api/util/hook.py | 3 ++- dev_requirements.txt | 11 +++++--- requirements.in | 8 ++++++ requirements.txt | 21 +++++++++------- setup.py | 14 +++++------ wsgi.py | 6 ++--- 14 files changed, 53 insertions(+), 71 deletions(-) rename cookie_api/{static => templates}/index.html (92%) delete mode 100644 cookie_api/util/app.py create mode 100644 requirements.in diff --git a/MANIFEST.in b/MANIFEST.in index ff2579c..43f3450 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ +include wsgi recursive-include cookie_api/templates * recursive-include cookie_api/static * \ No newline at end of file diff --git a/cookie_api/app.py b/cookie_api/app.py index 5f8190b..73245e4 100644 --- a/cookie_api/app.py +++ b/cookie_api/app.py @@ -1,5 +1,4 @@ -from apistar import Route -from apistar.http import HTMLResponse +from apistar import Route, App from apistar_jwt import JWT from apistar_mail import MailComponent import logbook @@ -7,9 +6,9 @@ from sqlalchemy import create_engine from pathlib import Path from cookie_api.auth import auth_routes -from cookie_api.logger import global_init +from cookie_api.logger import init_logging from cookie_api.resources import cookie_routes -from cookie_api.util import SQLAlchemyHook, SQLAlchemySession, MetaApp as App +from cookie_api.util import SQLAlchemyHook, SQLAlchemySession BASEDIR = Path(__file__).parent TEMPLATE_DIR = BASEDIR.joinpath('templates') @@ -17,18 +16,15 @@ STATIC_DIR = BASEDIR.joinpath('static') logger = logbook.Logger(__name__) -engine = create_engine('postgresql://apistar@localhost:5432/apistar') - app_settings = { "LOGGING": { - "LEVEL": "DEBUG" + "LEVEL": "INFO" } } -def return_index_html(): - with open(STATIC_DIR.joinpath('index.html'), mode='rb') as f: - return HTMLResponse(content=f.read()) +def return_index_html(app: App): + return app.render_template('index.html') index = Route('/', 'GET', return_index_html) @@ -38,7 +34,7 @@ _routes = cookie_routes + auth_routes + [index] _hooks = [SQLAlchemyHook] _components = [ - SQLAlchemySession(engine=engine), + SQLAlchemySession(engine=create_engine('postgresql://apistar@localhost:5432/apistar')), JWT({ 'JWT_USER_ID': 'sub', 'JWT_SECRET': 'thisisasecret', @@ -60,7 +56,8 @@ def application_factory(routes=_routes, components=_components, hooks=_hooks, se """Returns an instance of Cookie API""" _settings = {**app_settings, **settings} - global_init(_settings) + init_logging(_settings) + logger.debug("Template directory {}".format(template_dir)) logger.debug("Static directory {}".format(STATIC_DIR)) diff --git a/cookie_api/logger.py b/cookie_api/logger.py index 302faa4..5282f2f 100644 --- a/cookie_api/logger.py +++ b/cookie_api/logger.py @@ -2,7 +2,7 @@ import sys import logbook -def global_init(settings={}): +def init_logging(settings={}): _logging_setting = settings.get("LOGGING", {"LEVEL": logbook.TRACE}) _log_file = _logging_setting.get("LOG_FILE") diff --git a/cookie_api/models.py b/cookie_api/models.py index e36bc97..72f7c1c 100644 --- a/cookie_api/models.py +++ b/cookie_api/models.py @@ -10,12 +10,10 @@ from sqlalchemy.types import DateTime as DateTimeType BCRYPT_LOG_ROUNDS = 11 -# can be moved to models util? class utcnow(expression.FunctionElement): type = DateTimeType() -# Can be moved to the models util? @compiles(utcnow, 'postgresql') def pg_utcnow(element, compiler, **kw): return "TIMEZONE('utc', CURRENT_TIMESTAMP)" diff --git a/cookie_api/resources/cookies.py b/cookie_api/resources/cookies.py index 73f6864..2528946 100644 --- a/cookie_api/resources/cookies.py +++ b/cookie_api/resources/cookies.py @@ -1,7 +1,7 @@ import typing import logbook -from apistar import http, Route, App +from apistar import Route, App from apistar_jwt import authentication_required, JWTUser from sqlalchemy.orm import Session diff --git a/cookie_api/static/index.html b/cookie_api/templates/index.html similarity index 92% rename from cookie_api/static/index.html rename to cookie_api/templates/index.html index 6e8b546..9ebef88 100644 --- a/cookie_api/static/index.html +++ b/cookie_api/templates/index.html @@ -11,7 +11,7 @@
diff --git a/cookie_api/util/__init__.py b/cookie_api/util/__init__.py index 25ca187..015585a 100644 --- a/cookie_api/util/__init__.py +++ b/cookie_api/util/__init__.py @@ -1,5 +1,4 @@ -from .app import MetaApp from .http import MetaJSONResponse, ExtJSONResponse from .component import SQLAlchemySession, DBSession from .hook import SQLAlchemyHook -from .validators import Decimal \ No newline at end of file +from .validators import Decimal diff --git a/cookie_api/util/app.py b/cookie_api/util/app.py deleted file mode 100644 index aba9e2a..0000000 --- a/cookie_api/util/app.py +++ /dev/null @@ -1,28 +0,0 @@ -import sys - -from apistar import App, exceptions -from apistar.http import Response, HTMLResponse -from apistar.server.components import ReturnValue -from .http import MetaJSONResponse - - -class MetaApp(App): - """ - A WSGI App subclass with a MetaJSONResponse default response type - """ - - def render_response(self, return_value: ReturnValue) -> Response: - if isinstance(return_value, Response): - return return_value - elif isinstance(return_value, str): - return HTMLResponse(return_value) - return MetaJSONResponse(return_value) - - def exception_handler(self, exc: Exception) -> Response: - if isinstance(exc, exceptions.HTTPException): - - return MetaJSONResponse(exc.detail, status_code=exc.status_code, headers=exc.get_headers()) - raise - - def error_handler(self) -> Response: - return MetaJSONResponse('Server error', status_code=500, exc_info=sys.exc_info()) \ No newline at end of file diff --git a/cookie_api/util/hook.py b/cookie_api/util/hook.py index 7e3344b..c94c05b 100644 --- a/cookie_api/util/hook.py +++ b/cookie_api/util/hook.py @@ -1,6 +1,7 @@ from apistar.http import Response from .component import Session, DBSession + class SQLAlchemyHook: def on_request(self, session: Session): return @@ -12,4 +13,4 @@ class SQLAlchemyHook: def on_error(self, session: Session, response: Response): session.rollback() DBSession.remove() - return response \ No newline at end of file + return response diff --git a/dev_requirements.txt b/dev_requirements.txt index f00f3ab..1af3f59 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -4,7 +4,10 @@ # # pip-compile --output-file dev_requirements.txt dev_requirements.in # -alembic==0.9.9 +--trusted-host pypi.python.org +--trusted-host git.aigalatic.com + +alembic==0.9.10 atomicwrites==1.1.5 # via pytest attrs==18.1.0 # via pytest coverage==4.5.1 # via pytest-cov @@ -12,12 +15,12 @@ mako==1.0.7 # via alembic markupsafe==1.0 # via mako more-itertools==4.2.0 # via pytest pluggy==0.6.0 # via pytest, tox -py==1.5.3 # via pytest, tox +py==1.5.4 # via pytest, tox pytest-cov==2.5.1 -pytest==3.6.1 +pytest==3.6.3 python-dateutil==2.7.3 # via alembic python-editor==1.0.3 # via alembic six==1.11.0 # via more-itertools, pytest, python-dateutil, tox -sqlalchemy==1.2.8 # via alembic +sqlalchemy==1.2.9 # via alembic tox==3.0.0 virtualenv==16.0.0 # via tox diff --git a/requirements.in b/requirements.in new file mode 100644 index 0000000..fe0ef7b --- /dev/null +++ b/requirements.in @@ -0,0 +1,8 @@ +apistar +apistar-jwt +apistar-mail +bcrypt +psycopg2-binary +itsdangerous +logbook +sqlalchemy \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 99e8a43..a41d2a7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,28 +2,31 @@ # This file is autogenerated by pip-compile # To update, run: # -# pip-compile --output-file requirements.txt setup.py +# pip-compile --output-file requirements.txt requirements.in # -apistar-jwt==0.4.2 +--trusted-host pypi.python.org +--trusted-host git.aigalatic.com + +apistar-jwt==0.5.0 apistar-mail==0.3.0 -apistar==0.5.18 +apistar==0.5.40 bcrypt==3.1.4 certifi==2018.4.16 # via requests cffi==1.11.5 # via bcrypt chardet==3.0.4 # via requests click==6.7 # via apistar -idna==2.6 # via requests +idna==2.7 # via requests itsdangerous==0.24 jinja2==2.10 # via apistar -logbook==1.3.3 +logbook==1.4.0 markupsafe==1.0 # via jinja2 -psycopg2-binary==2.7.4 +psycopg2-binary==2.7.5 pycparser==2.18 # via cffi pyjwt==1.6.4 # via apistar-jwt pyyaml==3.12 # via apistar -requests==2.18.4 # via apistar +requests==2.19.1 # via apistar six==1.11.0 # via bcrypt -sqlalchemy==1.2.8 -urllib3==1.22 # via requests +sqlalchemy==1.2.9 +urllib3==1.23 # via requests werkzeug==0.14.1 # via apistar whitenoise==3.3.1 # via apistar diff --git a/setup.py b/setup.py index 2c66302..222cf6d 100644 --- a/setup.py +++ b/setup.py @@ -4,14 +4,14 @@ with open('README.md') as readme_file: readme = readme_file.read() requirements = [ - 'apistar-jwt==0.4.2', + 'apistar-jwt==0.5.0', 'apistar-mail==0.3.0', - 'apistar==0.5.18', + 'apistar==0.5.40', 'bcrypt==3.1.4', 'itsdangerous==0.24', - 'logbook==1.3.3', - 'psycopg2-binary==2.7.4', - 'sqlalchemy==1.2.8', + 'logbook==1.4.0', + 'psycopg2-binary==2.7.5', + 'sqlalchemy==1.2.9', ] test_requirements = [ @@ -40,7 +40,7 @@ setup( 'Development Status :: 2 - Pre-Alpha', 'Environment :: Web Environment', 'Intended Audience :: Developers', - 'License :: OSI Approved :: BSD License', + 'License :: OSI Approved :: MIT License', 'Natural Language :: English', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', @@ -50,7 +50,7 @@ setup( }, entry_points={ 'console_scripts': [ - 'wsgi_runner=wsgi:main' + 'wsgi_serve=wsgi:main' ] } ) diff --git a/wsgi.py b/wsgi.py index e49a085..907ae87 100644 --- a/wsgi.py +++ b/wsgi.py @@ -12,7 +12,7 @@ from sqlalchemy import create_engine from cookie_api import application_factory from cookie_api.util import SQLAlchemySession, SQLAlchemyHook -from config import db_config, jwt_config, mail_config +from config import db_config, jwt_config, mail_config, logging_config components = [ SQLAlchemySession(create_engine(db_config)), @@ -24,7 +24,7 @@ hooks = [ SQLAlchemyHook() ] -app = application_factory(components=components, hooks=hooks) +app = application_factory(components=components, hooks=hooks, settings={**logging_config}) def main(): @@ -32,4 +32,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main()