tiferet 1.0.0a8__py3-none-any.whl → 1.0.0a9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tiferet/data/error.py CHANGED
@@ -17,8 +17,8 @@ class ErrorMessageData(ErrorMessage, DataObject):
17
17
  class Options():
18
18
  serialize_when_none = False
19
19
  roles = {
20
- 'to_data.yaml': DataObject.allow('id'),
21
- 'to_object.yaml': DataObject.allow()
20
+ 'to_data': DataObject.allow(),
21
+ 'to_model': DataObject.allow()
22
22
  }
23
23
 
24
24
 
@@ -31,8 +31,8 @@ class ErrorData(Error, DataObject):
31
31
  class Options():
32
32
  serialize_when_none = False
33
33
  roles = {
34
- 'to_data.yaml': DataObject.deny('id'),
35
- 'to_object.yaml': DataObject.allow()
34
+ 'to_data': DataObject.deny('id'),
35
+ 'to_model': DataObject.allow()
36
36
  }
37
37
 
38
38
  # * attribute: message
@@ -44,13 +44,30 @@ class ErrorData(Error, DataObject):
44
44
  )
45
45
  )
46
46
 
47
+ # * to_primitive
48
+ def to_primitive(self, role: str = 'to_data', **kwargs) -> dict:
49
+ '''
50
+ Converts the data object to a primitive dictionary.
51
+
52
+ :param role: The role.
53
+ :type role: str
54
+ :param kwargs: Additional keyword arguments.
55
+ :type kwargs: dict
56
+ :return: The primitive dictionary.
57
+ :rtype: dict
58
+ '''
59
+
60
+ # Convert the data object to a primitive dictionary.
61
+ return super().to_primitive(
62
+ role,
63
+ **kwargs
64
+ )
65
+
47
66
  # * method: map
48
- def map(self, role: str = 'to_object.yaml', **kwargs):
67
+ def map(self, **kwargs) -> Error:
49
68
  '''
50
69
  Maps the error data to an error object.
51
70
 
52
- :param role: The role for the mapping.
53
- :type role: str
54
71
  :param kwargs: Additional keyword arguments.
55
72
  :type kwargs: dict
56
73
  :return: A new error object.
@@ -58,11 +75,13 @@ class ErrorData(Error, DataObject):
58
75
  '''
59
76
 
60
77
  # Map the error messages.
61
- return super().map(Error, role, **kwargs)
78
+ return super().map(Error,
79
+ **self.to_primitive('to_model'),
80
+ **kwargs)
62
81
 
63
- # * method: new
82
+ # * method: from_data
64
83
  @staticmethod
65
- def new(**kwargs) -> 'ErrorData':
84
+ def from_data(**kwargs) -> 'ErrorData':
66
85
  '''
67
86
  Creates a new ErrorData object.
68
87
 
@@ -73,6 +92,7 @@ class ErrorData(Error, DataObject):
73
92
  '''
74
93
 
75
94
  # Create a new ErrorData object.
76
- return ErrorData(
77
- super(ErrorData, ErrorData).new(**kwargs)
95
+ return super(ErrorData, ErrorData).from_data(
96
+ ErrorData,
97
+ **kwargs
78
98
  )
tiferet/data/feature.py CHANGED
@@ -1,6 +1,9 @@
1
+ # *** imports
1
2
 
3
+ # ** infra
2
4
  from schematics.types.serializable import serializable
3
5
 
6
+ #
4
7
  from ..domain import *
5
8
  from ..domain.feature import Feature, FeatureCommand
6
9
 
@@ -20,11 +23,11 @@ class FeatureCommandData(FeatureCommand, DataObject):
20
23
 
21
24
  # Define the roles for the feature handler data.
22
25
  roles = {
23
- 'to_object.yaml': DataObject.allow(),
24
- 'to_data.yaml': DataObject.allow()
26
+ 'to_model': DataObject.allow(),
27
+ 'to_data': DataObject.allow()
25
28
  }
26
29
 
27
- def map(self, role: str = 'to_object', **kwargs) -> FeatureCommand:
30
+ def map(self, role: str = 'to_model', **kwargs) -> FeatureCommand:
28
31
  '''
29
32
  Maps the feature handler data to a feature handler object.
30
33
 
@@ -53,8 +56,8 @@ class FeatureData(Feature, DataObject):
53
56
 
54
57
  # Define the roles for the feature data.
55
58
  roles = {
56
- 'to_object.yaml': DataObject.deny('feature_key'),
57
- 'to_data.yaml': DataObject.deny('feature_key', 'group_id', 'id')
59
+ 'to_model': DataObject.deny('feature_key'),
60
+ 'to_data': DataObject.deny('feature_key', 'group_id', 'id')
58
61
  }
59
62
 
60
63
  commands = t.ListType(t.ModelType(FeatureCommandData),
@@ -69,7 +72,7 @@ class FeatureData(Feature, DataObject):
69
72
  # Return the feature key.
70
73
  return self.id.split('.')[-1]
71
74
 
72
- def map(self, role: str = 'to_object.yaml', **kwargs) -> Feature:
75
+ def map(self, role: str = 'to_model', **kwargs) -> Feature:
73
76
  '''
74
77
  Maps the feature data to a feature object.
75
78
 
@@ -82,10 +85,13 @@ class FeatureData(Feature, DataObject):
82
85
  '''
83
86
 
84
87
  # Map the feature data to a feature object.
85
- return super().map(Feature, role, **kwargs)
88
+ return super().map(Feature, role,
89
+ feature_key=self.feature_key,
90
+ **kwargs
91
+ )
86
92
 
87
93
  @staticmethod
88
- def new(**kwargs) -> 'FeatureData':
94
+ def from_data(**kwargs) -> 'FeatureData':
89
95
  '''
90
96
  Initializes a new FeatureData object from a Feature object.
91
97
 
@@ -96,37 +102,7 @@ class FeatureData(Feature, DataObject):
96
102
  '''
97
103
 
98
104
  # Create a new FeatureData object.
99
- _data = FeatureData(
100
- dict(**kwargs,),
101
- strict=False
105
+ return super(FeatureData, FeatureData).from_data(
106
+ FeatureData,
107
+ **kwargs
102
108
  )
103
-
104
- # Validate and return the new FeatureData object.
105
- _data.validate()
106
- return _data
107
-
108
- @staticmethod
109
- def from_yaml_data(id: str, group_id: str, **kwargs) -> 'FeatureData':
110
- '''
111
- Initializes a new FeatureData object from yaml data.
112
-
113
- :param id: The feature id.
114
- :type id: str
115
- :param group_id: The context group id.
116
- :type group_id: str
117
- :param kwargs: Additional keyword arguments.
118
- :type kwargs: dict
119
- :return: A new FeatureData object.
120
- :rtype: FeatureData
121
- '''
122
-
123
- # Create a new FeatureData object.
124
- _data = FeatureData(
125
- dict(**kwargs,
126
- id=id, group_id=group_id
127
- ),
128
- strict=False)
129
-
130
- # Validate and return the new FeatureData object.
131
- _data.validate()
132
- return _data
@@ -1,5 +1,5 @@
1
1
  from .core import *
2
- from .app import AppInterface
3
- from .container import ContainerAttribute
2
+ from .app import AppInterface, AppDependency, AppRepositoryConfiguration
3
+ from .container import ContainerAttribute, ContainerDependency
4
4
  from .error import Error
5
5
  from .feature import Feature
tiferet/domain/app.py CHANGED
@@ -184,61 +184,3 @@ class AppRepositoryConfiguration(ModuleDependency):
184
184
  **kwargs),
185
185
  strict=False,
186
186
  )
187
-
188
-
189
- # ** model: app_configuration
190
- class AppConfiguration(Entity):
191
- '''
192
- The application configuration object.
193
- '''
194
-
195
- # * attribute: name
196
- name = StringType(
197
- required=True,
198
- metadata=dict(
199
- description='The name of the application.'
200
- )
201
- )
202
-
203
- # * attribute: description
204
- description = StringType(
205
- metadata=dict(
206
- description='The description of the application.'
207
- )
208
- )
209
-
210
- # * attribute: app_repo
211
- app_repo = ModelType(AppRepositoryConfiguration,
212
- required=True,
213
- metadata=dict(
214
- description='The application repository configuration.'
215
- ),
216
- )
217
-
218
- # * attribute: interfaces
219
- interfaces = ListType(
220
- ModelType(AppInterface),
221
- required=True,
222
- default=[],
223
- metadata=dict(
224
- description='The application interfaces.'
225
- )
226
- )
227
-
228
- # * method: new
229
- @staticmethod
230
- def new(**kwargs) -> 'AppConfiguration':
231
- '''
232
- Initializes a new AppConfiguration object.
233
-
234
- :param kwargs: Additional keyword arguments.
235
- :type kwargs: dict
236
- :return: A new AppConfiguration object.
237
- :rtype: AppConfiguration
238
- '''
239
-
240
- # Create and return a new AppConfiguration object.
241
- return super(AppConfiguration, AppConfiguration).new(
242
- AppConfiguration,
243
- **kwargs
244
- )
@@ -22,7 +22,7 @@ class ContainerDependency(ModuleDependency):
22
22
  '''
23
23
 
24
24
  # * attribute: flag
25
- flag = t.StringType(
25
+ flag = StringType(
26
26
  required=True,
27
27
  metadata=dict(
28
28
  description='The flag for the container dependency.'
@@ -30,8 +30,8 @@ class ContainerDependency(ModuleDependency):
30
30
  )
31
31
 
32
32
  # * attribute: parameters
33
- parameters = t.DictType(
34
- t.StringType,
33
+ parameters = DictType(
34
+ StringType,
35
35
  default={},
36
36
  metadata=dict(
37
37
  description='The container dependency parameters.'
@@ -51,7 +51,7 @@ class ContainerDependency(ModuleDependency):
51
51
  '''
52
52
 
53
53
  # Create and return a new ContainerDependency object.
54
- super(ContainerDependency, ContainerDependency).new(
54
+ return super(ContainerDependency, ContainerDependency).new(
55
55
  ContainerDependency,
56
56
  **kwargs)
57
57
 
@@ -63,7 +63,7 @@ class ContainerAttribute(Entity):
63
63
  '''
64
64
 
65
65
  # * attribute: id
66
- id = t.StringType(
66
+ id = StringType(
67
67
  required=True,
68
68
  metadata=dict(
69
69
  description='The unique identifier for the container attribute.'
@@ -71,7 +71,7 @@ class ContainerAttribute(Entity):
71
71
  )
72
72
 
73
73
  # * attribute: type
74
- type = t.StringType(
74
+ type = StringType(
75
75
  required=True,
76
76
  choices=CONTAINER_ATTRIBUTE_TYPE_CHOICES,
77
77
  metadata=dict(
@@ -80,8 +80,8 @@ class ContainerAttribute(Entity):
80
80
  )
81
81
 
82
82
  # * attribute: dependencies
83
- dependencies = t.ListType(
84
- t.ModelType(ContainerDependency),
83
+ dependencies = ListType(
84
+ ModelType(ContainerDependency),
85
85
  default=[],
86
86
  metadata=dict(
87
87
  description='The container attribute dependencies.'
@@ -101,10 +101,27 @@ class ContainerAttribute(Entity):
101
101
  '''
102
102
 
103
103
  # Create and return a new ContainerAttribute object.
104
- super(ContainerAttribute, ContainerAttribute).new(
104
+ return super(ContainerAttribute, ContainerAttribute).new(
105
105
  ContainerAttribute,
106
106
  **kwargs)
107
107
 
108
+ # * method: get_dependency
109
+ def get_dependency(self, flag: str) -> ContainerDependency:
110
+ '''
111
+ Gets a container dependency by flag.
112
+
113
+ :param flag: The flag for the container dependency.
114
+ :type flag: str
115
+ :return: The container dependency.
116
+ :rtype: ContainerDependency
117
+ '''
118
+
119
+ # Return the dependency with the matching flag.
120
+ return next(
121
+ (dependency for dependency in self.dependencies if dependency.flag == flag),
122
+ None
123
+ )
124
+
108
125
  # * method: set_dependency
109
126
  def set_dependency(self, dependency: ContainerDependency):
110
127
  '''
@@ -122,20 +139,3 @@ class ContainerAttribute(Entity):
122
139
 
123
140
  # Append the dependency otherwise.
124
141
  self.dependencies.append(dependency)
125
-
126
- # * method: get_dependency
127
- def get_dependency(self, flag: str) -> ContainerDependency:
128
- '''
129
- Gets a container dependency by flag.
130
-
131
- :param flag: The flag for the container dependency.
132
- :type flag: str
133
- :return: The container dependency.
134
- :rtype: ContainerDependency
135
- '''
136
-
137
- # Return the dependency with the matching flag.
138
- return next(
139
- (dependency for dependency in self.dependencies if dependency.flag == flag),
140
- None
141
- )
tiferet/domain/core.py CHANGED
@@ -88,6 +88,7 @@ class DataObject(Model):
88
88
  def map(self,
89
89
  type: ModelObject,
90
90
  role: str = 'to_model',
91
+ validate: bool = True,
91
92
  **kwargs
92
93
  ) -> ModelObject:
93
94
  '''
@@ -97,6 +98,8 @@ class DataObject(Model):
97
98
  :type type: type
98
99
  :param role: The role for the mapping.
99
100
  :type role: str
101
+ :param validate: True to validate the model object.
102
+ :type validate: bool
100
103
  :param kwargs: Additional keyword arguments for mapping.
101
104
  :type kwargs: dict
102
105
  :return: A new model object.
@@ -112,13 +115,19 @@ class DataObject(Model):
112
115
  # Map the data object to a model object.
113
116
  _object = type.new(**_data, strict=False)
114
117
 
118
+ # Validate if specified.
119
+ if validate:
120
+ _object.validate()
121
+
115
122
  # Return the model data.
116
123
  return _object
117
-
124
+
118
125
  # ** method: from_model
119
126
  @staticmethod
120
127
  def from_model(
128
+ data: 'DataObject',
121
129
  model: ModelObject,
130
+ validate: bool = True,
122
131
  **kwargs
123
132
  ) -> 'DataObject':
124
133
  '''
@@ -126,6 +135,10 @@ class DataObject(Model):
126
135
 
127
136
  :param model: The type of model object to map from.
128
137
  :type model: type
138
+ :param data: The data object to map from.
139
+ :type data: DataObject
140
+ :param validate: True to validate the data object.
141
+ :type validate: bool
129
142
  :param kwargs: Keyword arguments.
130
143
  :type kwargs: dict
131
144
  :return: A new data object.
@@ -133,10 +146,35 @@ class DataObject(Model):
133
146
  '''
134
147
 
135
148
  # Create a new data object.
136
- return DataObject(
137
- model.new(**kwargs, strict=False),
138
- strict=False,
139
- )
149
+ obj = data(dict(
150
+ **model.to_primitive(),
151
+ **kwargs
152
+ ), strict=False)
153
+
154
+ # Validate the data object if specified.
155
+ if validate:
156
+ obj.validate()
157
+
158
+ # Return the data object.
159
+ return obj
160
+
161
+ @staticmethod
162
+ def from_data(
163
+ data: type,
164
+ **kwargs
165
+ ) -> 'DataObject':
166
+ '''
167
+ Initializes a new data object from a dictionary.
168
+
169
+ :param data: The type of data object to map from.
170
+ :param kwargs: Keyword arguments.
171
+ :type kwargs: dict
172
+ :return: A new data object.
173
+ :rtype: DataObject
174
+ '''
175
+
176
+ # Create a new data object.
177
+ return data(dict(**kwargs), strict=False)
140
178
 
141
179
  # ** method: allow
142
180
  @staticmethod
tiferet/domain/error.py CHANGED
@@ -28,6 +28,23 @@ class ErrorMessage(ValueObject):
28
28
  )
29
29
  )
30
30
 
31
+ # * method: new
32
+ @staticmethod
33
+ def new(**kwargs) -> 'ErrorMessage':
34
+ '''Initializes a new ErrorMessage object.
35
+
36
+ :param kwargs: Additional keyword arguments.
37
+ :type kwargs: dict
38
+ :return: A new ErrorMessage object.
39
+ :rtype: ErrorMessage
40
+ '''
41
+
42
+ # Create and return a new ErrorMessage object.
43
+ return super(ErrorMessage, ErrorMessage).new(
44
+ ErrorMessage,
45
+ **kwargs
46
+ )
47
+
31
48
  # * method: format
32
49
  def format(self, *args) -> str:
33
50
  '''
@@ -106,6 +123,7 @@ class Error(Entity):
106
123
 
107
124
  # Create and return a new Error object.
108
125
  return super(Error, Error).new(
126
+ Error,
109
127
  id=id,
110
128
  name=name,
111
129
  error_code=error_code,
tiferet/domain/feature.py CHANGED
@@ -127,7 +127,7 @@ class Feature(Entity):
127
127
 
128
128
  # * method: new
129
129
  @staticmethod
130
- def new(name: str, group_id: str, feature_key: str, description: str = None, **kwargs) -> 'Feature':
130
+ def new(name: str, group_id: str, feature_key: str = None, id: str = None, description: str = None, **kwargs) -> 'Feature':
131
131
  '''Initializes a new Feature object.
132
132
 
133
133
  :param name: The name of the feature.
@@ -136,6 +136,8 @@ class Feature(Entity):
136
136
  :type group_id: str
137
137
  :param feature_key: The key of the feature.
138
138
  :type feature_key: str
139
+ :param id: The identifier of the feature.
140
+ :type id: str
139
141
  :param description: The description of the feature.
140
142
  :type description: str
141
143
  :param kwargs: Additional keyword arguments.
@@ -143,8 +145,13 @@ class Feature(Entity):
143
145
  :return: A new Feature object.
144
146
  '''
145
147
 
148
+ # Set the feature key as the snake case of the name if not provided.
149
+ if not feature_key:
150
+ feature_key = name.lower().replace(' ', '_')
151
+
146
152
  # Feature ID is the group ID and feature key separated by a period.
147
- id = f'{group_id}.{feature_key}'
153
+ if not id:
154
+ id = f'{group_id}.{feature_key}'
148
155
 
149
156
  # Set the description as the name if not provided.
150
157
  if not description:
@@ -152,6 +159,7 @@ class Feature(Entity):
152
159
 
153
160
  # Create and return a new Feature object.
154
161
  return super(Feature, Feature).new(
162
+ Feature,
155
163
  id=id,
156
164
  name=name,
157
165
  group_id=group_id,
@@ -170,7 +178,7 @@ class Feature(Entity):
170
178
  '''
171
179
 
172
180
  # Add the handler to the feature.
173
- if position:
181
+ if position is not None:
174
182
  self.commands.insert(position, handler)
175
183
  else:
176
184
  self.commands.append(handler)
tiferet/repos/__init__.py CHANGED
@@ -0,0 +1,7 @@
1
+ # *** imports
2
+
3
+ # ** app
4
+ from .app import AppRepository
5
+ from .container import ContainerRepository
6
+ from .error import ErrorRepository
7
+ from .feature import FeatureRepository
tiferet/repos/app.py CHANGED
@@ -3,7 +3,7 @@
3
3
  # ** app
4
4
  from ..domain.app import AppInterface
5
5
  from ..data.app import AppInterfaceYamlData
6
- from ..clients import yaml as yaml_client
6
+ from ..clients import yaml_client
7
7
 
8
8
 
9
9
  # *** repository
@@ -72,7 +72,7 @@ class YamlProxy(object):
72
72
  interfaces = yaml_client.load(
73
73
  self.config_file,
74
74
  create_data=lambda data: [
75
- AppInterfaceYamlData.new(
75
+ AppInterfaceYamlData.from_data(
76
76
  id=interface_id,
77
77
  **record
78
78
  ).map() for interface_id, record in data.items()],
@@ -95,9 +95,13 @@ class YamlProxy(object):
95
95
  # Load the app interface data from the yaml configuration file.
96
96
  _data: AppInterface = yaml_client.load(
97
97
  self.config_file,
98
- create_data=lambda data: AppInterfaceYamlData.new (
98
+ create_data=lambda data: AppInterfaceYamlData.from_data(
99
99
  id=id, **data),
100
100
  start_node=lambda data: data.get('interfaces').get(id))
101
101
 
102
102
  # Return the app interface object.
103
- return _data.map()
103
+ # If the data is None, return None.
104
+ try:
105
+ return _data.map()
106
+ except AttributeError:
107
+ return None
@@ -51,18 +51,6 @@ class ContainerRepository(object):
51
51
  # Not implemented.
52
52
  raise NotImplementedError()
53
53
 
54
- # * method: save_attribute
55
- def save_attribute(self, attribute: ContainerAttribute):
56
- '''
57
- Save the container attribute.
58
-
59
- :param attribute: The container attribute.
60
- :type attribute: ContainerAttribute
61
- '''
62
-
63
- # Not implemented.
64
- raise NotImplementedError
65
-
66
54
 
67
55
  # ** proxy: yaml_proxy
68
56
  class YamlProxy(ContainerRepository):
@@ -71,27 +59,17 @@ class YamlProxy(ContainerRepository):
71
59
  '''
72
60
 
73
61
  # * init
74
- def __init__(self, container_config_file: str, read_role: str = 'to_object.yaml', write_role: str = 'to_data.yaml'):
62
+ def __init__(self, container_config_file: str):
75
63
  '''
76
64
  Initialize the yaml proxy.
77
65
 
78
66
  :param container_config_file: The YAML file path for the container configuration.
79
67
  :type container_config_file: str
80
- :param read_role: The read role for the yaml proxy.
81
- :type read_role: str
82
- :param write_role: The write role for the yaml proxy.
83
- :type write_role: str
84
68
  '''
85
69
 
86
70
  # Set the container configuration file.
87
71
  self.config_file = container_config_file
88
72
 
89
- # Set the read role.
90
- self.read_role = read_role
91
-
92
- # Set the write role.
93
- self.write_role = write_role
94
-
95
73
  # * method: get_attribute
96
74
  def get_attribute(self, attribute_id: str, type: str) -> ContainerAttribute:
97
75
  '''
@@ -108,7 +86,7 @@ class YamlProxy(ContainerRepository):
108
86
  # Load the attribute data from the yaml configuration file.
109
87
  data = yaml_client.load(
110
88
  self.config_file,
111
- create_data=lambda data: ContainerAttributeYamlData.new(
89
+ create_data=lambda data: ContainerAttributeYamlData.from_data(
112
90
  id=attribute_id, **data),
113
91
  start_node=lambda data: data.get('attrs').get(attribute_id),
114
92
  )
@@ -118,7 +96,7 @@ class YamlProxy(ContainerRepository):
118
96
  return None
119
97
 
120
98
  # Return the attribute.
121
- return data.map(self.read_role)
99
+ return data.map()
122
100
 
123
101
  # * method: list_all
124
102
  def list_all(self) -> Tuple[List[ContainerAttribute], Dict[str, str]]:
@@ -133,32 +111,13 @@ class YamlProxy(ContainerRepository):
133
111
  attr_data, consts = yaml_client.load(
134
112
  self.config_file,
135
113
  create_data=lambda data: (
136
- [ContainerAttributeYamlData.new(id=id, **attr_data) for id, attr_data in data.get('attrs', {}).items()],
114
+ [ContainerAttributeYamlData.from_data(id=id, **attr_data) for id, attr_data in data.get('attrs', {}).items()],
137
115
  data.get('const', {}),
138
116
  ),
139
117
  )
140
118
 
141
119
  # Return the list of container attributes.
142
120
  return (
143
- [data.map(self.read_role) for data in attr_data],
121
+ [data.map() for data in attr_data],
144
122
  consts
145
123
  )
146
-
147
- # * method: save_attribute
148
- def save_attribute(self, attribute: ContainerAttribute):
149
- '''
150
- Save the attribute to the yaml file.
151
-
152
- :param attribute: The attribute to save.
153
- :type attribute: ContainerAttribute
154
- '''
155
-
156
- # Create a new container attribute data object.
157
- data = ContainerAttributeYamlData.from_model(attribute)
158
-
159
- # Update the attribute data.
160
- yaml_client.save(
161
- self.config_file,
162
- data.to_primitive(role=self.write_role),
163
- f'container/attrs/{attribute.id}'
164
- )