diff --git a/tests/utils/test_substitution.py b/tests/utils/test_substitution.py index 66cb501..60a49aa 100644 --- a/tests/utils/test_substitution.py +++ b/tests/utils/test_substitution.py @@ -5,7 +5,7 @@ from varst.utils.rst_file import RstFile from varst.utils.substitution import Substitution -from varst.utils.substitution import substitution_text +from varst.utils.substitution import substitution_def TEST_DATA_PATH = os.path.join( Path(__file__).resolve().parent.parent, 'data', @@ -29,5 +29,15 @@ def test_find_substitution(substitution: Substitution): substitution.find("not exist name") +def test_update_substitution(substitution: Substitution): + substitution.update("status", "true") + substitution.update("with whitespace", "true") + + assert substitution.find("status") == """.. |status| replace:: true\n""" + assert substitution.find( + "with whitespace", + ) == """.. |with whitespace| replace:: true\n""" + + def test_create_substitution(): - assert substitution_text("key", "value") == """.. |key| replace:: value""" + assert substitution_def("key", "value") == """.. |key| replace:: value""" diff --git a/varst/utils/parser.py b/varst/utils/parser.py index 8d7fdec..7933d73 100644 --- a/varst/utils/parser.py +++ b/varst/utils/parser.py @@ -20,8 +20,8 @@ def __init__(self) -> None: self.sub_pairs: Dict[str, str] = {} self._parser.add_argument( - "name=value", nargs="*", - help="name-value pairs of substitutions", + "substitutions", nargs="*", + help="pairs of substitution definition. format is 'text=data'", type=_pattern_type, ) self._parser.add_argument( @@ -52,7 +52,7 @@ def parse(self, argv: Optional[Sequence[str]]) -> None: self.input_file = self.output_file = arg_dict['input'] if arg_dict['output'] is not None: self.output_file = arg_dict['output'] - self.sub_pairs = _parse_kv(arg_dict['name=value']) + self.sub_pairs = _parse_kv(arg_dict['substitutions']) _VARIABLE_PATTERN = re.compile(r"[^=]+=[^=]+") diff --git a/varst/utils/substitution.py b/varst/utils/substitution.py index 30b7d95..64afdea 100644 --- a/varst/utils/substitution.py +++ b/varst/utils/substitution.py @@ -8,48 +8,48 @@ class Substitution: def __init__(self, rst_file: RstFile): self.rst_file = rst_file - def find(self, name: str) -> str: - """Find substitution text by name. + def find(self, text: str) -> str: + """Find substitution definition by substitution text. Args: - name: The string value of substitution name. + text: The string value of substitution text. Returns: - Returns substitution text if substitution exists. + Returns substitution definition if it exists. Raises: - KeyError: If substitution is not in file. + KeyError: If substitution definition is not in file. """ - pattern = fr"""\.\.[ ]+\|({name})\|""" + pattern = fr"""\.\.[ ]+\|({text})\|""" for content in self.rst_file.contents: if re.match(pattern, content): return content - raise KeyError(name) + raise KeyError(text) - def update(self, name: str, value: str) -> None: - """Update substitution text with name to value. + def update(self, text: str, data: str) -> None: + """Update substitution definition by using substitution text and data. Args: - name: The string value of substitution name. - value: The string value of substitution text. + text: The string value of substitution text. + data: The string value of substitution data. """ - origin = self.find(name) - new = substitution_text(name, value) + origin = self.find(text) + new = substitution_def(text, data) origin_idx = self.rst_file.contents.index(origin) self.rst_file.contents.pop(origin_idx) self.rst_file.contents.insert(origin_idx, new + "\n") -def substitution_text(name: str, value: str) -> str: - """Create substitution text by using name and value. +def substitution_def(text: str, data: str) -> str: + """Create substitution definition by using substitution text and data. Args: - name: The string value of substitution name. - value: The string value of substitution text. + text: The string value of substitution text. + data: The string value of substitution data. Returns: - Returns substitution text. + Returns substitution definition. """ - return f'.. |{name}| replace:: {value}' + return f'.. |{text}| replace:: {data}'