linuxgaming

This is my own Linux gaming aggregate web app I built for personal use.
git clone git://git.beardyjay.co.uk/linuxgaming
Log | Files | Refs | README

commit b49b0953cbbc0c9364b3ba53a090e2fface86cbf
parent 1a952985649dee92fcbfa5603faea6bc315e54f6
Author: Jay Scott <jay@beardyjay.co.uk>
Date:   Wed, 18 Jul 2018 21:43:03 +0100

refactoring

Diffstat:
Mlinuxgaming/__init__.py | 18+++++++++++-------
Mlinuxgaming/database.py | 33++++++++++++++++++++++++++-------
Mlinuxgaming/util.py | 22+++++++++++++---------
3 files changed, 50 insertions(+), 23 deletions(-)

diff --git a/linuxgaming/__init__.py b/linuxgaming/__init__.py @@ -1,8 +1,12 @@ +""" +Main application + +""" +from datetime import datetime, timedelta from flask import render_template, Flask from flask_compress import Compress from flask_pymongo import PyMongo from flask_htmlmin import HTMLMIN -from datetime import datetime, timedelta import dateutil.parser from . import update @@ -10,18 +14,18 @@ from . import details from . import search from . import database -compress = Compress() +COMPRESS = Compress() def create_app(): - # create and configure the app + """ Create the Flask application """ app = Flask(__name__, static_url_path='/static') app.config.from_object('config') # page performance tweeks app.config['MINIFY_PAGE'] = True - compress.init_app(app) + COMPRESS.init_app(app) HTMLMIN(app) # db init @@ -50,7 +54,7 @@ def create_app(): @app.errorhandler(404) def page_not_found(page): - app.logger.info('page not found') + app.logger.info('page not found %s', page) return render_template( "message.html", msg="I think you are lost!"), 404 @@ -58,7 +62,7 @@ def create_app(): def _jinja2_filter_datetime(date): date = dateutil.parser.parse(str(date)) native = date.replace(tzinfo=None) - format = '%a %d %b %X %Y' - return native.strftime(format) + new_format = '%a %d %b %X %Y' + return native.strftime(new_format) return app diff --git a/linuxgaming/database.py b/linuxgaming/database.py @@ -1,23 +1,42 @@ +""" +Database helpers + +""" + from flask import current_app, abort -def find_all(query={}): +def find_all(query=None): + """ + return mongodb cursor results from a find query + + :param query: the mongodb query + :return: mongoDB cursor + """ try: - d = current_app.mongo.db.items.find(query).sort('date', -1) - except Exception as e: - current_app.logger.error('DB replace error %s', e) + data = current_app.mongo.db.items.find(query).sort('date', -1) + except RuntimeError as error: + current_app.logger.error('DB replace error %s', error) abort(500) - return d + return data def replace_one(query, data, upsert=True): + """ + Replace one document helper + + :param query: the mongodb query + :param data: data to replace/insert + :param upsert: insert if dociment doesn't exist + :return: boolean + """ try: current_app.mongo.db.items.replace_one(query, data, upsert) - except Exception as e: - current_app.logger.error('DB replace error %s', e) + except RuntimeError as error: + current_app.logger.error('DB replace error %s', error) abort(500) return True diff --git a/linuxgaming/util.py b/linuxgaming/util.py @@ -1,18 +1,22 @@ +import json import yaml import feedparser -import json import requests from flask import (current_app, abort) def load_yaml(): - """Return the YAML parsed config file.""" + """ + parse the configuration file + + :return: dict of contents + """ try: with open('config/feed_config.yaml', 'r') as ymlfile: cfg = yaml.load(ymlfile) - except (yaml.YAMLError, FileNotFoundError) as e: - current_app.logger.error('YAML read error %s', e) + except (yaml.YAMLError, FileNotFoundError) as error: + current_app.logger.error('YAML read error %s', error) abort(500) return cfg @@ -25,8 +29,8 @@ def feed_parse(url): if res.entries: return res.entries - else: - current_app.logger.error('FEED parse error %s', url) + + current_app.logger.error('FEED parse error %s', url) return None @@ -37,7 +41,7 @@ def get_gog_info(query): response = requests.get(gog_api_url + query) - if response.status_code == 200: - return json.loads(response.content.decode('utf-8')) - else: + if response.status_code != 200: abort(500) + + return json.loads(response.content.decode('utf-8'))