Codestyling
This commit is contained in:
parent
d900f9296a
commit
b0a5de05aa
@ -1,4 +1,5 @@
|
|||||||
import argparse, json
|
import argparse
|
||||||
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .template import DocxTemplate, TemplateError
|
from .template import DocxTemplate, TemplateError
|
||||||
@ -45,7 +46,7 @@ def get_args(parser: argparse.ArgumentParser) -> dict:
|
|||||||
raise RuntimeError(f'Correct usage is:\n{parser.usage}') from e
|
raise RuntimeError(f'Correct usage is:\n{parser.usage}') from e
|
||||||
|
|
||||||
|
|
||||||
def is_argument_valid(arg_name: str, arg_value: str,overwrite: bool) -> bool:
|
def is_argument_valid(arg_name: str, arg_value: str, overwrite: bool) -> bool:
|
||||||
# Basic checks for the arguments
|
# Basic checks for the arguments
|
||||||
if arg_name == TEMPLATE_ARG:
|
if arg_name == TEMPLATE_ARG:
|
||||||
return Path(arg_value).is_file() and arg_value.endswith('.docx')
|
return Path(arg_value).is_file() and arg_value.endswith('.docx')
|
||||||
@ -58,7 +59,7 @@ def is_argument_valid(arg_name: str, arg_value: str,overwrite: bool) -> bool:
|
|||||||
return arg_value in [True, False]
|
return arg_value in [True, False]
|
||||||
|
|
||||||
|
|
||||||
def check_exists_ask_overwrite(arg_value:str, overwrite: bool) -> bool:
|
def check_exists_ask_overwrite(arg_value: str, overwrite: bool) -> bool:
|
||||||
# If output file does not exist or command was run with overwrite option,
|
# If output file does not exist or command was run with overwrite option,
|
||||||
# returns True, else asks for overwrite confirmation. If overwrite is
|
# returns True, else asks for overwrite confirmation. If overwrite is
|
||||||
# confirmed returns True, else raises FileExistsError.
|
# confirmed returns True, else raises FileExistsError.
|
||||||
@ -74,12 +75,12 @@ def check_exists_ask_overwrite(arg_value:str, overwrite: bool) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def validate_all_args(parsed_args:dict) -> None:
|
def validate_all_args(parsed_args: dict) -> None:
|
||||||
overwrite = parsed_args[OVERWRITE_ARG]
|
overwrite = parsed_args[OVERWRITE_ARG]
|
||||||
# Raises AssertionError if any of the arguments is not validated
|
# Raises AssertionError if any of the arguments is not validated
|
||||||
try:
|
try:
|
||||||
for arg_name, arg_value in parsed_args.items():
|
for arg_name, arg_value in parsed_args.items():
|
||||||
if not is_argument_valid(arg_name, arg_value,overwrite):
|
if not is_argument_valid(arg_name, arg_value, overwrite):
|
||||||
raise AssertionError
|
raise AssertionError
|
||||||
except AssertionError as e:
|
except AssertionError as e:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
@ -104,12 +105,12 @@ 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: dict) -> DocxTemplate:
|
def render_docx(doc: DocxTemplate, json_data: dict) -> DocxTemplate:
|
||||||
try:
|
try:
|
||||||
doc.render(json_data)
|
doc.render(json_data)
|
||||||
return doc
|
return doc
|
||||||
except TemplateError as e:
|
except TemplateError as e:
|
||||||
raise RuntimeError(f'An error ocurred while trying to render the docx') from e
|
raise RuntimeError('An error ocurred while trying to render the docx') from e
|
||||||
|
|
||||||
|
|
||||||
def save_file(doc: DocxTemplate, parsed_args: dict) -> None:
|
def save_file(doc: DocxTemplate, parsed_args: dict) -> None:
|
||||||
@ -133,7 +134,7 @@ def main() -> None:
|
|||||||
validate_all_args(parsed_args)
|
validate_all_args(parsed_args)
|
||||||
json_data = get_json_data(Path(parsed_args[JSON_ARG]).resolve())
|
json_data = get_json_data(Path(parsed_args[JSON_ARG]).resolve())
|
||||||
doc = make_docxtemplate(Path(parsed_args[TEMPLATE_ARG]).resolve())
|
doc = make_docxtemplate(Path(parsed_args[TEMPLATE_ARG]).resolve())
|
||||||
doc = render_docx(doc,json_data)
|
doc = render_docx(doc, json_data)
|
||||||
save_file(doc, parsed_args)
|
save_file(doc, parsed_args)
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
print('Error: '+e.__str__())
|
print('Error: '+e.__str__())
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import sys, os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
TEMPLATE_PATH = 'templates/module_execute_tpl.docx'
|
TEMPLATE_PATH = 'templates/module_execute_tpl.docx'
|
||||||
@ -19,4 +19,3 @@ os.system(cmd)
|
|||||||
|
|
||||||
if output_path.exists():
|
if output_path.exists():
|
||||||
print(f' --> File {output_path.resolve()} has been generated.')
|
print(f' --> File {output_path.resolve()} has been generated.')
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user