From f743708a4397019eaaed7b16644759cca5c9d9a7 Mon Sep 17 00:00:00 2001 From: ae <85891169+novitae@users.noreply.github.com> Date: Sat, 7 Dec 2024 21:15:59 +0100 Subject: [PATCH] Module obj as dict --- njsparser/parser/types.py | 41 +++++++++++++++++++++++----------- pyproject.toml | 2 +- test/test_parser/test_types.py | 31 ++++++++++++++++++++----- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/njsparser/parser/types.py b/njsparser/parser/types.py index 28f5fd5..4b4723c 100644 --- a/njsparser/parser/types.py +++ b/njsparser/parser/types.py @@ -161,24 +161,27 @@ class Module(Element): ... ) >>> i.module_id 30777 - >>> i.module_scripts_raw() + >>> i.module_chunks_raw() {'71523': 'static/chunks/25c8a87d-0d1c991f726a4cc1.js', '10411': 'static/chunks/app/(webapp)/%5Blang%5D/(public)/user/layout-bd7c1d222b477529.js'} - >>> i.module_scripts + >>> i.module_chunks {'71523': '/_next/static/chunks/25c8a87d-0d1c991f726a4cc1.js', '10411': '/_next/static/chunks/app/(webapp)/%5Blang%5D/(public)/user/layout-bd7c1d222b477529.js'} >>> i.module_name 'default' ``` """ - value: list + value: list | dict value_class = "I" def __post_init__(self): if ENABLE_TYPE_VERIF is True: - assert isinstance(self.value, list) - assert len(self.value) == 3 + assert isinstance(self.value, (list, dict)) + assert 3 <= len(self.value) <= 4 assert isinstance(self.module_id, int) - assert isinstance(self.value[1], list) - assert len(self.value[1]) % 2 == 0 + if isinstance(self.value, list): + assert isinstance(self.value[1], list) + assert len(self.value[1]) % 2 == 0 + else: + assert isinstance(self.value["chunks"], list) assert isinstance(self.module_name, str) @property @@ -188,9 +191,9 @@ def module_id(self) -> int: Returns: int: The module id. """ - return self.value[0] + return self.value[0] if isinstance(self.value, list) else int(self.value["id"]) - def module_scripts_raw(self) -> dict[str, str]: + def module_chunks_raw(self) -> dict[str, str]: """Returns the raw script[script id: script relative path]. Returns: @@ -199,16 +202,16 @@ def module_scripts_raw(self) -> dict[str, str]: return dict({ self.value[1][x]: self.value[1][x+1] for x in range(0, len(self.value), 2) - }) + }) if isinstance(self.value, list) else dict([item.split(":", 1) for item in self.value["chunks"]]) @property - def module_scripts(self): + def module_chunks(self): """The modules scripts id to their absolute path. Returns: dict[str, str]: The script map with absolute paths. """ - return {key: join(_N, value) for key, value in self.module_scripts_raw().items()} + return {key: join(_N, value) for key, value in self.module_chunks_raw().items()} @property def module_name(self) -> str: @@ -217,7 +220,19 @@ def module_name(self) -> str: Returns: str: The name of the module. """ - return self.value[2] + return self.value[2] if isinstance(self.value, list) else self.value["name"] + + @property + def is_async(self) -> bool: + """Tells if the module loading is async or not. + + Returns: + bool: True if it is. + """ + if isinstance(self.value, dict): + return self.value["async"] + else: + return False @dataclass(frozen=True) class Text(Element): diff --git a/pyproject.toml b/pyproject.toml index 87fef4e..687bbfb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "njsparser" -version = "2.3" +version = "2.4" description = "A Python NextJS data parser from HTML" authors = ["ae <85891169+novitae@users.noreply.github.com>"] readme = "README.md" diff --git a/test/test_parser/test_types.py b/test/test_parser/test_types.py index 48fd0b6..6a42f71 100644 --- a/test/test_parser/test_types.py +++ b/test/test_parser/test_types.py @@ -51,7 +51,7 @@ def test_serializer_default(): option=orjson.OPT_PASSTHROUGH_DATACLASS ) -_flightModulePayload = dict( +_flightModulePayload_1 = dict( value=[ 30777, [ @@ -65,12 +65,32 @@ def test_serializer_default(): value_class="I", index=1, ) +_flightModulePayload_2 = dict( + value={ + 'id': '47858', + 'chunks': [ + '272:static/chunks/webpack-2f0e36f832c3608a.js', + '667:static/chunks/2443530c-7d590f93d1ab76bc.js', + '139:static/chunks/139-1e0b88e46566ba7f.js' + ], + 'name': '', + 'async': False + }, + value_class="I", + index=1, +) def test_Module(): - i = Module(**_flightModulePayload) + i = Module(**_flightModulePayload_1) assert i.module_id == 30777 - assert i.module_scripts_raw() == {'71523': 'static/chunks/25c8a87d-0d1c991f726a4cc1.js', '10411': 'static/chunks/app/(webapp)/%5Blang%5D/(public)/user/layout-bd7c1d222b477529.js'} - assert i.module_scripts == {'71523': '/_next/static/chunks/25c8a87d-0d1c991f726a4cc1.js', '10411': '/_next/static/chunks/app/(webapp)/%5Blang%5D/(public)/user/layout-bd7c1d222b477529.js'} + assert i.module_chunks_raw() == {'71523': 'static/chunks/25c8a87d-0d1c991f726a4cc1.js', '10411': 'static/chunks/app/(webapp)/%5Blang%5D/(public)/user/layout-bd7c1d222b477529.js'} + assert i.module_chunks == {'71523': '/_next/static/chunks/25c8a87d-0d1c991f726a4cc1.js', '10411': '/_next/static/chunks/app/(webapp)/%5Blang%5D/(public)/user/layout-bd7c1d222b477529.js'} assert i.module_name == "default" + assert i.is_async is False + i2 = Module(**_flightModulePayload_2) + assert i2.module_id == 47858 + assert i2.module_chunks_raw() == {'272': 'static/chunks/webpack-2f0e36f832c3608a.js', '667': 'static/chunks/2443530c-7d590f93d1ab76bc.js', '139': 'static/chunks/139-1e0b88e46566ba7f.js'} + assert i2.module_name == "" + assert i2.is_async is False _flightTextPayload = dict(value=(hw := "hello world"), value_class="T", index=1) def test_Text(): @@ -172,7 +192,8 @@ def test_Error(): def test_resolve_type(): assert isinstance(resolve_type(**_flightHintPreloadPayload_1), HintPreload) assert isinstance(resolve_type(**_flightHintPreloadPayload_2), HintPreload) - assert isinstance(resolve_type(**_flightModulePayload), Module) + assert isinstance(resolve_type(**_flightModulePayload_1), Module) + assert isinstance(resolve_type(**_flightModulePayload_2), Module) assert isinstance(resolve_type(**_flightTextPayload), Text) assert isinstance(resolve_type(**_flightDataPayload_1), Data) assert isinstance(resolve_type(**_flightDataPayload_2), Data)