Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
CuriousLearner committed Feb 26, 2024
1 parent 6a73acb commit 5d2bc3b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
5 changes: 3 additions & 2 deletions pattern_library/loader_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ def do_include(parser, token):
isolated_context=isolated_context,
)


def visit_extends(self, node, frame):
"""This method overrides the jinja extends tag
Is called as part of the compiler CodeGenerator
Is called as part of the compiler CodeGenerator
and adds a line to use the template_new_context as
part of the runtime render to pull in the dpl context
Handles visiting extends
Expand Down Expand Up @@ -196,4 +197,4 @@ def template_new_context(

return new_context(
self.environment, self.name, self.blocks, vars, shared, self.globals, locals
)
)
9 changes: 6 additions & 3 deletions pattern_library/monkey_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,26 @@ def node_render(context):

return tag_func


# have to export the original jinja visit Extends
# in the case jinja tags are being overriden
jinja_visit_Extends = None


def override_jinja_tags():
"""
Overrides jinja extends and include tags for use in your pattern library.
Call it in your settings to override tags
Call it in your settings to override tags
"""
global jinja_visit_Extends
try:
from jinja2.compiler import CodeGenerator as JinjaCodeGenerator
from jinja2.environment import Template as JinjaTemplate
except ModuleNotFoundError:
ModuleNotFoundError("install jinja2 to override jinja tags")

from .loader_tags import template_new_context, visit_extends

jinja_visit_Extends = JinjaCodeGenerator.visit_Extends
JinjaTemplate.new_context = template_new_context
JinjaCodeGenerator.visit_Extends = visit_extends
JinjaCodeGenerator.visit_Extends = visit_extends
15 changes: 10 additions & 5 deletions pattern_library/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from pattern_library.exceptions import TemplateIsNotPattern



from django.utils.html import escape


def path_to_section():
section_config = get_sections()
sections = {}
Expand Down Expand Up @@ -243,14 +243,17 @@ def get_pattern_source(cls, template):
@classmethod
def get_template_ancestors(cls, template_name, context=None):
template = get_template(template_name)
return cls._get_engine(template).get_template_ancestors(template_name, context=context)
return cls._get_engine(template).get_template_ancestors(
template_name, context=context
)

@classmethod
def _get_engine(cls, template):
if "jinja" in str(type(template)).lower():
return JinjaTemplateRenderer
return DTLTemplateRenderer


class DTLTemplateRenderer:
@staticmethod
def get_pattern_source(template):
Expand Down Expand Up @@ -287,7 +290,7 @@ class JinjaTemplateRenderer:
@staticmethod
def get_pattern_source(template):
with open(template.template.filename) as f:
source = escape(f.read())
source = escape(f.read())
return source

@classmethod
Expand All @@ -306,12 +309,14 @@ def get_template_ancestors(cls, template_name, context=None, ancestors=None):
context = Context()

pattern_template = get_template(template_name)
#todo - make sure envrionment has context passed in
# todo - make sure envrionment has context passed in
environment = pattern_template.template.environment
nodelist = environment.parse(pattern_template.template.name)
parent_template_name = nodelist.find(Extends)
if parent_template_name:
ancestors.append(parent_template_name)
cls.get_template_ancestors(parent_template_name, context=context, ancestors=ancestors)
cls.get_template_ancestors(
parent_template_name, context=context, ancestors=ancestors
)

return ancestors
1 change: 0 additions & 1 deletion tests/tests/test_context_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def modifier_3(context, request):


class ContextModifierTestCase(SimpleTestCase):

maxDiff = None

def setUp(self):
Expand Down
9 changes: 7 additions & 2 deletions tests/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
class TestGetTemplateAncestors(SimpleTestCase):
def setUp(self):
self.renderer = get_renderer()

def test_page(self):
self.assertEqual(
self.renderer.get_template_ancestors("patterns/pages/test_page/test_page.html"),
self.renderer.get_template_ancestors(
"patterns/pages/test_page/test_page.html"
),
[
"patterns/pages/test_page/test_page.html",
"patterns/base_page.html",
Expand All @@ -25,7 +28,9 @@ def test_page(self):

def test_fragment(self):
self.assertEqual(
self.renderer.get_template_ancestors("patterns/atoms/test_atom/test_atom.html"),
self.renderer.get_template_ancestors(
"patterns/atoms/test_atom/test_atom.html"
),
[
"patterns/atoms/test_atom/test_atom.html",
],
Expand Down

0 comments on commit 5d2bc3b

Please sign in to comment.