Added function for rendering docx instead of directly calling method on DocxTemplate instance. Added exception chaining to a few functions there were missing it.

This commit is contained in:
Lucas 2021-09-29 10:35:26 -03:00
parent a78462ee0f
commit b6f9c992f5

View File

@ -32,9 +32,9 @@ def get_args(parser: argparse.ArgumentParser) -> dict:
# --help or -h flag raises a SystemExit with code 0. # --help or -h flag raises a SystemExit with code 0.
except SystemExit as e: except SystemExit as e:
if e.code == 0: if e.code == 0:
raise RuntimeError() raise RuntimeError() from e
else: else:
raise RuntimeError(f'Correct usage is:\n{parser.usage}') raise RuntimeError(f'Correct usage is:\n{parser.usage}') from e
def is_argument_valid(arg_name: str, arg_value: str) -> bool: def is_argument_valid(arg_name: str, arg_value: str) -> bool:
@ -72,7 +72,7 @@ def validate_all_args(parsed_args:dict) -> None:
raise argparse.ArgumentError raise argparse.ArgumentError
except argparse.ArgumentError as e: except argparse.ArgumentError as e:
raise RuntimeError( raise RuntimeError(
f'The specified {arg_name} "{arg_value}" is not valid.') f'The specified {arg_name} "{arg_value}" is not valid.') from e
def get_json_data(json_path: Path) -> dict: def get_json_data(json_path: Path) -> dict:
@ -83,7 +83,7 @@ def get_json_data(json_path: Path) -> dict:
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
print( print(
f'There was an error on line {e.lineno}, column {e.colno} while trying to parse file {json_path}') f'There was an error on line {e.lineno}, column {e.colno} while trying to parse file {json_path}')
raise RuntimeError('Failed to get json data.') raise RuntimeError('Failed to get json data.') from e
def make_docxtemplate(template_path: Path) -> DocxTemplate: def make_docxtemplate(template_path: Path) -> DocxTemplate:
@ -93,6 +93,14 @@ def make_docxtemplate(template_path: Path) -> DocxTemplate:
raise RuntimeError('Could not create docx template.') from e raise RuntimeError('Could not create docx template.') from e
def render_docx(doc:DocxTemplate, json_data) -> DocxTemplate:
try:
doc.render(json_data)
return doc
except TemplateError as e:
raise RuntimeError(f'An error ocurred while trying to render the docx') from e
def save_file(doc: DocxTemplate, output_path: Path) -> None: def save_file(doc: DocxTemplate, output_path: Path) -> None:
try: try:
doc.save(output_path) doc.save(output_path)
@ -112,7 +120,7 @@ def main() -> None:
validate_all_args(parsed_args) validate_all_args(parsed_args)
json_data = get_json_data(Path(parsed_args['Json']).resolve()) json_data = get_json_data(Path(parsed_args['Json']).resolve())
doc = make_docxtemplate(Path(parsed_args['Template']).resolve()) doc = make_docxtemplate(Path(parsed_args['Template']).resolve())
doc.render(json_data) doc = render_docx(doc,json_data)
save_file(doc, Path(parsed_args['Output']).resolve()) save_file(doc, Path(parsed_args['Output']).resolve())
except RuntimeError as e: except RuntimeError as e:
print(e) print(e)