Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions migration_scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
```
62 changes: 62 additions & 0 deletions migration_scripts/upgrade_from_2.4_to_2.5.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
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("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):
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):
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():
from_file, to_file = get_args().from_file, get_args().to_file

try:
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)

if to_file is None:
to_file = from_file

tree.write(to_file)


if __name__ == "__main__":
main()