-
Notifications
You must be signed in to change notification settings - Fork 3
/
component.py
166 lines (142 loc) · 6.72 KB
/
component.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
from typing import Any, Callable, Dict, List
from jsonschema import validate
from pykechain.enums import PropertyType, _AllRepresentations
from pykechain.exceptions import IllegalArgumentError
from pykechain.models.representations.representation_base import BaseRepresentation
from pykechain.models.validators.validator_schemas import representation_jsonschema_stub
class RepresentationsComponent:
"""
Aggregate class to use representations on an object.
To add representations to a Pykechain class, create an instance of this class in its __init__() method.
.. versionadded:: 3.7
"""
def __init__(
self, parent_object, representation_options: Dict, update_method: Callable
):
"""
Extract the json with the representation options.
:param parent_object: Object to which this representation component is attached
:param representation_options: json with list of representations
:param update_method: method of the parent_object that is used to update the representations
"""
self._parent_object = parent_object
self._repr_options = representation_options
self._update_method: Callable = update_method
# Construct representation objects
self._representations: List["AnyRepresentation"] = []
representations_json = self._repr_options
for representation_json in representations_json:
self._representations.append(
BaseRepresentation.parse(
obj=self._parent_object, json=representation_json
)
)
def get_representations(self) -> List["AnyRepresentation"]:
"""
Get list of representation objects.
:return: list of Representations
:raises IllegalArgumentError if representations are set with incorrect options
"""
return self._representations
def set_representations(self, representations: List["AnyRepresentation"]) -> None:
"""Set the representations."""
self._validate_representations(representations)
# set the internal representation list
self._representations = list(set(representations))
# dump to _json options
self._dump_representations()
# update the options to KE-chain backend
self._update_method(self._repr_options)
def _validate_representations(self, representations: Any):
"""Check provided representation inputs."""
if not isinstance(representations, (tuple, list)):
raise IllegalArgumentError(
"Should be a list or tuple with Representation objects, got {}".format(
type(representations)
)
)
for r in representations:
if not isinstance(r, BaseRepresentation):
raise IllegalArgumentError(
f"Representation '{r}' should be a Representation object"
)
if not _valid_object_type(r, self._parent_object):
raise IllegalArgumentError(
f"Representation '{r}' can not be added to "
f"'{self._parent_object}' as the representation is not "
"allowed on that propertyType. Please check relation between allowed"
"representation and property type accordingly."
)
r.validate_json()
def _dump_representations(self):
"""Dump the representations as json inside the _repr_options dictionary."""
representations_json = []
for r in self._representations:
json_format = r.as_json()
validate(json_format, representation_jsonschema_stub)
representations_json.append(json_format)
self._repr_options = representations_json
def _valid_object_type(representation: BaseRepresentation, obj: "Base") -> bool:
"""
Check whether the representation can be used on the provided object.
:param representation: representation to check
:type representation: BaseRepresentation
:param obj: object to attach the representation to
:type obj: Base
:return: True if feasible, False if not.
:rtype bool
"""
rtype = representation.rtype
if rtype == _AllRepresentations.CUSTOM_ICON:
from pykechain.models import Activity, Scope
return isinstance(obj, (Activity, Scope))
else:
from pykechain.models import Property
if not isinstance(obj, Property):
return False
else:
if rtype == _AllRepresentations.BUTTON:
return obj.type in (
PropertyType.SINGLE_SELECT_VALUE,
PropertyType.MULTI_SELECT_VALUE,
)
elif rtype == _AllRepresentations.DECIMAL_PLACES:
return obj.type == PropertyType.FLOAT_VALUE
elif rtype == _AllRepresentations.SIGNIFICANT_DIGITS:
return obj.type == PropertyType.FLOAT_VALUE
elif rtype == _AllRepresentations.THOUSANDS_SEPARATOR:
return obj.type in (PropertyType.INT_VALUE, PropertyType.FLOAT_VALUE)
elif rtype == _AllRepresentations.LINK_TARGET:
return obj.type == PropertyType.LINK_VALUE
elif rtype == _AllRepresentations.AUTOFILL:
return obj.type in (
PropertyType.DATETIME_VALUE,
PropertyType.DATE_VALUE,
PropertyType.TIME_VALUE,
PropertyType.USER_REFERENCES_VALUE,
)
elif rtype == _AllRepresentations.GEOCOORDINATE:
return obj.type == PropertyType.GEOJSON_VALUE
elif rtype == _AllRepresentations.USE_PROPERTY_NAME:
return obj.type in (
PropertyType.REFERENCES_VALUE,
PropertyType.ACTIVITY_REFERENCES_VALUE,
PropertyType.USER_REFERENCES_VALUE,
PropertyType.SCOPE_REFERENCES_VALUE,
PropertyType.SERVICE_REFERENCES_VALUE,
)
elif rtype == _AllRepresentations.CAMERA_SCANNER_INPUT:
return obj.type in (
PropertyType.INT_VALUE,
PropertyType.FLOAT_VALUE,
PropertyType.TEXT_VALUE,
PropertyType.CHAR_VALUE,
)
elif rtype == _AllRepresentations.SIGNATURE:
return obj.type == PropertyType.ATTACHMENT_VALUE
elif rtype == _AllRepresentations.FILE_DISPLAY:
return obj.type == PropertyType.STOREDFILE_REFERENCES_VALUE
elif rtype == _AllRepresentations.SHOW_ONLY_SCOPE_MEMBERS:
return obj.type == PropertyType.USER_REFERENCES_VALUE
else:
return False