Skip to content

Auto populate IDOM component registry #24

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

Merged
merged 23 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
precompile component regex
  • Loading branch information
Archmonger committed Nov 1, 2021
commit 7a9ba2d52fb1229c4def2e72b9ba6203f46bfc26
5 changes: 2 additions & 3 deletions src/django_idom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django_idom.config import IDOM_REGISTERED_COMPONENTS


COMPONENT_REGEX_PATTERN = r"{% *idom_component ((\"[^\"']*\")|('[^\"']*')).*?%}"
COMPONENT_REGEX = re.compile(r"{% *idom_component ((\"[^\"']*\")|('[^\"']*')).*?%}")
_logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -98,12 +98,11 @@ def _get_templates(self, paths: Set) -> Set:

def _get_components(self, templates: Set) -> Set:
"""Obtains a set of all IDOM components by parsing HTML templates."""
component_regex = re.compile(COMPONENT_REGEX_PATTERN)
components = set()
for template in templates:
try:
with open(template, "r", encoding="utf-8") as template_file:
match = component_regex.findall(template_file.read())
match = COMPONENT_REGEX.findall(template_file.read())
if not match:
continue
components.update(
Expand Down
46 changes: 16 additions & 30 deletions tests/test_app/tests/test_regex.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,27 @@
from django.test import TestCase

from django_idom.utils import COMPONENT_REGEX_PATTERN
from django_idom.utils import COMPONENT_REGEX


class RegexTests(TestCase):
def test_component_regex(self):
self.assertRegex(r'{%idom_component "my.component"%}', COMPONENT_REGEX_PATTERN)
self.assertRegex(r"{%idom_component 'my.component'%}", COMPONENT_REGEX_PATTERN)
self.assertRegex(
r'{% idom_component "my.component" %}', COMPONENT_REGEX_PATTERN
)
self.assertRegex(
r"{% idom_component 'my.component' %}", COMPONENT_REGEX_PATTERN
)
self.assertRegex(r'{%idom_component "my.component"%}', COMPONENT_REGEX)
self.assertRegex(r"{%idom_component 'my.component'%}", COMPONENT_REGEX)
self.assertRegex(r'{% idom_component "my.component" %}', COMPONENT_REGEX)
self.assertRegex(r"{% idom_component 'my.component' %}", COMPONENT_REGEX)
self.assertRegex(
r'{% idom_component "my.component" class="my_thing" %}',
COMPONENT_REGEX_PATTERN,
COMPONENT_REGEX,
)
self.assertRegex(
r'{% idom_component "my.component" class="my_thing" attr="attribute" %}',
COMPONENT_REGEX_PATTERN,
)
self.assertNotRegex(
r'{% not_a_real_thing "my.component" %}', COMPONENT_REGEX_PATTERN
)
self.assertNotRegex(
r"{% idom_component my.component %}", COMPONENT_REGEX_PATTERN
)
self.assertNotRegex(
r"""{% idom_component 'my.component" %}""", COMPONENT_REGEX_PATTERN
)
self.assertNotRegex(
r'{ idom_component "my.component" }', COMPONENT_REGEX_PATTERN
)
self.assertNotRegex(
r'{{ idom_component "my.component" }}', COMPONENT_REGEX_PATTERN
)
self.assertNotRegex(r"idom_component", COMPONENT_REGEX_PATTERN)
self.assertNotRegex(r"{%%}", COMPONENT_REGEX_PATTERN)
self.assertNotRegex(r"", COMPONENT_REGEX_PATTERN)
COMPONENT_REGEX,
)
self.assertNotRegex(r'{% not_a_real_thing "my.component" %}', COMPONENT_REGEX)
self.assertNotRegex(r"{% idom_component my.component %}", COMPONENT_REGEX)
self.assertNotRegex(r"""{% idom_component 'my.component" %}""", COMPONENT_REGEX)
self.assertNotRegex(r'{ idom_component "my.component" }', COMPONENT_REGEX)
self.assertNotRegex(r'{{ idom_component "my.component" }}', COMPONENT_REGEX)
self.assertNotRegex(r"idom_component", COMPONENT_REGEX)
self.assertNotRegex(r"{%%}", COMPONENT_REGEX)
self.assertNotRegex(r"", COMPONENT_REGEX)