Skip to content

Commit

Permalink
Merge pull request #37 from junghoon-vans/refactor/correct-term-in-co…
Browse files Browse the repository at this point in the history
…de-to-follow-syntax-of-docutils

refactor: Correct term in code to follow syntax of docutils
  • Loading branch information
junghoon-vans authored Dec 22, 2022
2 parents 61f5081 + 8349578 commit 558c91d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
14 changes: 12 additions & 2 deletions tests/utils/test_substitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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"""
6 changes: 3 additions & 3 deletions varst/utils/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"[^=]+=[^=]+")
Expand Down
38 changes: 19 additions & 19 deletions varst/utils/substitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'

0 comments on commit 558c91d

Please sign in to comment.