From 18780cf14023a960aa00ccc78fc58203c762703e Mon Sep 17 00:00:00 2001 From: Hannah Eslinger Date: Mon, 28 Aug 2023 14:03:02 -0600 Subject: [PATCH 1/3] Make migrations scripts --- migration_scripts/README.md | 1 + migration_scripts/upgrade_from_2.4_to_2.5.py | 30 ++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 migration_scripts/README.md create mode 100644 migration_scripts/upgrade_from_2.4_to_2.5.py diff --git a/migration_scripts/README.md b/migration_scripts/README.md new file mode 100644 index 00000000..a9c9bd28 --- /dev/null +++ b/migration_scripts/README.md @@ -0,0 +1 @@ +These scripts each take in a BuildingSync file and upgrade it from one schema to another. \ No newline at end of file diff --git a/migration_scripts/upgrade_from_2.4_to_2.5.py b/migration_scripts/upgrade_from_2.4_to_2.5.py new file mode 100644 index 00000000..9b764a9d --- /dev/null +++ b/migration_scripts/upgrade_from_2.4_to_2.5.py @@ -0,0 +1,30 @@ +""" +Simply makes UsefulLife an int. +""" +import argparse +from lxml import etree as ET + +def get_args(): + parser = argparse.ArgumentParser() + parser.add_argument("file_path", type=str, help="path to file to update") + return parser.parse_args() + +def update_usefulLife(tree): + for usefulLife in tree.findall('.//*{http://buildingsync.net/schemas/bedes-auc/2019}UsefulLife'): + usefulLife.text = usefulLife.text.split(".", 1)[0] + +def main(): + file_path = get_args().file_path + + try: + tree = ET.parse(file_path) + except (FileNotFoundError, ET.ParseError) as e: + exit(f"File could not be read \n{e} \naborting...") + + update_usefulLife(tree) + + tree.write(file_path) + + +if __name__ == "__main__": + main() From 7361a79b71a3882336c6898c445ceae79ab04db3 Mon Sep 17 00:00:00 2001 From: Hannah Eslinger Date: Mon, 28 Aug 2023 16:41:52 -0600 Subject: [PATCH 2/3] Address comments --- migration_scripts/README.md | 10 ++++- migration_scripts/upgrade_from_2.4_to_2.5.py | 43 ++++++++++++++++---- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/migration_scripts/README.md b/migration_scripts/README.md index a9c9bd28..ab0c01dd 100644 --- a/migration_scripts/README.md +++ b/migration_scripts/README.md @@ -1 +1,9 @@ -These scripts each take in a BuildingSync file and upgrade it from one schema to another. \ No newline at end of file +These scripts each take in a BuildingSync file and upgrade it from one schema to another. ex: + +``` +python migration_scripts/upgrade_from_2.4_to_2.5.py path/to/file +``` +or, if you want to write to a new file: +``` +python migration_scripts/upgrade_from_2.4_to_2.5.py path/to/file --to_file ./new_file.xml +``` \ No newline at end of file diff --git a/migration_scripts/upgrade_from_2.4_to_2.5.py b/migration_scripts/upgrade_from_2.4_to_2.5.py index 9b764a9d..ba35ed06 100644 --- a/migration_scripts/upgrade_from_2.4_to_2.5.py +++ b/migration_scripts/upgrade_from_2.4_to_2.5.py @@ -2,28 +2,57 @@ Simply makes UsefulLife an int. """ import argparse +import logging from lxml import etree as ET + +logging.basicConfig(level=logging.INFO) + + def get_args(): parser = argparse.ArgumentParser() - parser.add_argument("file_path", type=str, help="path to file to update") + parser.add_argument("from_file", type=str, help="path to file to update") + parser.add_argument( + "--to_file", + type=str, + help="path to write update to", + required=False, + default=None, + ) return parser.parse_args() + +def update_version(tree): + tree.getroot().set("version", "2.5.0") + + def update_usefulLife(tree): - for usefulLife in tree.findall('.//*{http://buildingsync.net/schemas/bedes-auc/2019}UsefulLife'): - usefulLife.text = usefulLife.text.split(".", 1)[0] + for usefulLife in tree.findall( + ".//*{http://buildingsync.net/schemas/bedes-auc/2019}UsefulLife" + ): + new_value = str(round(float(usefulLife.text))) + if new_value != usefulLife.text: + logging.info( + f"changing {tree.getelementpath(usefulLife)} value from {usefulLife.text} to {new_value}" + ) + usefulLife.text = new_value + def main(): - file_path = get_args().file_path + from_file, to_file = get_args().from_file, get_args().to_file try: - tree = ET.parse(file_path) + tree = ET.parse(from_file) except (FileNotFoundError, ET.ParseError) as e: exit(f"File could not be read \n{e} \naborting...") + update_version(tree) update_usefulLife(tree) - - tree.write(file_path) + + if to_file is None: + to_file = from_file + + tree.write(to_file) if __name__ == "__main__": From 32c70fabb94fd9ef205a9414eb5fb482ae802136 Mon Sep 17 00:00:00 2001 From: Hannah Eslinger Date: Wed, 30 Aug 2023 14:40:11 -0600 Subject: [PATCH 3/3] Add comment --- migration_scripts/upgrade_from_2.4_to_2.5.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/migration_scripts/upgrade_from_2.4_to_2.5.py b/migration_scripts/upgrade_from_2.4_to_2.5.py index ba35ed06..05aad55b 100644 --- a/migration_scripts/upgrade_from_2.4_to_2.5.py +++ b/migration_scripts/upgrade_from_2.4_to_2.5.py @@ -23,7 +23,10 @@ def get_args(): def update_version(tree): - tree.getroot().set("version", "2.5.0") + root = tree.getroot() + + root.set("version", "2.5.0") + root.insert(0, ET.Comment('This BuildingSync v2.5 document was generated from a BuildingSync v2.4 document via the BuildingSync migration scripts')) def update_usefulLife(tree):