Skip to content

Commit 1732651

Browse files
yt-msMidnighter
authored andcommitted
refactor(Workspace): default zipping or not based on filename
1 parent 1667b70 commit 1732651

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/structurizr/workspace.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def __init__(
196196

197197
@classmethod
198198
def load(cls, filename: Union[str, Path]) -> "Workspace":
199-
"""Load a workspace from a file (which may optionally be gzipped)."""
199+
"""Load a workspace from a JSON file (which may optionally be gzipped)."""
200200
filename = Path(filename)
201201
try:
202202
with gzip.open(filename, "rt") as handle:
@@ -217,20 +217,25 @@ def dump(
217217
self,
218218
filename: Union[str, Path],
219219
*,
220-
zip: bool = False,
220+
zip: Optional[bool] = None,
221221
indent: Optional[int] = None,
222222
**kwargs
223223
):
224224
"""
225-
Save a workspace to a file, optionally zipped.
225+
Save a workspace as JSON to a file, optionally gzipped.
226+
227+
By default, filenames ending with `.gz` will be zipped and anything else won't,
228+
however this can be overridden by explicitly passing the `zip` argument.
226229
227230
Arguments:
228-
filename (str/Path): filename to write to.
229-
zip (bool): if true then contents will be zipped with GZip.
230-
indent (int): if specified then pretty-print the JSON with given indent.
231+
filename: filename to write to.
232+
zip: if specified then controls whether the contents are gzipped.
233+
indent: if specified then pretty-print the JSON with given indent.
231234
kwargs: other arguments to pass through to `json.dumps()`.
232235
"""
233236
filename = Path(filename)
237+
if zip is None:
238+
zip = str(filename).endswith(".gz")
234239
with gzip.open(filename, "wt") if zip else filename.open("wt") as handle:
235240
handle.write(self.dumps(indent=indent, **kwargs))
236241

tests/integration/test_workspace_io.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,30 @@ def test_save_and_load_workspace_to_gzipped_file(monkeypatch, tmp_path: Path):
136136

137137
filepath = tmp_path / "test_workspace.json.gz"
138138

139-
workspace.dump(filepath, zip=True)
139+
workspace.dump(filepath)
140140
workspace2 = Workspace.load(filepath)
141141

142142
expected = WorkspaceIO.from_orm(workspace)
143143
actual = WorkspaceIO.from_orm(workspace2)
144144
assert json.loads(actual.json()) == json.loads(expected.json())
145145

146146

147+
def test_workspace_overridding_zip_flag(monkeypatch, tmp_path: Path):
148+
"""Test that default zipping can be overridden explicitly."""
149+
monkeypatch.syspath_prepend(EXAMPLES)
150+
example = import_module("getting_started")
151+
workspace = example.main()
152+
153+
filepath = tmp_path / "test_workspace.json.gz"
154+
155+
workspace.dump(filepath, zip=False)
156+
contents = filepath.read_text()
157+
assert "My software system" in contents
158+
159+
# Make sure can be loaded even though its not zipped and ends with .gz
160+
Workspace.load(filepath)
161+
162+
147163
def test_load_unknown_file_raises_file_not_found():
148164
"""Test that attempting to load a non-existent file raises FileNotFound."""
149165
with pytest.raises(FileNotFoundError):

0 commit comments

Comments
 (0)