Skip to content

Commit

Permalink
Raise SystemExit when templates are unmatched
Browse files Browse the repository at this point in the history
Some drive-by cleaning and typing.
  • Loading branch information
berland committed Sep 2, 2022
1 parent 895007c commit 51ed0f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
33 changes: 16 additions & 17 deletions semeio/jobs/design_kw/design_kw.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# pylint: disable=logging-fstring-interpolation
import logging
import shlex
import re

import shlex
from typing import List


_STATUS_FILE_NAME = "DESIGN_KW.OK"

_logger = logging.getLogger(__name__)


def run(
template_file_name,
result_file_name,
log_level,
parameters_file_name="parameters.txt",
):
template_file_name: str,
result_file_name: str,
log_level: int,
parameters_file_name: str = "parameters.txt",
) -> bool:
# Get all key, value pairs
# If FWL key is having multiple entries in the parameters file
# KeyError is raised. This will be logged, and no OK
Expand All @@ -36,22 +34,23 @@ def run(
with open(template_file_name, "r", encoding="utf-8") as template_file:
template = template_file.readlines()

if valid:
with open(result_file_name, "w", encoding="utf-8") as result_file:
for line in template:
if not is_comment(line):
for key, value in key_vals.items():
line = line.replace(f"<{key}>", str(value))
with open(result_file_name, "w", encoding="utf-8") as result_file:
for line in template:
if not is_comment(line):
for key, value in key_vals.items():
line = line.replace(f"<{key}>", str(value))

if not all_matched(line, template_file_name, template):
valid = False
if not all_matched(line, template_file_name, template):
valid = False

result_file.write(line)
result_file.write(line)

if valid:
with open(_STATUS_FILE_NAME, "w", encoding="utf-8") as status_file:
status_file.write("DESIGN_KW OK\n")

return valid


def all_matched(line, template_file_name, template):
valid = True
Expand Down
9 changes: 6 additions & 3 deletions semeio/jobs/scripts/design_kw.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable=logging-fstring-interpolation
import argparse
import logging
import sys

from semeio import valid_file
from semeio.jobs.design_kw import design_kw
Expand Down Expand Up @@ -50,12 +50,15 @@ def create_parser():
return parser


def main_entry_point():
def main_entry_point() -> bool:
parser = create_parser()
parsed_args = parser.parse_args()

design_kw.run(
valid = design_kw.run(
template_file_name=parsed_args.templatefile,
result_file_name=parsed_args.resultfile,
log_level=parsed_args.log_level,
)

if not valid:
sys.exit(1)
10 changes: 10 additions & 0 deletions tests/jobs/design_kw/test_design_kw_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def input_data(tmpdir):


_templatefile = "template.yml.tmpl"
_templatefile_unmatched = "template_unmatched.yml.tmpl"
_resultfile = "result.txt"
_log_level = "DEBUG"
_default_log_level = "WARNING"
Expand Down Expand Up @@ -63,3 +64,12 @@ def test_argparse_result_file_missing(monkeypatch):
monkeypatch.setattr(sys, "argv", ["script_name", _templatefile])
with pytest.raises(SystemExit):
design_kw.main_entry_point()


@pytest.mark.usefixtures("input_data")
def test_unmatched_template(monkeypatch):
monkeypatch.setattr(
sys, "argv", ["script_name", _templatefile_unmatched, _resultfile]
)
with pytest.raises(SystemExit):
design_kw.main_entry_point()

0 comments on commit 51ed0f9

Please sign in to comment.