-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(system): Added new method to save the system to a given folder. #23
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||
# How to save the system | ||||||
|
||||||
## Exporting the entire contents of the system | ||||||
|
||||||
If the user wants to createa folder that containts all the information of the | ||||||
system `infrasys` provides a simple method `system.save("my_folder")` that | ||||||
creates a folder (if it does not exists) and save all the contents of the | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
system including the `system.to_json()` and the time series arrow files and | ||||||
sqlite. | ||||||
|
||||||
To archive the system into a zip file, the user can use `system.save("my_folder", | ||||||
zip=True)`. This will create a zip folder of the contents of `my_folder` and | ||||||
delete the folder once the archive is done. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
"""Defines a System""" | ||
|
||
import json | ||
import shutil | ||
from operator import itemgetter | ||
from collections import defaultdict | ||
from datetime import datetime | ||
|
@@ -284,6 +285,58 @@ def from_dict( | |
logger.info("Deserialized system {}", system.label) | ||
return system | ||
|
||
def save( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a |
||
self, | ||
fpath: Path | str, | ||
filename: str = "system.json", | ||
zip: bool = False, | ||
overwrite: bool = False, | ||
) -> None: | ||
"""Save the contents of a system and the Time series in a single directory. | ||
|
||
By default, this method creates the user specified folder using the | ||
`to_json` method. If user sets `zip = True`, we create the folder of | ||
the user (if it does not exists), zip it to the same location specified | ||
and delete the folder. | ||
|
||
Parameters | ||
---------- | ||
fpath : Path | str | ||
Filepath to write the contents of the system. | ||
zip : bool | ||
Set to True if you want to archive to a zip file. | ||
filename: str | ||
Name of the sytem to serialize. Default value: "system.json". | ||
overwrite: bool | ||
Overwrites the system if it already exist on the fpath. | ||
|
||
Examples | ||
-------- | ||
>>> system.save("/home/$USER/systems/my_system") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this trick some users into thinking that |
||
INFO: Wrote system data to systems/system.json | ||
INFO: Copied time series data to systems/my_system/system_time_series | ||
|
||
>>> system.save("/home/$USER/systems/my_system", zip=True) | ||
INFO: Wrote system data to systems/system1.json | ||
INFO: Copied time series data to systems/system1_time_series | ||
|
||
|
||
See Also | ||
-------- | ||
to_json: System serialization | ||
""" | ||
if isinstance(fpath, str): | ||
fpath = Path(fpath) | ||
self.to_json(fpath / filename, overwrite=overwrite) | ||
|
||
if zip: | ||
logger.info("Archiving system and time series into a single file") | ||
_ = shutil.make_archive(str(fpath), "zip", fpath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel uncertain about this behavior because it will zip whatever else was in the directory (and then delete it). Would it be better to require that the directory not exist and use the overwrite flag to delete it? The result would be that |
||
logger.debug("Removing {}", fpath) | ||
shutil.rmtree(fpath) | ||
|
||
return | ||
|
||
def add_component(self, component: Component, **kwargs) -> None: | ||
"""Add one component to the system. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.