python-eigen-ingenuity-nocli 0.5.6__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/__init__.py +29 -0
- eigeningenuity/assetmodel.py +433 -0
- eigeningenuity/auth.py +178 -0
- eigeningenuity/core/__init__.py +236 -0
- eigeningenuity/elastic.py +171 -0
- eigeningenuity/events.py +88 -0
- eigeningenuity/historian.py +786 -0
- eigeningenuity/historianmulti.py +481 -0
- eigeningenuity/settings.py +87 -0
- eigeningenuity/smartdash.py +103 -0
- eigeningenuity/sql.py +132 -0
- eigeningenuity/util.py +548 -0
- modelbuilder/__init__.py +7 -0
- modelbuilder/src/__init__.py +0 -0
- modelbuilder/src/assetmodelbuilder.py +164 -0
- modelbuilder/src/assetmodelcsvwriter.py +31 -0
- modelbuilder/src/assetmodelutilities.py +51 -0
- modelbuilder/src/configmanager.py +500 -0
- modelbuilder/src/connectors/__init__.py +0 -0
- modelbuilder/src/connectors/boltconnector.py +108 -0
- modelbuilder/src/connectors/dbconnectors.py +40 -0
- modelbuilder/src/connectors/genericconnector.py +30 -0
- modelbuilder/src/connectors/httpsconnector.py +132 -0
- modelbuilder/src/csvcontentsmanager.py +135 -0
- modelbuilder/src/csvwriter.py +305 -0
- modelbuilder/src/fileanalyser.py +91 -0
- modelbuilder/src/filemanager.py +400 -0
- modelbuilder/src/messages.py +61 -0
- 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 +282 -0
- modelbuilder/src/processors/propertymanager.py +129 -0
- modelbuilder/src/processors/relationshipfileprocessor.py +285 -0
- modelbuilder/src/processors/relationshipmanager.py +31 -0
- modelbuilder/src/queries/__init__.py +0 -0
- modelbuilder/src/queries/cypherbuilder.py +323 -0
- modelbuilder/src/queries/query.py +82 -0
- modelbuilder/src/queries/typedefs.py +180 -0
- modelbuilder/src/versionmanager.py +356 -0
- python_eigen_ingenuity_nocli-0.5.6.dist-info/METADATA +30 -0
- python_eigen_ingenuity_nocli-0.5.6.dist-info/RECORD +44 -0
- python_eigen_ingenuity_nocli-0.5.6.dist-info/WHEEL +5 -0
- python_eigen_ingenuity_nocli-0.5.6.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
|
|
3
|
+
from fileanalyser import FileAnalyser
|
|
4
|
+
from processors.cypherfileprocessor import CypherFileProcessor
|
|
5
|
+
from processors.nodefileprocessor import NodeFileProcessor
|
|
6
|
+
from processors.relationshipfileprocessor import RelationshipFileProcessor
|
|
7
|
+
import assetmodelutilities as amu
|
|
8
|
+
from queries.query import *
|
|
9
|
+
from versionmanager import VersionManager
|
|
10
|
+
from messages import *
|
|
11
|
+
import os
|
|
12
|
+
|
|
13
|
+
class FileManager:
|
|
14
|
+
|
|
15
|
+
def __init__(self, config_manager):
|
|
16
|
+
self.fileanalyser = FileAnalyser(config_manager)
|
|
17
|
+
self.config_manager = config_manager
|
|
18
|
+
self.validation_mode = config_manager.in_validation_mode()
|
|
19
|
+
self.report_queries = config_manager.get_show_query()
|
|
20
|
+
self.show_progress = config_manager.get_display_rate()
|
|
21
|
+
self.show_return = config_manager.get_show_return()
|
|
22
|
+
self.process_order = config_manager.get_process_order()
|
|
23
|
+
self.display_rate = config_manager.get_display_rate()
|
|
24
|
+
self.no_versions = config_manager.get_noversion()
|
|
25
|
+
self.start_from = config_manager.get_start_from()
|
|
26
|
+
self.batch_rate = config_manager.get_batch_rate()
|
|
27
|
+
self.stop_on_failure = config_manager.get_stop_on_failure()
|
|
28
|
+
self.process_file_types = config_manager.get_process_file_types()
|
|
29
|
+
|
|
30
|
+
# Create a list of error each type
|
|
31
|
+
self.both_file_list = []
|
|
32
|
+
self.neither_file_list = []
|
|
33
|
+
self.file_not_found_list = []
|
|
34
|
+
self.unrecognised_file_type_list = []
|
|
35
|
+
self.incorrect_file_list = []
|
|
36
|
+
self.incomplete_file_list = []
|
|
37
|
+
self.incomplete_file_errors = []
|
|
38
|
+
|
|
39
|
+
# And a list of lists for the queries in specified priority order
|
|
40
|
+
self.valid_file_list = [[], [], [], [], [], []]
|
|
41
|
+
|
|
42
|
+
if not self.no_versions:
|
|
43
|
+
self.version_manager = VersionManager(config_manager)
|
|
44
|
+
|
|
45
|
+
self.fileanalyser = FileAnalyser(config_manager)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def analyse_a_file(self, filename):
|
|
49
|
+
a_file = filename.replace('\n', '')
|
|
50
|
+
a_file_split = a_file.split('.')
|
|
51
|
+
if len(a_file_split) > 1:
|
|
52
|
+
file_type = a_file_split[-1]
|
|
53
|
+
else:
|
|
54
|
+
file_type = ''
|
|
55
|
+
match file_type:
|
|
56
|
+
case 'csv':
|
|
57
|
+
self.analyse_csv_file(a_file)
|
|
58
|
+
case 'cypher' | 'qry':
|
|
59
|
+
self.analyse_cypher_file(a_file)
|
|
60
|
+
case 'lst':
|
|
61
|
+
self.analyse_lst_file(a_file)
|
|
62
|
+
case _:
|
|
63
|
+
self.unrecognised_file_type_list.append(a_file)
|
|
64
|
+
|
|
65
|
+
def add_file_to_process_list(self, file_processor, file_type):
|
|
66
|
+
if file_type in ['pre', 'pre-file', 'post', 'post-file']:
|
|
67
|
+
file_type_check = 'q'
|
|
68
|
+
else:
|
|
69
|
+
file_type_check = file_type
|
|
70
|
+
|
|
71
|
+
if file_type_check in self.process_file_types:
|
|
72
|
+
priority = self.process_order[file_type]
|
|
73
|
+
self.valid_file_list[priority] += [{'processor': file_processor, 'type': file_type}]
|
|
74
|
+
else:
|
|
75
|
+
info_message(file_processor.get_filename(), ' not processed as requested')
|
|
76
|
+
|
|
77
|
+
def analyse_lst_file(self, filename):
|
|
78
|
+
|
|
79
|
+
f_name = amu.find_file(self.config_manager.get_default_path(), filename)
|
|
80
|
+
if f_name is not None:
|
|
81
|
+
lst_File = open(f_name, 'r')
|
|
82
|
+
for a_File in lst_File:
|
|
83
|
+
if not a_File.startswith('#') and len(a_File) > 0:
|
|
84
|
+
self.analyse_a_file(a_File)
|
|
85
|
+
lst_File.close()
|
|
86
|
+
else:
|
|
87
|
+
warning_message(f'{filename} not found')
|
|
88
|
+
|
|
89
|
+
def analyse_files(self):
|
|
90
|
+
|
|
91
|
+
message('Checking file types...')
|
|
92
|
+
preprocess_list = self.config_manager.get_preprocess_query_list()
|
|
93
|
+
for a_query in preprocess_list:
|
|
94
|
+
if a_query is not None:
|
|
95
|
+
a_query_split = a_query.split('.')
|
|
96
|
+
if len(a_query_split) > 1:
|
|
97
|
+
file_type = a_query_split[-1]
|
|
98
|
+
else:
|
|
99
|
+
file_type = ''
|
|
100
|
+
if file_type == 'cypher' or file_type == 'qry':
|
|
101
|
+
this_file_processor = CypherFileProcessor(a_query, self.config_manager)
|
|
102
|
+
self.add_file_to_process_list(this_file_processor, 'pre-file')
|
|
103
|
+
else:
|
|
104
|
+
query_processor = CypherFileProcessor(a_query, self.config_manager, True)
|
|
105
|
+
self.add_file_to_process_list(query_processor, 'pre')
|
|
106
|
+
|
|
107
|
+
labels_to_delete_list = self.config_manager.get_labels_to_delete_list()
|
|
108
|
+
if labels_to_delete_list == []:
|
|
109
|
+
query = f'MATCH (n) DETACH DELETE n'
|
|
110
|
+
query_processor = CypherFileProcessor(query, self.config_manager, True)
|
|
111
|
+
self.add_file_to_process_list(query_processor, 'pre')
|
|
112
|
+
else:
|
|
113
|
+
for label_list in labels_to_delete_list:
|
|
114
|
+
match_list = ''
|
|
115
|
+
no_match_list = ''
|
|
116
|
+
for label in label_list.split(':'):
|
|
117
|
+
if label.startswith('!'):
|
|
118
|
+
no_match_list += ':' + label[1:]
|
|
119
|
+
else:
|
|
120
|
+
match_list += ':' + label
|
|
121
|
+
query = f'MATCH (n{match_list}) '
|
|
122
|
+
if no_match_list != '':
|
|
123
|
+
query += f'WHERE NOT n{no_match_list} '
|
|
124
|
+
query += 'DETACH DELETE n'
|
|
125
|
+
query_processor = CypherFileProcessor(query, self.config_manager, True)
|
|
126
|
+
self.add_file_to_process_list(query_processor, 'pre')
|
|
127
|
+
|
|
128
|
+
# Process each file in the list, and determine its content type (ignoring any clues in the filename!)
|
|
129
|
+
# Analyse each of the specified input .csv files to determine if they contain Nodes or Relationships
|
|
130
|
+
# If the file doesn't have a valid combination of columns (or is missing) they are placed in the appropriate list
|
|
131
|
+
file_list = self.config_manager.get_input_file_list()
|
|
132
|
+
for a_file in file_list:
|
|
133
|
+
a_file_split = a_file.split(',')
|
|
134
|
+
for a_split_file in a_file_split:
|
|
135
|
+
if a_split_file != '':
|
|
136
|
+
self.analyse_a_file(a_split_file.strip())
|
|
137
|
+
|
|
138
|
+
query_list = self.config_manager.get_inline_query_list()
|
|
139
|
+
for a_query in query_list:
|
|
140
|
+
if a_query is not None:
|
|
141
|
+
query_processor = CypherFileProcessor(a_query, self.config_manager, True)
|
|
142
|
+
self.add_file_to_process_list(query_processor, 'q')
|
|
143
|
+
|
|
144
|
+
postprocess_list = self.config_manager.get_postprocess_query_list()
|
|
145
|
+
for a_query in postprocess_list:
|
|
146
|
+
if a_query is not None:
|
|
147
|
+
a_query_split = a_query.split('.')
|
|
148
|
+
if len(a_query_split) > 1:
|
|
149
|
+
file_type = a_query_split[-1]
|
|
150
|
+
else:
|
|
151
|
+
file_type = ''
|
|
152
|
+
if file_type == 'cypher' or file_type == 'qry':
|
|
153
|
+
this_file_processor = CypherFileProcessor(a_query, self.config_manager)
|
|
154
|
+
self.add_file_to_process_list(this_file_processor, 'post-file')
|
|
155
|
+
else:
|
|
156
|
+
query_processor = CypherFileProcessor(a_query, self.config_manager, True)
|
|
157
|
+
self.add_file_to_process_list(query_processor, 'post')
|
|
158
|
+
|
|
159
|
+
def add_file_to_list(self, this_file_processor, this_file_type, file_errors=None):
|
|
160
|
+
|
|
161
|
+
match this_file_type:
|
|
162
|
+
case 'nodes':
|
|
163
|
+
self.add_file_to_process_list(this_file_processor, 'n')
|
|
164
|
+
case 'relationships':
|
|
165
|
+
self.add_file_to_process_list(this_file_processor, 'r')
|
|
166
|
+
case 'both':
|
|
167
|
+
self.both_file_list.append(this_file_processor.get_filename())
|
|
168
|
+
case 'neither':
|
|
169
|
+
self.neither_file_list.append(this_file_processor.get_filename())
|
|
170
|
+
case 'fnf':
|
|
171
|
+
self.file_not_found_list.append(this_file_processor.get_filename())
|
|
172
|
+
case 'incorrect file format':
|
|
173
|
+
self.incorrect_file_list.append(this_file_processor.get_filename())
|
|
174
|
+
case 'incomplete row(s)':
|
|
175
|
+
self.incomplete_file_list.append([this_file_processor.get_filename(), file_errors])
|
|
176
|
+
case _:
|
|
177
|
+
pass
|
|
178
|
+
|
|
179
|
+
def analyse_csv_file(self, csv_file):
|
|
180
|
+
# Analyse each of the specified input .csv files to determine if they contain Nodes or Relationships
|
|
181
|
+
# If the file doesn't have a valid combination of columns (or is missing) they are placed in the appropriate list
|
|
182
|
+
this_csv_file_type, csv_contents, file_errors = self.fileanalyser.determine_file_type(csv_file, 'csv')
|
|
183
|
+
if this_csv_file_type == 'nodes':
|
|
184
|
+
this_csv_file_processor = NodeFileProcessor(csv_file, self.config_manager, csv_contents)
|
|
185
|
+
elif this_csv_file_type == 'relationships':
|
|
186
|
+
this_csv_file_processor = RelationshipFileProcessor(csv_file, self.config_manager, csv_contents)
|
|
187
|
+
else:
|
|
188
|
+
# Use NodeFileProcessor as placeholder. It won't be used, other than to process the errors
|
|
189
|
+
this_csv_file_processor = NodeFileProcessor(csv_file, self.config_manager, csv_contents)
|
|
190
|
+
self.add_file_to_list(this_csv_file_processor, this_csv_file_type, file_errors)
|
|
191
|
+
|
|
192
|
+
def analyse_cypher_file(self, query_file):
|
|
193
|
+
this_file_processor = CypherFileProcessor(query_file, self.config_manager)
|
|
194
|
+
self.add_file_to_process_list(this_file_processor, 'c')
|
|
195
|
+
|
|
196
|
+
def process_files(self, db_connector):
|
|
197
|
+
|
|
198
|
+
# Report on the missing or faulty files
|
|
199
|
+
self.process_invalid_files()
|
|
200
|
+
|
|
201
|
+
# Finally, process the files that we can
|
|
202
|
+
self.all_results = QueryResult(False)
|
|
203
|
+
self.all_version_results = QueryResult(False)
|
|
204
|
+
queries_processed = self.process_valid_files(db_connector)
|
|
205
|
+
info_message(f'{queries_processed} queries processed in total')
|
|
206
|
+
self.all_results.print_results('Total updates made: ')
|
|
207
|
+
self.all_version_results.print_results('Total version updates made: ', '')
|
|
208
|
+
|
|
209
|
+
def process_invalid_files(self):
|
|
210
|
+
self.process_not_found_files()
|
|
211
|
+
self.process_unrecognised_file_type_files()
|
|
212
|
+
self.process_incorrect_files()
|
|
213
|
+
self.process_incomplete_files()
|
|
214
|
+
self.process_neither_found_files()
|
|
215
|
+
self.process_both_files()
|
|
216
|
+
|
|
217
|
+
def process_not_found_files(self):
|
|
218
|
+
for file in self.file_not_found_list:
|
|
219
|
+
warning_message(f'{file} not found')
|
|
220
|
+
|
|
221
|
+
def process_unrecognised_file_type_files(self):
|
|
222
|
+
for file in self.unrecognised_file_type_list:
|
|
223
|
+
error_message(f'{file} has unrecognised file type - file not processed')
|
|
224
|
+
|
|
225
|
+
def process_incorrect_files(self):
|
|
226
|
+
for file in self.incorrect_file_list:
|
|
227
|
+
error_message(f'{file} is not a correctly formatted .csv file - file not processed')
|
|
228
|
+
|
|
229
|
+
def process_incomplete_files(self):
|
|
230
|
+
for file in self.incomplete_file_list:
|
|
231
|
+
error_message(f'{file[0]} has incomplete data in rows {file[1]} - file not processed')
|
|
232
|
+
|
|
233
|
+
def process_neither_found_files(self):
|
|
234
|
+
for file in self.neither_file_list:
|
|
235
|
+
error_message(f'Insufficient data in {file} to determine content type - file not processed')
|
|
236
|
+
|
|
237
|
+
def process_both_files(self):
|
|
238
|
+
for file in self.both_file_list:
|
|
239
|
+
error_message(f'Ambiguous data in {file} - could be multiple content types - file not processed')
|
|
240
|
+
|
|
241
|
+
def process_valid_files(self, db_connector):
|
|
242
|
+
num_queries = 0
|
|
243
|
+
for group in self.valid_file_list:
|
|
244
|
+
for file in group:
|
|
245
|
+
processor = file['processor']
|
|
246
|
+
match file['type']:
|
|
247
|
+
case 'n':
|
|
248
|
+
if self.batch_rate == 0:
|
|
249
|
+
queries, file_result, version_result = self.process_file(processor, NodeFileProcessor.process_node_file, db_connector)
|
|
250
|
+
else:
|
|
251
|
+
queries, file_result, version_result = self.process_file(processor, NodeFileProcessor.process_node_file_batch, db_connector)
|
|
252
|
+
case 'r':
|
|
253
|
+
if self.batch_rate == 0:
|
|
254
|
+
queries, file_result, version_result = self.process_file(processor, RelationshipFileProcessor.process_relationship_file, db_connector)
|
|
255
|
+
else:
|
|
256
|
+
queries, file_result, version_result = self.process_file(processor, RelationshipFileProcessor.process_relationship_file_batch, db_connector)
|
|
257
|
+
case 'c' | 'pre-file' | 'post-file':
|
|
258
|
+
queries, file_result, version_result = self.process_file(processor, CypherFileProcessor.process_cypher_file, db_connector)
|
|
259
|
+
case 'q' | 'pre' | 'post':
|
|
260
|
+
queries, file_result, version_result = self.process_file(processor, CypherFileProcessor.process_query_list, db_connector)
|
|
261
|
+
case _:
|
|
262
|
+
# This should never happen!
|
|
263
|
+
queries = 0
|
|
264
|
+
file_result = QueryResult(False)
|
|
265
|
+
version_result = QueryResult(False)
|
|
266
|
+
|
|
267
|
+
num_queries += queries
|
|
268
|
+
self.all_results.combine_results(file_result)
|
|
269
|
+
self.all_version_results.combine_results(version_result)
|
|
270
|
+
if self.stop_on_failure and file_result.status != 200:
|
|
271
|
+
break
|
|
272
|
+
|
|
273
|
+
return num_queries
|
|
274
|
+
|
|
275
|
+
def process_file(self, file_to_process, file_processor, db_connector):
|
|
276
|
+
if not self.no_versions:
|
|
277
|
+
self.version_manager.set_db_connector(db_connector)
|
|
278
|
+
node_queries = 0
|
|
279
|
+
num_errors = 0
|
|
280
|
+
error_list = []
|
|
281
|
+
file_results = QueryResult(False)
|
|
282
|
+
file_results.set_status(200)
|
|
283
|
+
version_results = QueryResult(False)
|
|
284
|
+
|
|
285
|
+
query_count = 0
|
|
286
|
+
file_queries = 0
|
|
287
|
+
error_filename = self.config_manager.get_default_path() + 'Failed_queries_' + amu.get_time_for_filename() + '.cypher'
|
|
288
|
+
if error_filename.endswith('.cypher'):
|
|
289
|
+
self.error_file = None
|
|
290
|
+
try:
|
|
291
|
+
self.error_file = open(error_filename, 'w')
|
|
292
|
+
except:
|
|
293
|
+
self.error_file = None
|
|
294
|
+
else:
|
|
295
|
+
self.error_file = None
|
|
296
|
+
|
|
297
|
+
if file_to_process.is_inline_query():
|
|
298
|
+
report_message(f'Processing query "{file_to_process.get_filename()}" at {amu.get_formatted_time_now()}')
|
|
299
|
+
else:
|
|
300
|
+
report_message(f'Processing file "{file_to_process.get_filename()}" at {amu.get_formatted_time_now()}')
|
|
301
|
+
# Build all the queries from the file, depending on the content type of the file
|
|
302
|
+
queries = file_processor(file_to_process)
|
|
303
|
+
message(f'{len(queries)} queries generated from {file_to_process.get_filename()}, processing...')
|
|
304
|
+
|
|
305
|
+
if not self.validation_mode:
|
|
306
|
+
|
|
307
|
+
num_queries = len(queries)
|
|
308
|
+
if self.display_rate == 'auto':
|
|
309
|
+
if num_queries > 10000:
|
|
310
|
+
display_rate = 1000
|
|
311
|
+
elif num_queries > 1000:
|
|
312
|
+
display_rate = 100
|
|
313
|
+
elif num_queries > 100:
|
|
314
|
+
display_rate = 10
|
|
315
|
+
else:
|
|
316
|
+
display_rate = 1
|
|
317
|
+
else:
|
|
318
|
+
try:
|
|
319
|
+
display_rate = int(ast.literal_eval(self.display_rate))
|
|
320
|
+
except:
|
|
321
|
+
display_rate = 0
|
|
322
|
+
|
|
323
|
+
if display_rate > 0:
|
|
324
|
+
message(f'0 of {num_queries} queries processed at {amu.get_formatted_time_now()}', False)
|
|
325
|
+
|
|
326
|
+
# And action each one in turn
|
|
327
|
+
for query in queries:
|
|
328
|
+
query_count += 1
|
|
329
|
+
|
|
330
|
+
if self.report_queries:
|
|
331
|
+
message(f'{query_count} of {num_queries}: {query.text}')
|
|
332
|
+
update_current_node = True
|
|
333
|
+
version_result = None
|
|
334
|
+
if query.type == 'node':
|
|
335
|
+
if not self.no_versions:
|
|
336
|
+
update_current_node, version_result = self.version_manager.make_new_version_of_node(query, db_connector)
|
|
337
|
+
if update_current_node:
|
|
338
|
+
version_results.combine_results(version_result)
|
|
339
|
+
result = db_connector.execute(query, query_count)
|
|
340
|
+
if result.get_status() == 200:
|
|
341
|
+
|
|
342
|
+
# Display info on how many of each batch were created
|
|
343
|
+
# new_nodes = result.get_updates()['nodes created']
|
|
344
|
+
# new_relationships = result.get_updates()['relationships created']
|
|
345
|
+
# if (query.type == 'batch node' and new_nodes != query.batch_size) or (query.type == 'batch relationship' and new_relationships != query.batch_size):
|
|
346
|
+
# info_message(f'Batch {query_count} contained {query.batch_size} updates: only {new_nodes+new_relationships} created')
|
|
347
|
+
|
|
348
|
+
if self.report_queries:
|
|
349
|
+
result.print_results('Updates made: ')
|
|
350
|
+
if version_result:
|
|
351
|
+
version_result.print_results('Version updates made: ', '')
|
|
352
|
+
if self.show_return:
|
|
353
|
+
db_connector.print_response(result.get_response())
|
|
354
|
+
file_results.combine_results(result)
|
|
355
|
+
file_queries += 1
|
|
356
|
+
else:
|
|
357
|
+
if result.get_status() == 414 | 505:
|
|
358
|
+
error_message(f'Query {query_count} returned ', f'{result.get_status()}')
|
|
359
|
+
else:
|
|
360
|
+
error_message(f'{query.text} returned ', f'{result.get_status()}')
|
|
361
|
+
num_errors += 1
|
|
362
|
+
error_list.append(query_count)
|
|
363
|
+
if self.error_file:
|
|
364
|
+
try:
|
|
365
|
+
self.error_file.write(query.text+'\n')
|
|
366
|
+
except:
|
|
367
|
+
pass
|
|
368
|
+
if self.stop_on_failure:
|
|
369
|
+
file_results.set_status(result.get_status())
|
|
370
|
+
break
|
|
371
|
+
else:
|
|
372
|
+
info_message(f'No changes made by ', query.text)
|
|
373
|
+
|
|
374
|
+
if query.type == 'batch node':
|
|
375
|
+
batch_info = f'{query.batch_size} nodes, {query.property_count} properties, '
|
|
376
|
+
else:
|
|
377
|
+
batch_info = ''
|
|
378
|
+
|
|
379
|
+
if display_rate > 0 and not self.report_queries:
|
|
380
|
+
if query_count % display_rate == 0 or query_count == num_queries:
|
|
381
|
+
message(f'{query_count} of {num_queries} queries processed at {amu.get_formatted_time_now()} ({query_count / num_queries * 100:.2f}%)', False)
|
|
382
|
+
else:
|
|
383
|
+
message_no_cr(f'{query_count} of {num_queries} queries processed ({batch_info}{query_count / num_queries * 100:.2f}%) {chr(13)}')
|
|
384
|
+
|
|
385
|
+
report_message(f'{file_queries} queries processed from {file_to_process.get_filename()} at {amu.get_formatted_time_now()}')
|
|
386
|
+
file_results.print_results('Updates made: ')
|
|
387
|
+
version_results.print_results('Version updates made: ', '')
|
|
388
|
+
if self.error_file:
|
|
389
|
+
self.error_file.close()
|
|
390
|
+
if num_errors > 0:
|
|
391
|
+
report_message(f'{num_errors} queries returned errors: {error_list}')
|
|
392
|
+
report_message(f'Failed queries recorded in {error_filename}')
|
|
393
|
+
else:
|
|
394
|
+
try:
|
|
395
|
+
os.remove(error_filename)
|
|
396
|
+
except:
|
|
397
|
+
pass
|
|
398
|
+
node_queries += file_queries
|
|
399
|
+
|
|
400
|
+
return node_queries, file_results, version_results
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
none = '\033[0m'
|
|
2
|
+
bold = '\033[1m'
|
|
3
|
+
pale = '\033[2m'
|
|
4
|
+
italic = '\033[3m'
|
|
5
|
+
underline = '\033[4m'
|
|
6
|
+
grey = '\033[90m'
|
|
7
|
+
red = '\033[91m'
|
|
8
|
+
green = '\033[92m'
|
|
9
|
+
yellow = '\033[93m'
|
|
10
|
+
blue = '\033[94m'
|
|
11
|
+
purple = '\033[95m'
|
|
12
|
+
cyan = '\033[96m'
|
|
13
|
+
|
|
14
|
+
info = green
|
|
15
|
+
warning = yellow
|
|
16
|
+
error = red
|
|
17
|
+
status = blue
|
|
18
|
+
report = cyan
|
|
19
|
+
details = cyan
|
|
20
|
+
|
|
21
|
+
print_cr = False
|
|
22
|
+
|
|
23
|
+
def set_cr_needed(needed):
|
|
24
|
+
global print_cr
|
|
25
|
+
print_cr = needed
|
|
26
|
+
|
|
27
|
+
def check_cr_needed():
|
|
28
|
+
global print_cr
|
|
29
|
+
if print_cr:
|
|
30
|
+
print()
|
|
31
|
+
print_cr = False
|
|
32
|
+
|
|
33
|
+
def message(message1, check_cr=True):
|
|
34
|
+
if check_cr:
|
|
35
|
+
check_cr_needed()
|
|
36
|
+
print(f'{message1}')
|
|
37
|
+
set_cr_needed(False)
|
|
38
|
+
|
|
39
|
+
def message_no_cr(message1):
|
|
40
|
+
print(f'{message1}', end='')
|
|
41
|
+
set_cr_needed(True)
|
|
42
|
+
|
|
43
|
+
def report_message(message1, message2 = ''):
|
|
44
|
+
check_cr_needed()
|
|
45
|
+
print(f'{report}{message1}{none}{message2}')
|
|
46
|
+
set_cr_needed(False)
|
|
47
|
+
|
|
48
|
+
def info_message(message1, message2 = ''):
|
|
49
|
+
check_cr_needed()
|
|
50
|
+
print(f'{info}{message1}{none}{message2}')
|
|
51
|
+
set_cr_needed(False)
|
|
52
|
+
|
|
53
|
+
def warning_message(message1, message2 = ''):
|
|
54
|
+
check_cr_needed()
|
|
55
|
+
print(f'{yellow}Warning! {none}{message1}{details}{message2}{none}')
|
|
56
|
+
set_cr_needed(False)
|
|
57
|
+
|
|
58
|
+
def error_message(message1, message2 = ''):
|
|
59
|
+
check_cr_needed()
|
|
60
|
+
print(f'{error}Error! {none}{message1}{status}{message2}{none}')
|
|
61
|
+
set_cr_needed(False)
|
|
File without changes
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import assetmodelutilities as amu
|
|
2
|
+
from queries.query import Neo4jQuery
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class CypherFileProcessor:
|
|
6
|
+
|
|
7
|
+
def __init__(self, filename, config, inline_query=False):
|
|
8
|
+
|
|
9
|
+
self.filename = filename
|
|
10
|
+
self.inline_query = inline_query
|
|
11
|
+
|
|
12
|
+
self.def_path = config.get_default_path()
|
|
13
|
+
self.validate_only = config.in_validation_mode()
|
|
14
|
+
self.summarise = config.in_summary_mode()
|
|
15
|
+
|
|
16
|
+
def process_cypher_file(self):
|
|
17
|
+
|
|
18
|
+
queries = []
|
|
19
|
+
try:
|
|
20
|
+
cypher_file_name = amu.find_file(self.def_path, self.filename)
|
|
21
|
+
cypher_file = open(cypher_file_name, 'r', encoding='UTF8')
|
|
22
|
+
cypher_list = list(cypher_file)
|
|
23
|
+
file_queries = 0
|
|
24
|
+
next_query = ''
|
|
25
|
+
cypher_file_size = len(cypher_list)
|
|
26
|
+
entry_count = 0
|
|
27
|
+
while entry_count < cypher_file_size:
|
|
28
|
+
this_entry = cypher_list[entry_count].strip()
|
|
29
|
+
if not this_entry.startswith('#') and not this_entry == '\n':
|
|
30
|
+
next_query += this_entry + '\n'
|
|
31
|
+
if not this_entry.endswith(',') and not this_entry.endswith('\\'):
|
|
32
|
+
while next_query.endswith('\n'):
|
|
33
|
+
next_query = next_query[0:-1]
|
|
34
|
+
query = Neo4jQuery('cypher', next_query.replace('\\', '').strip())
|
|
35
|
+
next_query = ''
|
|
36
|
+
if self.validate_only and not self.summarise:
|
|
37
|
+
print(query.text)
|
|
38
|
+
queries.append(query)
|
|
39
|
+
file_queries += 1
|
|
40
|
+
entry_count += 1
|
|
41
|
+
|
|
42
|
+
# Process any leftover query at the end of the file
|
|
43
|
+
# This shouldn't normally happen, but will if the last entry ends with a comma or backslash
|
|
44
|
+
# Any trailing backslash is removed, so the query may be OK but will be missing any continuation clause(s)
|
|
45
|
+
# But a trailing comma will be left - result is a query that will generate an error if executed!
|
|
46
|
+
# This is done intentionally as the query in the input file is incomplete
|
|
47
|
+
if next_query != '':
|
|
48
|
+
query = Neo4jQuery('cypher', next_query[0:-1].replace('\\', '').strip())
|
|
49
|
+
if self.validate_only and not self.summarise:
|
|
50
|
+
print(query.text)
|
|
51
|
+
queries.append(query)
|
|
52
|
+
file_queries += 1
|
|
53
|
+
|
|
54
|
+
cypher_file.close()
|
|
55
|
+
except:
|
|
56
|
+
print(f'{self.filename} not found')
|
|
57
|
+
|
|
58
|
+
return queries
|
|
59
|
+
|
|
60
|
+
def process_query_list(self):
|
|
61
|
+
|
|
62
|
+
queries = []
|
|
63
|
+
query = Neo4jQuery('query', self.filename)
|
|
64
|
+
if self.validate_only and not self.summarise:
|
|
65
|
+
print(query.text)
|
|
66
|
+
queries.append(query)
|
|
67
|
+
return queries
|
|
68
|
+
|
|
69
|
+
def get_filename(self):
|
|
70
|
+
return self.filename
|
|
71
|
+
|
|
72
|
+
def is_inline_query(self):
|
|
73
|
+
return self.inline_query
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import assetmodelutilities as amu
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def validate_label_string(label_string):
|
|
5
|
+
# The input string can both comma and colon separated.
|
|
6
|
+
# We will return a string of colon separated labels, but without a leading colon
|
|
7
|
+
# Labels that do not conform to Neo4j naming rules will be back-ticked (`)
|
|
8
|
+
# For example:
|
|
9
|
+
# fruit --> fruit
|
|
10
|
+
# apple,tree --> apple:tree
|
|
11
|
+
# birds&bees --> `birds&bees`
|
|
12
|
+
# one,two:!three --> one:two:!three
|
|
13
|
+
|
|
14
|
+
result = ''
|
|
15
|
+
for a_label in label_string.replace(',', ':').split(':'):
|
|
16
|
+
# Labels may be prefixed with '!' or '!!'. Check for these before validating
|
|
17
|
+
if a_label.startswith('!!'):
|
|
18
|
+
result += ':!!' + amu.validate(a_label[2:])
|
|
19
|
+
elif a_label.startswith('!'):
|
|
20
|
+
result += ':!' + amu.validate(a_label[1:])
|
|
21
|
+
elif a_label:
|
|
22
|
+
result += ':' + amu.validate(a_label)
|
|
23
|
+
|
|
24
|
+
# Remove the leading ':' from the result because we don't want it
|
|
25
|
+
if result.startswith(':'):
|
|
26
|
+
result = result[1:]
|
|
27
|
+
|
|
28
|
+
return result
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def validate_labels(labels):
|
|
32
|
+
# We either get a string, or a list of strings.
|
|
33
|
+
# Process as appropriate and return a string or list of strings to match input
|
|
34
|
+
if isinstance(labels, str):
|
|
35
|
+
return validate_label_string(labels)
|
|
36
|
+
else:
|
|
37
|
+
return [validate_label_string(i) for i in labels]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def validate_label_string_to_list(labels):
|
|
41
|
+
# Validate a single input string as normal
|
|
42
|
+
# But return the result as a list of the individual labels (without colons) instead of the combined string
|
|
43
|
+
validated_string = validate_label_string(labels)
|
|
44
|
+
validated_list = [i for i in validated_string.split(':') if i != '']
|
|
45
|
+
return list(set(validated_list))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def sort_labels(required_labels, labels):
|
|
49
|
+
# So this is where the real magic happens
|
|
50
|
+
# Combine the various sets of input labels into 4 output lists
|
|
51
|
+
# merge_labels - labels used in the MERGE clause
|
|
52
|
+
# new_labels - labels added to SET clause
|
|
53
|
+
# remove labels - labels that are removed from a node via a REMOVE clause
|
|
54
|
+
# relationship labels - used in the MATCH clause to find the start/end nodes of a relationship (not used for node updates)
|
|
55
|
+
|
|
56
|
+
# First off, make a list of all the unique labels given to us
|
|
57
|
+
# Note the string(s) in the input may contain more than one label, hence the split by colon
|
|
58
|
+
unique_labels = []
|
|
59
|
+
for label in labels:
|
|
60
|
+
for split_label in label.replace(',', ':').split(':'):
|
|
61
|
+
if split_label and split_label not in unique_labels:
|
|
62
|
+
unique_labels.append(split_label)
|
|
63
|
+
|
|
64
|
+
# Now the first sort
|
|
65
|
+
wanted_label_list = [] # Labels that are required
|
|
66
|
+
unwanted_label_list = [] # Labels that are not wanted (i.e. prefixed with ! in the required label list)
|
|
67
|
+
remove_label_list = [] # Labels that are to be removed (i.e. prefixed with ! in the .csv file)
|
|
68
|
+
for label in unique_labels:
|
|
69
|
+
if label.startswith('!!'):
|
|
70
|
+
wanted_label_list.append(label[2:])
|
|
71
|
+
elif not ('!'+label in required_labels or label.startswith('!')):
|
|
72
|
+
wanted_label_list.append(label)
|
|
73
|
+
elif label.startswith('!'):
|
|
74
|
+
remove_label_list.append(label[1:])
|
|
75
|
+
else:
|
|
76
|
+
unwanted_label_list.append('!' + label)
|
|
77
|
+
|
|
78
|
+
return wanted_label_list, unwanted_label_list, remove_label_list
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def sort_node_labels(required_labels, input_labels):
|
|
82
|
+
wanted_label_list, unwanted_label_list, remove_label_list = sort_labels(required_labels, input_labels)
|
|
83
|
+
# MERGE clause: required labels without !
|
|
84
|
+
merge_labels = [i for i in required_labels if (not ('!' + i in unwanted_label_list) and not (i.startswith('!')))]
|
|
85
|
+
# Labels to add via the SET clause: specified in the csv file
|
|
86
|
+
new_labels = [i for i in wanted_label_list if i not in merge_labels]
|
|
87
|
+
|
|
88
|
+
return merge_labels, new_labels, remove_label_list
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def sort_relationship_labels(required_labels, input_labels):
|
|
92
|
+
wanted_label_list, unwanted_label_list, remove_label_list = sort_labels(required_labels, input_labels)
|
|
93
|
+
# Labels used in the relationship MATCH queries
|
|
94
|
+
relationship_labels = [i for i in required_labels + wanted_label_list if (not ('!' + i in unwanted_label_list) and not (i in remove_label_list) and not (i.startswith('!')))]
|
|
95
|
+
|
|
96
|
+
return relationship_labels
|
|
97
|
+
|
|
98
|
+
def sort_output_labels(node_labels, version_labels):
|
|
99
|
+
# We need the specifed node labels, but not any version labels
|
|
100
|
+
# This means that any version nodes will not be processed
|
|
101
|
+
input_labels = node_labels + ['!' + label for label in version_labels if not label.startswith('!')]
|
|
102
|
+
# Use the same sort algorithm, but we don't use any required labels
|
|
103
|
+
wanted_label_list, unwanted_label_list, remove_label_list = sort_labels('', input_labels)
|
|
104
|
+
|
|
105
|
+
return wanted_label_list, remove_label_list
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def strip_quotes(quote_list):
|
|
109
|
+
stripped_list = [i[1:-1].replace('``', '`') if i.startswith('`') and i.endswith('`') else i for i in quote_list]
|
|
110
|
+
return stripped_list
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def merge_labels(self, required_labels, unrequired_labels, remove_labels):
|
|
114
|
+
merged_labels = [i for i in self.required_labels + required_labels if (not ('!' + i in unrequired_labels) and not (i in remove_labels) and not (i.startswith('!')))]
|
|
115
|
+
return merged_labels
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def combine_required_labels(config_labels, override_labels):
|
|
119
|
+
valid_config_labels = validate_labels(config_labels)
|
|
120
|
+
valid_override_labels = validate_label_string_to_list(override_labels)
|
|
121
|
+
required_labels = [i for i in valid_config_labels if (not(i.startswith('!') and ('!'+i not in valid_config_labels)) and ('!'+i not in valid_override_labels))] + [i for i in valid_override_labels if (not(i.startswith('!')) and ('!'+i not in valid_override_labels))]
|
|
122
|
+
unwanted_labels = [i for i in valid_config_labels if (i.startswith('!') and (i[1:] not in valid_override_labels) and ('!' + i not in valid_override_labels))] + [i for i in valid_override_labels if i.startswith('!') and not i.startswith('!!')]
|
|
123
|
+
|
|
124
|
+
combined_list = list(set(required_labels + unwanted_labels))
|
|
125
|
+
if '' in combined_list:
|
|
126
|
+
combined_list.remove('')
|
|
127
|
+
|
|
128
|
+
return combined_list
|
|
129
|
+
|
|
130
|
+
def validate_output_labels(label_list):
|
|
131
|
+
output_list = ['`'+label+'`' if label.startswith('!') else label for label in label_list]
|
|
132
|
+
return ':'.join(output_list)
|
|
133
|
+
|