Changed code to be python < 3.6 compatible
This commit is contained in:
parent
5dfe448a9a
commit
5e8aeae272
@ -41,9 +41,9 @@ def get_args(parser):
|
|||||||
# --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 SystemExit from e
|
raise SystemExit
|
||||||
else:
|
else:
|
||||||
raise RuntimeError('Correct usage is:\n{parser.usage}'.format(parser=parser)) from e
|
raise RuntimeError('Correct usage is:\n{parser.usage}'.format(parser=parser))
|
||||||
|
|
||||||
|
|
||||||
def is_argument_valid(arg_name, arg_value, overwrite):
|
def is_argument_valid(arg_name, arg_value, overwrite):
|
||||||
@ -70,8 +70,8 @@ def check_exists_ask_overwrite(arg_value, overwrite):
|
|||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
raise FileExistsError
|
raise FileExistsError
|
||||||
except FileExistsError as e:
|
except FileExistsError:
|
||||||
raise RuntimeError('File %s already exists, please choose a different name.' % arg_value) from e
|
raise RuntimeError('File %s already exists, please choose a different name.' % arg_value)
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -83,11 +83,11 @@ def validate_all_args(parsed_args):
|
|||||||
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:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
'The specified {arg_name} "{arg_value}" is not valid.'.format(
|
'The specified {arg_name} "{arg_value}" is not valid.'.format(
|
||||||
arg_name=arg_name, arg_value=arg_value
|
arg_name=arg_name, arg_value=arg_value
|
||||||
)) from e
|
))
|
||||||
|
|
||||||
|
|
||||||
def get_json_data(json_path):
|
def get_json_data(json_path):
|
||||||
@ -100,22 +100,22 @@ def get_json_data(json_path):
|
|||||||
'There was an error on line {e.lineno}, column {e.colno} while trying to parse file {json_path}'.format(
|
'There was an error on line {e.lineno}, column {e.colno} while trying to parse file {json_path}'.format(
|
||||||
e=e, json_path=json_path
|
e=e, json_path=json_path
|
||||||
))
|
))
|
||||||
raise RuntimeError('Failed to get json data.') from e
|
raise RuntimeError('Failed to get json data.')
|
||||||
|
|
||||||
|
|
||||||
def make_docxtemplate(template_path):
|
def make_docxtemplate(template_path):
|
||||||
try:
|
try:
|
||||||
return DocxTemplate(template_path)
|
return DocxTemplate(template_path)
|
||||||
except TemplateError as e:
|
except TemplateError:
|
||||||
raise RuntimeError('Could not create docx template.') from e
|
raise RuntimeError('Could not create docx template.')
|
||||||
|
|
||||||
|
|
||||||
def render_docx(doc, json_data):
|
def render_docx(doc, json_data):
|
||||||
try:
|
try:
|
||||||
doc.render(json_data)
|
doc.render(json_data)
|
||||||
return doc
|
return doc
|
||||||
except TemplateError as e:
|
except TemplateError:
|
||||||
raise RuntimeError('An error ocurred while trying to render the docx') from e
|
raise RuntimeError('An error ocurred while trying to render the docx')
|
||||||
|
|
||||||
|
|
||||||
def save_file(doc: DocxTemplate, parsed_args: dict) -> None:
|
def save_file(doc: DocxTemplate, parsed_args: dict) -> None:
|
||||||
@ -126,7 +126,7 @@ def save_file(doc: DocxTemplate, parsed_args: dict) -> None:
|
|||||||
print('Document successfully generated and saved at {output_path}'.format(output_path=output_path))
|
print('Document successfully generated and saved at {output_path}'.format(output_path=output_path))
|
||||||
except PermissionError as e:
|
except PermissionError as e:
|
||||||
print('{e.strerror}. Could not save file {e.filename}.'.foramt(e=e))
|
print('{e.strerror}. Could not save file {e.filename}.'.foramt(e=e))
|
||||||
raise RuntimeError('Failed to save file.') from e
|
raise RuntimeError('Failed to save file.')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user