From 631dc761c6f757e2336b5c75cf93018e303a1dd2 Mon Sep 17 00:00:00 2001 From: odecif Date: Sat, 18 Nov 2023 02:52:39 +0100 Subject: [PATCH] Major overhaul * Now using Blueprints instead of importing modules. * Now using def create_app instead of just app --- .gitignore | 2 + {api => bcnsGDSAPI}/api.conf | 0 {api => bcnsGDSAPI}/api.py | 0 bcnsGDSAPI/devapi.conf | 12 +++ {api => bcnsGDSAPI}/modules/db_connect.py | 0 {api => bcnsGDSAPI}/modules/functions.py | 0 {api => bcnsGDSAPI}/modules/game.py | 0 {api => bcnsGDSAPI}/modules/gamelist.py | 0 .../modules/gamelist_functions.py | 0 {api => bcnsGDSAPI}/requirements.txt | 0 bcnsGDSSite/__init__.py | 86 +++++++++++++++++++ bcnsGDSSite/devsite.conf | 11 +++ {site => bcnsGDSSite}/index.py | 0 {site => bcnsGDSSite}/language/en_US.json | 0 {site => bcnsGDSSite}/language/sv_SE.json | 0 {site => bcnsGDSSite}/modules/game.py | 26 +++--- {site => bcnsGDSSite}/modules/gamelist.py | 20 ++--- bcnsGDSSite/modules/init.py | 42 +++++++++ {site => bcnsGDSSite}/requirements.txt | 0 {site => bcnsGDSSite}/site.conf | 0 {site => bcnsGDSSite}/static/css/template.css | 0 {site => bcnsGDSSite}/templates/error.html | 0 {site => bcnsGDSSite}/templates/game.html | 8 +- {site => bcnsGDSSite}/templates/gamelist.html | 6 +- {site => bcnsGDSSite}/templates/home.html | 0 .../templates/navigation.html | 2 +- site/modules/init.py | 54 ------------ 27 files changed, 186 insertions(+), 83 deletions(-) rename {api => bcnsGDSAPI}/api.conf (100%) rename {api => bcnsGDSAPI}/api.py (100%) create mode 100644 bcnsGDSAPI/devapi.conf rename {api => bcnsGDSAPI}/modules/db_connect.py (100%) rename {api => bcnsGDSAPI}/modules/functions.py (100%) rename {api => bcnsGDSAPI}/modules/game.py (100%) rename {api => bcnsGDSAPI}/modules/gamelist.py (100%) rename {api => bcnsGDSAPI}/modules/gamelist_functions.py (100%) rename {api => bcnsGDSAPI}/requirements.txt (100%) create mode 100644 bcnsGDSSite/__init__.py create mode 100644 bcnsGDSSite/devsite.conf rename {site => bcnsGDSSite}/index.py (100%) rename {site => bcnsGDSSite}/language/en_US.json (100%) rename {site => bcnsGDSSite}/language/sv_SE.json (100%) rename {site => bcnsGDSSite}/modules/game.py (79%) rename {site => bcnsGDSSite}/modules/gamelist.py (81%) create mode 100644 bcnsGDSSite/modules/init.py rename {site => bcnsGDSSite}/requirements.txt (100%) rename {site => bcnsGDSSite}/site.conf (100%) rename {site => bcnsGDSSite}/static/css/template.css (100%) rename {site => bcnsGDSSite}/templates/error.html (100%) rename {site => bcnsGDSSite}/templates/game.html (81%) rename {site => bcnsGDSSite}/templates/gamelist.html (72%) rename {site => bcnsGDSSite}/templates/home.html (100%) rename {site => bcnsGDSSite}/templates/navigation.html (79%) delete mode 100644 site/modules/init.py diff --git a/.gitignore b/.gitignore index ec5d4af..e9eb2e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.venv/ +instance/ __pycache__/ *.py[cod] api/devapi.conf diff --git a/api/api.conf b/bcnsGDSAPI/api.conf similarity index 100% rename from api/api.conf rename to bcnsGDSAPI/api.conf diff --git a/api/api.py b/bcnsGDSAPI/api.py similarity index 100% rename from api/api.py rename to bcnsGDSAPI/api.py diff --git a/bcnsGDSAPI/devapi.conf b/bcnsGDSAPI/devapi.conf new file mode 100644 index 0000000..1ed0a41 --- /dev/null +++ b/bcnsGDSAPI/devapi.conf @@ -0,0 +1,12 @@ +[Database] +db_type = MySQL +host = localhost +user = odecif +pass = SDR:0508 +db_name = gamedistrosys +contentpath = /home/odecif/projects/bcns-gameDistribution_testcontent/ +nfo_suffix = _index.nfo + +[Running] +host = localhost +port = 5501 diff --git a/api/modules/db_connect.py b/bcnsGDSAPI/modules/db_connect.py similarity index 100% rename from api/modules/db_connect.py rename to bcnsGDSAPI/modules/db_connect.py diff --git a/api/modules/functions.py b/bcnsGDSAPI/modules/functions.py similarity index 100% rename from api/modules/functions.py rename to bcnsGDSAPI/modules/functions.py diff --git a/api/modules/game.py b/bcnsGDSAPI/modules/game.py similarity index 100% rename from api/modules/game.py rename to bcnsGDSAPI/modules/game.py diff --git a/api/modules/gamelist.py b/bcnsGDSAPI/modules/gamelist.py similarity index 100% rename from api/modules/gamelist.py rename to bcnsGDSAPI/modules/gamelist.py diff --git a/api/modules/gamelist_functions.py b/bcnsGDSAPI/modules/gamelist_functions.py similarity index 100% rename from api/modules/gamelist_functions.py rename to bcnsGDSAPI/modules/gamelist_functions.py diff --git a/api/requirements.txt b/bcnsGDSAPI/requirements.txt similarity index 100% rename from api/requirements.txt rename to bcnsGDSAPI/requirements.txt diff --git a/bcnsGDSSite/__init__.py b/bcnsGDSSite/__init__.py new file mode 100644 index 0000000..952e2f3 --- /dev/null +++ b/bcnsGDSSite/__init__.py @@ -0,0 +1,86 @@ +from datetime import date +from flask import render_template, Flask, request +from json import JSONEncoder +from bcnsGDSSite.modules.gamelist import gamelist +from bcnsGDSSite.modules.game import game +import bcnsGDSSite.modules.init +import os +import inspect +import json +import glob +import tomllib + + +class MyJSONEncoder(JSONEncoder): + def default(self, o): + if isinstance(o, date): + return o.isoformat() + + return super().default(o) + + +def create_app(test_config=None): + app = Flask(__name__, instance_relative_config=True) + app.register_blueprint(gamelist) + app.register_blueprint(game) + print(app.url_map) + app.config.from_mapping( + SECRET_KEY='dev', + DATABASE=os.path.join(app.instance_path, 'bcns_gds.sqlite'), + ) + + app.config['JSON_AS_ASCII'] = False + + if test_config is None: + # load the instance config, if it exists, when not testing + app.config.from_file("config.toml", load=tomllib.load, text=False) + else: + # load the test config if passed in + app.config.from_mapping(test_config) + + # ensure the instance folder exists + try: + os.makedirs(app.instance_path) + except OSError: + pass + + +# import bcnsGDSSite.modules.gamelist # noqa: E402 + bcnsGDSSite.modules.init.set_apihost("http://" + + app.config['API']['host'] + + ':' + app.config['API']['port']) + + bcnsGDSSite.modules.init.set_applanguage( + app.config['MAIN']['app_language']) + languages = bcnsGDSSite.modules.init.get_languages() + if not languages: + languages_path = os.path.dirname( + os.path.abspath(inspect.getfile( + inspect.currentframe()))) + language_list = glob.glob(languages_path + "/language/*.json") + print("### Loaded languages") + for language in language_list: + filename = os.path.basename(language) + lang_code = filename.split('.')[0] + with open(language, 'r', encoding='utf-8') as file: + print("# ", lang_code) + languages[lang_code] = json.loads(file.read()) + bcnsGDSSite.modules.init.set_languages(language) + + # Check if valid language code is set. If not, return app default + def lang(lang_code): + if lang_code not in languages: + return "en_US" + return lang_code + + @app.route("/", methods=["GET", "POST"]) + @app.route("//", methods=["GET", "POST"]) + def home(lang_code=False): + lang_code = lang(lang_code) + if request.form: + lang_code = request.form["language_select"] + return render_template('home.html', **languages[lang_code], + lang_code=lang_code, + languages=languages.keys()) + + return app diff --git a/bcnsGDSSite/devsite.conf b/bcnsGDSSite/devsite.conf new file mode 100644 index 0000000..404616d --- /dev/null +++ b/bcnsGDSSite/devsite.conf @@ -0,0 +1,11 @@ +[Main] +hostip = 172.29.254.97 +debug = True +host = localhost +port = 5500 +contentpath = /home/odecif/project/bcns-gameDistribution_testcontent + +[API] +#host = localhost +host = 172.29.29.51 +port = 5501 diff --git a/site/index.py b/bcnsGDSSite/index.py similarity index 100% rename from site/index.py rename to bcnsGDSSite/index.py diff --git a/site/language/en_US.json b/bcnsGDSSite/language/en_US.json similarity index 100% rename from site/language/en_US.json rename to bcnsGDSSite/language/en_US.json diff --git a/site/language/sv_SE.json b/bcnsGDSSite/language/sv_SE.json similarity index 100% rename from site/language/sv_SE.json rename to bcnsGDSSite/language/sv_SE.json diff --git a/site/modules/game.py b/bcnsGDSSite/modules/game.py similarity index 79% rename from site/modules/game.py rename to bcnsGDSSite/modules/game.py index 7cdbfc1..91b5228 100644 --- a/site/modules/game.py +++ b/bcnsGDSSite/modules/game.py @@ -1,13 +1,16 @@ -from __main__ import app import json import requests import base64 -from flask import render_template, request, Response -import modules.init +from flask import render_template, request, Response, Blueprint +import bcnsGDSSite.modules.init -host_endpoint = modules.init.host_endpoint() -languages = modules.init.get_languages() -app_language = modules.init.app_language +host_endpoint = bcnsGDSSite.modules.init.get_apihost() +languages = bcnsGDSSite.modules.init.get_languages() +app_language = bcnsGDSSite.modules.init.app_language + + +game = Blueprint('game', __name__, + template_folder='templates') # Check if valid language code is set. If not, return app default @@ -18,8 +21,8 @@ def lang(lang_code): # Show game -@app.route("//game") -def game(lang_code): +@game.route("//game") +def showgame(lang_code): lang_code = lang(lang_code) gamepath = request.args.get("gamepath") @@ -38,13 +41,14 @@ def game(lang_code): # Show game artwork -@app.route("//game/artwork") +@game.route("//game/artwork") def artwork(lang_code): pass # Download a game -@app.route("//game/download") +#@app.route("//game/download") +@game.route("//game/download") def download(lang_code): gamepath = request.args.get("gamepath") gametitle = request.args.get("gametitle") @@ -66,7 +70,7 @@ def download(lang_code): # Download file from backend. Could be documents, patches etc. # gamepath full path to current games NFO-file # filepath path from gamepath to the file in question -@app.route("//game/getfile") +@game.route("//game/getfile") def getfile(lang_code): gamepath = request.args.get("gamepath") filepath = request.args.get("filepath") diff --git a/site/modules/gamelist.py b/bcnsGDSSite/modules/gamelist.py similarity index 81% rename from site/modules/gamelist.py rename to bcnsGDSSite/modules/gamelist.py index 3b6a20c..61a5dbc 100644 --- a/site/modules/gamelist.py +++ b/bcnsGDSSite/modules/gamelist.py @@ -1,14 +1,15 @@ -from __main__ import app import json import requests -from flask import render_template, redirect, url_for, request -import modules.init +from flask import render_template, redirect, url_for, request, Blueprint +import bcnsGDSSite.modules.init import base64 -host_endpoint = modules.init.host_endpoint() -languages = modules.init.get_languages() -app_language = modules.init.app_language +host_endpoint = bcnsGDSSite.modules.init.get_apihost() +languages = bcnsGDSSite.modules.init.get_languages() +app_language = bcnsGDSSite.modules.init.app_language +gamelist = Blueprint('gamelist', __name__, + template_folder='templates') # Check if valid language code is set. If not, return app default def lang(lang_code): @@ -18,9 +19,8 @@ def lang(lang_code): # Show gamelist -@app.route("//gamelist", methods=["GET", "POST"]) -# @app.route("/gamelist") -def gamelist(lang_code): +@gamelist.route("//gamelist", methods=["GET", "POST"]) +def showgamelist(lang_code): lang_code = lang(lang_code) thumbnails = request.args.get("thumbnails") glist = None @@ -66,7 +66,7 @@ def gamelist(lang_code): # Update/Refresh the gamelist by re-scanning the game archive (slow) -@app.route("//gamelist/update") +@gamelist.route("//gamelist/update") def gamelist_update(lang_code): lang_code = lang(lang_code) response = requests.get( diff --git a/bcnsGDSSite/modules/init.py b/bcnsGDSSite/modules/init.py new file mode 100644 index 0000000..ece5a0e --- /dev/null +++ b/bcnsGDSSite/modules/init.py @@ -0,0 +1,42 @@ +import os +import inspect + +global languages +languages = {} + +global apihost +apihost = "http://media.odecif.net:5501" + +global app_language +app_language = "" + +def set_languages(languages_dict): + global languages + languages = languages_dict + return True + + +def get_languages(): + global languages + return languages + + +def set_apihost(new_apihost): + global apihost + apihost = new_apihost + return apihost + + +def get_apihost(): + global apihost + return apihost + + +def set_applanguage(new_applanguage): + global app_language + app_language = new_applanguage + return app_language + +def init(): + global apihost + apihost = "" diff --git a/site/requirements.txt b/bcnsGDSSite/requirements.txt similarity index 100% rename from site/requirements.txt rename to bcnsGDSSite/requirements.txt diff --git a/site/site.conf b/bcnsGDSSite/site.conf similarity index 100% rename from site/site.conf rename to bcnsGDSSite/site.conf diff --git a/site/static/css/template.css b/bcnsGDSSite/static/css/template.css similarity index 100% rename from site/static/css/template.css rename to bcnsGDSSite/static/css/template.css diff --git a/site/templates/error.html b/bcnsGDSSite/templates/error.html similarity index 100% rename from site/templates/error.html rename to bcnsGDSSite/templates/error.html diff --git a/site/templates/game.html b/bcnsGDSSite/templates/game.html similarity index 81% rename from site/templates/game.html rename to bcnsGDSSite/templates/game.html index 303cd55..32c3858 100644 --- a/site/templates/game.html +++ b/bcnsGDSSite/templates/game.html @@ -18,7 +18,7 @@ {% if game.game.documents %} {% for document in game.game.documents %} {% if document.type == 'manual' %} - {{lang_game_get_manual}} {{document.path}}
+ {{lang_game_get_manual}} {{document.path}}
{% endif %} {% endfor %} {% else %} @@ -26,15 +26,15 @@ {% endif %}

{{lang_game_download_header}}

{{lang_game_download_ingress}}

- - + +

{{lang_game_download_magnet_link}}

diff --git a/site/templates/gamelist.html b/bcnsGDSSite/templates/gamelist.html similarity index 72% rename from site/templates/gamelist.html rename to bcnsGDSSite/templates/gamelist.html index 441288e..af39a9e 100644 --- a/site/templates/gamelist.html +++ b/bcnsGDSSite/templates/gamelist.html @@ -9,7 +9,7 @@ {% block content %}

{{lang_gamelist_body_header}}

{{lang_gamelist_body_ingress}}

- +
{{lang_game_artwork}}
{% for part in game.game.artwork %} - {{part.type}} + {{part.type}} {% endfor %}
@@ -23,12 +23,12 @@ {% for thumbnail in thumbnails %} {% if thumbnail.nfo == item.nfo %} - + {% endif %} {% else %} {% endfor %} - + diff --git a/site/templates/home.html b/bcnsGDSSite/templates/home.html similarity index 100% rename from site/templates/home.html rename to bcnsGDSSite/templates/home.html diff --git a/site/templates/navigation.html b/bcnsGDSSite/templates/navigation.html similarity index 79% rename from site/templates/navigation.html rename to bcnsGDSSite/templates/navigation.html index b46c3e0..074ec3f 100644 --- a/site/templates/navigation.html +++ b/bcnsGDSSite/templates/navigation.html @@ -13,7 +13,7 @@ diff --git a/site/modules/init.py b/site/modules/init.py deleted file mode 100644 index 0e08907..0000000 --- a/site/modules/init.py +++ /dev/null @@ -1,54 +0,0 @@ -import argparse -import configparser -import os -import inspect - -global languages -languages = {} - - -def set_languages(languages_dict): - global languages - languages = languages_dict - return True - - -def get_languages(): - global languages - return languages - - -def init(): - global config - global app_language - - app_language = 'en_US' - # Decide what config-file to use - parser = argparse.ArgumentParser() - parser.add_argument('-e', '--environment', choices=['dev', 'prod', 'test'], - default='prod', - help='choose what environment type to run.' - ' Defaults to prod') - - args = parser.parse_args() - - if args.environment == 'dev': - print("Using devsite.conf") - configfile = '/../devsite.conf' - elif args.environment == 'test': - print("Using testsite.conf") - configfile = '/../testsite.conf' - else: - print("Using site.conf") - configfile = '/../site.conf' - - config = configparser.RawConfigParser() - config.read(os.path.dirname( - os.path.abspath(inspect.getfile( - inspect.currentframe()))) + configfile) - - -# Constructor for the API-endpoint -def host_endpoint(): - return ('http://' + config.get('API', 'host') + ':' + - config.get('API', 'port'))
{{lang_game_artwork}}
{{item.game.title}}{{item.game.title}} {{item.game.year}} {{item.game.genre}} {{item.game.developer}}