Skip to content

Commit

Permalink
Module obj as dict
Browse files Browse the repository at this point in the history
  • Loading branch information
novitae committed Dec 7, 2024
1 parent cb6bb53 commit f743708
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
41 changes: 28 additions & 13 deletions njsparser/parser/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "njsparser"
version = "2.3"
version = "2.4"
description = "A Python NextJS data parser from HTML"
authors = ["ae <[email protected]>"]
readme = "README.md"
Expand Down
31 changes: 26 additions & 5 deletions test/test_parser/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_serializer_default():
option=orjson.OPT_PASSTHROUGH_DATACLASS
)

_flightModulePayload = dict(
_flightModulePayload_1 = dict(
value=[
30777,
[
Expand All @@ -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():
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f743708

Please sign in to comment.