From ea14442b059f9683599363b5ea16477c1cd2c0ed Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Fri, 7 Jun 2019 10:16:25 +0200 Subject: [PATCH] Move everything into a blueprint --- .gitignore | 1 + main.py | 59 ++++++++++++++++++++++------------------ templates/base.html | 2 +- templates/baseindex.html | 2 +- templates/group.html | 2 +- templates/groups.html | 2 +- templates/layout.html | 2 +- templates/navbar.html | 18 ++++++------ templates/outofdate.html | 6 ++-- templates/package.html | 2 +- templates/queue.html | 2 +- templates/search.html | 4 +-- 12 files changed, 54 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index 5c591a1..90c9612 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules package-lock.json *srcinfo.json +.mypy_cache diff --git a/main.py b/main.py index 71543ad..e94d5d4 100755 --- a/main.py +++ b/main.py @@ -42,7 +42,7 @@ from typing import List, Set, Dict, Tuple, Optional, Generator, Any import requests from flask import Flask, render_template, request, url_for, redirect, \ - make_response + make_response, Blueprint class Repository: @@ -167,11 +167,10 @@ class AppState: UPDATE_INTERVAL = 60 * 5 REQUEST_TIMEOUT = 60 +CACHE_LOCAL = False state = AppState() -app = Flask(__name__) - -app.config["CACHE_LOCAL"] = False +packages = Blueprint('packages', __name__, template_folder='templates') def cache_route(f): @@ -480,7 +479,7 @@ def parse_repo(repo: str, repo_variant: str, url: str) -> Dict[str, Source]: source.add_desc(d, base_url) - if app.config["CACHE_LOCAL"]: + if CACHE_LOCAL: fn = url.replace("/", "_").replace(":", "_") if not os.path.exists(fn): r = requests.get(url, timeout=REQUEST_TIMEOUT) @@ -519,7 +518,7 @@ def parse_repo(repo: str, repo_variant: str, url: str) -> Dict[str, Source]: return sources -@app.template_filter('timestamp') +@packages.app_template_filter('timestamp') def _jinja2_filter_timestamp(d: int) -> str: try: return datetime.datetime.fromtimestamp( @@ -528,7 +527,7 @@ def _jinja2_filter_timestamp(d: int) -> str: return "-" -@app.template_filter('filesize') +@packages.app_template_filter('filesize') def _jinja2_filter_filesize(d: int) -> str: d = int(d) if d > 1024 ** 3: @@ -537,17 +536,17 @@ def _jinja2_filter_filesize(d: int) -> str: return "%.2f MB" % (d / (1024 ** 2)) -@app.context_processor +@packages.context_processor def funcs(): def package_url(package, name=None): if name is None: - res = url_for("package", name=name or package.name) + res = url_for(".package", name=name or package.name) res += "?repo=" + package.repo if package.repo_variant: res += "&variant=" + package.repo_variant else: - res = url_for("package", name=re.split("[<>=]+", name)[0]) + res = url_for(".package", name=re.split("[<>=]+", name)[0]) if package.repo_variant: res += "?repo=" + package.repo res += "&variant=" + package.repo_variant @@ -573,7 +572,7 @@ def funcs(): update_timestamp=update_timestamp) -@app.route('/repos') +@packages.route('/repos') @cache_route def repos(): global REPOSITORIES @@ -581,13 +580,13 @@ def repos(): return render_template('repos.html', repos=REPOSITORIES) -@app.route('/') +@packages.route('/') def index(): - return redirect(url_for('updates')) + return redirect(url_for('.updates')) -@app.route('/base') -@app.route('/base/') +@packages.route('/base') +@packages.route('/base/') @cache_route def base(name=None): global state @@ -599,8 +598,8 @@ def base(name=None): return render_template('baseindex.html', sources=state.sources) -@app.route('/group/') -@app.route('/group/') +@packages.route('/group/') +@packages.route('/group/') @cache_route def group(name=None): global state @@ -622,7 +621,7 @@ def group(name=None): return render_template('groups.html', groups=groups) -@app.route('/package/') +@packages.route('/package/') @cache_route def package(name): global state @@ -640,7 +639,7 @@ def package(name): return render_template('package.html', packages=packages) -@app.route('/updates') +@packages.route('/updates') @cache_route def updates(): global state @@ -967,7 +966,7 @@ def get_arch_info_for_base(s: Source) -> Optional[tuple]: return None -@app.route('/outofdate') +@packages.route('/outofdate') @cache_route def outofdate(): global state @@ -1010,7 +1009,7 @@ def outofdate(): win_only=win_only) -@app.route('/queue') +@packages.route('/queue') @cache_route def queue(): global state @@ -1034,7 +1033,7 @@ def queue(): return render_template('queue.html', updates=updates) -@app.route('/new') +@packages.route('/new') @cache_route def new(): global state @@ -1056,7 +1055,7 @@ def new(): return render_template('new.html', new=new) -@app.route('/removals') +@packages.route('/removals') @cache_route def removals(): global state @@ -1072,7 +1071,7 @@ def removals(): return render_template('removals.html', missing=missing) -@app.route('/search') +@packages.route('/search') @cache_route def search(): global state @@ -1103,7 +1102,7 @@ def search(): def check_needs_update(_last_time: List[str] = [""]) -> Generator: """Raises RequestException""" - if app.config["CACHE_LOCAL"]: + if CACHE_LOCAL: yield True return @@ -1148,7 +1147,7 @@ def update_sourceinfos() -> None: url = SRCINFO_CONFIG[0][0] print("Loading %r" % url) - if app.config["CACHE_LOCAL"]: + if CACHE_LOCAL: fn = url.replace("/", "_").replace(":", "_") if not os.path.exists(fn): r = requests.get(url, timeout=REQUEST_TIMEOUT) @@ -1291,7 +1290,13 @@ class SrcInfoPackage(object): return packages +app = Flask(__name__) +app.register_blueprint(packages) + + def main(argv: List[str]) -> Any: + global CACHE_LOCAL + from twisted.internet import reactor from twisted.web.server import Site from twisted.web.wsgi import WSGIResource @@ -1305,7 +1310,7 @@ def main(argv: List[str]) -> Any: parser.add_argument("-d", "--debug", action="store_true") args = parser.parse_args() - app.config["CACHE_LOCAL"] = args.cache + CACHE_LOCAL = args.cache print("http://localhost:%d" % args.port) if args.debug: diff --git a/templates/base.html b/templates/base.html index ef8c593..c4225f5 100644 --- a/templates/base.html +++ b/templates/base.html @@ -32,7 +32,7 @@
Group(s):
{% for g in s.groups %} - {{ g }}{{ ", " if not loop.last }} + {{ g }}{{ ", " if not loop.last }} {% else %} - {% endfor %} diff --git a/templates/baseindex.html b/templates/baseindex.html index 2c5e444..b456a9e 100644 --- a/templates/baseindex.html +++ b/templates/baseindex.html @@ -18,7 +18,7 @@ {% for s in sources %} - {{ s.name }} + {{ s.name }} {{ s.version }} {{ s.desc }} diff --git a/templates/group.html b/templates/group.html index 9f82461..7b2b8ec 100644 --- a/templates/group.html +++ b/templates/group.html @@ -3,7 +3,7 @@
-

Group: {{ name }}

+

Group: {{ name }}

Packages that are part of the group
diff --git a/templates/groups.html b/templates/groups.html index c2c064d..947b704 100644 --- a/templates/groups.html +++ b/templates/groups.html @@ -17,7 +17,7 @@ {% for g, count in groups|dictsort %} - {{ g }} + {{ g }} {{ count }} {% endfor %} diff --git a/templates/layout.html b/templates/layout.html index 30616d8..abd01c0 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -9,7 +9,7 @@
diff --git a/templates/package.html b/templates/package.html index fe859cc..adc7534 100644 --- a/templates/package.html +++ b/templates/package.html @@ -19,7 +19,7 @@
Base Package:
-
{{ p.base }}
+
{{ p.base }}
Repo:
{{ package_name(p, p.repo) }}
diff --git a/templates/queue.html b/templates/queue.html index 432985b..f1732c1 100644 --- a/templates/queue.html +++ b/templates/queue.html @@ -25,7 +25,7 @@ {% for (srcpkg, s, p) in updates %} {{ srcpkg.date }} - {{ s.name }} + {{ s.name }} {{ p.version }} → {{ srcpkg.build_version }} diff --git a/templates/search.html b/templates/search.html index 5aea75a..e378e20 100644 --- a/templates/search.html +++ b/templates/search.html @@ -5,7 +5,7 @@
-
+ Search in