diff options
author | 2018-02-06 01:01:59 +0000 | |
---|---|---|
committer | 2018-02-06 01:01:59 +0000 | |
commit | 1fed2a2cdcacd3bfcc0b69f7b1a1bb536c8e5268 (patch) | |
tree | 6ec570b58a6652a7ac935c75b4296bcf5f4509b5 /app.py | |
parent | flake8-todo too pedantic for us - hopefully travis didn't cache it (diff) |
Revert "Finally fix this garbage"
This reverts commit 57abb43
Diffstat (limited to 'app.py')
-rw-r--r-- | app.py | 111 |
1 files changed, 6 insertions, 105 deletions
@@ -1,109 +1,10 @@ -#!/usr/bin/env python3.6 +# coding=utf-8 -# Stdlib -from importlib.util import module_from_spec, spec_from_file_location -import mimetypes -import os +from pysite.route_manager import RouteManager -# External Libraries -from flask import Flask -from werkzeug.wrappers import Response -app = Flask() +manager = RouteManager() +app = manager.app - -def static_file(path: str): # type: (str) -> (req: {Response}) -> Coroutine - async def inner(req): # type: ({Response}) -> Coroutine - with open(path) as file: - return Response( - response=file.read(), mimetype=mimetypes.guess_type(path)[0]) - - return inner - - -def find_static_files(dir_: str) -> list: # type: (str) -> List[str] - data = [] - for path, _, files in os.walk(dir_): - if not files: - continue - - for file in files: - if not file.split(".")[-1] not in ("html", "css", "js"): - continue - - pathname = f"{path[len(dir_):]}/{file.strip()}" - data.append(pathname.split(".")[0], static_file(pathname)) - - return data - - -def find_routes( - dir_: str) -> list: # type: (str) -> List[Tuple[str, str, callable]] - data = [] - for path, _, files in os.walk(dir_): - if not files: - continue - - for file in files: - if not file.endswith(".py"): - continue - pathname = f"{path}/{file.strip()}" - spec = spec_from_file_location(file[:-3], pathname) - module = module_from_spec(spec) - spec.loader.exec_module(module) - - if not hasattr(module, "Index"): - raise Exception("No `Index` class!") - - res = module.Index() - del module, spec - - route_paths = res.path - for path_ in route_paths: - # TODO: Add all request types here - for method in ("GET", "POST", "DELETE", "PATCH"): - if hasattr(res, method.lower()): - data.append((path_, method, getattr( - res, method.lower()))) - - return data - - -def find_errors(dir_: str) -> list: # type: (str) -> List[str] - data = [] - for path, _, files in os.walk(dir_): - if not files: - continue - - for file in files: - if not file.endswith(".py"): - continue - pathname = f"{path}/{file.strip()}" - spec = spec_from_file_location(file[:-3], pathname) - module = module_from_spec(spec) - spec.loader.exec_module(module) - - if not hasattr(module, "Index"): - raise Exception("No `Index` class!") - - res = module.Index() - del module, spec - data.append((res.error_code, res.err)) - - return data - - -routes = find_routes("routes") -static = find_static_files("static") -errors = find_errors("error_handlers") - -for path, method, handle in routes: - app.add_url_rule(path, path, handle, method=method) - -for path, handle in static: - app.add_url_rule(path, path, handle, method="GET") - -for errcode, handler in errors: - app.register_error_handler(errcode, handler) - -app.run(port=80) +if __name__ == '__main__': + manager.run() |