Skip to content

Commit

Permalink
routing: minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
s-cork committed Mar 27, 2022
1 parent 48be569 commit 2f44571
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions client_code/routing/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions client_code/routing/_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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__, '')}"
Expand Down
14 changes: 7 additions & 7 deletions client_code/routing/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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
)

0 comments on commit 2f44571

Please # to comment.