From df9dba8f38cf60c9a5ca172a8b44e4355a29156f Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Mon, 28 Nov 2022 23:59:30 +0900 Subject: [PATCH 1/3] feat: Set default value of output path as input path --- varst/utils/parser.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/varst/utils/parser.py b/varst/utils/parser.py index 16936a9..1ecffb1 100644 --- a/varst/utils/parser.py +++ b/varst/utils/parser.py @@ -37,7 +37,6 @@ def __init__(self) -> None: "--output", type=_file_type, help="rst file path as output", - default="./README.rst", ) def parse(self, argv: Optional[Sequence[str]]) -> None: @@ -51,8 +50,9 @@ def parse(self, argv: Optional[Sequence[str]]) -> None: args = self._parser.parse_args(argv) arg_dict = vars(args) - self.input_file = arg_dict['input'] - self.output_file = arg_dict['output'] + self.input_file = self.output_file = arg_dict['input'] + if arg_dict['output'] is not None: + self.output_file = arg_dict['output'] self.variables = _parse_kv(arg_dict['name=value']) From 75c6fb5c58484d6591a05f18eacf34fb6c3e5145 Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Tue, 29 Nov 2022 00:12:18 +0900 Subject: [PATCH 2/3] refactor: Rename variable in parser to sub_pairs --- tests/utils/test_parser.py | 4 ++-- varst/application.py | 2 +- varst/utils/parser.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/utils/test_parser.py b/tests/utils/test_parser.py index 6998cca..50179c7 100644 --- a/tests/utils/test_parser.py +++ b/tests/utils/test_parser.py @@ -17,8 +17,8 @@ def parser() -> Generator[Parser, None, None]: yield parser -def test_parse_variables(parser: Parser): - assert parser.variables == { +def test_parse_sub_pairs(parser: Parser): + assert parser.sub_pairs == { 'varst': 'variable to reStructuredText', 'version': '0.2.0', 'release': 'v0.2.0', diff --git a/varst/application.py b/varst/application.py index 84226ff..8e3c312 100644 --- a/varst/application.py +++ b/varst/application.py @@ -24,6 +24,6 @@ def run(self, argv: Optional[Sequence[str]]) -> None: rst_file = RstFile(src=self.parser.input_file) substitution = Substitution(rst_file) - for k, v in self.parser.variables.items(): + for k, v in self.parser.sub_pairs.items(): substitution.update(k, v) rst_file.save(dest=self.parser.output_file) diff --git a/varst/utils/parser.py b/varst/utils/parser.py index 1ecffb1..f10d96d 100644 --- a/varst/utils/parser.py +++ b/varst/utils/parser.py @@ -17,7 +17,7 @@ class Parser: input_file: str = "" output_file: str = "" - variables: Dict[str, str] = {} + sub_pairs: Dict[str, str] = {} def __init__(self) -> None: self._parser.add_argument( @@ -53,7 +53,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.variables = _parse_kv(arg_dict['name=value']) + self.sub_pairs = _parse_kv(arg_dict['name=value']) _VARIABLE_PATTERN = re.compile(r"[^=]+=[^=]+") From 36510188d99f01a0ab1a01359075081e77085ee9 Mon Sep 17 00:00:00 2001 From: Jeonghun-Ban Date: Tue, 29 Nov 2022 00:52:24 +0900 Subject: [PATCH 3/3] refactor: Change variable initialization to be done in init method --- varst/utils/parser.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/varst/utils/parser.py b/varst/utils/parser.py index f10d96d..8d7fdec 100644 --- a/varst/utils/parser.py +++ b/varst/utils/parser.py @@ -13,13 +13,12 @@ class Parser: """Parser class that parse arguments from cli""" - _parser = ArgumentParser() - - input_file: str = "" - output_file: str = "" - sub_pairs: Dict[str, str] = {} - def __init__(self) -> None: + self._parser = ArgumentParser() + self.input_file: str = "" + self.output_file: str = "" + self.sub_pairs: Dict[str, str] = {} + self._parser.add_argument( "name=value", nargs="*", help="name-value pairs of substitutions",