From 2f445710caf09b56c663af222c52f7122c39eb9f Mon Sep 17 00:00:00 2001 From: stu Date: Sun, 27 Mar 2022 14:18:39 +0800 Subject: [PATCH] routing: minor refactor --- client_code/routing/_decorators.py | 4 ++-- client_code/routing/_router.py | 11 ++++++----- client_code/routing/_utils.py | 14 +++++++------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/client_code/routing/_decorators.py b/client_code/routing/_decorators.py index 0fe6899a..ac1cf4e7 100644 --- a/client_code/routing/_decorators.py +++ b/client_code/routing/_decorators.py @@ -39,7 +39,7 @@ def template(path="", priority=0, condition=None): def template_wrapper(cls): info = TemplateInfo(cls, path, condition) - _router.add_top_level_info("template", cls, priority, info) + _router.add_info("template", cls, priority, info) cls_init = cls.__init__ @@ -74,7 +74,7 @@ def redirect(path, priority=0, condition=None): def redirect_wrapper(fn): info = RedirectInfo(fn, path, condition) - _router.add_top_level_info("redirect", redirect, priority, info) + _router.add_info("redirect", redirect, priority, info) return fn return redirect_wrapper diff --git a/client_code/routing/_router.py b/client_code/routing/_router.py index c135eb57..a7f86279 100644 --- a/client_code/routing/_router.py +++ b/client_code/routing/_router.py @@ -243,8 +243,9 @@ def get_form_to_add( ) # check if path is cached with another template - if len(route_info.templates) > 1: - for template in route_info.templates: + templates = route_info.template + if len(templates) > 1: + for template in templates: form = _cache.get((url_hash, template), None) if form is not None: logger.debug( @@ -366,15 +367,15 @@ def load_error_form(): def add_route_info(route_info): logger.debug( - " route registered: (form={form.__name__!r}, url_pattern={url_pattern!r}, url_keys={url_keys}, title={title!r})".format( + " route registered: (form={form.__name__!r}, url_pattern={url_pattern!r}, url_keys={url_keys}, title={title!r}, template={template!r})".format( **route_info._asdict() ) ) - for template in route_info.templates: + for template in route_info.template: _routes.setdefault(template, []).append(route_info) -def add_top_level_info(info_type, callable_, priority, info): +def add_info(info_type, callable_, priority, info): global _ordered_info, _templates logger.debug( f"{info_type} registered: {repr(info).replace(type(info).__name__, '')}" diff --git a/client_code/routing/_utils.py b/client_code/routing/_utils.py index acc7fc12..609c434a 100644 --- a/client_code/routing/_utils.py +++ b/client_code/routing/_utils.py @@ -103,7 +103,7 @@ def _get_url_hash(url_pattern, url_dict): _RouteInfoBase = namedtuple( "route_info", - ["form", "templates", "url_pattern", "url_keys", "title", "fwr", "url_parts"], + ["form", "template", "url_pattern", "url_keys", "title", "fwr", "url_parts"], ) TemplateInfo = namedtuple("template_info", ["form", "path", "condition"]) @@ -117,19 +117,19 @@ def as_dynamic_var(part): return part[1:-1], True return part, False - def __new__(cls, form, templates, url_pattern, url_keys, title, fwr, url_parts=()): + def __new__(cls, form, template, url_pattern, url_keys, title, fwr, url_parts=()): if url_pattern.endswith("/"): url_pattern = url_pattern[:-1] url_keys = frozenset(url_keys) - if templates is None or type(templates) is str: - templates = (templates,) - elif type(templates) is not tuple: - templates = tuple(templates) + if template is None or type(template) is str: + template = (template,) + elif type(template) is not tuple: + template = tuple(template) url_parts = [cls.as_dynamic_var(part) for part in url_pattern.split("/")] return _RouteInfoBase.__new__( - cls, form, templates, url_pattern, url_keys, title, fwr, url_parts + cls, form, template, url_pattern, url_keys, title, fwr, url_parts )