Skip to content

Commit f301f38

Browse files
committed
add saving method
1 parent 0181a1e commit f301f38

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

csv2cmi.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,17 @@ def replace_short_titles(self, project: configparser) -> None:
484484
# if there is no matching section, we assume that there shouldn't be one
485485
pass
486486

487+
def save_to_file(self, file_name: Path) -> None:
488+
"""Save CMI to file."""
489+
tree = ElementTree(self.cmi)
490+
try:
491+
tree.write(file_name, encoding="utf-8", xml_declaration=True, method="xml")
492+
print(f"CMI file written to {file_name}")
493+
sys.exit(0)
494+
except PermissionError:
495+
logging.error("Could not save the file due to insufficient permission")
496+
sys.exit(1)
497+
487498

488499
if __name__ == "__main__":
489500
args = parser.parse_args()
@@ -621,16 +632,9 @@ def replace_short_titles(self, project: configparser) -> None:
621632
cmi_object.replace_short_titles(config)
622633

623634
# save cmi to file
624-
tree = ElementTree(cmi_object.cmi)
625635
if args.output:
626636
letters_xml = Path(args.output)
627637
else:
628638
letters_xml = letters_csv.with_suffix(".xml")
629639

630-
try:
631-
tree.write(letters_xml, encoding="utf-8", xml_declaration=True, method="xml")
632-
print(f"CMI file written to {letters_xml}")
633-
sys.exit(0)
634-
except PermissionError:
635-
logging.error("Could not save the file due to insufficient permission")
636-
sys.exit(1)
640+
cmi_object.save_to_file(letters_xml)

0 commit comments

Comments
 (0)