Skip to content

Commit

Permalink
Release version 0.0.0.dev52
Browse files Browse the repository at this point in the history
  • Loading branch information
AAriam committed Dec 6, 2024
1 parent 665acd8 commit 0831a1d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 48 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespaces = true
# ----------------------------------------- Project Metadata -------------------------------------
#
[project]
version = "0.0.0.dev51"
version = "0.0.0.dev52"
name = "PySerials"
dependencies = [
"jsonschema >= 4.21.0, < 5",
Expand All @@ -26,8 +26,8 @@ dependencies = [
"ruamel.yaml >= 0.17.32, < 0.18", # https://yaml.readthedocs.io/en/stable/
"ruamel.yaml.string >= 0.1.1, < 1",
"tomlkit >= 0.11.8, < 0.12", # https://tomlkit.readthedocs.io/en/stable/,
"MDit == 0.0.0.dev48",
"ExceptionMan == 0.0.0.dev48",
"MDit == 0.0.0.dev49",
"ExceptionMan == 0.0.0.dev49",
"ProtocolMan == 0.0.0.dev2",
]
requires-python = ">=3.10"
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ jsonpath-ng >= 1.6.1, < 2
ruamel.yaml >= 0.17.32, < 0.18
ruamel.yaml.string >= 0.1.1, < 1
tomlkit >= 0.11.8, < 0.12
MDit == 0.0.0.dev48
ExceptionMan == 0.0.0.dev48
MDit == 0.0.0.dev49
ExceptionMan == 0.0.0.dev49
ProtocolMan == 0.0.0.dev2
95 changes: 52 additions & 43 deletions src/pyserials/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def fill(
current_chain=(path,),
)

def _recursive_subst(self, templ, current_path: str, relative_path_anchor: str, level: int, current_chain: tuple[str, ...]):
def _recursive_subst(self, templ, current_path: str, relative_path_anchor: str, level: int, current_chain: tuple[str, ...], is_key: bool = False):

def get_code_value(match: _re.Match | str):

Expand Down Expand Up @@ -207,7 +207,6 @@ def getter_function(path: str, default: Any = None, search: bool = False):
path_invalid=current_path,
exception=e,
)
self._visited_paths[current_path] = (output, True)
return output

def get_address_value(match: _re.Match | str, return_all_matches: bool = False, from_code: bool = False):
Expand All @@ -224,9 +223,7 @@ def get_address_value(match: _re.Match | str, return_all_matches: bool = False,
)
if num_periods:
if relative_path_anchor != current_path:
path_fields = self._extract_fields(current_path)
has_template_key = any(field in self._template_keys for field in path_fields)
anchor_path = relative_path_anchor if has_template_key else current_path
anchor_path = relative_path_anchor if is_relative_template else current_path
else:
anchor_path = current_path
root_path_expr = anchor_path
Expand All @@ -241,8 +238,13 @@ def get_address_value(match: _re.Match | str, return_all_matches: bool = False,
)
root_path_expr = root_path_expr.left
path_expr = self._concat_json_paths(root_path_expr, path_expr)
value, matched = self._visited_paths.get(path_expr) or get_value(path_expr, return_all_matches, from_code)
self._visited_paths[path_expr] = (value, matched)
cached_result = self._visited_paths.get(path_expr)
if cached_result:
value, matched = cached_result
else:
value, matched = get_value(path_expr, return_all_matches, from_code)
if not self._is_relative_template(path_expr):
self._visited_paths[path_expr] = (value, matched)
if from_code:
return value, matched
if matched:
Expand Down Expand Up @@ -345,49 +347,49 @@ def raise_error(path_invalid: str, description_template: str, exception: Excepti
return self._visited_paths[current_path][0]

self._check_endless_loop(templ, current_chain)
is_relative_template = self._is_relative_template(current_path)

if isinstance(templ, str):
# Handle value blocks
pattern_value = self._get_value_regex_pattern(level=level)
match_value = pattern_value.fullmatch(templ)
if match_value:
return get_address_value(fill_nested_values(match_value))
if match_value := pattern_value.fullmatch(templ):
out = get_address_value(fill_nested_values(match_value))
# Handle list blocks
match_list = self._pattern_list.fullmatch(templ)
if match_list:
return get_address_value(fill_nested_values(match_list), return_all_matches=True)
elif match_list := self._pattern_list.fullmatch(templ):
out = get_address_value(fill_nested_values(match_list), return_all_matches=True)
# Handle code blocks
match_code = self._pattern_code.fullmatch(templ)
if match_code:
return get_code_value(match_code)
elif match_code := self._pattern_code.fullmatch(templ):
out = get_code_value(match_code)
# Handle unpack blocks
match_unpack = self._pattern_unpack.fullmatch(templ)
if match_unpack:
elif match_unpack := self._pattern_unpack.fullmatch(templ):
unpack_value = match_unpack.group(1)
submatch_code = self._pattern_code.fullmatch(unpack_value)
if submatch_code:
return get_code_value(submatch_code)
unpack_value = fill_nested_values(unpack_value)
submatch_list = self._pattern_list.fullmatch(unpack_value)
if submatch_list:
return get_address_value(submatch_list, return_all_matches=True)
return get_address_value(unpack_value)
if submatch_code := self._pattern_code.fullmatch(unpack_value):
out = get_code_value(submatch_code)
else:
unpack_value = fill_nested_values(unpack_value)
if submatch_list := self._pattern_list.fullmatch(unpack_value):
out = get_address_value(submatch_list, return_all_matches=True)
else:
out = get_address_value(unpack_value)
# Handle strings
code_blocks_filled = self._pattern_code.sub(
lambda x: self._stringer(get_code_value(x)),
templ
)
nested_values_filled = fill_nested_values(code_blocks_filled)
unpacked_filled = self._pattern_unpack.sub(string_filler_unpack, nested_values_filled)
lists_filled = self._pattern_list.sub(
lambda x: self._stringer(get_address_value(x)),
unpacked_filled
)
templ_values_filled = pattern_value.sub(
lambda x: self._stringer(get_address_value(x)),
lists_filled
)
return templ_values_filled
else:
code_blocks_filled = self._pattern_code.sub(
lambda x: self._stringer(get_code_value(x)),
templ
)
nested_values_filled = fill_nested_values(code_blocks_filled)
unpacked_filled = self._pattern_unpack.sub(string_filler_unpack, nested_values_filled)
lists_filled = self._pattern_list.sub(
lambda x: self._stringer(get_address_value(x)),
unpacked_filled
)
out = pattern_value.sub(
lambda x: self._stringer(get_address_value(x)),
lists_filled
)
if not is_relative_template and not is_key:
self._visited_paths[current_path] = (out, True)
return out

if isinstance(templ, list):
out = []
Expand All @@ -410,7 +412,8 @@ def raise_error(path_invalid: str, description_template: str, exception: Excepti
)
else:
out.append(elem_filled)
self._visited_paths[current_path] = (out, True)
if not is_relative_template:
self._visited_paths[current_path] = (out, True)
return out

if isinstance(templ, dict):
Expand All @@ -422,6 +425,7 @@ def raise_error(path_invalid: str, description_template: str, exception: Excepti
relative_path_anchor=relative_path_anchor,
level=0,
current_chain=current_chain,
is_key=True,
)
if isinstance(key, str) and self._pattern_unpack.fullmatch(key):
new_dict.update(key_filled)
Expand All @@ -437,7 +441,8 @@ def raise_error(path_invalid: str, description_template: str, exception: Excepti
level=0,
current_chain=current_chain + (new_path,),
)
self._visited_paths[current_path] = (new_dict, True)
if not is_relative_template:
self._visited_paths[current_path] = (new_dict, True)
return new_dict
return templ

Expand Down Expand Up @@ -471,6 +476,10 @@ def _get_value_regex_pattern(self, level: int = 0) -> _RegexPattern:
self._pattern_value[level] = pattern
return pattern

def _is_relative_template(self, jsonpath):
path_fields = self._extract_fields(jsonpath)
return any(field in self._template_keys for field in path_fields)

@staticmethod
def _remove_leading_periods(s: str) -> (str, int):
match = _re.match(r"^(\.*)(.*)", s)
Expand Down

0 comments on commit 0831a1d

Please sign in to comment.