-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArchitecture.py
289 lines (235 loc) · 10.1 KB
/
Architecture.py
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from contextlib import contextmanager # contextmanager decorator (for IntegrationSet)
import copy # required to create integration copies for middleware functionality
import pydiagrams.Model as Model
# constants for properties
url = 'url'
direction= 'direction'
class Namespace:
pass
class IntegrationFragment():
""" Temporary class used during the creation of a integration when used with operations,
eg: SimpleItem1 >> SimpleItem2 % 'Label'
This class stores the result of the intermediate operation: SimpleItem2 % 'label'
"""
def __init__(self, simpleItem, description):
self.simpleItem = simpleItem
self.description = description
##########################################################################
# Item Classes
class Item(Model.Item):
def __init__(self, label, **attrs):
super().__init__(label, **attrs)
def get_attributes_by_type(self, t):
""" return local attributes that are instances of type t """
#return [(k,v) for (k,v) in self.__dict__.items() if isinstance(v, t)]
return [v for v in list(self.__dict__.values()) if isinstance(v, t)]
@property
def Description(self):
return self.label
class SimpleItem(Item):
""" Items that can be owned by Systems """
@property
def isEntity(self):
return isinstance(self, EntityItem)
@property
def isModule(self):
return isinstance(self, ModuleItem)
# >>
# This creates an internal integration from a ModuleItem to an EntityItem
# can be called on a list of items or a single item
def __rshift__(self, itemSet):
def link(item, description=None):
if isinstance(item, SimpleItem):
# Determine integration type, Internal or External
if self.owner == item.owner:
# The owner of the items match, therefore Internal Integration
assert isinstance(self.owner, SystemItem), 'Item does not belong to a System'
newOwner = self.owner
# switch around any erroroneous Entity >> Module defintions
if self.isEntity and item.isModule:
sourceItem = item
destItem = self
else:
sourceItem = self
destItem = item
else:
newOwner = self.owner.get_root_owner().current_context_item
assert newOwner is not None, 'Can only add integrations from within a IntegrationSet context'
sourceItem = self
destItem = item
# Create the IntegrationItem
integration = IntegrationItem(
sourceSystem = self.owner, sourceItem = sourceItem,
destSystem = item.owner, destItem = destItem,
description=description)
newOwner.add_integration(integration)
#print(f'Link created Integration: {integration=}')
return integration
else:
raise ValueError('>> operator only supported for Modules & Entities')
if type(itemSet) == list:
for item in itemSet:
link(item)
return None
elif isinstance(itemSet, IntegrationFragment):
#print(f'Link: {self=}, {itemSet.description=}')
return link(itemSet.simpleItem, itemSet.description)
else:
return link(itemSet)
# %
# This operator enables an integration to be constructed with a label/description
# eg: SimpleItem1 >> SimpleItem2 % 'Description'
# means: create an integration from SimpleItem1 to SimpleItem2 with description 'Description'
def __mod__(self, description):
#print (f'Description being added to: {self=}, {description=}')
return IntegrationFragment(self, description)
class ModuleItem(SimpleItem):
pass
class EntityItem(SimpleItem):
pass
class ArchItem(Item):
""" Item with Integrations (eg: System, IntegrationSet) """
def __init__(self, label, **attrs):
super().__init__(label, **attrs)
self.integrations = []
def add_integration(self, integration):
integration.owner = self
self.integrations.append(integration)
@property
def Ints(self):
return self.integrations
def get_root_owner(self):
return self.owner
class SystemItem(ArchItem):
def __init__(self, label, entities, modules, **attrs):
super().__init__(label, **attrs)
self.create_attributes(EntityItem, entities)
self.create_attributes(ModuleItem, modules)
@property
def Entities(self):
""" Return a dict of the entities in this class """
return self.get_attributes_by_type(EntityItem)
@property
def Modules(self):
""" Return a dict of the entities in this class """
return self.get_attributes_by_type(ModuleItem)
def create_attributes(self, itemType, itemList):
""" Create an attribute for this each item in itemList.
This is used to create Modules and Entity references
"""
if not itemList:
return
for item in itemList:
# Create an instance of the item
if type(item) == str:
i = itemType(item)
elif type(item) == tuple:
i = itemType(item[1])
i.Id = item[0]
i.owner = self
setattr(self, i.Id, i)
#print ('Created {itemType}: "{system}".{item}'.format(itemType=itemType, system=self.label, item=i.Id))
class IntegrationSetItem(ArchItem):
def __init__(self, label, isMiddleware=False, **attrs):
super().__init__(label, **attrs)
self.isMiddleware = isMiddleware
self.mw_system = None # System that is the middleware
def set_middleware(self, system):
print('Enabled middlewares')
self.isMiddleware = True
self.mw_system = system
def add_integration(self, integration):
if not self.isMiddleware:
super().add_integration(integration)
else:
i1 = copy.copy(integration)
i2 = copy.copy(integration)
i1.destSystem = self.mw_system
i2.sourceSystem = self.mw_system
# Add the sourceItem to the middleware system
if not hasattr(self.mw_system, i1.sourceItem.Id):
new_item = copy.copy(i1.sourceItem)
new_item.owner = self.mw_system
setattr(self.mw_system, new_item.Id, new_item)
for i in [i1, i2]:
super().add_integration(i)
class IntegrationItem(Item):
def __init__(self,
sourceSystem, sourceItem,
destSystem, destItem,
description = None,
url = None,
direction = None,
edge_color = '#000000'):
super().__init__(label = None)
self.sourceSystem = sourceSystem
self.sourceItem = sourceItem
self.destSystem = destSystem
self.destItem = destItem
self.description = description
self.direction = direction
self.url = url
self.edge_color = edge_color
# & operator
# This operator enables an integration to have properties appended to it
# eg: SimpleItem1 >> SimpleItem2 % 'Description' & {url: 'http://example.com/my-integration', direction='right'}
def __and__(self, attrs):
assert type(attrs) == dict, "% must be used with a dictionary. eg: % {url: 'http://example.com/my-integration'}"
self.__dict__.update(attrs)
return self
def __repr__(self):
return 'owner={owner} {sourceSystem.Id}.{sourceItem.Id} >> {destSystem.Id}.{destItem.Id} % "{description}"'.format(**self.__dict__)
def goVia(self, system):
""" Makes an integration go via a certain system.
If an integrations is A >> B, then this makes the integration A >> system >> B
It returns the pair: [A >> system, system >> B]
"""
i0 = IntegrationItem(self.sourceSystem, self.sourceItem,
system)
#TODO
class UserGroupItem(Item):
pass
##########################################################################
class ArchitectureModel(Model.Model):
def __init__(self):
super().__init__(diagram=None)
self.integrationSets = []
self.current_context_item = None
self.app_user_groups = Model.ManyToMany()
def System(self, label, entities=None, modules=None, **attrs):
""" Return the System type """
new_item = SystemItem(label, entities, modules, **attrs)
return self.add_item(new_item)
def Application(self, *args, **kwargs):
""" Same as System, provided for compatibility with ARK model """
return self.System(*args, **kwargs)
def UserGroup(self, label, **kwargs):
return self.add_item(UserGroupItem(label))
@contextmanager
def IntegrationSet(self, label, isMiddleware=False, **attrs):
intSet = self.add_item(IntegrationSetItem(label, isMiddleware, **attrs))
if isMiddleware:
intSet.set_middleware(self.System(label, **attrs))
self.current_context_item = intSet
self.integrationSets.append( intSet )
yield self.current_context_item
self.current_context_item = None
@property
def Systems(self):
return [v for v in self.values_by_type(SystemItem)]
@property
def IntegrationSets(self):
return [v for v in self.values_by_type(IntegrationSetItem)]
@property
def Integrations(self):
ints = []
for IS in self.IntegrationSets:
ints += IS.Ints
return sorted(ints, key=lambda x:(x.sourceSystem.Id, x.destSystem.Id))
class ArchitectureContext(Model.Context):
def __init__(self):
super().__init__(ArchitectureModel())
def __exit__(self, *args):
print ('About to exit')
super().__exit__(*args)
print ('Done exit')