python-eigen-ingenuity 0.4.35b2__py3-none-any.whl → 0.4.35b3__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.
- eigeningenuity/assetmodel.py +1 -1
- eigeningenuity/historianmulti.py +2 -2
- eigeningenuity/util.py +1 -1
- modelbuilder/src/assetmodelcsvwriter.py +1 -0
- modelbuilder/src/assetmodelutilities.py +16 -86
- modelbuilder/src/configmanager.py +68 -79
- modelbuilder/src/connectors/httpsconnector.py +11 -13
- modelbuilder/src/csvcontentsmanager.py +70 -38
- modelbuilder/src/csvwriter.py +29 -65
- modelbuilder/src/fileanalyser.py +91 -0
- modelbuilder/src/filemanager.py +40 -29
- modelbuilder/src/processors/__init__.py +0 -0
- modelbuilder/src/processors/cypherfileprocessor.py +73 -0
- modelbuilder/src/processors/labelmanager.py +133 -0
- modelbuilder/src/processors/nodefileprocessor.py +266 -0
- modelbuilder/src/processors/propertymanager.py +87 -0
- modelbuilder/src/processors/relationshipfileprocessor.py +283 -0
- modelbuilder/src/processors/relationshipmanager.py +31 -0
- modelbuilder/src/queries/cypherbuilder.py +3 -15
- modelbuilder/src/queries/typedefs.py +2 -2
- modelbuilder/src/versionmanager.py +13 -11
- {python_eigen_ingenuity-0.4.35b2.dist-info → python_eigen_ingenuity-0.4.35b3.dist-info}/METADATA +1 -1
- python_eigen_ingenuity-0.4.35b3.dist-info/RECORD +46 -0
- python_eigen_ingenuity-0.4.35b2.dist-info/RECORD +0 -38
- {python_eigen_ingenuity-0.4.35b2.dist-info → python_eigen_ingenuity-0.4.35b3.dist-info}/WHEEL +0 -0
- {python_eigen_ingenuity-0.4.35b2.dist-info → python_eigen_ingenuity-0.4.35b3.dist-info}/entry_points.txt +0 -0
- {python_eigen_ingenuity-0.4.35b2.dist-info → python_eigen_ingenuity-0.4.35b3.dist-info}/top_level.txt +0 -0
|
@@ -1,20 +1,67 @@
|
|
|
1
1
|
import assetmodelutilities as amu
|
|
2
|
+
import processors.propertymanager as pm
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
|
|
5
|
+
class CSVContentsManager:
|
|
4
6
|
|
|
5
7
|
# Manages the contents of a .CSV file
|
|
6
8
|
# Returns the data requested by the Processor classes
|
|
7
9
|
|
|
8
|
-
def __init__(self, csv_data, header_mappings, default_data_type):
|
|
10
|
+
def __init__(self, csv_data, header_mappings, alias_mappings, default_data_type):
|
|
9
11
|
file_headers = next(csv_data)
|
|
10
|
-
header_names = [(header_mappings[i.split(':')[0]] if i.split(':')[0] in header_mappings.keys() else i.split(':')[0]).replace('\ufeff', '') for i in file_headers]
|
|
11
|
-
self.header_names = [amu.validate_property(i) for i in header_names]
|
|
12
|
-
self.header_types = [i.split(':')[1] if len(i.split(':')) > 1 else default_data_type for i in file_headers]
|
|
13
|
-
self.headers = [self.header_names[i].split(':')[0] + ':' + self.header_types[i] if self.header_types[i] != '' else self.header_names[i] + ':' + j if j != '' else self.header_names[i] for i, j in enumerate(self.header_types)]
|
|
14
12
|
self.data_list = list(csv_data)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
unmapped_keys = []
|
|
14
|
+
mapped_keys = []
|
|
15
|
+
self.formats = []
|
|
16
|
+
self.types = []
|
|
17
|
+
|
|
18
|
+
for header in file_headers:
|
|
19
|
+
this_format = ''
|
|
20
|
+
this_type = ''
|
|
21
|
+
split_header = header.split(':')
|
|
22
|
+
this_key = split_header[0].replace('\ufeff', '')
|
|
23
|
+
for format_type in split_header[1:]:
|
|
24
|
+
if format_type and format_type in amu.all_types:
|
|
25
|
+
this_type += ':' + format_type
|
|
26
|
+
else:
|
|
27
|
+
this_format += ':' + format_type
|
|
28
|
+
if this_format == '' and default_data_type != '':
|
|
29
|
+
this_format = ':' + default_data_type
|
|
30
|
+
|
|
31
|
+
unmapped_keys += [this_key]
|
|
32
|
+
|
|
33
|
+
# Now apply key mappings from config file
|
|
34
|
+
if this_key in header_mappings:
|
|
35
|
+
mapped_key = header_mappings[this_key]
|
|
36
|
+
elif this_key + this_format in header_mappings:
|
|
37
|
+
mapped_key = header_mappings[this_key + this_format]
|
|
38
|
+
else:
|
|
39
|
+
mapped_key = this_key
|
|
40
|
+
|
|
41
|
+
if this_key in alias_mappings:
|
|
42
|
+
alias_mapped_key = alias_mappings[this_key]
|
|
43
|
+
else:
|
|
44
|
+
alias_mapped_key = this_key
|
|
45
|
+
|
|
46
|
+
if this_key != alias_mapped_key:
|
|
47
|
+
this_mapped_key = alias_mapped_key
|
|
48
|
+
elif this_key != mapped_key:
|
|
49
|
+
split_mapped_key = mapped_key.split(':')
|
|
50
|
+
this_mapped_key = split_mapped_key[0]
|
|
51
|
+
# Mapped Key may have new format...
|
|
52
|
+
if len(split_mapped_key) > 1:
|
|
53
|
+
if split_mapped_key[1] != '':
|
|
54
|
+
this_format = ':' + split_mapped_key[1]
|
|
55
|
+
else:
|
|
56
|
+
this_format = ''
|
|
57
|
+
else:
|
|
58
|
+
this_mapped_key = this_key
|
|
59
|
+
mapped_keys += [this_mapped_key]
|
|
60
|
+
|
|
61
|
+
self.unmapped_keys = pm.validate_properties(unmapped_keys)
|
|
62
|
+
self.mapped_keys = pm.validate_properties(mapped_keys)
|
|
63
|
+
self.formats += [this_format]
|
|
64
|
+
self.types += [this_type]
|
|
18
65
|
|
|
19
66
|
def get_row_count(self):
|
|
20
67
|
return len(self.data_list)
|
|
@@ -29,7 +76,7 @@ class csvContentsManager:
|
|
|
29
76
|
name_list = [column_name]
|
|
30
77
|
else:
|
|
31
78
|
name_list = column_name
|
|
32
|
-
column_list = [i for i, j in enumerate(self.
|
|
79
|
+
column_list = [i for i, j in enumerate(self.mapped_keys) if (j in name_list or len(set(self.types[i].split(':')).intersection(set(type_list))) != 0)]
|
|
33
80
|
return column_list
|
|
34
81
|
|
|
35
82
|
def get_other_column_numbers_list(self, column_name, type_list=[]):
|
|
@@ -38,11 +85,11 @@ class csvContentsManager:
|
|
|
38
85
|
name_list = [column_name]
|
|
39
86
|
else:
|
|
40
87
|
name_list = column_name
|
|
41
|
-
column_list = [i for i, j in enumerate(self.
|
|
88
|
+
column_list = [i for i, j in enumerate(self.mapped_keys) if not(j in name_list or len(set(self.types[i].split(':')).intersection(set(type_list))) != 0)]
|
|
42
89
|
return column_list
|
|
43
90
|
|
|
44
91
|
def get_incomplete_rows(self):
|
|
45
|
-
num_headers = len(self.
|
|
92
|
+
num_headers = len(self.mapped_keys)
|
|
46
93
|
incomplete_rows = []
|
|
47
94
|
row_count = 0
|
|
48
95
|
for a_row in self.data_list:
|
|
@@ -51,43 +98,28 @@ class csvContentsManager:
|
|
|
51
98
|
incomplete_rows.append(row_count)
|
|
52
99
|
return incomplete_rows
|
|
53
100
|
|
|
54
|
-
def get_column_values(self, row_number, column_numbers, delimiter=''):
|
|
55
|
-
# This one is not currently used
|
|
56
|
-
# Return the values in the given row, for the columns in the provided list (skipping empty values)
|
|
57
|
-
# Returned as string, with an optional delimiter between values
|
|
58
|
-
this_row = self.data_list[row_number]
|
|
59
|
-
columns = delimiter.join([this_row[column] for column in column_numbers if this_row[column]])
|
|
60
|
-
return columns
|
|
61
|
-
|
|
62
101
|
def get_column_values_list(self, row_number, column_numbers):
|
|
63
102
|
# Return the values in the given row, for the columns in the provided list (skips empty values)
|
|
64
103
|
# Returned as a List (of strings)
|
|
65
|
-
# Also validate the returned values to ensure they are legal Cypher names
|
|
66
104
|
this_row = self.data_list[row_number]
|
|
67
|
-
columns_list = [
|
|
105
|
+
columns_list = [this_row[i] for i in column_numbers if len(this_row[i]) > 0]
|
|
68
106
|
return columns_list
|
|
69
107
|
|
|
70
|
-
def
|
|
108
|
+
def get_column_values_list_with_formatted_keys(self, row_number, column_numbers):
|
|
71
109
|
# Return the values in the given row, for the columns in the provided list (skips empty values)
|
|
72
110
|
# Returned as a List (of strings)
|
|
73
111
|
this_row = self.data_list[row_number]
|
|
74
|
-
|
|
75
|
-
properties_list = [{this_row[i]:self.header_names[i]} if self.header_types[i] in node_types else {this_row[i]: ''} for i in column_numbers if len(this_row[i])]
|
|
112
|
+
properties_list = [{self.mapped_keys[column]+self.formats[column]:this_row[column]} for column in column_numbers if len(this_row[column])]
|
|
76
113
|
return properties_list
|
|
77
114
|
|
|
78
|
-
def
|
|
79
|
-
# This one is not currently used
|
|
80
|
-
# Return key:values pairs from the given row, for the columns in the provided list (skips empty values)
|
|
81
|
-
# Returned as a comma separated string e.g. 'key1:value1, key2:value2'
|
|
82
|
-
# Treat everything as a string here (using " as a delimiter). Actual data types will be determined later - see queries.typedefs.py
|
|
83
|
-
delimiter = '"'
|
|
115
|
+
def get_property_values_dict(self, row_number, column_numbers, allow_blanks=False, default_key='', prefix=''):
|
|
84
116
|
this_row = self.data_list[row_number]
|
|
85
|
-
|
|
86
|
-
|
|
117
|
+
split_values = [this_row[column].split('::') for column in column_numbers]
|
|
118
|
+
this_list = {prefix + (self.mapped_keys[column] or default_key) + self.formats[column]: this_row[column] if len(split_values[count]) == 1 else {prefix + split_values[count][0] + self.formats[column]: split_values[count][1]} for count, column in enumerate(column_numbers) if this_row[column] or allow_blanks}
|
|
119
|
+
return this_list
|
|
87
120
|
|
|
88
|
-
def
|
|
89
|
-
# Return key:values pairs from the given row, for the columns in the provided list (skips empty values)
|
|
90
|
-
# Returned as a Dict (assumes all columns are unique (which they should be!))
|
|
121
|
+
def get_property_values_list(self, row_number, column_numbers, allow_blanks=False, default_key='', override_key=''):
|
|
91
122
|
this_row = self.data_list[row_number]
|
|
92
|
-
|
|
93
|
-
|
|
123
|
+
split_values = [this_row[column].split('::') for column in column_numbers]
|
|
124
|
+
this_list = [{(override_key or self.mapped_keys[column] or default_key) + self.formats[column]: this_row[column]} if len(split_values[count]) == 1 else {split_values[count][0] + self.formats[column]: split_values[count][1]} for count, column in enumerate(column_numbers) if this_row[column] or allow_blanks]
|
|
125
|
+
return this_list
|
modelbuilder/src/csvwriter.py
CHANGED
|
@@ -3,9 +3,8 @@ from queries.query import Neo4jQuery
|
|
|
3
3
|
from queries.cypherbuilder import QueryBuilder, ValidAfterWhere
|
|
4
4
|
from queries.typedefs import *
|
|
5
5
|
from messages import *
|
|
6
|
-
import assetmodelutilities
|
|
7
|
-
|
|
8
|
-
## json import neo4j
|
|
6
|
+
import assetmodelutilities as amu
|
|
7
|
+
from processors import labelmanager as lm
|
|
9
8
|
|
|
10
9
|
class CSVFileWriter:
|
|
11
10
|
|
|
@@ -18,9 +17,7 @@ class CSVFileWriter:
|
|
|
18
17
|
self.csv_node_primary_property = config_manager.get_csv_node_property_list()
|
|
19
18
|
self.file_suffix = config_manager.get_csv_file_suffix()
|
|
20
19
|
self.write_relationship_file = config_manager.get_relationship_csvfile_requested()
|
|
21
|
-
# self.from_labels = config_manager.get_from_csv_labels()
|
|
22
20
|
self.to_labels = config_manager.get_to_csv_labels()
|
|
23
|
-
# self.csv_from_primary_property = config_manager.get_csv_from_property_list()
|
|
24
21
|
self.csv_to_primary_property = config_manager.get_csv_to_property_list()
|
|
25
22
|
if self.csv_to_primary_property != self.csv_node_primary_property:
|
|
26
23
|
self.to_prefix = self.csv_to_primary_property + '::'
|
|
@@ -34,6 +31,7 @@ class CSVFileWriter:
|
|
|
34
31
|
self.required_labels = config_manager.get_required_labels()
|
|
35
32
|
self.primary_property = config_manager.get_primary_property()
|
|
36
33
|
self.unique_id_property = config_manager.get_unique_id_property()
|
|
34
|
+
self.version_labels = config_manager.get_version_labels_list()
|
|
37
35
|
|
|
38
36
|
self.relationship_alias = config_manager.get_relationship_alias()[0]
|
|
39
37
|
self.from_alias = config_manager.get_from_alias()[0]
|
|
@@ -78,7 +76,6 @@ class CSVFileWriter:
|
|
|
78
76
|
# output_file = def_path + 'Nodes ' + suffix + '-' + label_suffix.replace(':', '-') + '.csv'
|
|
79
77
|
output_file = def_path + 'Nodes ' + suffix
|
|
80
78
|
|
|
81
|
-
# with open(output_file+'.csv', 'w', encoding='UTF8', newline='') as f:
|
|
82
79
|
try:
|
|
83
80
|
with open(output_file+'.csv', 'w', newline='') as f:
|
|
84
81
|
writer = csv.DictWriter(f, fieldnames=self.all_keys, delimiter=';', quoting=csv.QUOTE_MINIMAL)
|
|
@@ -90,59 +87,36 @@ class CSVFileWriter:
|
|
|
90
87
|
writer = csv.DictWriter(f, fieldnames=self.all_keys, delimiter=';', quoting=csv.QUOTE_MINIMAL)
|
|
91
88
|
writer.writeheader()
|
|
92
89
|
writer.writerows(self.all_nodes)
|
|
93
|
-
# print(f'Written {len(self.all_nodes)} lines to {output_file}.csv @2')
|
|
94
90
|
except:
|
|
95
91
|
pass
|
|
96
92
|
|
|
97
|
-
## json with open(output_file + '.json', 'w') as f:
|
|
98
|
-
## json json.dump(self.all_nodes, f)
|
|
99
|
-
|
|
100
93
|
if self.write_relationship_file:
|
|
101
94
|
def_path = self.config_manager.get_default_path()
|
|
102
95
|
suffix = self.file_suffix
|
|
103
96
|
output_file = def_path + 'Relationships ' + suffix
|
|
104
97
|
|
|
105
|
-
# with open(output_file + '.csv', 'w', encoding='UTF8', newline='') as f:
|
|
106
98
|
try:
|
|
107
99
|
with open(output_file+'.csv', 'w', newline='') as f:
|
|
108
100
|
writer = csv.DictWriter(f, fieldnames=self.all_relationship_keys, delimiter=';', quoting=csv.QUOTE_MINIMAL)
|
|
109
101
|
writer.writeheader()
|
|
110
102
|
writer.writerows(self.all_relationships)
|
|
111
103
|
except:
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
## json with open(output_file + '.json', 'w') as f:
|
|
121
|
-
## json json.dump(self.all_relationships, f)
|
|
122
|
-
|
|
123
|
-
pass
|
|
104
|
+
try:
|
|
105
|
+
with open(output_file+'.csv', 'w', encoding='UTF8', newline='') as f:
|
|
106
|
+
writer = csv.DictWriter(f, fieldnames=self.all_relationship_keys, delimiter=';', quoting=csv.QUOTE_MINIMAL)
|
|
107
|
+
writer.writeheader()
|
|
108
|
+
writer.writerows(self.all_relationships)
|
|
109
|
+
except:
|
|
110
|
+
pass
|
|
124
111
|
|
|
125
112
|
def update_node_data(self, data):
|
|
126
113
|
|
|
127
114
|
for node in data:
|
|
128
115
|
|
|
129
|
-
# This is needed for json file output.
|
|
130
|
-
# Can simply use node['Properties'] if json support not implemented
|
|
131
|
-
## json unchecked_properties = node['Properties']
|
|
132
|
-
## json properties = {}
|
|
133
|
-
## json for k, v in unchecked_properties.items():
|
|
134
|
-
## json if isinstance(v, neo4j.time.Date):
|
|
135
|
-
## json properties[k] = str(v)
|
|
136
|
-
## json elif isinstance(v, neo4j.time.DateTime):
|
|
137
|
-
## json properties[k] = str(v)
|
|
138
|
-
## json else:
|
|
139
|
-
## json properties[k] = v
|
|
140
|
-
|
|
141
116
|
properties = node['Properties']
|
|
142
|
-
properties[self.node_label] =
|
|
117
|
+
properties[self.node_label] = lm.validate_output_labels(node['FromLabels'])
|
|
143
118
|
|
|
144
119
|
self.pop_property(properties, self.config_manager.get_update_time_property())
|
|
145
|
-
# self.pop_property(properties, self.config_manager.get_primary_id_property())
|
|
146
120
|
self.pop_property(properties, self.config_manager.get_creation_time_property())
|
|
147
121
|
try:
|
|
148
122
|
self.pop_property(properties, self.config_manager.get_version_counter())
|
|
@@ -154,7 +128,6 @@ class CSVFileWriter:
|
|
|
154
128
|
|
|
155
129
|
if primary_key != self.primary_property:
|
|
156
130
|
properties.pop(primary_key)
|
|
157
|
-
# all_keys = list(set([*all_keys, self.primary_property]))
|
|
158
131
|
if primary_key in self.all_keys:
|
|
159
132
|
self.all_keys.remove(primary_key)
|
|
160
133
|
|
|
@@ -168,16 +141,10 @@ class CSVFileWriter:
|
|
|
168
141
|
if relationship['Relationship']:
|
|
169
142
|
properties = relationship['RelationshipProperties']
|
|
170
143
|
|
|
171
|
-
## json for k, v in properties.items():
|
|
172
|
-
## json if isinstance(v, neo4j.time.Date):
|
|
173
|
-
## json properties[k] = str(v)
|
|
174
|
-
## json if isinstance(v, neo4j.time.DateTime):
|
|
175
|
-
## json properties[k] = str(v)
|
|
176
|
-
|
|
177
144
|
properties[self.from_alias] = relationship['From']
|
|
178
145
|
properties[self.to_alias] = self.to_prefix + relationship['To']
|
|
179
|
-
properties[self.from_label] =
|
|
180
|
-
properties[self.to_label] =
|
|
146
|
+
properties[self.from_label] = lm.validate_output_labels(relationship['FromLabels'])
|
|
147
|
+
properties[self.to_label] = lm.validate_output_labels(relationship['ToLabels'])
|
|
181
148
|
properties[self.relationship_alias] = relationship['Relationship']
|
|
182
149
|
|
|
183
150
|
self.pop_property(properties, self.config_manager.get_update_time_property())
|
|
@@ -235,7 +202,7 @@ class CSVFileWriter:
|
|
|
235
202
|
to_excl_labels = to_labels[1]
|
|
236
203
|
|
|
237
204
|
where_literal = f'NOT {node_ref}:' + f' AND NOT {node_ref}:'.join(excl_labels)
|
|
238
|
-
to_where_literal = f'NOT {
|
|
205
|
+
to_where_literal = f'NOT {to_node_ref}:' + f' AND NOT {to_node_ref}:'.join(to_excl_labels)
|
|
239
206
|
|
|
240
207
|
node_query = (QueryBuilder()
|
|
241
208
|
.match()
|
|
@@ -274,7 +241,7 @@ class CSVFileWriter:
|
|
|
274
241
|
batch_num = 0
|
|
275
242
|
while batch_num < num_batches:
|
|
276
243
|
num_none = 0
|
|
277
|
-
message_no_cr(f'Processing batch {batch_num+1} of {num_batches} at {
|
|
244
|
+
message_no_cr(f'Processing batch {batch_num+1} of {num_batches} at {amu.get_formatted_time_now_noms()}{chr(13)}')
|
|
278
245
|
this_batch = node_list[batch_num * self.batch_size:(batch_num + 1) * self.batch_size]
|
|
279
246
|
while None in this_batch:
|
|
280
247
|
this_batch.remove(None)
|
|
@@ -289,24 +256,18 @@ class CSVFileWriter:
|
|
|
289
256
|
if self.write_node_file:
|
|
290
257
|
this_query_text = this_query.return_literal(node_response_text).toStr().replace('\\"', '"')
|
|
291
258
|
this_node_query = Neo4jQuery('get data', this_query_text)
|
|
292
|
-
print(f'NQ: {this_query_text}')
|
|
293
259
|
result = self.db_connector.execute(this_node_query)
|
|
294
260
|
response = result.get_response()
|
|
295
261
|
if response:
|
|
296
262
|
self.update_node_data(response)
|
|
297
|
-
# else:
|
|
298
|
-
# print(this_query_text)
|
|
299
263
|
|
|
300
264
|
if self.write_relationship_file:
|
|
301
265
|
this_query_text = this_query.toStr().replace('\\"', '"') + ' ' + relationship_query.return_literal(relationship_response_text).toStr()
|
|
302
266
|
this_relationship_query = Neo4jQuery('get data', this_query_text)
|
|
303
|
-
print(f'RQ: {this_query_text}')
|
|
304
267
|
result = self.db_connector.execute(this_relationship_query)
|
|
305
268
|
response = result.get_response()
|
|
306
269
|
if response:
|
|
307
270
|
self.update_relationship_data(response)
|
|
308
|
-
# else:
|
|
309
|
-
# print(this_query_text)
|
|
310
271
|
|
|
311
272
|
batch_num += 1
|
|
312
273
|
|
|
@@ -328,14 +289,17 @@ class CSVFileWriter:
|
|
|
328
289
|
|
|
329
290
|
def write_csv_files(self):
|
|
330
291
|
if self.write_node_file or self.write_relationship_file:
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
292
|
+
wanted_labels, unwanted_labels = lm.sort_output_labels(self.node_labels, self.version_labels)
|
|
293
|
+
wanted_to_labels, unwanted_to_labels = lm.sort_output_labels(self.to_labels, self.version_labels)
|
|
294
|
+
node_list = self.get_node_list([wanted_labels, unwanted_labels])
|
|
295
|
+
num_nodes_found = len(node_list)
|
|
296
|
+
if num_nodes_found == 1:
|
|
297
|
+
intro = 'There is 1 node'
|
|
298
|
+
else:
|
|
299
|
+
intro = f'There are {num_nodes_found} nodes'
|
|
300
|
+
print(f'{intro} with :{":".join(lm.strip_quotes(wanted_labels))}', end='')
|
|
301
|
+
if unwanted_labels:
|
|
302
|
+
print(f' but not :{" nor :".join(lm.strip_quotes(unwanted_labels))}')
|
|
303
|
+
else:
|
|
304
|
+
print()
|
|
305
|
+
self.process_data(node_list, [wanted_labels, unwanted_labels], [wanted_to_labels, unwanted_to_labels])
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
|
|
3
|
+
import messages
|
|
4
|
+
from csvcontentsmanager import CSVContentsManager
|
|
5
|
+
import assetmodelutilities as amu
|
|
6
|
+
|
|
7
|
+
class FileAnalyser():
|
|
8
|
+
|
|
9
|
+
def __init__(self, config):
|
|
10
|
+
self.separator = config.get_separator()
|
|
11
|
+
self.def_path = config.get_default_path()
|
|
12
|
+
|
|
13
|
+
self.header_mappings, self.alias_mappings = config.get_header_mappings()
|
|
14
|
+
self.default_data_type = config.get_default_data_type()
|
|
15
|
+
|
|
16
|
+
self.node_alias = config.get_node_alias()
|
|
17
|
+
self.from_node_alias = config.get_from_alias()
|
|
18
|
+
self.to_node_alias = config.get_to_alias()
|
|
19
|
+
self.relationship_alias = config.get_relationship_alias()
|
|
20
|
+
self.ignore_nodes = config.get_ignore_nodes()
|
|
21
|
+
|
|
22
|
+
self.node_types = amu.node_types
|
|
23
|
+
self.from_types = amu.from_types
|
|
24
|
+
self.to_types = amu.to_types
|
|
25
|
+
self.relation_types = amu.relation_types
|
|
26
|
+
self.label_types = amu.label_types
|
|
27
|
+
self.all_types = amu.all_types
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def determine_file_type(self, filename, file_format):
|
|
31
|
+
|
|
32
|
+
contents = None
|
|
33
|
+
file_name = amu.find_file(self.def_path, filename)
|
|
34
|
+
if file_name is None:
|
|
35
|
+
return 'fnf', contents, []
|
|
36
|
+
|
|
37
|
+
match file_format:
|
|
38
|
+
case 'csv':
|
|
39
|
+
try:
|
|
40
|
+
with open(file_name, encoding='utf8') as csv_file:
|
|
41
|
+
csv_data = csv.reader(csv_file, delimiter=self.separator)
|
|
42
|
+
contents = CSVContentsManager(csv_data, self.header_mappings, self.alias_mappings, self.default_data_type)
|
|
43
|
+
except:
|
|
44
|
+
try:
|
|
45
|
+
with open(file_name) as csv_file:
|
|
46
|
+
csv_data = csv.reader(csv_file, delimiter=self.separator)
|
|
47
|
+
contents = CSVContentsManager(csv_data, self.header_mappings, self.alias_mappings, self.default_data_type)
|
|
48
|
+
except:
|
|
49
|
+
return 'incorrect file format', contents, []
|
|
50
|
+
case _:
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
num_node_columns = contents.get_column_count(self.node_alias, self.node_types)
|
|
54
|
+
num_from_columns = contents.get_column_count(self.from_node_alias, self.from_types)
|
|
55
|
+
num_to_columns = contents.get_column_count(self.to_node_alias, self.to_types)
|
|
56
|
+
num_relation_columns = contents.get_column_count(self.relationship_alias, self.relation_types)
|
|
57
|
+
|
|
58
|
+
incomplete_rows = contents.get_incomplete_rows()
|
|
59
|
+
if len(incomplete_rows) > 0:
|
|
60
|
+
return 'incomplete row(s)', contents, incomplete_rows
|
|
61
|
+
|
|
62
|
+
# If there is a least one Node (including aliases) column, this could be a Node file
|
|
63
|
+
if num_node_columns > 0:
|
|
64
|
+
could_be_node = True
|
|
65
|
+
if num_node_columns > 1:
|
|
66
|
+
messages.warning_message(f'Multiple node columns found in ', f'{filename}')
|
|
67
|
+
else:
|
|
68
|
+
could_be_node = False
|
|
69
|
+
|
|
70
|
+
# Check if there are at least one each of From, To and Relationship (including aliases) columns
|
|
71
|
+
# If so, this could be a Relationship file
|
|
72
|
+
if num_from_columns > 0 and num_to_columns > 0 and num_relation_columns > 0:
|
|
73
|
+
could_be_relations = True
|
|
74
|
+
else:
|
|
75
|
+
could_be_relations = False
|
|
76
|
+
|
|
77
|
+
if could_be_node:
|
|
78
|
+
if could_be_relations:
|
|
79
|
+
if self.ignore_nodes:
|
|
80
|
+
file_type = 'relationships'
|
|
81
|
+
else:
|
|
82
|
+
file_type = 'both'
|
|
83
|
+
else:
|
|
84
|
+
file_type = 'nodes'
|
|
85
|
+
else:
|
|
86
|
+
if could_be_relations:
|
|
87
|
+
file_type = 'relationships'
|
|
88
|
+
else:
|
|
89
|
+
file_type = 'neither'
|
|
90
|
+
|
|
91
|
+
return file_type, contents, []
|
modelbuilder/src/filemanager.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import ast
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from fileanalyser import FileAnalyser
|
|
4
|
+
from processors.cypherfileprocessor import CypherFileProcessor
|
|
5
|
+
from processors.nodefileprocessor import NodeFileProcessor
|
|
6
|
+
from processors.relationshipfileprocessor import RelationshipFileProcessor
|
|
4
7
|
import assetmodelutilities as amu
|
|
5
8
|
from queries.query import *
|
|
6
9
|
from versionmanager import VersionManager
|
|
@@ -10,6 +13,7 @@ import os
|
|
|
10
13
|
class FileManager:
|
|
11
14
|
|
|
12
15
|
def __init__(self, config_manager):
|
|
16
|
+
self.fileanalyser = FileAnalyser(config_manager)
|
|
13
17
|
self.config_manager = config_manager
|
|
14
18
|
self.validation_mode = config_manager.in_validation_mode()
|
|
15
19
|
self.report_queries = config_manager.get_show_query()
|
|
@@ -20,6 +24,7 @@ class FileManager:
|
|
|
20
24
|
self.no_versions = config_manager.get_noversion()
|
|
21
25
|
self.start_from = config_manager.get_start_from()
|
|
22
26
|
self.batch_rate = config_manager.get_batch_rate()
|
|
27
|
+
self.stop_on_failure = config_manager.get_stop_on_failure()
|
|
23
28
|
|
|
24
29
|
# Create a list of error each type
|
|
25
30
|
self.both_file_list = []
|
|
@@ -36,6 +41,9 @@ class FileManager:
|
|
|
36
41
|
if not self.no_versions:
|
|
37
42
|
self.version_manager = VersionManager(config_manager)
|
|
38
43
|
|
|
44
|
+
self.fileanalyser = FileAnalyser(config_manager)
|
|
45
|
+
|
|
46
|
+
|
|
39
47
|
def analyse_a_file(self, filename):
|
|
40
48
|
a_file = filename.replace('\n', '')
|
|
41
49
|
a_file_split = a_file.split('.')
|
|
@@ -46,8 +54,6 @@ class FileManager:
|
|
|
46
54
|
match file_type:
|
|
47
55
|
case 'csv':
|
|
48
56
|
self.analyse_csv_file(a_file)
|
|
49
|
-
## json case 'json':
|
|
50
|
-
## json self.analyse_json_file(a_file)
|
|
51
57
|
case 'cypher' | 'qry':
|
|
52
58
|
self.analyse_cypher_file(a_file)
|
|
53
59
|
case 'lst':
|
|
@@ -57,7 +63,7 @@ class FileManager:
|
|
|
57
63
|
|
|
58
64
|
def add_file_to_process_list(self, file_processor, file_type):
|
|
59
65
|
priority = self.process_order[file_type]
|
|
60
|
-
self.valid_file_list[priority]
|
|
66
|
+
self.valid_file_list[priority] += [{'processor': file_processor, 'type': file_type}]
|
|
61
67
|
|
|
62
68
|
def analyse_lst_file(self, filename):
|
|
63
69
|
|
|
@@ -83,22 +89,22 @@ class FileManager:
|
|
|
83
89
|
else:
|
|
84
90
|
file_type = ''
|
|
85
91
|
if file_type == 'cypher' or file_type == 'qry':
|
|
86
|
-
this_file_processor =
|
|
92
|
+
this_file_processor = CypherFileProcessor(a_query, self.config_manager)
|
|
87
93
|
self.add_file_to_process_list(this_file_processor, 'pre-file')
|
|
88
94
|
else:
|
|
89
|
-
query_processor =
|
|
95
|
+
query_processor = CypherFileProcessor(a_query, self.config_manager, True)
|
|
90
96
|
self.add_file_to_process_list(query_processor, 'pre')
|
|
91
97
|
|
|
92
98
|
labels_to_delete_list = self.config_manager.get_labels_to_delete_list()
|
|
93
99
|
if labels_to_delete_list == []:
|
|
94
100
|
query = f'MATCH (n) DETACH DELETE n'
|
|
95
|
-
query_processor =
|
|
101
|
+
query_processor = CypherFileProcessor(query, self.config_manager, True)
|
|
96
102
|
self.add_file_to_process_list(query_processor, 'pre')
|
|
97
103
|
else:
|
|
98
104
|
for label_list in labels_to_delete_list:
|
|
99
105
|
match_list = ''
|
|
100
106
|
no_match_list = ''
|
|
101
|
-
for label in label_list
|
|
107
|
+
for label in label_list.split(':'):
|
|
102
108
|
if label.startswith('!'):
|
|
103
109
|
no_match_list += ':' + label[1:]
|
|
104
110
|
else:
|
|
@@ -107,7 +113,7 @@ class FileManager:
|
|
|
107
113
|
if no_match_list != '':
|
|
108
114
|
query += f'WHERE NOT n{no_match_list} '
|
|
109
115
|
query += 'DETACH DELETE n'
|
|
110
|
-
query_processor =
|
|
116
|
+
query_processor = CypherFileProcessor(query, self.config_manager, True)
|
|
111
117
|
self.add_file_to_process_list(query_processor, 'pre')
|
|
112
118
|
|
|
113
119
|
# Process each file in the list, and determine its content type (ignoring any clues in the filename!)
|
|
@@ -123,7 +129,7 @@ class FileManager:
|
|
|
123
129
|
query_list = self.config_manager.get_inline_query_list()
|
|
124
130
|
for a_query in query_list:
|
|
125
131
|
if a_query is not None:
|
|
126
|
-
query_processor =
|
|
132
|
+
query_processor = CypherFileProcessor(a_query, self.config_manager, True)
|
|
127
133
|
self.add_file_to_process_list(query_processor, 'q')
|
|
128
134
|
|
|
129
135
|
postprocess_list = self.config_manager.get_postprocess_query_list()
|
|
@@ -135,10 +141,10 @@ class FileManager:
|
|
|
135
141
|
else:
|
|
136
142
|
file_type = ''
|
|
137
143
|
if file_type == 'cypher' or file_type == 'qry':
|
|
138
|
-
this_file_processor =
|
|
144
|
+
this_file_processor = CypherFileProcessor(a_query, self.config_manager)
|
|
139
145
|
self.add_file_to_process_list(this_file_processor, 'post-file')
|
|
140
146
|
else:
|
|
141
|
-
query_processor =
|
|
147
|
+
query_processor = CypherFileProcessor(a_query, self.config_manager, True)
|
|
142
148
|
self.add_file_to_process_list(query_processor, 'post')
|
|
143
149
|
|
|
144
150
|
def add_file_to_list(self, this_file_processor, this_file_type, file_errors=None):
|
|
@@ -164,19 +170,18 @@ class FileManager:
|
|
|
164
170
|
def analyse_csv_file(self, csv_file):
|
|
165
171
|
# Analyse each of the specified input .csv files to determine if they contain Nodes or Relationships
|
|
166
172
|
# If the file doesn't have a valid combination of columns (or is missing) they are placed in the appropriate list
|
|
167
|
-
|
|
168
|
-
this_csv_file_type
|
|
173
|
+
this_csv_file_type, csv_contents, file_errors = self.fileanalyser.determine_file_type(csv_file, 'csv')
|
|
174
|
+
if this_csv_file_type == 'nodes':
|
|
175
|
+
this_csv_file_processor = NodeFileProcessor(csv_file, self.config_manager, csv_contents)
|
|
176
|
+
elif this_csv_file_type == 'relationships':
|
|
177
|
+
this_csv_file_processor = RelationshipFileProcessor(csv_file, self.config_manager, csv_contents)
|
|
178
|
+
else:
|
|
179
|
+
# Use NodeFileProcessor as placeholder. It won't be used, other than to process the errors
|
|
180
|
+
this_csv_file_processor = NodeFileProcessor(csv_file, self.config_manager, csv_contents)
|
|
169
181
|
self.add_file_to_list(this_csv_file_processor, this_csv_file_type, file_errors)
|
|
170
182
|
|
|
171
|
-
def analyse_json_file(self, json_file):
|
|
172
|
-
# Analyse each of the specified input .json files to determine if they contain Nodes or Relationships
|
|
173
|
-
# If the file doesn't have a valid combination of columns (or is missing) they are placed in the appropriate list
|
|
174
|
-
this_json_file_processor = FileProcessor(json_file, self.config_manager)
|
|
175
|
-
this_json_file_type, file_errors = this_json_file_processor.determine_file_type('json')
|
|
176
|
-
self.add_file_to_list(this_json_file_processor, this_json_file_type, file_errors)
|
|
177
|
-
|
|
178
183
|
def analyse_cypher_file(self, query_file):
|
|
179
|
-
this_file_processor =
|
|
184
|
+
this_file_processor = CypherFileProcessor(query_file, self.config_manager)
|
|
180
185
|
self.add_file_to_process_list(this_file_processor, 'c')
|
|
181
186
|
|
|
182
187
|
def process_files(self, db_connector):
|
|
@@ -232,18 +237,18 @@ class FileManager:
|
|
|
232
237
|
match file['type']:
|
|
233
238
|
case 'n':
|
|
234
239
|
if self.batch_rate == 0:
|
|
235
|
-
queries, file_result, version_result = self.process_file(processor,
|
|
240
|
+
queries, file_result, version_result = self.process_file(processor, NodeFileProcessor.process_node_file, db_connector)
|
|
236
241
|
else:
|
|
237
|
-
queries, file_result, version_result = self.process_file(processor,
|
|
242
|
+
queries, file_result, version_result = self.process_file(processor, NodeFileProcessor.process_node_file_batch, db_connector)
|
|
238
243
|
case 'r':
|
|
239
244
|
if self.batch_rate == 0:
|
|
240
|
-
queries, file_result, version_result = self.process_file(processor,
|
|
245
|
+
queries, file_result, version_result = self.process_file(processor, RelationshipFileProcessor.process_relationship_file, db_connector)
|
|
241
246
|
else:
|
|
242
|
-
queries, file_result, version_result = self.process_file(processor,
|
|
247
|
+
queries, file_result, version_result = self.process_file(processor, RelationshipFileProcessor.process_relationship_file_batch, db_connector)
|
|
243
248
|
case 'c' | 'pre-file' | 'post-file':
|
|
244
|
-
queries, file_result, version_result = self.process_file(processor,
|
|
249
|
+
queries, file_result, version_result = self.process_file(processor, CypherFileProcessor.process_cypher_file, db_connector)
|
|
245
250
|
case 'q' | 'pre' | 'post':
|
|
246
|
-
queries, file_result, version_result = self.process_file(processor,
|
|
251
|
+
queries, file_result, version_result = self.process_file(processor, CypherFileProcessor.process_query_list, db_connector)
|
|
247
252
|
case _:
|
|
248
253
|
# This should never happen!
|
|
249
254
|
queries = 0
|
|
@@ -253,6 +258,8 @@ class FileManager:
|
|
|
253
258
|
num_queries += queries
|
|
254
259
|
self.all_results.combine_results(file_result)
|
|
255
260
|
self.all_version_results.combine_results(version_result)
|
|
261
|
+
if self.stop_on_failure and file_result.status != 200:
|
|
262
|
+
break
|
|
256
263
|
|
|
257
264
|
return num_queries
|
|
258
265
|
|
|
@@ -263,6 +270,7 @@ class FileManager:
|
|
|
263
270
|
num_errors = 0
|
|
264
271
|
error_list = []
|
|
265
272
|
file_results = QueryResult(False)
|
|
273
|
+
file_results.set_status(200)
|
|
266
274
|
version_results = QueryResult(False)
|
|
267
275
|
|
|
268
276
|
query_count = 0
|
|
@@ -282,7 +290,7 @@ class FileManager:
|
|
|
282
290
|
else:
|
|
283
291
|
report_message(f'Processing file "{file_to_process.get_filename()}" at {amu.get_formatted_time_now()}')
|
|
284
292
|
# Build all the queries from the file, depending on the content type of the file
|
|
285
|
-
queries = file_processor(file_to_process
|
|
293
|
+
queries = file_processor(file_to_process)
|
|
286
294
|
message(f'{len(queries)} queries generated from {file_to_process.get_filename()}, processing...')
|
|
287
295
|
|
|
288
296
|
if not self.validation_mode:
|
|
@@ -348,6 +356,9 @@ class FileManager:
|
|
|
348
356
|
self.error_file.write(query.text+'\n')
|
|
349
357
|
except:
|
|
350
358
|
pass
|
|
359
|
+
if self.stop_on_failure:
|
|
360
|
+
file_results.set_status(result.get_status())
|
|
361
|
+
break
|
|
351
362
|
else:
|
|
352
363
|
info_message(f'No changes made by ', query.text)
|
|
353
364
|
|
|
File without changes
|