Skip to content

Commit 6a0a17d

Browse files
authored
Merge pull request #7 from espressif/test/from-idf-build-apps
Updated the test and added BoolStmt to __all__.
2 parents 6b46845 + 9e3b847 commit 6a0a17d

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

esp_bool_parser/__init__.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,42 @@
99

1010
__version__ = '0.1.1'
1111

12-
from .bool_parser import parse_bool_expr, register_addition_attribute
12+
import importlib
13+
14+
from .bool_parser import BoolStmt, parse_bool_expr, register_addition_attribute
15+
from .utils import lazy_load
16+
17+
__getattr__ = lazy_load(
18+
importlib.import_module(__name__),
19+
{
20+
'parse_bool_expr': parse_bool_expr,
21+
'register_addition_attribute': register_addition_attribute,
22+
'BoolStmt': BoolStmt,
23+
},
24+
{
25+
'IDF_PATH': '.constants',
26+
'SUPPORTED_TARGETS': '.constants',
27+
'PREVIEW_TARGETS': '.constants',
28+
'ALL_TARGETS': '.constants',
29+
'IDF_VERSION_MAJOR': '.constants',
30+
'IDF_VERSION_MINOR': '.constants',
31+
'IDF_VERSION_PATCH': '.constants',
32+
'IDF_VERSION': '.constants',
33+
'SOC_HEADERS': '.soc_header',
34+
},
35+
)
1336

1437
__all__ = [
38+
'ALL_TARGETS',
39+
'IDF_PATH',
40+
'IDF_VERSION',
41+
'IDF_VERSION_MAJOR',
42+
'IDF_VERSION_MINOR',
43+
'IDF_VERSION_PATCH',
44+
'PREVIEW_TARGETS',
45+
'SOC_HEADERS',
46+
'SUPPORTED_TARGETS',
47+
'BoolStmt',
1548
'parse_bool_expr',
1649
'register_addition_attribute',
1750
]

esp_bool_parser/utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,63 @@
11
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: Apache-2.0
33

4+
import importlib
45
import typing as t
56

67
from packaging.version import (
78
Version,
89
)
910

11+
_ModuleType: t.Any = type(importlib)
12+
13+
14+
def lazy_load(
15+
base_module: _ModuleType, name_obj_dict: t.Dict[str, t.Any], obj_module_dict: t.Dict[str, str]
16+
) -> t.Callable[[str], t.Any]:
17+
"""
18+
use __getattr__ in the __init__.py file to lazy load some objects
19+
20+
Args:
21+
base_module (ModuleType): base package module
22+
name_obj_dict (dict[str, any]): name, real object dict, used to store real objects,
23+
no need to add lazy-load objects
24+
obj_module_dict (dict[str, str]): dict of object name and module name
25+
26+
Returns:
27+
__getattr__ function
28+
29+
Example:
30+
31+
::
32+
33+
__getattr__ = lazy_load(
34+
importlib.import_module(__name__),
35+
{
36+
'IdfApp': IdfApp,
37+
'LinuxDut': LinuxDut,
38+
'LinuxSerial': LinuxSerial,
39+
'CaseTester': CaseTester,
40+
},
41+
{
42+
'IdfSerial': '.serial',
43+
'IdfDut': '.dut',
44+
},
45+
)
46+
"""
47+
48+
def __getattr__(object_name):
49+
if object_name in name_obj_dict:
50+
return name_obj_dict[object_name]
51+
elif object_name in obj_module_dict:
52+
module = importlib.import_module(obj_module_dict[object_name], base_module.__name__)
53+
imported = getattr(module, object_name)
54+
name_obj_dict[object_name] = imported
55+
return imported
56+
else:
57+
raise AttributeError('Attribute %s not found in module %s', object_name, base_module.__name__)
58+
59+
return __getattr__
60+
1061

1162
class InvalidInput(SystemExit):
1263
"""Invalid input from user"""

tests/test_bool_parser.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import pytest
55

6+
import esp_bool_parser.constants
67
from esp_bool_parser.bool_parser import parse_bool_expr, register_addition_attribute
8+
from esp_bool_parser.utils import to_version
79

810

911
@pytest.mark.parametrize(
@@ -87,3 +89,15 @@ def test_chain_rule(s, res):
8789
stmt = parse_bool_expr(s)
8890
result = stmt.get_value('', '')
8991
assert result == res
92+
93+
94+
def test_idf_version(monkeypatch):
95+
monkeypatch.setattr(esp_bool_parser.constants, 'IDF_VERSION', to_version('5.9.0'))
96+
statement = 'IDF_VERSION > "5.10.0"'
97+
assert parse_bool_expr(statement).get_value('esp32', 'foo') is False
98+
99+
statement = 'IDF_VERSION < "5.10.0"'
100+
assert parse_bool_expr(statement).get_value('esp32', 'foo') is True
101+
102+
statement = 'IDF_VERSION in ["5.9.0"]'
103+
assert parse_bool_expr(statement).get_value('esp32', 'foo') is True

tests/test_load.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
5+
def test_load():
6+
import esp_bool_parser
7+
8+
for el in esp_bool_parser.__all__:
9+
esp_bool_parser.__getattr__(el)

tests/test_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import shutil
5+
6+
import pytest
7+
8+
9+
@pytest.mark.skipif(not shutil.which('idf.py'), reason='idf.py not found')
10+
def test_idf_version_keywords_type():
11+
from esp_bool_parser.constants import (
12+
IDF_VERSION_MAJOR,
13+
IDF_VERSION_MINOR,
14+
IDF_VERSION_PATCH,
15+
)
16+
17+
assert isinstance(IDF_VERSION_MAJOR, int)
18+
assert isinstance(IDF_VERSION_MINOR, int)
19+
assert isinstance(IDF_VERSION_PATCH, int)

0 commit comments

Comments
 (0)