python-eigen-ingenuity 0.5.7b3__py3-none-any.whl → 0.5.7b4__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.
- modelbuilder/src/assetmodelbuilder.py +2 -1
- modelbuilder/src/configmanager.py +73 -56
- modelbuilder/src/csvcontentsmanager.py +11 -11
- modelbuilder/src/fileanalyser.py +9 -7
- modelbuilder/src/filemanager.py +90 -36
- modelbuilder/src/processors/nodefileprocessor.py +25 -43
- modelbuilder/src/processors/propertymanager.py +28 -20
- modelbuilder/src/processors/relationshipfileprocessor.py +21 -22
- modelbuilder/src/property.py +298 -0
- modelbuilder/src/queries/cypherbuilder.py +7 -5
- modelbuilder/src/queries/query.py +7 -5
- modelbuilder/src/queries/typedefs.py +10 -7
- modelbuilder/src/versionmanager.py +84 -70
- {python_eigen_ingenuity-0.5.7b3.dist-info → python_eigen_ingenuity-0.5.7b4.dist-info}/METADATA +1 -1
- {python_eigen_ingenuity-0.5.7b3.dist-info → python_eigen_ingenuity-0.5.7b4.dist-info}/RECORD +18 -17
- {python_eigen_ingenuity-0.5.7b3.dist-info → python_eigen_ingenuity-0.5.7b4.dist-info}/WHEEL +0 -0
- {python_eigen_ingenuity-0.5.7b3.dist-info → python_eigen_ingenuity-0.5.7b4.dist-info}/entry_points.txt +0 -0
- {python_eigen_ingenuity-0.5.7b3.dist-info → python_eigen_ingenuity-0.5.7b4.dist-info}/top_level.txt +0 -0
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import messages
|
|
2
2
|
from queries.cypherbuilder import QueryBuilder
|
|
3
|
-
from queries.typedefs import Properties
|
|
4
3
|
import assetmodelutilities as amu
|
|
5
4
|
from queries.query import Neo4jQuery
|
|
6
5
|
import uuid
|
|
7
6
|
import processors.labelmanager as lm
|
|
8
7
|
import processors.propertymanager as pm
|
|
8
|
+
import property
|
|
9
|
+
from property import Neo4jProperty
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class NodeFileProcessor:
|
|
12
13
|
|
|
13
|
-
def __init__(self, filename, config, contents):
|
|
14
|
+
def __init__(self, filename, config, contents, section):
|
|
14
15
|
self.filename = filename
|
|
15
16
|
self.contents = contents
|
|
16
17
|
|
|
17
|
-
self.primary_property = config.get_primary_property()
|
|
18
|
+
self.primary_property = config.get_primary_property()[section]
|
|
18
19
|
self.add_system_properties = not config.get_no_system_properties()
|
|
19
20
|
|
|
20
|
-
self.required_labels = config.get_required_labels()
|
|
21
|
-
self.required_properties = config.get_required_properties_dict()
|
|
22
|
-
self.unrequired_properties = config.get_unrequired_properties_list()
|
|
23
|
-
self.unique_id_property = config.get_unique_id_property()
|
|
24
|
-
self.creation_time_property = config.get_creation_time_property()
|
|
25
|
-
self.update_time_property = config.get_update_time_property()
|
|
21
|
+
self.required_labels = config.get_required_labels()[section]
|
|
22
|
+
self.required_properties = config.get_required_properties_dict()[section]
|
|
23
|
+
self.unrequired_properties = config.get_unrequired_properties_list()[section]
|
|
24
|
+
self.unique_id_property = config.get_unique_id_property()[section]
|
|
25
|
+
self.creation_time_property = config.get_creation_time_property()[section]
|
|
26
|
+
self.update_time_property = config.get_update_time_property()[section]
|
|
26
27
|
|
|
27
|
-
self.label_alias = config.get_label_alias()
|
|
28
|
-
self.node_alias = config.get_node_alias()
|
|
28
|
+
self.label_alias = config.get_label_alias()[section]
|
|
29
|
+
self.node_alias = config.get_node_alias()[section]
|
|
29
30
|
|
|
30
31
|
self.validate_only = config.in_validation_mode()
|
|
31
32
|
self.summarise = config.in_summary_mode()
|
|
@@ -39,6 +40,8 @@ class NodeFileProcessor:
|
|
|
39
40
|
self.label_types = amu.label_types
|
|
40
41
|
self.all_types = amu.all_types
|
|
41
42
|
|
|
43
|
+
self.section = section
|
|
44
|
+
|
|
42
45
|
def get_last_row(self, num_rows):
|
|
43
46
|
if self.last_row < 0:
|
|
44
47
|
last_row = num_rows
|
|
@@ -70,20 +73,8 @@ class NodeFileProcessor:
|
|
|
70
73
|
row_properties = self.contents.get_property_values_dict(this_row, property_columns, self.blank_property_list)
|
|
71
74
|
properties = pm.combine_properties(self.required_properties, row_properties)
|
|
72
75
|
# Remove any properties we don't want
|
|
73
|
-
property_list = list(properties.keys())
|
|
74
76
|
for unwanted in self.unrequired_properties:
|
|
75
|
-
|
|
76
|
-
if property.split(':')[0] == unwanted:
|
|
77
|
-
try:
|
|
78
|
-
properties.pop(property)
|
|
79
|
-
except:
|
|
80
|
-
pass
|
|
81
|
-
|
|
82
|
-
for k in list(properties):
|
|
83
|
-
if k.startswith(':'):
|
|
84
|
-
properties.pop(k)
|
|
85
|
-
else:
|
|
86
|
-
properties[property_ref_dot + k] = properties.pop(k)
|
|
77
|
+
temp = properties.pop(unwanted, None)
|
|
87
78
|
|
|
88
79
|
# Split out those properties that are dependent on others, so that we can add them in the right order
|
|
89
80
|
explicit_properties, calculated_properties = pm.split_out_functional_properties(properties)
|
|
@@ -94,7 +85,7 @@ class NodeFileProcessor:
|
|
|
94
85
|
# Build a simple MERGE query using queries
|
|
95
86
|
query_text = (QueryBuilder()
|
|
96
87
|
.merge()
|
|
97
|
-
.node(labels=merge_labels, ref_name=property_ref, properties=
|
|
88
|
+
.node(labels=merge_labels, ref_name=property_ref, properties=primary_property)
|
|
98
89
|
)
|
|
99
90
|
|
|
100
91
|
# Add in System properties if required
|
|
@@ -117,7 +108,7 @@ class NodeFileProcessor:
|
|
|
117
108
|
|
|
118
109
|
query_text = query_text.toStr()
|
|
119
110
|
|
|
120
|
-
query = Neo4jQuery('node', query_text, time_now, merge_labels, new_labels, primary_property, properties)
|
|
111
|
+
query = Neo4jQuery('node', query_text, time_now, merge_labels, new_labels, primary_property, properties, self.section)
|
|
121
112
|
|
|
122
113
|
if self.validate_only and not self.summarise:
|
|
123
114
|
print(query.text)
|
|
@@ -171,29 +162,20 @@ class NodeFileProcessor:
|
|
|
171
162
|
for primary_property in self.contents.get_property_values_list(this_row, node_columns, self.primary_property):
|
|
172
163
|
properties = pm.combine_properties(self.required_properties, row_properties)
|
|
173
164
|
# Remove any properties we don't want
|
|
174
|
-
property_list = list(properties.keys())
|
|
175
165
|
for unwanted in self.unrequired_properties:
|
|
176
|
-
|
|
177
|
-
if property.split(':')[0] == unwanted:
|
|
178
|
-
try:
|
|
179
|
-
properties.pop(property)
|
|
180
|
-
except:
|
|
181
|
-
pass
|
|
182
|
-
|
|
183
|
-
properties = {**properties, **primary_property}
|
|
166
|
+
temp = properties.pop(unwanted, None)
|
|
184
167
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
properties.pop(k)
|
|
168
|
+
primary_property_dict = {primary_property.key: primary_property}
|
|
169
|
+
all_properties = pm.combine_properties(properties, primary_property_dict)
|
|
188
170
|
|
|
189
|
-
explicit_properties, calculated_properties = pm.split_out_functional_properties(
|
|
171
|
+
explicit_properties, calculated_properties = pm.split_out_functional_properties(all_properties)
|
|
190
172
|
|
|
191
173
|
query_data = [explicit_properties, calculated_properties, time_now]
|
|
192
174
|
|
|
193
175
|
# So now we know all about this particular update. Let's put it in the query dictionary
|
|
194
176
|
# We'll group these by unique combinations of labels and primary key type
|
|
195
177
|
# Have to use a list since a dictionary key can't be a dictionary
|
|
196
|
-
dictionary_key = [new_labels, merge_labels, remove_labels,
|
|
178
|
+
dictionary_key = [new_labels, merge_labels, remove_labels, [primary_property.key], calculated_properties.keys()]
|
|
197
179
|
try:
|
|
198
180
|
dictionary_index = batch_keys.index(dictionary_key)
|
|
199
181
|
query_dictionary[dictionary_index].append(query_data)
|
|
@@ -230,7 +212,7 @@ class NodeFileProcessor:
|
|
|
230
212
|
update_properties = update[0] or {}
|
|
231
213
|
func_properties = update[1] or {}
|
|
232
214
|
if self.add_system_properties:
|
|
233
|
-
update_properties[self.update_time_property] = update[2]
|
|
215
|
+
update_properties[self.update_time_property] = Neo4jProperty(self.update_time_property, update[2])
|
|
234
216
|
payload.append(update_properties)
|
|
235
217
|
this_size += 1
|
|
236
218
|
query_count += 1
|
|
@@ -238,7 +220,7 @@ class NodeFileProcessor:
|
|
|
238
220
|
formatted_payload = []
|
|
239
221
|
property_count = 0
|
|
240
222
|
for a_payload in payload:
|
|
241
|
-
formatted_payload.append(
|
|
223
|
+
formatted_payload.append(property.format(a_payload, property_ref_dot))
|
|
242
224
|
property_count += len(a_payload)
|
|
243
225
|
|
|
244
226
|
# Build a MERGE query using queries
|
|
@@ -246,7 +228,7 @@ class NodeFileProcessor:
|
|
|
246
228
|
remove_labels = batch_keys[new_labels][2]
|
|
247
229
|
if batch_keys[new_labels][0]:
|
|
248
230
|
merge_labels += batch_keys[new_labels][0]
|
|
249
|
-
primary_property = batch_keys[new_labels][3]
|
|
231
|
+
primary_property = batch_keys[new_labels][3][0]
|
|
250
232
|
query_text = (QueryBuilder()
|
|
251
233
|
.unwind_list_as(formatted_payload, payload_ref)
|
|
252
234
|
.merge()
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import assetmodelutilities as amu
|
|
2
|
-
|
|
2
|
+
from property import Neo4jProperty
|
|
3
3
|
|
|
4
4
|
def remove_leading_chars(string, chars):
|
|
5
5
|
if string:
|
|
@@ -18,6 +18,7 @@ def validate_property(formatted_property):
|
|
|
18
18
|
# full name:str -> `full name`:str
|
|
19
19
|
validated = ''
|
|
20
20
|
property_name = formatted_property.split(':')[0]
|
|
21
|
+
|
|
21
22
|
if property_name:
|
|
22
23
|
validated += amu.validate(property_name)
|
|
23
24
|
# try to re-add the type prefix
|
|
@@ -65,8 +66,8 @@ def sort_properties(properties, default_type):
|
|
|
65
66
|
if property:
|
|
66
67
|
for prop in property.strip().split(','):
|
|
67
68
|
# prop has the format key[[:format]:|=value]
|
|
68
|
-
split_prop = prop.replace('=', ':').split(':')
|
|
69
|
-
num_fields = len(split_prop)
|
|
69
|
+
split_prop = (prop.replace('=', ':')+':').split(':')
|
|
70
|
+
num_fields = len(split_prop)-1
|
|
70
71
|
key = split_prop[0]
|
|
71
72
|
# Normally, there will 2 fields (property and value) or 3 (property, type and value)
|
|
72
73
|
# But if it's a datetime there could be another 2 (for minutes and seconds - hours is part of day field)
|
|
@@ -75,24 +76,30 @@ def sort_properties(properties, default_type):
|
|
|
75
76
|
# May need to revisit this logic if other data types are supported that break the parity rule!
|
|
76
77
|
if num_fields % 2 == 1:
|
|
77
78
|
# Odd number ==> there is a type
|
|
78
|
-
prop_type =
|
|
79
|
+
prop_type = split_prop[1]
|
|
79
80
|
# And recombine all the remaining fields back into the value string
|
|
80
|
-
|
|
81
|
+
if num_fields == 3:
|
|
82
|
+
prop_value = split_prop[2]
|
|
83
|
+
else:
|
|
84
|
+
prop_value = ':'.join(split_prop[2:-1])
|
|
81
85
|
else:
|
|
82
86
|
# even number ==> no type, so set it to the default or blank
|
|
83
87
|
prop_type = def_type
|
|
84
88
|
# And recombine all the remaining fields back into the value string
|
|
85
|
-
|
|
89
|
+
if num_fields == 2:
|
|
90
|
+
prop_value = split_prop[1]
|
|
91
|
+
else:
|
|
92
|
+
prop_value = ':'.join(split_prop[1:-1])
|
|
86
93
|
|
|
87
94
|
# Remove leading ! as they are not needed in the key
|
|
88
|
-
valid_key =
|
|
95
|
+
valid_key = remove_leading_chars(key.strip(), '!')
|
|
89
96
|
if key.startswith('!!'):
|
|
90
97
|
retain.append(valid_key)
|
|
91
98
|
else:
|
|
92
99
|
# If we have a format and/or value we have a new property to add
|
|
93
100
|
# ...unless it starts with ! in which case it belongs in the unwanted list (format/value are ignored)
|
|
94
101
|
if num_fields > 1 and not key.startswith('!'):
|
|
95
|
-
validated[valid_key
|
|
102
|
+
validated[valid_key] = Neo4jProperty(valid_key, prop_value, prop_type)
|
|
96
103
|
else:
|
|
97
104
|
# Add to the unwanted list
|
|
98
105
|
unwanted.append(valid_key)
|
|
@@ -126,22 +133,23 @@ def split_node_key(node):
|
|
|
126
133
|
|
|
127
134
|
def combine_properties(default_properties, actual_properties):
|
|
128
135
|
combined_properties = default_properties.copy()
|
|
129
|
-
|
|
130
|
-
|
|
136
|
+
combined_keys = combined_properties
|
|
137
|
+
for property in actual_properties:
|
|
138
|
+
property_key = actual_properties[property].key
|
|
139
|
+
property_value = actual_properties[property].value
|
|
140
|
+
|
|
141
|
+
if property_key not in combined_keys:
|
|
142
|
+
# New property, so add to the list
|
|
131
143
|
# Either value has been specified in csv
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
144
|
+
combined_properties[property] = actual_properties[property]
|
|
145
|
+
elif property_value != '':
|
|
146
|
+
# Replace the default property with the actual values from the csv file
|
|
147
|
+
combined_properties[property] = actual_properties[property]
|
|
135
148
|
|
|
136
149
|
return combined_properties
|
|
137
150
|
|
|
138
151
|
def split_out_functional_properties(properties):
|
|
139
|
-
explicit = {}
|
|
140
|
-
functional = {}
|
|
141
|
-
for key, value in properties.items():
|
|
142
|
-
if '{' in value and '}' in value:
|
|
143
|
-
functional[key] = value
|
|
144
|
-
else:
|
|
145
|
-
explicit[key] = value
|
|
152
|
+
explicit = {k: properties[k] for k in properties if k != '' and not properties[k].is_formula}
|
|
153
|
+
functional = {k: properties[k] for k in properties if k != '' and properties[k].is_formula}
|
|
146
154
|
|
|
147
155
|
return explicit, functional
|
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import messages
|
|
2
2
|
from queries.cypherbuilder import QueryBuilder
|
|
3
|
-
from queries.typedefs import Properties
|
|
4
3
|
import assetmodelutilities as amu
|
|
5
4
|
from queries.query import Neo4jQuery
|
|
6
5
|
import processors.labelmanager as lm
|
|
7
6
|
import processors.relationshipmanager as rm
|
|
8
7
|
import processors.propertymanager as pm
|
|
8
|
+
import property
|
|
9
|
+
from property import Neo4jProperty
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class RelationshipFileProcessor:
|
|
12
13
|
|
|
13
|
-
def __init__(self, filename, config, contents):
|
|
14
|
+
def __init__(self, filename, config, contents, section):
|
|
14
15
|
self.filename = filename
|
|
15
16
|
self.contents = contents
|
|
16
17
|
|
|
17
|
-
self.primary_property = config.get_primary_property()
|
|
18
|
+
self.primary_property = config.get_primary_property()[section]
|
|
18
19
|
self.add_system_properties = not config.get_no_system_properties()
|
|
19
20
|
|
|
20
|
-
self.required_labels = config.get_required_labels()
|
|
21
|
-
self.creation_time_property = config.get_creation_time_property()
|
|
22
|
-
self.update_time_property = config.get_update_time_property()
|
|
21
|
+
self.required_labels = config.get_required_labels()[section]
|
|
22
|
+
self.creation_time_property = config.get_creation_time_property()[section]
|
|
23
|
+
self.update_time_property = config.get_update_time_property()[section]
|
|
23
24
|
|
|
24
|
-
self.from_node_alias = config.get_from_alias()
|
|
25
|
-
self.from_label_alias = config.get_from_label_alias()
|
|
26
|
-
self.to_node_alias = config.get_to_alias()
|
|
27
|
-
self.to_label_alias = config.get_to_label_alias()
|
|
28
|
-
self.relationship_alias = config.get_relationship_alias()
|
|
25
|
+
self.from_node_alias = config.get_from_alias()[section]
|
|
26
|
+
self.from_label_alias = config.get_from_label_alias()[section]
|
|
27
|
+
self.to_node_alias = config.get_to_alias()[section]
|
|
28
|
+
self.to_label_alias = config.get_to_label_alias()[section]
|
|
29
|
+
self.relationship_alias = config.get_relationship_alias()[section]
|
|
29
30
|
|
|
30
31
|
self.validate_only = config.in_validation_mode()
|
|
31
32
|
self.summarise = config.in_summary_mode()
|
|
@@ -76,7 +77,7 @@ class RelationshipFileProcessor:
|
|
|
76
77
|
|
|
77
78
|
relationships = rm.validate_relationships(self.contents.get_column_values_list(this_row, relation_columns))
|
|
78
79
|
|
|
79
|
-
properties = self.contents.get_property_values_dict(this_row, property_columns, self.blank_property_list
|
|
80
|
+
properties = self.contents.get_property_values_dict(this_row, property_columns, self.blank_property_list)
|
|
80
81
|
# If there are multiple From or To nodes, create a relationship query for each combination
|
|
81
82
|
for from_node in self.contents.get_property_values_list(this_row, from_columns, self.primary_property, self.primary_property):
|
|
82
83
|
validated_csv_from_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, from_label_columns))
|
|
@@ -93,9 +94,9 @@ class RelationshipFileProcessor:
|
|
|
93
94
|
time_now = amu.get_formatted_time_now()
|
|
94
95
|
query_text = (QueryBuilder()
|
|
95
96
|
.match()
|
|
96
|
-
.node(labels=from_labels, ref_name=from_ref, properties=
|
|
97
|
+
.node(labels=from_labels, ref_name=from_ref, properties=from_node)
|
|
97
98
|
.match()
|
|
98
|
-
.node(labels=to_labels, ref_name=to_ref, properties=
|
|
99
|
+
.node(labels=to_labels, ref_name=to_ref, properties=to_node)
|
|
99
100
|
.merge()
|
|
100
101
|
.node(ref_name=from_ref)
|
|
101
102
|
.related_to(label=relation, ref_name=relation_ref)
|
|
@@ -178,8 +179,8 @@ class RelationshipFileProcessor:
|
|
|
178
179
|
if relation:
|
|
179
180
|
|
|
180
181
|
explicit_properties, calculated_properties = pm.split_out_functional_properties(properties)
|
|
181
|
-
new_labels = [from_labels, to_labels, relation,
|
|
182
|
-
query_data = [
|
|
182
|
+
new_labels = [from_labels, to_labels, relation, from_node.key, to_node.key, calculated_properties]
|
|
183
|
+
query_data = [from_node.value, to_node.value, explicit_properties, calculated_properties, time_now]
|
|
183
184
|
|
|
184
185
|
# So now we know all about this particular update. Let's put it in the query dictionary
|
|
185
186
|
try:
|
|
@@ -206,7 +207,6 @@ class RelationshipFileProcessor:
|
|
|
206
207
|
relationship = batch_keys[from_to_labels][2]
|
|
207
208
|
from_key, from_type = pm.split_node_key(batch_keys[from_to_labels][3])
|
|
208
209
|
to_key, to_type = pm.split_node_key(batch_keys[from_to_labels][4])
|
|
209
|
-
|
|
210
210
|
batch_num = 1
|
|
211
211
|
while batch_num <= num_batches:
|
|
212
212
|
if batch_num == num_batches:
|
|
@@ -222,17 +222,17 @@ class RelationshipFileProcessor:
|
|
|
222
222
|
update = updates[(batch_num - 1) * self.batch_rate + this_size]
|
|
223
223
|
update_properties = update[2] or {} # In case update_properties[2] is {}
|
|
224
224
|
func_properties = update[3] or {} # In case update_properties[3] is {}
|
|
225
|
-
update_properties[from_ref
|
|
226
|
-
update_properties[to_ref
|
|
225
|
+
update_properties[from_ref] = Neo4jProperty(from_ref, update[0], from_type)
|
|
226
|
+
update_properties[to_ref] = Neo4jProperty(to_ref, update[1], to_type)
|
|
227
227
|
if self.add_system_properties:
|
|
228
|
-
update_properties[self.update_time_property] = update[4]
|
|
228
|
+
update_properties[self.update_time_property] = Neo4jProperty(self.update_time_property, update[4])
|
|
229
229
|
payload.append(update_properties)
|
|
230
230
|
this_size += 1
|
|
231
231
|
query_count += 1
|
|
232
232
|
|
|
233
233
|
formatted_payload = []
|
|
234
234
|
for a_payload in payload:
|
|
235
|
-
formatted_payload.append(
|
|
235
|
+
formatted_payload.append(property.format(a_payload))
|
|
236
236
|
# Build a MERGE query using queries
|
|
237
237
|
from_keys = " {" + from_key + ":" + payload_ref_dot + from_ref + "}"
|
|
238
238
|
to_keys = " {" + to_key + ":" + payload_ref_dot + to_ref + "}"
|
|
@@ -282,4 +282,3 @@ class RelationshipFileProcessor:
|
|
|
282
282
|
|
|
283
283
|
def is_inline_query(self):
|
|
284
284
|
return False
|
|
285
|
-
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
import messages
|
|
3
|
+
import ast
|
|
4
|
+
import processors.propertymanager as pm
|
|
5
|
+
|
|
6
|
+
class Neo4jProperty():
|
|
7
|
+
|
|
8
|
+
def __init__(self, key, value = None, format = None, type = None):
|
|
9
|
+
|
|
10
|
+
# print(f'k: {key}')
|
|
11
|
+
# print(f'f: {format}')
|
|
12
|
+
# print(f'v: {value}')
|
|
13
|
+
# print(f't: {type}')
|
|
14
|
+
|
|
15
|
+
self.key = pm.validate_property(key)
|
|
16
|
+
# Sanitise the value
|
|
17
|
+
self.value = value.replace('\\', '\\\\').replace('\r', '\\r').replace('\n', '\\n').replace('"', '\\"')
|
|
18
|
+
# format is the data type e.g. int, str, bool
|
|
19
|
+
self.format = format
|
|
20
|
+
# type is the kind of thing this is, such as ID, START_ID etc. See assetmodelutilities.py for the full list
|
|
21
|
+
self.type = type
|
|
22
|
+
|
|
23
|
+
# We'll format the value the first time someone wants it, so say it's not formatted yet
|
|
24
|
+
self.formatted_values = {}
|
|
25
|
+
|
|
26
|
+
# Now check to see if the value might be a formula
|
|
27
|
+
if '{' in self.value or '}' in self.value:
|
|
28
|
+
self.formula, self.is_formula = parse_value(value, 'n.')
|
|
29
|
+
else:
|
|
30
|
+
self.formula = None
|
|
31
|
+
self.is_formula = False
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def set_format(self, new_format):
|
|
35
|
+
# If the format has changed, update it (otherwise, no need to do anything)
|
|
36
|
+
if new_format != self.format:
|
|
37
|
+
self.format = new_format
|
|
38
|
+
# And say the value hasn't been formatted using the new format
|
|
39
|
+
self.formatted_values = {}
|
|
40
|
+
|
|
41
|
+
def set_value(self, new_value):
|
|
42
|
+
# If the value has changed, update it (otherwise, no need to do anything)
|
|
43
|
+
if new_value != self.value:
|
|
44
|
+
self.value = new_value
|
|
45
|
+
# And say the value hasn't been formatted using the new format
|
|
46
|
+
self.formatted_values = {}
|
|
47
|
+
|
|
48
|
+
def get_value(self, ref):
|
|
49
|
+
# Check to see if we have a formatted value, and if not do it now
|
|
50
|
+
if not ref in self.formatted_values.keys():
|
|
51
|
+
self.formatted_values[ref] = format_value(self, ref)
|
|
52
|
+
|
|
53
|
+
return self.formatted_values[ref]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def format_value(self, ref):
|
|
58
|
+
|
|
59
|
+
quote = '"'
|
|
60
|
+
value = self.value
|
|
61
|
+
given_data_format = self.format
|
|
62
|
+
|
|
63
|
+
# Redo the formula if ref is not 'n.' (which we did in the init section) and it is a real formula
|
|
64
|
+
if self.is_formula:
|
|
65
|
+
if ref != 'n.':
|
|
66
|
+
formula, is_formula = parse_value(self.value, ref)
|
|
67
|
+
else:
|
|
68
|
+
formula = self.formula
|
|
69
|
+
else:
|
|
70
|
+
formula = ''
|
|
71
|
+
|
|
72
|
+
if self.is_formula and formula != '':
|
|
73
|
+
match given_data_format:
|
|
74
|
+
case 'str':
|
|
75
|
+
result = f'toString({formula})'
|
|
76
|
+
case 'int':
|
|
77
|
+
result = f'toInteger({formula})'
|
|
78
|
+
case 'float':
|
|
79
|
+
result = f'toFloat({formula})'
|
|
80
|
+
case 'date':
|
|
81
|
+
result = f'date({formula})'
|
|
82
|
+
case 'datetime':
|
|
83
|
+
result = f'datetime({formula})'
|
|
84
|
+
case 'bool':
|
|
85
|
+
result = f'toBoolean({formula})'
|
|
86
|
+
case '' | _:
|
|
87
|
+
result = formula
|
|
88
|
+
elif self.is_formula:
|
|
89
|
+
result = ''
|
|
90
|
+
else:
|
|
91
|
+
try:
|
|
92
|
+
match given_data_format:
|
|
93
|
+
case 'str':
|
|
94
|
+
result = quote + (formula or value) + quote
|
|
95
|
+
case 'int':
|
|
96
|
+
# Remove any leading zeroes because the literal_eval function does not support them
|
|
97
|
+
while value.startswith('0'):
|
|
98
|
+
value = value[1:]
|
|
99
|
+
result = int(ast.literal_eval(value))
|
|
100
|
+
if result != ast.literal_eval(value):
|
|
101
|
+
messages.warning_message('Invalid data: ', f'{value} is not a valid Integer, {result} used instead')
|
|
102
|
+
case 'float':
|
|
103
|
+
result = float(ast.literal_eval(value))
|
|
104
|
+
case 'bool' | 'boolean':
|
|
105
|
+
result = ast.literal_eval(value.capitalize())
|
|
106
|
+
if str(result) not in ['True', 'False']:
|
|
107
|
+
messages.error_message('Invalid data: ', f'{value} is not a valid boolean so not set')
|
|
108
|
+
result = ''
|
|
109
|
+
case 'date':
|
|
110
|
+
formatted_date = try_dates(value)
|
|
111
|
+
if formatted_date:
|
|
112
|
+
result = 'date("' + str(formatted_date) + '")'
|
|
113
|
+
else:
|
|
114
|
+
messages.error_message(f'Invalid data: {value} is not a valid date so value not set')
|
|
115
|
+
result = ''
|
|
116
|
+
case 'datetime':
|
|
117
|
+
formatted_datetime = try_datetimes(value)
|
|
118
|
+
if formatted_datetime:
|
|
119
|
+
result = 'datetime("' + str(formatted_datetime) + '")'
|
|
120
|
+
else:
|
|
121
|
+
messages.error_message(f'Invalid data: {value} is not a valid datetime so value not set')
|
|
122
|
+
result = ''
|
|
123
|
+
case '':
|
|
124
|
+
# No format requested, so we'll try to work it out
|
|
125
|
+
try:
|
|
126
|
+
# Use .capitalize() here to catch incorrectly formatted Booleans (e.g. 'true')
|
|
127
|
+
boolean_check = value.capitalize()
|
|
128
|
+
if boolean_check in ['True', 'False']:
|
|
129
|
+
data_type = bool
|
|
130
|
+
result = boolean_check
|
|
131
|
+
# Update value for use in Version management in VersionManager.are_there_changes()
|
|
132
|
+
self.value = boolean_check
|
|
133
|
+
self.format = 'bool'
|
|
134
|
+
else:
|
|
135
|
+
data_type = type(ast.literal_eval(value))
|
|
136
|
+
except:
|
|
137
|
+
# Now see if it's a date or datetime
|
|
138
|
+
formatted_date = try_dates(value)
|
|
139
|
+
formatted_time = try_times(value)
|
|
140
|
+
if formatted_date:
|
|
141
|
+
if formatted_time:
|
|
142
|
+
data_type = 'datetime'
|
|
143
|
+
result = 'datetime("' + str(value) + '")'
|
|
144
|
+
self.format = 'datetime'
|
|
145
|
+
else:
|
|
146
|
+
data_type = 'date'
|
|
147
|
+
result = 'date("' + str(formatted_date) + '")'
|
|
148
|
+
self.format = 'date'
|
|
149
|
+
else:
|
|
150
|
+
data_type = 'str'
|
|
151
|
+
self.format = 'str'
|
|
152
|
+
|
|
153
|
+
if value:
|
|
154
|
+
match str(data_type):
|
|
155
|
+
case 'str':
|
|
156
|
+
result = quote + (formula or value) + quote
|
|
157
|
+
case "<class 'int'>":
|
|
158
|
+
if value != '0' and value.startswith('0'):
|
|
159
|
+
# Has a leading 0, so treat as a string
|
|
160
|
+
result = quote + value + quote
|
|
161
|
+
else:
|
|
162
|
+
result = int(ast.literal_eval(value))
|
|
163
|
+
if result != ast.literal_eval(value):
|
|
164
|
+
messages.warning_message('Invalid data: ', f'{value} is not a valid Integer, {result} used instead')
|
|
165
|
+
self.format = 'int'
|
|
166
|
+
case "<class 'float'>":
|
|
167
|
+
result = ast.literal_eval(value)
|
|
168
|
+
self.format = 'float'
|
|
169
|
+
case "<class 'bool'>":
|
|
170
|
+
# result already set above, so
|
|
171
|
+
pass
|
|
172
|
+
case 'date' | 'datetime':
|
|
173
|
+
# result already set above, so
|
|
174
|
+
pass
|
|
175
|
+
case "<class 'list'>":
|
|
176
|
+
result = value
|
|
177
|
+
case _:
|
|
178
|
+
# Shouldn't get here, but just in case use a string
|
|
179
|
+
result = quote + value + quote
|
|
180
|
+
|
|
181
|
+
case _:
|
|
182
|
+
# Treat unsupported formats as strings
|
|
183
|
+
result = quote + value + quote
|
|
184
|
+
except:
|
|
185
|
+
messages.error_message('Invalid data: ', f"{value} is not valid for type '{given_data_format}' so value not set!")
|
|
186
|
+
result = ''
|
|
187
|
+
|
|
188
|
+
return result
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def parse_value(formula, ref):
|
|
192
|
+
num_open = 0
|
|
193
|
+
num_close = 0
|
|
194
|
+
parsed = ''
|
|
195
|
+
index = 0
|
|
196
|
+
open = False
|
|
197
|
+
property_name = ''
|
|
198
|
+
while index < len(formula):
|
|
199
|
+
this_char = formula[index]
|
|
200
|
+
next_char = (formula+' ')[index+1] # Added a space to formula, so we don't run off the end
|
|
201
|
+
if this_char == '{':
|
|
202
|
+
if next_char == '{':
|
|
203
|
+
if open:
|
|
204
|
+
property_name += this_char
|
|
205
|
+
else:
|
|
206
|
+
parsed += this_char
|
|
207
|
+
index += 1
|
|
208
|
+
else:
|
|
209
|
+
open = True
|
|
210
|
+
property_name = ''
|
|
211
|
+
num_open += 1
|
|
212
|
+
elif this_char == '}':
|
|
213
|
+
if next_char == '}':
|
|
214
|
+
if open:
|
|
215
|
+
property_name += this_char
|
|
216
|
+
else:
|
|
217
|
+
parsed += this_char
|
|
218
|
+
index += 1
|
|
219
|
+
else:
|
|
220
|
+
if open and property_name != '':
|
|
221
|
+
# Replace {property} with n.property (assuming ref = "n.")
|
|
222
|
+
parsed += ref + pm.validate_property(property_name)
|
|
223
|
+
else:
|
|
224
|
+
# We've just found {}, so discount this
|
|
225
|
+
property_name = property_name[:-1]
|
|
226
|
+
open = False
|
|
227
|
+
num_close += 1
|
|
228
|
+
else:
|
|
229
|
+
if open:
|
|
230
|
+
property_name += this_char
|
|
231
|
+
else:
|
|
232
|
+
parsed += this_char
|
|
233
|
+
index += 1
|
|
234
|
+
|
|
235
|
+
is_formula = (num_open == num_close) and (num_open > 0)
|
|
236
|
+
|
|
237
|
+
return parsed.strip(), is_formula
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def try_date_format(date, date_format):
|
|
241
|
+
try:
|
|
242
|
+
tested_date = datetime.datetime.strptime(date.replace('/', '-'), date_format).date()
|
|
243
|
+
except:
|
|
244
|
+
tested_date = None
|
|
245
|
+
return tested_date
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def try_dates(date):
|
|
249
|
+
date1 = try_date_format(date[:10], '%Y-%m-%d')
|
|
250
|
+
date2 = try_date_format(date[:10], '%d-%m-%Y')
|
|
251
|
+
date_found = date1 or date2
|
|
252
|
+
return date_found
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def try_datetimes(datetime):
|
|
256
|
+
date = try_dates(datetime)
|
|
257
|
+
time = try_times(datetime) or '00:00:00'
|
|
258
|
+
if date:
|
|
259
|
+
datetime_found = str(date) + 'T' + str(time)
|
|
260
|
+
else:
|
|
261
|
+
datetime_found = None
|
|
262
|
+
return datetime_found
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def try_time_format(time, time_format):
|
|
266
|
+
try:
|
|
267
|
+
tested_time = datetime.datetime.strptime(time, time_format).time()
|
|
268
|
+
except:
|
|
269
|
+
tested_time = None
|
|
270
|
+
return tested_time
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def try_times(time):
|
|
274
|
+
time1 = try_time_format(time[11:19], '%H:%M:%S')
|
|
275
|
+
if time1:
|
|
276
|
+
time_found = time[11:]
|
|
277
|
+
else:
|
|
278
|
+
time_found = None
|
|
279
|
+
return time_found
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def format_property_list(properties, comparison_operator = ':', boolean_operator = ',', property_ref='', key_ref=''):
|
|
283
|
+
if not isinstance(properties, dict):
|
|
284
|
+
prop_list = {properties.key: properties}
|
|
285
|
+
else:
|
|
286
|
+
prop_list = properties
|
|
287
|
+
pairs = [f'{key_ref}{prop_list[property].key}{comparison_operator}{prop_list[property].get_value(property_ref)}' for property in prop_list.keys()]
|
|
288
|
+
res = boolean_operator.join(pairs)
|
|
289
|
+
return res
|
|
290
|
+
|
|
291
|
+
def format(properties, property_ref=''):
|
|
292
|
+
# Reformat using identified data types
|
|
293
|
+
pairs = []
|
|
294
|
+
for p in properties.keys():
|
|
295
|
+
pairs.append(f'{properties[p].key}:{properties[p].get_value(property_ref)}')
|
|
296
|
+
return '{' + ','.join(pairs) + '}'
|
|
297
|
+
|
|
298
|
+
|