def main() -> None: """Commandline interface to this module.""" config = configparser.ConfigParser() config_path = util.get_abspath("wsgi.ini") config.read(config_path) datadir = util.get_abspath("data") workdir = helpers.get_workdir(config) relations = helpers.Relations(datadir, workdir) logpath = os.path.join(workdir, "cron.log") logging.basicConfig(filename=logpath, level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') logging.getLogger().addHandler(logging.StreamHandler()) start = time.time() # Query inactive relations once a month. relations.activate_all(time.localtime(start).tm_mday == 1) try: our_main(relations, config) # pylint: disable=broad-except except Exception: logging.error("main: unhandled exception: %s", traceback.format_exc()) delta = time.time() - start logging.info("main: finished in %s", str(datetime.timedelta(seconds=delta)))
def our_application(environ, start_response): """Dispatches the request based on its URI.""" status = '200 OK' requestUri = environ.get("REQUEST_URI") config = getConfig() workdir = helpers.get_workdir(config) relations = getRelations() if requestUri.startswith("/osm/streets/"): output = handleStreets(requestUri, workdir, relations) elif requestUri.startswith("/osm/street-housenumbers/"): output = handleStreetHousenumbers(requestUri, workdir, relations) elif requestUri.startswith("/osm/suspicious-streets/"): output = handleSuspiciousStreets(requestUri, workdir, relations) else: output = handleMain(relations, workdir) outputBytes = output.encode('utf-8') response_headers = [('Content-type', 'text/html; charset=utf-8'), ('Content-Length', str(len(outputBytes)))] start_response(status, response_headers) return [outputBytes]
def test_happy(self) -> None: """Tests the happy path.""" config = configparser.ConfigParser() config.read_dict({"wsgi": {"workdir": "/path/to/workdir"}}) actual = helpers.get_workdir(config) expected = "/path/to/workdir" self.assertEqual(actual, expected)
def our_application( environ: Dict[str, Any], start_response: 'StartResponse' ) -> Iterable[bytes]: """Dispatches the request based on its URI.""" config = get_config() if config.has_option("wsgi", "locale"): ui_locale = config.get("wsgi", "locale") else: ui_locale = "hu_HU.UTF-8" try: locale.setlocale(locale.LC_ALL, ui_locale) except locale.Error: # Ignore, this happens only on the cut-down CI environment. pass language = util.setup_localization(environ) if not language: language = "hu" request_uri = get_request_uri(environ) _ignore, _ignore, ext = request_uri.partition('.') relations = helpers.Relations(get_datadir(), helpers.get_workdir(config)) if ext == "txt": return our_application_txt(start_response, relations, request_uri) if request_uri.startswith("/osm/static/"): output, content_type = handle_static(request_uri) return send_response(start_response, content_type, "200 OK", output) doc = yattag.Doc() util.write_html_header(doc) with doc.tag("html", lang=language): write_html_head(doc, get_html_title(request_uri)) with doc.tag("body"): if request_uri.startswith("/osm/streets/"): doc.asis(handle_streets(relations, request_uri).getvalue()) elif request_uri.startswith("/osm/missing-streets/"): doc.asis(handle_missing_streets(relations, request_uri).getvalue()) elif request_uri.startswith("/osm/street-housenumbers/"): doc.asis(handle_street_housenumbers(relations, request_uri).getvalue()) elif request_uri.startswith("/osm/missing-housenumbers/"): doc.asis(handle_missing_housenumbers(relations, request_uri).getvalue()) elif request_uri.startswith("/osm/webhooks/github"): doc.asis(handle_github_webhook(environ).getvalue()) else: doc.asis(handle_main(request_uri, relations).getvalue()) return send_response(start_response, "text/html", "200 OK", doc.getvalue())