From 16f1cfaacef5f305bb997824c073c071ef01bb9d Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Thu, 22 Dec 2022 16:20:22 +0900 Subject: [PATCH 1/6] test: Add test case to update substitutions --- tests/utils/test_substitution.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/utils/test_substitution.py b/tests/utils/test_substitution.py index 66cb501..5607fe7 100644 --- a/tests/utils/test_substitution.py +++ b/tests/utils/test_substitution.py @@ -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""" From d5d6b9f0b267728f7fb0a62c58adefb8946a7961 Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Thu, 22 Dec 2022 16:36:07 +0900 Subject: [PATCH 2/6] refactor: Rename function to create substition definition --- tests/utils/test_substitution.py | 4 ++-- varst/utils/substitution.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/utils/test_substitution.py b/tests/utils/test_substitution.py index 5607fe7..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', @@ -40,4 +40,4 @@ def test_update_substitution(substitution: Substitution): 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/substitution.py b/varst/utils/substitution.py index 30b7d95..f3b8ef1 100644 --- a/varst/utils/substitution.py +++ b/varst/utils/substitution.py @@ -35,15 +35,15 @@ def update(self, name: str, value: str) -> None: """ origin = self.find(name) - new = substitution_text(name, value) + new = substitution_def(name, value) 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(name: str, value: str) -> str: + """Create substitution definition by using name and value. Args: name: The string value of substitution name. From f60d1e4dc84c50ecc02895ca2dc31ce72ae8504c Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Thu, 22 Dec 2022 16:49:41 +0900 Subject: [PATCH 3/6] docs: Update docstring to corrert term --- varst/utils/substitution.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/varst/utils/substitution.py b/varst/utils/substitution.py index f3b8ef1..3f9ade1 100644 --- a/varst/utils/substitution.py +++ b/varst/utils/substitution.py @@ -9,14 +9,14 @@ def __init__(self, rst_file: RstFile): self.rst_file = rst_file def find(self, name: str) -> str: - """Find substitution text by name. + """Find substitution definition by name. Args: name: The string value of substitution name. 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})\|""" @@ -27,7 +27,7 @@ def find(self, name: str) -> str: raise KeyError(name) def update(self, name: str, value: str) -> None: - """Update substitution text with name to value. + """Update substitution definition with name to value. Args: name: The string value of substitution name. @@ -49,7 +49,7 @@ def substitution_def(name: str, value: str) -> str: name: The string value of substitution name. value: The string value of substitution text. Returns: - Returns substitution text. + Returns substitution definition. """ return f'.. |{name}| replace:: {value}' From 16353a3fe7fb762ca79de84887b3d8d32c088c51 Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Thu, 22 Dec 2022 16:56:21 +0900 Subject: [PATCH 4/6] refactor: Rename parameters in subsitution utils --- varst/utils/substitution.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/varst/utils/substitution.py b/varst/utils/substitution.py index 3f9ade1..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 definition 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 definition if it exists. Raises: 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 definition 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_def(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_def(name: str, value: str) -> str: - """Create substitution definition 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 definition. """ - return f'.. |{name}| replace:: {value}' + return f'.. |{text}| replace:: {data}' From dd364ac29e531f94e3583a941ebada2440e1c6b8 Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Thu, 22 Dec 2022 17:19:19 +0900 Subject: [PATCH 5/6] refactor(parser): Rename argument to substitutions --- varst/utils/parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/varst/utils/parser.py b/varst/utils/parser.py index 8d7fdec..a37926c 100644 --- a/varst/utils/parser.py +++ b/varst/utils/parser.py @@ -20,7 +20,7 @@ def __init__(self) -> None: self.sub_pairs: Dict[str, str] = {} self._parser.add_argument( - "name=value", nargs="*", + "substitutions", nargs="*", help="name-value pairs of substitutions", type=_pattern_type, ) @@ -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"[^=]+=[^=]+") From 834957868939bf389af0178dd42c966cae89fa69 Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Thu, 22 Dec 2022 17:21:54 +0900 Subject: [PATCH 6/6] refactor(parser): Update help message for substitution argument --- varst/utils/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/varst/utils/parser.py b/varst/utils/parser.py index a37926c..7933d73 100644 --- a/varst/utils/parser.py +++ b/varst/utils/parser.py @@ -21,7 +21,7 @@ def __init__(self) -> None: self._parser.add_argument( "substitutions", nargs="*", - help="name-value pairs of substitutions", + help="pairs of substitution definition. format is 'text=data'", type=_pattern_type, ) self._parser.add_argument(