Skip to content

Commit

Permalink
change TemplateFiller
Browse files Browse the repository at this point in the history
  • Loading branch information
AAriam committed Nov 1, 2024
1 parent cdcccf5 commit 48fb342
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 211 deletions.
70 changes: 18 additions & 52 deletions src/pyserials/nested_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ def __init__(
end_list: str = "]]$",
end_unpack: str = "}}*",
end_code: str = "}}#",
recursive: bool = True,
raise_no_match: bool = True,
leave_no_match: bool = False,
no_match_value: Any = None,
code_context: dict[str, Any] | None = None,
code_context_partial: dict[str, Callable | tuple[Callable, str]] | None = None,
code_context_call: dict[str, Callable[[Callable], Any]] | None = None,
stringer: Callable[[str], str] = str,
unpack_string_joiner: str = ", ",
relative_template_keys: list[str] | None = None,
implicit_root: bool = True,
getter_function_name: str = "get",
):
self._data = data or {}
self._templater = _ps.update.TemplateFiller(
Expand All @@ -47,29 +49,21 @@ def __init__(
end_list=end_list,
end_unpack=end_unpack,
end_code=end_code,
raise_no_match=raise_no_match,
leave_no_match=leave_no_match,
no_match_value=no_match_value,
code_context=code_context,
code_context_partial=code_context_partial,
code_context_call=code_context_call,
stringer=stringer,
unpack_string_joiner=unpack_string_joiner,
relative_template_keys=relative_template_keys,
implicit_root=implicit_root,
getter_function_name=getter_function_name,
)
self._recursive = recursive
self._raise_no_match = raise_no_match
self._leave_no_match = leave_no_match
self._no_match_value = no_match_value
self._code_context = code_context or {}
self._stringer = stringer
self._unpack_string_joiner = unpack_string_joiner
self._relative_template_keys = relative_template_keys or []
self._implicit_root = implicit_root
return

def fill(
self,
path: str = "",
recursive: bool | None = None,
raise_no_match: bool | None = None,
leave_no_match: bool | None = None,
code_context: dict[str, Any] | None = None,
stringer: Callable[[str], str] | None = None,
unpack_string_joiner: str | None = None,
level: int = 0,
):
def fill(self, path: str = ""):
if not path:
value = self._data
else:
Expand All @@ -79,46 +73,18 @@ def fill(
filled_value = self.fill_data(
data=value,
current_path=path,
recursive=recursive,
raise_no_match=raise_no_match,
leave_no_match=leave_no_match,
code_context=code_context,
stringer=stringer,
unpack_string_joiner=unpack_string_joiner,
level=level,
)
if not path:
self._data = filled_value
else:
self.__setitem__(path, filled_value)
return filled_value

def fill_data(
self,
data,
current_path: str = "",
recursive: bool | None = None,
raise_no_match: bool | None = None,
leave_no_match: bool | None = None,
stringer: Callable[[str], str] | None = None,
code_context: dict[str, Any] | None = None,
unpack_string_joiner: str | None = None,
level: int = 0,
):
def fill_data(self, data, current_path: str = ""):
return self._templater.fill(
templated_data=data,
source_data=self._data,
data=self._data,
template=data,
current_path=current_path,
recursive=recursive if recursive is not None else self._recursive,
raise_no_match=raise_no_match if raise_no_match is not None else self._raise_no_match,
leave_no_match=leave_no_match if leave_no_match is not None else self._leave_no_match,
no_match_value=self._no_match_value,
code_context=code_context if code_context is not None else self._code_context,
stringer=stringer if stringer is not None else self._stringer,
unpack_string_joiner=unpack_string_joiner if unpack_string_joiner is not None else self._unpack_string_joiner,
relative_template_keys=self._relative_template_keys,
implicit_root=self._implicit_root,
level=level,
)

def __call__(self):
Expand Down
Loading

0 comments on commit 48fb342

Please sign in to comment.