Skip to content

Commit 76d5cd2

Browse files
Ben Dickinsonthibaudcolas
authored andcommitted
Blacken files
1 parent bf2045c commit 76d5cd2

23 files changed

+504
-367
lines changed

pattern_library/__init__.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,61 @@
66
default_app_config = "pattern_library.apps.PatternLibraryAppConfig"
77

88
__all__ = [
9-
'DEFAULT_SETTINGS',
10-
'get_setting',
11-
'get_pattern_template_suffix',
12-
'get_pattern_base_template_name',
13-
'get_base_template_names',
14-
'get_sections',
15-
'get_pattern_context_var_name',
16-
'register_context_modifier',
9+
"DEFAULT_SETTINGS",
10+
"get_setting",
11+
"get_pattern_template_suffix",
12+
"get_pattern_base_template_name",
13+
"get_base_template_names",
14+
"get_sections",
15+
"get_pattern_context_var_name",
16+
"register_context_modifier",
1717
]
1818

1919
DEFAULT_SETTINGS = {
2020
# PATTERN_BASE_TEMPLATE_NAME is the template that fragments will be wrapped with.
2121
# It should include any required CSS and JS and output
2222
# `pattern_library_rendered_pattern` from context.
23-
'PATTERN_BASE_TEMPLATE_NAME': 'patterns/base.html',
23+
"PATTERN_BASE_TEMPLATE_NAME": "patterns/base.html",
2424
# Any template in BASE_TEMPLATE_NAMES or any template that extends a template in
2525
# BASE_TEMPLATE_NAMES is a "page" and will be rendered as-is without being wrapped.
26-
'BASE_TEMPLATE_NAMES': ['patterns/base_page.html'],
27-
'TEMPLATE_SUFFIX': '.html',
26+
"BASE_TEMPLATE_NAMES": ["patterns/base_page.html"],
27+
"TEMPLATE_SUFFIX": ".html",
2828
# SECTIONS controls the groups of templates that appear in the navigation. The keys
2929
# are the group titles and the value are lists of template name prefixes that will
3030
# be searched to populate the groups.
31-
'SECTIONS': (
32-
('atoms', ['patterns/atoms']),
33-
('molecules', ['patterns/molecules']),
34-
('organisms', ['patterns/organisms']),
35-
('templates', ['patterns/templates']),
36-
('pages', ['patterns/pages']),
31+
"SECTIONS": (
32+
("atoms", ["patterns/atoms"]),
33+
("molecules", ["patterns/molecules"]),
34+
("organisms", ["patterns/organisms"]),
35+
("templates", ["patterns/templates"]),
36+
("pages", ["patterns/pages"]),
3737
),
3838
}
3939

4040

4141
def get_setting(attr):
4242
from django.conf import settings
43+
4344
library_settings = DEFAULT_SETTINGS.copy()
44-
library_settings.update(getattr(settings, 'PATTERN_LIBRARY', {}))
45+
library_settings.update(getattr(settings, "PATTERN_LIBRARY", {}))
4546
return library_settings.get(attr)
4647

4748

4849
def get_pattern_template_suffix():
49-
return get_setting('TEMPLATE_SUFFIX')
50+
return get_setting("TEMPLATE_SUFFIX")
5051

5152

5253
def get_pattern_base_template_name():
53-
return get_setting('PATTERN_BASE_TEMPLATE_NAME')
54+
return get_setting("PATTERN_BASE_TEMPLATE_NAME")
5455

5556

5657
def get_base_template_names():
57-
return get_setting('BASE_TEMPLATE_NAMES')
58+
return get_setting("BASE_TEMPLATE_NAMES")
5859

5960

6061
def get_sections():
61-
return get_setting('SECTIONS')
62+
return get_setting("SECTIONS")
6263

6364

6465
def get_pattern_context_var_name():
65-
return 'is_pattern_library'
66+
return "is_pattern_library"

pattern_library/apps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33

44
class PatternLibraryAppConfig(AppConfig):
5-
name = 'pattern_library'
6-
default_auto_field = 'django.db.models.AutoField'
5+
name = "pattern_library"
6+
default_auto_field = "django.db.models.AutoField"

pattern_library/cm_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import inspect
32
from importlib import import_module
43
from typing import Callable
@@ -23,7 +22,7 @@ def get_app_submodules(submodule_name):
2322
"""
2423
for name, module in get_app_modules():
2524
if module_has_submodule(module, submodule_name):
26-
yield name, import_module('%s.%s' % (name, submodule_name))
25+
yield name, import_module("%s.%s" % (name, submodule_name))
2726

2827

2928
def accepts_kwarg(func: Callable, kwarg: str) -> bool:

pattern_library/context_modifiers.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
GENERIC_CM_KEY = "__generic__"
1010
ORDER_ATTR_NAME = "__cm_order"
1111

12-
__all__ = [
13-
"ContextModifierRegistry",
14-
"register_context_modifier"
15-
]
12+
__all__ = ["ContextModifierRegistry", "register_context_modifier"]
1613

1714

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

2320
def search_for_modifiers(self) -> None:
2421
if not self.searched_for_modifiers:
25-
list(get_app_submodules('pattern_contexts'))
22+
list(get_app_submodules("pattern_contexts"))
2623
self.searched_for_modifiers = True
2724

2825
def register(self, func: Callable, template: str = None, order: int = 0) -> None:

pattern_library/exceptions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
class PatternLibraryException(Exception):
42
pass
53

pattern_library/loader_tags.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from django.template.loader_tags import IncludeNode as DjangoIncludeNode
55
from django.template.loader_tags import construct_relative_path
66

7-
from pattern_library.utils import (
8-
get_pattern_context, is_pattern_library_context
9-
)
7+
from pattern_library.utils import get_pattern_context, is_pattern_library_context
108

119
register = Library()
1210

@@ -15,6 +13,7 @@ class ExtendsNode(DjangoExtendsNode):
1513
"""
1614
A copy of Django's ExtendsNode that injects context from a file.
1715
"""
16+
1817
def render(self, context):
1918
if is_pattern_library_context(context):
2019
parent_name = self.parent_name.resolve(context)
@@ -58,11 +57,14 @@ class IncludeNode(DjangoIncludeNode):
5857
"""
5958
A copy of Django's IncludeNode that injects context from a file.
6059
"""
60+
6161
def render(self, context):
6262
if is_pattern_library_context(context):
6363
template = self.template.resolve(context)
6464
pattern_context = get_pattern_context(template)
65-
extra_context = {name: var.resolve(context) for name, var in self.extra_context.items()}
65+
extra_context = {
66+
name: var.resolve(context) for name, var in self.extra_context.items()
67+
}
6668

6769
if self.isolated_context:
6870
context = context.new()
@@ -88,7 +90,7 @@ def render(self, context):
8890
return super().render(context)
8991

9092

91-
@register.tag('extends')
93+
@register.tag("extends")
9294
def do_extends(parser, token):
9395
"""
9496
A copy of Django's built-in {% extends ... %} tag that uses our custom
@@ -101,11 +103,13 @@ def do_extends(parser, token):
101103
parent_name = parser.compile_filter(bits[1])
102104
nodelist = parser.parse()
103105
if nodelist.get_nodes_by_type(ExtendsNode):
104-
raise TemplateSyntaxError("'%s' cannot appear more than once in the same template" % bits[0])
106+
raise TemplateSyntaxError(
107+
"'%s' cannot appear more than once in the same template" % bits[0]
108+
)
105109
return ExtendsNode(nodelist, parent_name)
106110

107111

108-
@register.tag('include')
112+
@register.tag("include")
109113
def do_include(parser, token):
110114
"""
111115
A copy of Django's built-in {% include ... %} tag that uses our custom
@@ -122,21 +126,27 @@ def do_include(parser, token):
122126
while remaining_bits:
123127
option = remaining_bits.pop(0)
124128
if option in options:
125-
raise TemplateSyntaxError('The %r option was specified more '
126-
'than once.' % option)
127-
if option == 'with':
129+
raise TemplateSyntaxError(
130+
"The %r option was specified more " "than once." % option
131+
)
132+
if option == "with":
128133
value = token_kwargs(remaining_bits, parser, support_legacy=False)
129134
if not value:
130-
raise TemplateSyntaxError('"with" in %r tag needs at least '
131-
'one keyword argument.' % bits[0])
132-
elif option == 'only':
135+
raise TemplateSyntaxError(
136+
'"with" in %r tag needs at least ' "one keyword argument." % bits[0]
137+
)
138+
elif option == "only":
133139
value = True
134140
else:
135-
raise TemplateSyntaxError('Unknown argument for %r tag: %r.' %
136-
(bits[0], option))
141+
raise TemplateSyntaxError(
142+
"Unknown argument for %r tag: %r." % (bits[0], option)
143+
)
137144
options[option] = value
138-
isolated_context = options.get('only', False)
139-
namemap = options.get('with', {})
145+
isolated_context = options.get("only", False)
146+
namemap = options.get("with", {})
140147
bits[1] = construct_relative_path(parser.origin.template_name, bits[1])
141-
return IncludeNode(parser.compile_filter(bits[1]), extra_context=namemap,
142-
isolated_context=isolated_context)
148+
return IncludeNode(
149+
parser.compile_filter(bits[1]),
150+
extra_context=namemap,
151+
isolated_context=isolated_context,
152+
)

pattern_library/management/commands/render_patterns.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
from django.template.loader import render_to_string
55
from django.test.client import RequestFactory
66

7-
from pattern_library import (
8-
get_base_template_names, get_pattern_base_template_name
9-
)
7+
from pattern_library import get_base_template_names, get_pattern_base_template_name
108
from pattern_library.utils import (
11-
get_pattern_context, get_pattern_templates, get_template_ancestors,
12-
render_pattern
9+
get_pattern_context,
10+
get_pattern_templates,
11+
get_template_ancestors,
12+
render_pattern,
1313
)
1414

1515

@@ -19,44 +19,46 @@ class Command(BaseCommand):
1919
def add_arguments(self, parser):
2020
super().add_arguments(parser)
2121
parser.add_argument(
22-
'--output',
23-
'-o',
24-
action='store',
25-
dest='output_dir',
26-
default='dpl-rendered-patterns',
27-
help='Directory where to render your patterns',
22+
"--output",
23+
"-o",
24+
action="store",
25+
dest="output_dir",
26+
default="dpl-rendered-patterns",
27+
help="Directory where to render your patterns",
2828
type=str,
2929
)
3030
parser.add_argument(
31-
'--dry-run',
32-
action='store_true',
31+
"--dry-run",
32+
action="store_true",
3333
help="Render the patterns without writing them to disk.",
3434
)
3535
parser.add_argument(
36-
'--wrap-fragments',
37-
action='store_true',
36+
"--wrap-fragments",
37+
action="store_true",
3838
help="Render fragment patterns wrapped in the base template.",
3939
)
4040

4141
def handle(self, **options):
42-
self.verbosity = options['verbosity']
43-
self.dry_run = options['dry_run']
44-
self.wrap_fragments = options['wrap_fragments']
45-
self.output_dir = options['output_dir']
42+
self.verbosity = options["verbosity"]
43+
self.dry_run = options["dry_run"]
44+
self.wrap_fragments = options["wrap_fragments"]
45+
self.output_dir = options["output_dir"]
4646

4747
templates = get_pattern_templates()
4848

4949
factory = RequestFactory()
50-
request = factory.get('/')
50+
request = factory.get("/")
5151

5252
if self.verbosity >= 2:
5353
if self.dry_run:
54-
self.stderr.write(f'Target directory: {self.output_dir}. Dry run, not writing files to disk')
54+
self.stderr.write(
55+
f"Target directory: {self.output_dir}. Dry run, not writing files to disk"
56+
)
5557
else:
56-
self.stderr.write(f'Target directory: {self.output_dir}')
58+
self.stderr.write(f"Target directory: {self.output_dir}")
5759

5860
if self.wrap_fragments:
59-
self.stderr.write('Writing fragment patterns wrapped in base template')
61+
self.stderr.write("Writing fragment patterns wrapped in base template")
6062

6163
# Resolve the output dir according to the directory the command is run from.
6264
parent_dir = Path.cwd().joinpath(self.output_dir)
@@ -67,27 +69,31 @@ def handle(self, **options):
6769
self.render_group(request, parent_dir, templates)
6870

6971
def render_group(self, request, parent_dir: Path, pattern_templates):
70-
for template in pattern_templates['templates_stored']:
72+
for template in pattern_templates["templates_stored"]:
7173
if self.verbosity >= 2:
72-
self.stderr.write(f'Pattern: {template.pattern_filename}')
74+
self.stderr.write(f"Pattern: {template.pattern_filename}")
7375
if self.verbosity >= 1:
7476
self.stderr.write(template.origin.template_name)
7577

7678
render_path = parent_dir.joinpath(template.pattern_filename)
77-
rendered_pattern = self.render_pattern(request, template.origin.template_name)
79+
rendered_pattern = self.render_pattern(
80+
request, template.origin.template_name
81+
)
7882

7983
if self.dry_run:
8084
if self.verbosity >= 2:
8185
self.stdout.write(rendered_pattern)
8286
else:
8387
render_path.write_text(rendered_pattern)
8488

85-
if not pattern_templates['template_groups']:
89+
if not pattern_templates["template_groups"]:
8690
return
8791

88-
for pattern_type_group, pattern_templates in pattern_templates['template_groups'].items():
92+
for pattern_type_group, pattern_templates in pattern_templates[
93+
"template_groups"
94+
].items():
8995
if self.verbosity >= 2:
90-
self.stderr.write(f'Group: {pattern_type_group}')
96+
self.stderr.write(f"Group: {pattern_type_group}")
9197
group_parent = parent_dir.joinpath(pattern_type_group)
9298
if not self.dry_run:
9399
group_parent.mkdir(exist_ok=True)
@@ -104,12 +110,14 @@ def render_pattern(self, request, pattern_template_name):
104110
pattern_template_name,
105111
context=get_pattern_context(pattern_template_name),
106112
)
107-
pattern_is_fragment = set(pattern_template_ancestors).isdisjoint(set(get_base_template_names()))
113+
pattern_is_fragment = set(pattern_template_ancestors).isdisjoint(
114+
set(get_base_template_names())
115+
)
108116

109117
if pattern_is_fragment:
110118
base_template = get_pattern_base_template_name()
111119
context = get_pattern_context(base_template)
112-
context['pattern_library_rendered_pattern'] = rendered_pattern
120+
context["pattern_library_rendered_pattern"] = rendered_pattern
113121
return render_to_string(base_template, request=request, context=context)
114122
else:
115123
return rendered_pattern

0 commit comments

Comments
 (0)