Midnighter / structurizr-python

A Python 3 port of Simon Brown's Structurizr diagrams-as-code architecture description tool.
https://structurizr.com/
Apache License 2.0
66 stars 16 forks source link

Support for loading/saving workspace from/to file and string #48

Closed yt-ms closed 3 years ago

yt-ms commented 3 years ago

Checklist

Describe the solution you would like.

To make it simpler to work with workspaces outside the Structurizr API, Workspace should be extended to allow loading and saving from/to files and strings. Mirroring the JSON api, we should support something like:

@classmethod
Workspace.load(cls, file: Union[str, Path]) -> Workspace  # Already supported, handles zipped and unzipped

@classmethod
Workspace.loads(json_string: str) -> Workspace

Workspace.dump(file: Union[str, Path], *, zip: bool = False, indent: int = None) -> None

Workspace.dumps(*, indent: int = None) -> str

We should consider adding a **kwargs which gets passed through to the json method to allow finer-grained control of (de)serialisatoin.

Additional context

Currently to write to file, you have to do something along the lines of:

with open("my-workspace.json", "w") as f:
    f.write(WorkspaceIO.from_orm(workspace).json(indent=2))

which is a bit of a faff.

It'd also be good to show saving and loading to/from file in an example.