esp-bool-parser 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- esp_bool_parser/__init__.py +17 -0
- esp_bool_parser/bool_parser.py +279 -0
- esp_bool_parser/constants.py +61 -0
- esp_bool_parser/soc_header.py +141 -0
- esp_bool_parser/utils.py +26 -0
- esp_bool_parser-0.1.0.dist-info/LICENSE +202 -0
- esp_bool_parser-0.1.0.dist-info/METADATA +85 -0
- esp_bool_parser-0.1.0.dist-info/RECORD +9 -0
- esp_bool_parser-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Tools for building ESP-IDF related apps.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
# ruff: noqa: E402
|
|
9
|
+
|
|
10
|
+
__version__ = '0.1.0'
|
|
11
|
+
|
|
12
|
+
from .bool_parser import parse_bool_expr, register_addition_attribute
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
'parse_bool_expr',
|
|
16
|
+
'register_addition_attribute',
|
|
17
|
+
]
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import operator
|
|
4
|
+
import os
|
|
5
|
+
import typing as t
|
|
6
|
+
from ast import (
|
|
7
|
+
literal_eval,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
from packaging.version import (
|
|
11
|
+
Version,
|
|
12
|
+
)
|
|
13
|
+
from pyparsing import (
|
|
14
|
+
Keyword,
|
|
15
|
+
Literal,
|
|
16
|
+
ParseResults,
|
|
17
|
+
QuotedString,
|
|
18
|
+
Suppress,
|
|
19
|
+
Word,
|
|
20
|
+
alphas,
|
|
21
|
+
delimitedList,
|
|
22
|
+
hexnums,
|
|
23
|
+
infixNotation,
|
|
24
|
+
nums,
|
|
25
|
+
opAssoc,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
from .utils import (
|
|
29
|
+
InvalidInput,
|
|
30
|
+
to_version,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Stmt:
|
|
35
|
+
"""
|
|
36
|
+
Statement
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def get_value(self, target: str, config_name: str) -> t.Any:
|
|
40
|
+
"""
|
|
41
|
+
Lazy calculated. All subclasses of `Stmt` should implement this function.
|
|
42
|
+
|
|
43
|
+
:param target: ESP-IDF target
|
|
44
|
+
:type target: str
|
|
45
|
+
:param config_name: config name
|
|
46
|
+
:type target: str
|
|
47
|
+
:return: the value of the statement
|
|
48
|
+
"""
|
|
49
|
+
raise NotImplementedError('Please implement this function in sub classes')
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class ChipAttr(Stmt):
|
|
53
|
+
"""
|
|
54
|
+
Attributes defined in SOC Header Files and other keywords as followed:
|
|
55
|
+
|
|
56
|
+
- IDF_TARGET: target
|
|
57
|
+
- INCLUDE_DEFAULT: take the default build targets into account or not
|
|
58
|
+
- IDF_VERSION_MAJOR: major version of ESP-IDF
|
|
59
|
+
- IDF_VERSION_MINOR: minor version of ESP-IDF
|
|
60
|
+
- IDF_VERSION_PATCH: patch version of ESP-IDF
|
|
61
|
+
- CONFIG_NAME: config name defined in the config rules
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
addition_attr: t.Dict[str, t.Callable] = {}
|
|
65
|
+
|
|
66
|
+
def __init__(self, t: ParseResults):
|
|
67
|
+
self.attr: str = t[0]
|
|
68
|
+
|
|
69
|
+
def get_value(self, target: str, config_name: str) -> t.Any:
|
|
70
|
+
if self.attr in self.addition_attr:
|
|
71
|
+
return self.addition_attr[self.attr](target=target, config_name=config_name)
|
|
72
|
+
|
|
73
|
+
if self.attr == 'IDF_TARGET':
|
|
74
|
+
return target
|
|
75
|
+
|
|
76
|
+
if self.attr == 'CONFIG_NAME':
|
|
77
|
+
return config_name
|
|
78
|
+
|
|
79
|
+
if self.attr in os.environ:
|
|
80
|
+
return os.environ[self.attr]
|
|
81
|
+
|
|
82
|
+
from .constants import (
|
|
83
|
+
IDF_VERSION,
|
|
84
|
+
IDF_VERSION_MAJOR,
|
|
85
|
+
IDF_VERSION_MINOR,
|
|
86
|
+
IDF_VERSION_PATCH,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
if self.attr == 'IDF_VERSION':
|
|
90
|
+
return IDF_VERSION
|
|
91
|
+
|
|
92
|
+
if self.attr == 'IDF_VERSION_MAJOR':
|
|
93
|
+
return IDF_VERSION_MAJOR
|
|
94
|
+
|
|
95
|
+
if self.attr == 'IDF_VERSION_MINOR':
|
|
96
|
+
return IDF_VERSION_MINOR
|
|
97
|
+
|
|
98
|
+
if self.attr == 'IDF_VERSION_PATCH':
|
|
99
|
+
return IDF_VERSION_PATCH
|
|
100
|
+
|
|
101
|
+
from .soc_header import SOC_HEADERS
|
|
102
|
+
|
|
103
|
+
if self.attr in SOC_HEADERS[target]:
|
|
104
|
+
return SOC_HEADERS[target][self.attr]
|
|
105
|
+
|
|
106
|
+
return 0 # default return 0 as false
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class Integer(Stmt):
|
|
110
|
+
def __init__(self, t: ParseResults):
|
|
111
|
+
self.expr: str = t[0]
|
|
112
|
+
|
|
113
|
+
def get_value(self, target: str, config_name: str) -> t.Any: # noqa: ARG002
|
|
114
|
+
return literal_eval(self.expr)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class String(Stmt):
|
|
118
|
+
def __init__(self, t: ParseResults):
|
|
119
|
+
self.expr: str = t[0]
|
|
120
|
+
|
|
121
|
+
def get_value(self, target: str, config_name: str) -> t.Any: # noqa: ARG002
|
|
122
|
+
return literal_eval(f'"{self.expr}"') # double quotes is swallowed by QuotedString
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class List_(Stmt):
|
|
126
|
+
def __init__(self, t: ParseResults):
|
|
127
|
+
self.expr = t
|
|
128
|
+
|
|
129
|
+
def get_value(self, target: str, config_name: str) -> t.Any:
|
|
130
|
+
return [item.get_value(target, config_name) for item in self.expr]
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class BoolStmt(Stmt):
|
|
134
|
+
_OP_DICT = {
|
|
135
|
+
'==': operator.eq,
|
|
136
|
+
'!=': operator.ne,
|
|
137
|
+
'>': operator.gt,
|
|
138
|
+
'>=': operator.ge,
|
|
139
|
+
'<': operator.lt,
|
|
140
|
+
'<=': operator.le,
|
|
141
|
+
'not in': lambda x, y: x not in y,
|
|
142
|
+
'in': lambda x, y: x in y,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
def __init__(self, t: ParseResults):
|
|
146
|
+
self.left: Stmt = t[0]
|
|
147
|
+
self.comparison: str = t[1]
|
|
148
|
+
self.right: Stmt = t[2]
|
|
149
|
+
|
|
150
|
+
def get_value(self, target: str, config_name: str) -> t.Any:
|
|
151
|
+
_l = self.left.get_value(target, config_name)
|
|
152
|
+
_r = self.right.get_value(target, config_name)
|
|
153
|
+
|
|
154
|
+
if self.comparison not in ['in', 'not in']:
|
|
155
|
+
# will use version comparison if any of the operands is a Version
|
|
156
|
+
if any(isinstance(x, Version) for x in [_l, _r]):
|
|
157
|
+
_l = to_version(_l)
|
|
158
|
+
_r = to_version(_r)
|
|
159
|
+
else:
|
|
160
|
+
# use str for "in" and "not in" operator
|
|
161
|
+
if isinstance(_l, Version):
|
|
162
|
+
_l = str(_l)
|
|
163
|
+
if isinstance(_r, Version):
|
|
164
|
+
_r = str(_r)
|
|
165
|
+
|
|
166
|
+
if self.comparison in self._OP_DICT:
|
|
167
|
+
return self._OP_DICT[self.comparison](_l, _r)
|
|
168
|
+
|
|
169
|
+
raise InvalidInput(f'Unsupported comparison operator: "{self.comparison}"')
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
class BoolExpr(Stmt):
|
|
173
|
+
pass
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _and(_l, _r):
|
|
177
|
+
return _l and _r
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _or(_l, _r):
|
|
181
|
+
return _l or _r
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class BoolOr(BoolExpr):
|
|
185
|
+
def __init__(self, res: ParseResults):
|
|
186
|
+
self.bool_stmts: t.List[BoolStmt] = []
|
|
187
|
+
for stmt in res[0]:
|
|
188
|
+
if stmt != 'or':
|
|
189
|
+
self.bool_stmts.append(stmt)
|
|
190
|
+
|
|
191
|
+
def get_value(self, target: str, config_name: str) -> t.Any:
|
|
192
|
+
for stmt in self.bool_stmts:
|
|
193
|
+
if stmt.get_value(target, config_name):
|
|
194
|
+
return True
|
|
195
|
+
return False
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
class BoolAnd(BoolExpr):
|
|
199
|
+
def __init__(self, res: ParseResults):
|
|
200
|
+
self.bool_stmts: t.List[BoolStmt] = []
|
|
201
|
+
for stmt in res[0]:
|
|
202
|
+
if stmt != 'and':
|
|
203
|
+
self.bool_stmts.append(stmt)
|
|
204
|
+
|
|
205
|
+
def get_value(self, target: str, config_name: str) -> t.Any:
|
|
206
|
+
for stmt in self.bool_stmts:
|
|
207
|
+
if not stmt.get_value(target, config_name):
|
|
208
|
+
return False
|
|
209
|
+
return True
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
CAP_WORD = Word(alphas.upper(), nums + alphas.upper() + '_').setParseAction(ChipAttr)
|
|
213
|
+
|
|
214
|
+
DECIMAL_NUMBER = Word(nums)
|
|
215
|
+
HEX_NUMBER = Literal('0x') + Word(hexnums)
|
|
216
|
+
INTEGER = (HEX_NUMBER | DECIMAL_NUMBER).setParseAction(Integer)
|
|
217
|
+
|
|
218
|
+
STRING = QuotedString('"').setParseAction(String)
|
|
219
|
+
|
|
220
|
+
LIST = Suppress('[') + delimitedList(INTEGER | STRING).setParseAction(List_) + Suppress(']')
|
|
221
|
+
|
|
222
|
+
BOOL_OPERAND = CAP_WORD | INTEGER | STRING | LIST
|
|
223
|
+
|
|
224
|
+
EQ = Keyword('==').setParseAction(lambda t: t[0])
|
|
225
|
+
NE = Keyword('!=').setParseAction(lambda t: t[0])
|
|
226
|
+
LE = Keyword('<=').setParseAction(lambda t: t[0])
|
|
227
|
+
LT = Keyword('<').setParseAction(lambda t: t[0])
|
|
228
|
+
GE = Keyword('>=').setParseAction(lambda t: t[0])
|
|
229
|
+
GT = Keyword('>').setParseAction(lambda t: t[0])
|
|
230
|
+
NOT_IN = Keyword('not in').setParseAction(lambda t: t[0])
|
|
231
|
+
IN = Keyword('in').setParseAction(lambda t: t[0])
|
|
232
|
+
|
|
233
|
+
BOOL_STMT = BOOL_OPERAND + (EQ | NE | LE | LT | GE | GT | NOT_IN | IN) + BOOL_OPERAND
|
|
234
|
+
BOOL_STMT.setParseAction(BoolStmt)
|
|
235
|
+
|
|
236
|
+
AND = Keyword('and')
|
|
237
|
+
OR = Keyword('or')
|
|
238
|
+
|
|
239
|
+
BOOL_EXPR = infixNotation(
|
|
240
|
+
BOOL_STMT,
|
|
241
|
+
[
|
|
242
|
+
(AND, 2, opAssoc.LEFT, BoolAnd),
|
|
243
|
+
(OR, 2, opAssoc.LEFT, BoolOr),
|
|
244
|
+
],
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def register_addition_attribute(attr: str, action: t.Callable[..., t.Any]) -> None:
|
|
249
|
+
"""
|
|
250
|
+
Register an additional attribute for ChipAttr.
|
|
251
|
+
|
|
252
|
+
:param attr: The name of the additional attribute (string).
|
|
253
|
+
:param action: A callable that processes **kwargs. The `target` and `config_name`
|
|
254
|
+
parameters will be passed as kwargs when the attribute is detected.
|
|
255
|
+
"""
|
|
256
|
+
ChipAttr.addition_attr[attr] = action
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def parse_bool_expr(stmt: str) -> BoolStmt:
|
|
260
|
+
"""
|
|
261
|
+
Parse a boolean expression.
|
|
262
|
+
|
|
263
|
+
:param stmt: A string containing the boolean expression.
|
|
264
|
+
:return: A `BoolStmt` object representing the parsed expression.
|
|
265
|
+
|
|
266
|
+
.. note::
|
|
267
|
+
|
|
268
|
+
You can use this function to parse a boolean expression and evaluate its value based on the given context.
|
|
269
|
+
For example:
|
|
270
|
+
|
|
271
|
+
.. code:: python
|
|
272
|
+
|
|
273
|
+
stmt_string = 'IDF_TARGET == "esp32"'
|
|
274
|
+
stmt: BoolStmt = parse_bool_expr(stmt_string)
|
|
275
|
+
value = stmt.get_value("esp32", "config_name")
|
|
276
|
+
print(value)
|
|
277
|
+
# Output: True
|
|
278
|
+
"""
|
|
279
|
+
return BOOL_EXPR.parseString(stmt)[0]
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import importlib
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import sys
|
|
9
|
+
import typing as t
|
|
10
|
+
|
|
11
|
+
from .utils import (
|
|
12
|
+
to_version,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
LOGGER = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
_idf_env = os.getenv('IDF_PATH') or ''
|
|
18
|
+
if not _idf_env:
|
|
19
|
+
LOGGER.warning('IDF_PATH environment variable is not set. Entering test mode...')
|
|
20
|
+
LOGGER.warning('- Setting IDF_PATH to current directory...')
|
|
21
|
+
IDF_PATH = os.path.abspath(_idf_env)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
_idf_py_actions = os.path.join(IDF_PATH, 'tools', 'idf_py_actions')
|
|
25
|
+
sys.path.append(_idf_py_actions)
|
|
26
|
+
try:
|
|
27
|
+
_idf_py_constant_py = importlib.import_module('constants')
|
|
28
|
+
except ModuleNotFoundError:
|
|
29
|
+
LOGGER.warning(
|
|
30
|
+
'- Set supported/preview targets to empty list... (ESP-IDF constants.py module not found under %s)',
|
|
31
|
+
_idf_py_actions,
|
|
32
|
+
)
|
|
33
|
+
_idf_py_constant_py = object() # type: ignore
|
|
34
|
+
SUPPORTED_TARGETS = getattr(_idf_py_constant_py, 'SUPPORTED_TARGETS', [])
|
|
35
|
+
PREVIEW_TARGETS = getattr(_idf_py_constant_py, 'PREVIEW_TARGETS', [])
|
|
36
|
+
ALL_TARGETS = SUPPORTED_TARGETS + PREVIEW_TARGETS
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _idf_version_from_cmake() -> t.Tuple[int, int, int]:
|
|
40
|
+
version_path = os.path.join(IDF_PATH, 'tools', 'cmake', 'version.cmake')
|
|
41
|
+
if not os.path.isfile(version_path):
|
|
42
|
+
LOGGER.warning('- Setting ESP-IDF version to 1.0.0... (ESP-IDF version.cmake not exists at %s)', version_path)
|
|
43
|
+
return 1, 0, 0
|
|
44
|
+
|
|
45
|
+
regex = re.compile(r'^\s*set\s*\(\s*IDF_VERSION_([A-Z]{5})\s+(\d+)')
|
|
46
|
+
ver = {}
|
|
47
|
+
try:
|
|
48
|
+
with open(version_path) as f:
|
|
49
|
+
for line in f:
|
|
50
|
+
m = regex.match(line)
|
|
51
|
+
|
|
52
|
+
if m:
|
|
53
|
+
ver[m.group(1)] = m.group(2)
|
|
54
|
+
|
|
55
|
+
return int(ver['MAJOR']), int(ver['MINOR']), int(ver['PATCH'])
|
|
56
|
+
except (KeyError, OSError):
|
|
57
|
+
raise ValueError(f'Cannot find ESP-IDF version in {version_path}')
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
IDF_VERSION_MAJOR, IDF_VERSION_MINOR, IDF_VERSION_PATCH = _idf_version_from_cmake()
|
|
61
|
+
IDF_VERSION = to_version(f'{IDF_VERSION_MAJOR}.{IDF_VERSION_MINOR}.{IDF_VERSION_PATCH}')
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import logging
|
|
4
|
+
import os.path
|
|
5
|
+
import typing as t
|
|
6
|
+
from pathlib import (
|
|
7
|
+
Path,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
from pyparsing import (
|
|
11
|
+
CaselessLiteral,
|
|
12
|
+
Char,
|
|
13
|
+
Combine,
|
|
14
|
+
Group,
|
|
15
|
+
Literal,
|
|
16
|
+
MatchFirst,
|
|
17
|
+
OneOrMore,
|
|
18
|
+
Optional,
|
|
19
|
+
ParseException,
|
|
20
|
+
ParseResults,
|
|
21
|
+
QuotedString,
|
|
22
|
+
Word,
|
|
23
|
+
alphas,
|
|
24
|
+
hexnums,
|
|
25
|
+
nums,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
from .constants import (
|
|
29
|
+
ALL_TARGETS,
|
|
30
|
+
IDF_PATH,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
LOGGER = logging.getLogger(__name__)
|
|
34
|
+
|
|
35
|
+
# Group for parsing literal suffix of a numbers, e.g. 100UL
|
|
36
|
+
_literal_symbol = Group(CaselessLiteral('L') | CaselessLiteral('U'))
|
|
37
|
+
_literal_suffix = OneOrMore(_literal_symbol)
|
|
38
|
+
|
|
39
|
+
# Define name
|
|
40
|
+
_name = Word(alphas, alphas + nums + '_')
|
|
41
|
+
|
|
42
|
+
# Define value, either a hex, int or a string
|
|
43
|
+
_hex_value = Combine(Literal('0x') + Word(hexnums) + Optional(_literal_suffix).suppress())('hex_value')
|
|
44
|
+
_str_value = QuotedString('"')('str_value')
|
|
45
|
+
_int_value = Combine(Optional('-') + Word(nums))('int_value') + ~Char('.') + Optional(_literal_suffix)('literal_suffix')
|
|
46
|
+
|
|
47
|
+
# Remove optional parenthesis around values
|
|
48
|
+
_value = Optional('(').suppress() + MatchFirst([_hex_value, _str_value, _int_value])('value') + Optional(')').suppress()
|
|
49
|
+
|
|
50
|
+
_define_expr = '#define' + Optional(_name)('name') + Optional(_value)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def get_defines(header_path: str) -> t.List[str]:
|
|
54
|
+
defines = []
|
|
55
|
+
LOGGER.debug('Reading macros from %s...', header_path)
|
|
56
|
+
with open(header_path) as f:
|
|
57
|
+
output = f.read()
|
|
58
|
+
|
|
59
|
+
for line in output.split('\n'):
|
|
60
|
+
line = line.strip()
|
|
61
|
+
if len(line):
|
|
62
|
+
defines.append(line)
|
|
63
|
+
|
|
64
|
+
return defines
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def parse_define(define_line: str) -> ParseResults:
|
|
68
|
+
res = _define_expr.parseString(define_line)
|
|
69
|
+
|
|
70
|
+
return res
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class SocHeader(dict):
|
|
74
|
+
CAPS_HEADER_FILEPATTERN = '*_caps.h'
|
|
75
|
+
|
|
76
|
+
def __init__(self, target: str) -> None:
|
|
77
|
+
if target != 'linux':
|
|
78
|
+
soc_header_dict = self._parse_soc_header(target)
|
|
79
|
+
else:
|
|
80
|
+
soc_header_dict = {}
|
|
81
|
+
|
|
82
|
+
super().__init__(**soc_header_dict)
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
def _get_dirs_from_candidates(candidates: t.List[str]) -> t.List[str]:
|
|
86
|
+
dirs = []
|
|
87
|
+
for d in candidates:
|
|
88
|
+
if not os.path.isdir(d):
|
|
89
|
+
LOGGER.debug('folder "%s" not found. Skipping...', os.path.abspath(d))
|
|
90
|
+
else:
|
|
91
|
+
dirs.append(d)
|
|
92
|
+
|
|
93
|
+
return dirs
|
|
94
|
+
|
|
95
|
+
@classmethod
|
|
96
|
+
def _parse_soc_header(cls, target: str) -> t.Dict[str, t.Any]:
|
|
97
|
+
soc_headers_dirs = cls._get_dirs_from_candidates(
|
|
98
|
+
[
|
|
99
|
+
# master c5 mp
|
|
100
|
+
os.path.abspath(os.path.join(IDF_PATH, 'components', 'soc', target, 'mp', 'include', 'soc')),
|
|
101
|
+
# other branches
|
|
102
|
+
os.path.abspath(os.path.join(IDF_PATH, 'components', 'soc', target, 'include', 'soc')),
|
|
103
|
+
# release/v4.2
|
|
104
|
+
os.path.abspath(os.path.join(IDF_PATH, 'components', 'soc', 'soc', target, 'include', 'soc')),
|
|
105
|
+
]
|
|
106
|
+
)
|
|
107
|
+
esp_rom_headers_dirs = cls._get_dirs_from_candidates(
|
|
108
|
+
[
|
|
109
|
+
# master c5 mp
|
|
110
|
+
os.path.abspath(os.path.join(IDF_PATH, 'components', 'esp_rom', target, 'mp', target)),
|
|
111
|
+
# others
|
|
112
|
+
os.path.abspath(os.path.join(IDF_PATH, 'components', 'esp_rom', target)),
|
|
113
|
+
]
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
header_files: t.List[str] = []
|
|
117
|
+
for d in [*soc_headers_dirs, *esp_rom_headers_dirs]:
|
|
118
|
+
LOGGER.debug('Checking dir %s', d)
|
|
119
|
+
header_files += [str(p.resolve()) for p in Path(d).glob(cls.CAPS_HEADER_FILEPATTERN)]
|
|
120
|
+
|
|
121
|
+
output_dict = {}
|
|
122
|
+
for f in header_files:
|
|
123
|
+
LOGGER.debug('Checking header file %s', f)
|
|
124
|
+
for line in get_defines(f):
|
|
125
|
+
try:
|
|
126
|
+
res = parse_define(line)
|
|
127
|
+
except ParseException:
|
|
128
|
+
LOGGER.debug('Failed to parse: %s', line)
|
|
129
|
+
continue
|
|
130
|
+
|
|
131
|
+
if 'str_value' in res:
|
|
132
|
+
output_dict[res.name] = res.str_value
|
|
133
|
+
elif 'int_value' in res:
|
|
134
|
+
output_dict[res.name] = int(res.int_value)
|
|
135
|
+
elif 'hex_value' in res:
|
|
136
|
+
output_dict[res.name] = int(res.hex_value, 16)
|
|
137
|
+
|
|
138
|
+
return output_dict
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
SOC_HEADERS: t.Dict[str, SocHeader] = {target: SocHeader(target) for target in ALL_TARGETS}
|
esp_bool_parser/utils.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import typing as t
|
|
5
|
+
|
|
6
|
+
from packaging.version import (
|
|
7
|
+
Version,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class InvalidInput(SystemExit):
|
|
12
|
+
"""Invalid input from user"""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class InvalidIfClause(SystemExit):
|
|
16
|
+
"""Invalid if clause in manifest file"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def to_version(s: t.Any) -> Version:
|
|
20
|
+
if isinstance(s, Version):
|
|
21
|
+
return s
|
|
22
|
+
|
|
23
|
+
try:
|
|
24
|
+
return Version(str(s))
|
|
25
|
+
except ValueError:
|
|
26
|
+
raise InvalidInput(f'Invalid version: {s}')
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: esp-bool-parser
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tools for building ESP-IDF related apps.
|
|
5
|
+
Author-email: Fu Hanxi <fuhanxi@espressif.com>
|
|
6
|
+
Requires-Python: >=3.7
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Requires-Dist: pyparsing
|
|
17
|
+
Requires-Dist: packaging
|
|
18
|
+
Requires-Dist: sphinx ; extra == "doc"
|
|
19
|
+
Requires-Dist: sphinx-rtd-theme ; extra == "doc"
|
|
20
|
+
Requires-Dist: sphinx_copybutton ; extra == "doc"
|
|
21
|
+
Requires-Dist: myst-parser ; extra == "doc"
|
|
22
|
+
Requires-Dist: sphinxcontrib-mermaid ; extra == "doc"
|
|
23
|
+
Requires-Dist: sphinx-argparse ; extra == "doc"
|
|
24
|
+
Requires-Dist: sphinx-tabs ; extra == "doc"
|
|
25
|
+
Requires-Dist: pytest ; extra == "test"
|
|
26
|
+
Requires-Dist: pytest-cov ; extra == "test"
|
|
27
|
+
Project-URL: changelog, https://github.com/espressif/esp-bool-parsers/blob/main/CHANGELOG.md
|
|
28
|
+
Project-URL: documentation, https://docs.espressif.com/projects/esp-bool-parser
|
|
29
|
+
Project-URL: homepage, https://github.com/espressif/esp-bool-parser
|
|
30
|
+
Project-URL: repository, https://github.com/espressif/esp-bool-parser
|
|
31
|
+
Provides-Extra: doc
|
|
32
|
+
Provides-Extra: test
|
|
33
|
+
|
|
34
|
+
# esp-bool-parser
|
|
35
|
+
|
|
36
|
+
`esp-bool-parser` is a package that provides a way to process boolean statements based on `soc_caps` files in the ESP-IDF.
|
|
37
|
+
|
|
38
|
+
It helps you locate `soc_headers` files in the ESP-IDF, parse them, and store the parsed values as constants, which are then used in `ChipAttr`.
|
|
39
|
+
|
|
40
|
+
When you import `esp_bool_parser`, you will gain access to the following functions:
|
|
41
|
+
|
|
42
|
+
### Key Functions
|
|
43
|
+
|
|
44
|
+
#### `parse_bool_expr(stmt: str)`
|
|
45
|
+
|
|
46
|
+
Parses a boolean expression.
|
|
47
|
+
|
|
48
|
+
- **Parameters:**
|
|
49
|
+
- `stmt` (str): A string containing the boolean expression.
|
|
50
|
+
|
|
51
|
+
- **Returns:**
|
|
52
|
+
- A parsed representation of the boolean expression.
|
|
53
|
+
|
|
54
|
+
- **Usage Example:**
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
stmt_string = 'IDF_TARGET == "esp32"'
|
|
58
|
+
stmt = parse_bool_expr(stmt_string)
|
|
59
|
+
result = stmt.get_value("esp32", "config_name")
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### `register_addition_attribute(attr: str, action: t.Callable[..., t.Any]) -> None`
|
|
63
|
+
|
|
64
|
+
Registers an additional attribute for `ChipAttr`.
|
|
65
|
+
|
|
66
|
+
You can extend the functionality of `ChipAttr` by adding custom handlers for new attributes.
|
|
67
|
+
Use the `register_addition_attribute` function to register additional attributes.
|
|
68
|
+
When these attributes are encountered, the associated handler function will be called.
|
|
69
|
+
Additionally, you can override existing attributes, as the newly registered handler will take priority over the original ones.
|
|
70
|
+
|
|
71
|
+
- **Parameters:**
|
|
72
|
+
- `attr` (str): The name of the additional attribute.
|
|
73
|
+
- `action` (Callable): A callable that processes `**kwargs`. The `target` and `config_name` parameters will be passed as `kwargs` when the attribute is detected.
|
|
74
|
+
|
|
75
|
+
- **Usage Example:**
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
def my_action(target, config_name, **kwargs):
|
|
79
|
+
# Custom logic to handle the attribute
|
|
80
|
+
print(f"Processing {target} with {config_name}")
|
|
81
|
+
return target
|
|
82
|
+
|
|
83
|
+
register_addition_attribute("CUSTOM_ATTR", my_action)
|
|
84
|
+
```
|
|
85
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
esp_bool_parser/__init__.py,sha256=RG4kHKlaRIYMxetf1abfCFxOljS5cSGFNREviYIbDhE,347
|
|
2
|
+
esp_bool_parser/bool_parser.py,sha256=al9fOJyo6o-Kpemy8DXICqeaZtl2zccx6SxF0wrue2s,7750
|
|
3
|
+
esp_bool_parser/constants.py,sha256=PzdTHNzOK08Pg1QnP3UeLZcNiSCEOhXWrhWENN3Nx2Y,2064
|
|
4
|
+
esp_bool_parser/soc_header.py,sha256=A-7nIsHcAKMv7uO6qCPN-BAfoAH8Qjypgg7TxYXXdZo,4368
|
|
5
|
+
esp_bool_parser/utils.py,sha256=WOgVQVg9PajUIsp0njV2AZ334zf9mcbINQBBobA0Sz4,526
|
|
6
|
+
esp_bool_parser-0.1.0.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
7
|
+
esp_bool_parser-0.1.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
|
|
8
|
+
esp_bool_parser-0.1.0.dist-info/METADATA,sha256=V5oRhsv2tKn9kXsSMk5QtXo5UrAK_FSuTUEOR4WU_yg,3264
|
|
9
|
+
esp_bool_parser-0.1.0.dist-info/RECORD,,
|