forked from Midnighter/structurizr-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.py
More file actions
271 lines (240 loc) · 9.99 KB
/
workspace.py
File metadata and controls
271 lines (240 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Copyright (c) 2020, Moritz E. Beber.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Provide the workspace model."""
import gzip
from datetime import datetime
from pathlib import Path
from typing import Any, Optional, Union
from pydantic import Field
from pydantic.types import StrBytes
from .abstract_base import AbstractBase
from .base_model import BaseModel
from .model import Model, ModelIO
from .view import ViewSet, ViewSetIO
__all__ = ("WorkspaceIO", "Workspace")
class WorkspaceIO(BaseModel):
"""
Represent a Structurizr workspace.
A workspace is a wrapper for a software architecture model, views, and
documentation.
Attributes:
id (int): The workspace ID.
name (str): The name of the workspace.
description (str): A short description of the workspace.
version (str): A version number for the workspace.
revision (int): The internal revision number.
thumbnail (str): The thumbnail associated with the workspace; a Base64
encoded PNG file as a Data URI (data:image/png;base64).
last_modified_date (datetime.datetime): The last modified date, in ISO 8601
format (e.g. "2018-09-08T12:40:03Z").
last_modified_user (str): A string identifying the user who last modified the
workspace (e.g. an e-mail address or username).
last_modified_agent (str): A string identifying the agent that was last used to
modify the workspace (e.g. "structurizr-java/1.2.0").
model (Model): A software architecture model.
views (Views): The set of views onto a software architecture model.
documentation (Documentation): The documentation associated with this software
architecture model.
configuration (WorkspaceConfiguration): The workspace configuration.
"""
id: Optional[int] = Field(default=None, description="The workspace ID.")
name: Optional[str] = Field(default=None, description="The name of the workspace.")
description: Optional[str] = Field(
default=None, description="A short description of the workspace."
)
version: Optional[str] = Field(
default=None, description="A version number for the workspace."
)
revision: Optional[int] = Field(
default=None, description="The internal revision number."
)
thumbnail: Optional[str] = Field(
default=None,
description="The thumbnail associated with the workspace; a Base64 encoded PNG"
" file as a Data URI (data:image/png;base64).",
)
last_modified_date: Optional[datetime] = Field(
default=None,
alias="lastModifiedDate",
description="The last modified date, in ISO 8601 format"
" (e.g. '2018-09-08T12:40:03Z').",
)
last_modified_user: Optional[str] = Field(
default=None,
alias="lastModifiedUser",
description="A string identifying the user who last modified the workspace"
" (e.g. an e-mail address or username).",
)
last_modified_agent: Optional[str] = Field(
default=None,
alias="lastModifiedAgent",
description="A string identifying the agent that was last used to modify the"
" workspace (e.g. 'structurizr-java/1.2.0').",
)
model: Optional[ModelIO] = Field(None, description="A software architecture model.")
views: Optional[ViewSetIO] = Field(
default=None, description="The set of views onto a software architecture model."
)
documentation: Optional[Any] = Field(
default=None,
description="The documentation associated with this software architecture "
"model.",
)
configuration: Optional[Any] = Field(
default=None, description="The workspace configuration."
)
class Workspace(AbstractBase):
"""
Represent a Structurizr workspace.
A workspace is a wrapper for a software architecture model, views, and
documentation.
Attributes:
id (int): The workspace ID.
name (str): The name of the workspace.
description (str): A short description of the workspace.
version (str): A version number for the workspace.
revision (int): The internal revision number.
thumbnail (str): The thumbnail associated with the workspace; a Base64
encoded PNG file as a Data URI (data:image/png;base64).
last_modified_date (datetime.datetime): The last modified date, in ISO 8601
format (e.g. "2018-09-08T12:40:03Z").
last_modified_user (str): A string identifying the user who last modified the
workspace (e.g. an e-mail address or username).
last_modified_agent (str): A string identifying the agent that was last used to
modify the workspace (e.g. "structurizr-java/1.2.0").
model (Model): A software architecture model.
views (Views): The set of views onto a software architecture model.
documentation (Documentation): The documentation associated with this software
architecture model.
configuration (WorkspaceConfiguration): The workspace configuration.
"""
def __init__(
self,
*,
name: str,
description: str,
id: Optional[int] = None,
version: Optional[str] = None,
revision: Optional[int] = None,
thumbnail: Optional[str] = None,
last_modified_date: Optional[datetime] = None,
last_modified_user: Optional[str] = None,
last_modified_agent: Optional[str] = None,
model: Optional[Model] = None,
views: Optional[ViewSet] = None,
# TODO
documentation: Optional[Any] = None,
# TODO
configuration: Optional[Any] = None,
**kwargs
) -> None:
"""
Initialize a new workspace.
Args:
name:
description:
id:
version:
revision:
thumbnail:
last_modified_date:
last_modified_user:
last_modified_agent:
model:
views:
documentation:
configuration:
**kwargs:
"""
super().__init__(**kwargs)
self.id = id
self.name = name
self.description = description
self.version = version
self.revision = revision
self.thumbnail = thumbnail
self.last_modified_date = last_modified_date
self.last_modified_user = last_modified_user
self.last_modified_agent = last_modified_agent
self.model = Model() if model is None else model
self.views = ViewSet(model=self.model) if views is None else views
self.documentation = documentation
self.configuration = configuration
@classmethod
def load(cls, filename: Union[str, Path]) -> "Workspace":
"""Load a workspace from a JSON file (which may optionally be gzipped)."""
filename = Path(filename)
try:
with gzip.open(filename, "rt") as handle:
return cls.loads(handle.read())
except FileNotFoundError as error:
raise error
except OSError:
with filename.open() as handle:
return cls.loads(handle.read())
@classmethod
def loads(cls, json: StrBytes) -> "Workspace":
"""Load a workspace from a JSON string or bytes."""
ws_io = WorkspaceIO.parse_raw(json)
return cls.hydrate(ws_io)
def dump(
self,
filename: Union[str, Path],
*,
zip: Optional[bool] = None,
indent: Optional[int] = None,
**kwargs
):
"""
Save a workspace as JSON to a file, optionally gzipped.
By default, filenames ending with `.gz` will be zipped and anything else won't,
however this can be overridden by explicitly passing the `zip` argument.
Arguments:
filename: filename to write to.
zip: if specified then controls whether the contents are gzipped.
indent: if specified then pretty-print the JSON with given indent.
kwargs: other arguments to pass through to `json.dumps()`.
"""
filename = Path(filename)
if zip is None:
zip = str(filename).endswith(".gz")
with gzip.open(filename, "wt") if zip else filename.open("wt") as handle:
handle.write(self.dumps(indent=indent, **kwargs))
def dumps(self, indent: Optional[int] = None, **kwargs):
"""
Export a workspace as a JSON string.
Args:
indent (int): if specified then pretty-print the JSON with given indent.
kwargs: other arguments to pass through to `json.dumps()`.
"""
return WorkspaceIO.from_orm(self).json(indent=indent, **kwargs)
@classmethod
def hydrate(cls, workspace_io: WorkspaceIO) -> "Workspace":
"""Create a new instance of Workspace from its IO."""
model = Model.hydrate(workspace_io.model)
views = ViewSet.hydrate(views=workspace_io.views, model=model)
return cls(
id=workspace_io.id,
name=workspace_io.name,
description=workspace_io.description,
version=workspace_io.version,
revision=workspace_io.revision,
thumbnail=workspace_io.thumbnail,
last_modified_date=workspace_io.last_modified_date,
last_modified_user=workspace_io.last_modified_user,
last_modified_agent=workspace_io.last_modified_agent,
model=model,
views=views,
# documentation=Documentation.hydrate(workspace_io.documentation),
)