Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Black #187

Merged
merged 5 commits into from
Jan 24, 2022
Merged

Black #187

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Change Poetry version to be `>=1.1.12,<2` in Docker development setup (prevents `JSONDecodeError` issue under Python 3.10) ([#178](https://github.com/torchbox/django-pattern-library/pull/178)).
- Move demo/test app pattern-library from `/pattern-library/` to `/` ([#178](https://github.com/torchbox/django-pattern-library/pull/178)).
- Allow `.yml` extension for YAML files ([#161](https://github.com/torchbox/django-pattern-library/issues/161)).
- Python files are now formatted by `black` ([#187](https://github.com/torchbox/django-pattern-library/pull/187)).

### Removed

Expand Down
17 changes: 11 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ FROM python:3.10
WORKDIR /app

RUN useradd --create-home dpl && \
pip install "poetry>=1.1.12,<2" && \
poetry config virtualenvs.create false
mkdir -p /venv/ && \
chown -R dpl:dpl /venv/ /app/

ENV PATH=/venv/bin:/home/dpl/.local/bin:$PATH \
VIRTUAL_ENV=/venv/ \
DJANGO_SETTINGS_MODULE=tests.settings.dev

USER dpl

RUN pip install --user "poetry>=1.1.12,<2" && \
python -m venv /venv/

COPY pyproject.toml ./
RUN poetry install --no-root

ENV DJANGO_SETTINGS_MODULE=tests.settings.dev \
PYTHONPATH=.

USER dpl
CMD django-admin runserver 0.0.0.0:8000
47 changes: 24 additions & 23 deletions pattern_library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,61 @@
default_app_config = "pattern_library.apps.PatternLibraryAppConfig"

__all__ = [
'DEFAULT_SETTINGS',
'get_setting',
'get_pattern_template_suffix',
'get_pattern_base_template_name',
'get_base_template_names',
'get_sections',
'get_pattern_context_var_name',
'register_context_modifier',
"DEFAULT_SETTINGS",
"get_setting",
"get_pattern_template_suffix",
"get_pattern_base_template_name",
"get_base_template_names",
"get_sections",
"get_pattern_context_var_name",
"register_context_modifier",
]

DEFAULT_SETTINGS = {
# PATTERN_BASE_TEMPLATE_NAME is the template that fragments will be wrapped with.
# It should include any required CSS and JS and output
# `pattern_library_rendered_pattern` from context.
'PATTERN_BASE_TEMPLATE_NAME': 'patterns/base.html',
"PATTERN_BASE_TEMPLATE_NAME": "patterns/base.html",
# Any template in BASE_TEMPLATE_NAMES or any template that extends a template in
# BASE_TEMPLATE_NAMES is a "page" and will be rendered as-is without being wrapped.
'BASE_TEMPLATE_NAMES': ['patterns/base_page.html'],
'TEMPLATE_SUFFIX': '.html',
"BASE_TEMPLATE_NAMES": ["patterns/base_page.html"],
"TEMPLATE_SUFFIX": ".html",
# SECTIONS controls the groups of templates that appear in the navigation. The keys
# are the group titles and the value are lists of template name prefixes that will
# be searched to populate the groups.
'SECTIONS': (
('atoms', ['patterns/atoms']),
('molecules', ['patterns/molecules']),
('organisms', ['patterns/organisms']),
('templates', ['patterns/templates']),
('pages', ['patterns/pages']),
"SECTIONS": (
("atoms", ["patterns/atoms"]),
("molecules", ["patterns/molecules"]),
("organisms", ["patterns/organisms"]),
("templates", ["patterns/templates"]),
("pages", ["patterns/pages"]),
),
}


def get_setting(attr):
from django.conf import settings

library_settings = DEFAULT_SETTINGS.copy()
library_settings.update(getattr(settings, 'PATTERN_LIBRARY', {}))
library_settings.update(getattr(settings, "PATTERN_LIBRARY", {}))
return library_settings.get(attr)


def get_pattern_template_suffix():
return get_setting('TEMPLATE_SUFFIX')
return get_setting("TEMPLATE_SUFFIX")


def get_pattern_base_template_name():
return get_setting('PATTERN_BASE_TEMPLATE_NAME')
return get_setting("PATTERN_BASE_TEMPLATE_NAME")


def get_base_template_names():
return get_setting('BASE_TEMPLATE_NAMES')
return get_setting("BASE_TEMPLATE_NAMES")


def get_sections():
return get_setting('SECTIONS')
return get_setting("SECTIONS")


def get_pattern_context_var_name():
return 'is_pattern_library'
return "is_pattern_library"
4 changes: 2 additions & 2 deletions pattern_library/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class PatternLibraryAppConfig(AppConfig):
name = 'pattern_library'
default_auto_field = 'django.db.models.AutoField'
name = "pattern_library"
default_auto_field = "django.db.models.AutoField"
3 changes: 1 addition & 2 deletions pattern_library/cm_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import inspect
from importlib import import_module
from typing import Callable
Expand All @@ -23,7 +22,7 @@ def get_app_submodules(submodule_name):
"""
for name, module in get_app_modules():
if module_has_submodule(module, submodule_name):
yield name, import_module('%s.%s' % (name, submodule_name))
yield name, import_module("%s.%s" % (name, submodule_name))


def accepts_kwarg(func: Callable, kwarg: str) -> bool:
Expand Down
7 changes: 2 additions & 5 deletions pattern_library/context_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
GENERIC_CM_KEY = "__generic__"
ORDER_ATTR_NAME = "__cm_order"

__all__ = [
"ContextModifierRegistry",
"register_context_modifier"
]
__all__ = ["ContextModifierRegistry", "register_context_modifier"]


class ContextModifierRegistry(defaultdict):
Expand All @@ -22,7 +19,7 @@ def __init__(self):

def search_for_modifiers(self) -> None:
if not self.searched_for_modifiers:
list(get_app_submodules('pattern_contexts'))
list(get_app_submodules("pattern_contexts"))
self.searched_for_modifiers = True

def register(self, func: Callable, template: str = None, order: int = 0) -> None:
Expand Down
2 changes: 0 additions & 2 deletions pattern_library/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


class PatternLibraryException(Exception):
pass

Expand Down
48 changes: 29 additions & 19 deletions pattern_library/loader_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from django.template.loader_tags import IncludeNode as DjangoIncludeNode
from django.template.loader_tags import construct_relative_path

from pattern_library.utils import (
get_pattern_context, is_pattern_library_context
)
from pattern_library.utils import get_pattern_context, is_pattern_library_context

register = Library()

Expand All @@ -15,6 +13,7 @@ class ExtendsNode(DjangoExtendsNode):
"""
A copy of Django's ExtendsNode that injects context from a file.
"""

def render(self, context):
if is_pattern_library_context(context):
parent_name = self.parent_name.resolve(context)
Expand Down Expand Up @@ -58,11 +57,14 @@ class IncludeNode(DjangoIncludeNode):
"""
A copy of Django's IncludeNode that injects context from a file.
"""

def render(self, context):
if is_pattern_library_context(context):
template = self.template.resolve(context)
pattern_context = get_pattern_context(template)
extra_context = {name: var.resolve(context) for name, var in self.extra_context.items()}
extra_context = {
name: var.resolve(context) for name, var in self.extra_context.items()
}

if self.isolated_context:
context = context.new()
Expand All @@ -88,7 +90,7 @@ def render(self, context):
return super().render(context)


@register.tag('extends')
@register.tag("extends")
def do_extends(parser, token):
"""
A copy of Django's built-in {% extends ... %} tag that uses our custom
Expand All @@ -101,11 +103,13 @@ def do_extends(parser, token):
parent_name = parser.compile_filter(bits[1])
nodelist = parser.parse()
if nodelist.get_nodes_by_type(ExtendsNode):
raise TemplateSyntaxError("'%s' cannot appear more than once in the same template" % bits[0])
raise TemplateSyntaxError(
"'%s' cannot appear more than once in the same template" % bits[0]
)
return ExtendsNode(nodelist, parent_name)


@register.tag('include')
@register.tag("include")
def do_include(parser, token):
"""
A copy of Django's built-in {% include ... %} tag that uses our custom
Expand All @@ -122,21 +126,27 @@ def do_include(parser, token):
while remaining_bits:
option = remaining_bits.pop(0)
if option in options:
raise TemplateSyntaxError('The %r option was specified more '
'than once.' % option)
if option == 'with':
raise TemplateSyntaxError(
"The %r option was specified more " "than once." % option
)
if option == "with":
value = token_kwargs(remaining_bits, parser, support_legacy=False)
if not value:
raise TemplateSyntaxError('"with" in %r tag needs at least '
'one keyword argument.' % bits[0])
elif option == 'only':
raise TemplateSyntaxError(
'"with" in %r tag needs at least ' "one keyword argument." % bits[0]
)
elif option == "only":
value = True
else:
raise TemplateSyntaxError('Unknown argument for %r tag: %r.' %
(bits[0], option))
raise TemplateSyntaxError(
"Unknown argument for %r tag: %r." % (bits[0], option)
)
options[option] = value
isolated_context = options.get('only', False)
namemap = options.get('with', {})
isolated_context = options.get("only", False)
namemap = options.get("with", {})
bits[1] = construct_relative_path(parser.origin.template_name, bits[1])
return IncludeNode(parser.compile_filter(bits[1]), extra_context=namemap,
isolated_context=isolated_context)
return IncludeNode(
parser.compile_filter(bits[1]),
extra_context=namemap,
isolated_context=isolated_context,
)
Loading