-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathosisoft_plugin_common.py
More file actions
760 lines (645 loc) · 26.2 KB
/
osisoft_plugin_common.py
File metadata and controls
760 lines (645 loc) · 26.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
import os
import copy
import time
from osisoft_constants import OSIsoftConstants
from safe_logger import SafeLogger
from datetime import datetime, timezone
import dateutil.parser as date_parser
import re
regex_iso8601 = r'^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$'
match_iso8601 = re.compile(regex_iso8601).match
logger = SafeLogger("pi-system plugin", ["Authorization", "sharepoint_username", "sharepoint_password", "client_secret"])
class PISystemConnectorError(ValueError):
pass
def get_credentials(config, can_raise=True):
error_message = None
credentials = config.get('credentials', {})
auth_type = credentials.get("auth_type", "basic")
osisoft_basic = credentials.get("osisoft_basic", {})
ssl_cert_path = credentials.get("ssl_cert_path")
if ssl_cert_path:
setup_ssl_certificate(ssl_cert_path)
username = osisoft_basic.get("user")
password = osisoft_basic.get("password")
show_advanced_parameters = config.get('show_advanced_parameters', False)
server_url = credentials.get("default_server")
is_ssl_check_disabled = False
if show_advanced_parameters:
setup_ssl_certificate(config.get("ssl_cert_path"))
default_server = credentials.get("default_server")
overwrite_server_url = config.get("server_url")
can_disable_ssl_check = credentials.get("can_disable_ssl_check", False)
can_override_server_url = credentials.get("can_override_server_url", False)
is_ssl_check_disabled = config.get("is_ssl_check_disabled", False)
if not overwrite_server_url:
server_url = default_server
else:
if not can_override_server_url:
error_message = "You cannot override the server URL on this preset. Please refer to your Dataiku admin"
else:
server_url = overwrite_server_url
if (not can_disable_ssl_check) and is_ssl_check_disabled:
error_message = "You cannot disable SSL check on this preset. Please refer to your Dataiku admin"
is_ssl_check_disabled = can_disable_ssl_check and is_ssl_check_disabled
if can_raise and error_message:
raise PISystemConnectorError(error_message)
if can_raise:
return auth_type, username, password, server_url, is_ssl_check_disabled
else:
return auth_type, username, password, server_url, is_ssl_check_disabled, error_message
def get_batch_parameters(config):
credentials = config.get("credentials", {})
max_request_size = credentials.get("max_request_size", 1000)
estimated_density = credentials.get("estimated_density", 6000)
maximum_points_returned = credentials.get("maximum_points_returned", 1000000)
return max_request_size, estimated_density, maximum_points_returned
def compute_time_spent(start, end, bla):
# 2023-06-30T13:05:10.8692786Z->2024-06-30T13:05:10.9640942Z
start = iso_to_epoch(start)
end = iso_to_epoch(end)
return end - start
def get_advanced_parameters(config):
show_advanced_parameters = config.get('show_advanced_parameters', False)
batch_size = 500
use_batch_mode = False
if show_advanced_parameters:
use_batch_mode = config.get("use_batch_mode", False)
batch_size = config.get("batch_size", 500)
return use_batch_mode, batch_size
def check_debug_mode(config):
return config.get('show_advanced_parameters', False) and config.get('is_debug_mode', False)
def check_must_convert_object_to_string(config):
return config.get('show_advanced_parameters', False) and config.get('must_convert_object_to_string', False)
def convert_schema_objects_to_string(input_schema):
schema = copy.deepcopy(input_schema)
if isinstance(schema, list):
columns = schema
else:
columns = schema.get("columns", [])
for column in columns:
column_type = column.get("type")
if column_type == "object":
column["type"] = "string"
return schema
def get_interpolated_parameters(config):
data_type = config.get("data_type")
interval = None
sync_time = None
boundary_type = None
if data_type == "InterpolatedData":
interval = config.get("interval")
sync_time = config.get("sync_time")
if sync_time:
boundary_type = config.get("boundary_type")
return interval, sync_time, boundary_type
def get_summary_parameters(config):
data_type = config.get("data_type")
summary_type = None
summary_duration = None
if data_type == "SummaryData":
summary_type = config.get("summary_type")
summary_duration = config.get("summary_duration")
return summary_type, summary_duration
def build_select_choices(choices=None):
if not choices:
return {"choices": []}
if isinstance(choices, str):
return {"choices": [{"label": "{}".format(choices)}]}
if isinstance(choices, list):
return {"choices": choices}
if isinstance(choices, dict):
returned_choices = []
for choice_key in choices:
returned_choices.append({
"label": choice_key,
"value": choices.get(choice_key)
})
def build_requests_params(**kwargs):
requests_params_options = {
"start_time": "starttime",
"end_time": "endtime",
"start_date": "starttime",
"end_date": "endtime",
"interval": "interval",
"sync_time": "syncTime",
"sync_time_boundary_type": "syncTimeBoundaryType",
"record_boundary_type": "boundaryType",
"boundary_type": "syncTimeBoundaryType",
"name_filter": "nameFilter",
"category_name": "categoryName",
"description": "descriptionFilter",
"template_name": "templateName",
"referenced_element_name_filter": "referencedElementNameFilter",
"referenced_element_template": "referencedElementTemplate",
"severity_levels": "severity",
"max_count": "maxCount",
"start_index": "startIndex",
"summary_type": "summaryType",
"summary_duration": "summaryDuration",
"selected_fields": "selectedFields",
}
requests_params = build_query_requests_params(
query_name=kwargs.get("query_name"),
query_category=kwargs.get("query_category"),
query_template=kwargs.get("query_template"),
query_attribute=kwargs.get("query_attribute")
)
for kwarg in kwargs:
requests_param_key = requests_params_options.get(kwarg)
if requests_param_key and kwargs.get(kwarg):
value = kwargs.get(kwarg)
if type(value) is list:
requests_params.update({requests_param_key: value})
else:
requests_params.update({requests_param_key: "{}".format(value)})
search_mode = kwargs.get("search_mode")
if search_mode and (kwargs.get("start_time") or kwargs.get("end_time")):
requests_params.update({"searchMode": "{}".format(search_mode)})
if search_mode in OSIsoftConstants.SEARCHMODES_ENDTIME_INCOMPATIBLE:
requests_params.pop("endtime", None)
search_full_hierarchy = kwargs.get("search_full_hierarchy")
if search_full_hierarchy:
requests_params.update({"searchFullHierarchy": True})
resource_path = kwargs.get("resource_path")
if resource_path:
requests_params.update({"path": escape(resource_path)})
requests_params = escape_dates(requests_params)
return requests_params
def escape_dates(requests_params):
if not requests_params:
return requests_params
parameters_to_escape = ["starttime", "endtime", "syncTime"]
for parameter_to_escape in parameters_to_escape:
escaped_date = requests_params.get(parameter_to_escape, "").replace("+", "%2B")
if escaped_date:
requests_params[parameter_to_escape] = escaped_date
return requests_params
def build_query_requests_params(query_name=None, query_category=None, query_template=None, query_attribute=None):
params = {}
query_elements = []
if query_name:
query_elements.append("name:({})".format(query_name))
if query_category:
query_elements.append("afcategories:({})".format(query_category))
if query_template:
query_elements.append("afelementtemplate:({})".format(query_template))
if query_attribute:
query_elements.append("attributename:({})".format(query_attribute))
if query_elements:
return params.update({"q": " AND ".join(query_elements)})
else:
return {}
char_to_escape = {
"%": "%25",
" ": "%20",
"!": "%21",
'"': "%22",
"#": "%23",
"$": "%24",
"&": "%26",
"'": "%27",
"(": "%28",
")": "%29",
"*": "%2A",
"+": "%2B",
",": "%2C",
"-": "%2D",
".": "%2E",
"/": "%2F",
":": "%3A",
";": "%3B",
"<": "%3C",
"=": "%3D",
">": "%3E",
"?": "%3F",
"@": "%40",
"[": "%5B",
"]": "%5D"
}
def escape(string_to_escape):
for char in char_to_escape:
string_to_escape = string_to_escape.replace(char, char_to_escape.get(char))
string_to_escape = string_to_escape.replace("\\", "%5C")
return string_to_escape
def assert_time_format(date, error_source):
# https://docs.osisoft.com/bundle/pi-web-api-reference/page/help/topics/time-strings.html
pass
def assert_server_url_ok(server_url):
if not server_url:
raise ValueError("The server URL is not set")
def get_schema_as_arrays(dataset_schema):
columns = dataset_schema.get("columns", [])
column_names = []
column_types = []
for column in columns:
column_names.append(column.get("name"))
column_types.append(column.get("type"))
return column_names, column_types
def normalize_af_path(af_path):
return "\\\\" + af_path.strip("\\")
def setup_ssl_certificate(ssl_cert_path):
if ssl_cert_path:
if os.path.isfile(ssl_cert_path):
os.environ['REQUESTS_CA_BUNDLE'] = ssl_cert_path
os.environ['CURL_CA_BUNDLE'] = ssl_cert_path
def remove_unwanted_columns(row):
for unwated_column in OSIsoftConstants.SCHEMA_ATTRIBUTES_METRICS_FILTER:
row.pop(unwated_column, None)
def format_output(input_row, reference_row=None, is_enumeration_value=False):
output_row = copy.deepcopy(input_row)
type_column = None
if "Value" in output_row and isinstance(output_row.get("Value"), dict):
type_column = output_row.get("Type")
output_row.update(output_row.get("Value"))
output_row.pop("Good", None)
output_row.pop("Questionable", None)
output_row.pop("Substituted", None)
output_row.pop("Annotated", None)
if is_enumeration_value:
value = output_row.pop("Value", None)
if isinstance(value, dict):
output_row["Value"] = value.get("Name", "")
output_row["Value_ID"] = value.get("Value", None)
elif value is not None:
output_row["Value"] = value
if reference_row:
if type_column:
reference_row["Type"] = type_column
output_row.update(reference_row)
if "Path" in output_row:
output_row["ElementName"] = get_element_name_from_path(output_row.get("Path"))
return output_row
def filter_columns_from_schema(schema_columns, columns_to_remove):
output_schema = []
for column in schema_columns:
if column.get("name") not in columns_to_remove:
output_schema.append(column)
return output_schema
def is_filtered_out(item, filters=None):
if not filters:
return False
for filter_key in filters:
if filter_key not in item:
return True
filter_value = filters.get(filter_key)
item_value = item.get(filter_key)
if filter_value != item_value:
return True
return False
def is_server_throttling(response):
if response is None:
return True
if response.status_code in [409, 429, 503]:
logger.warning("Error {}, headers = {}".format(response.status_code, response.headers))
seconds_before_retry = decode_retry_after_header(response)
logger.warning("Sleeping for {} seconds".format(seconds_before_retry))
time.sleep(seconds_before_retry)
return True
return False
def decode_retry_after_header(response):
seconds_before_retry = OSIsoftConstants.DEFAULT_WAIT_BEFORE_RETRY
raw_header_value = response.headers.get("Retry-After", str(OSIsoftConstants.DEFAULT_WAIT_BEFORE_RETRY))
if raw_header_value.isdigit():
seconds_before_retry = int(raw_header_value)
else:
# Date format, "Wed, 21 Oct 2015 07:28:00 GMT"
try:
datetime_now = datetime.now()
datetime_header = datetime.strptime(raw_header_value, '%a, %d %b %Y %H:%M:%S GMT')
if datetime_header.timestamp() > datetime_now.timestamp():
# target date in the future
seconds_before_retry = (datetime_header - datetime_now).seconds
except Exception as err:
logger.error("decode_retry_after_header error {}".format(err))
seconds_before_retry = OSIsoftConstants.DEFAULT_WAIT_BEFORE_RETRY
return seconds_before_retry
def is_child_attribute_path(path):
if not path:
return False
reversed_path = path[::-1]
has_one_pipe = False
for char in reversed_path:
if char == '|':
if has_one_pipe:
return True
has_one_pipe = True
if char == '\\':
return False
return False
def get_combined_description(default_columns, actual_columns):
default_column_names = []
output_columns = []
for default_column in default_columns:
default_column_name = default_column.get("name")
default_column_names.append(default_column_name)
output_columns.append(default_column)
for actual_column in actual_columns:
if actual_column not in default_column_names:
output_columns.append({
"name": actual_column,
"type": "string"
})
return output_columns
def get_base_for_data_type(data_type, object_id, **kwargs):
schema = OSIsoftConstants.RECIPE_SCHEMA_PER_DATA_TYPE.get(data_type)
base = {}
for item in schema:
item_name = item.get("name")
base[item_name] = None
base['object_id'] = object_id
for kwarg in kwargs:
value = kwargs.get(kwarg)
if value:
base[kwarg] = value
ret = copy.deepcopy(base)
return ret
def get_max_count(config):
# some data_type requests only returns a maximum of 1k items
# This can be increased by using maxCount
DATA_TYPES_REQUIRING_MAXCOUNT = ["InterpolatedData", "PlotData", "RecordedData"]
DEFAULT_MAXCOUNT = 10000
max_count = None
data_type = config.get("data_type", None)
if data_type in DATA_TYPES_REQUIRING_MAXCOUNT:
if config.get("show_advanced_parameters", False):
max_count = config.get("max_count", DEFAULT_MAXCOUNT)
else:
max_count = DEFAULT_MAXCOUNT
if isinstance(max_count, float):
max_count = int(max_count)
return max_count
def epoch_to_iso(epoch):
logger.info("Converting '{}' epoch to iso".format(epoch))
iso_timestamp = datetime.fromtimestamp(epoch, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
logger.info("Iso for '{}' is '{}'".format(epoch, iso_timestamp))
return iso_timestamp
def iso_to_epoch(iso_timestamp):
# logger.info("Converting iso timestamp '{}' to epoch".format(iso_timestamp))
if is_epoch(iso_timestamp):
# logger.info("Timestamp is already epoch")
return iso_timestamp
epoch_timestamp = None
try:
parsed_timestamp = date_parser.parse(iso_timestamp)
epoch_timestamp = parsed_timestamp.timestamp()
except Exception:
logger.error("Error when converting iso timestamp '{}' to epoch".format(iso_timestamp))
return None
# logger.info("Timestamp is now '{}'".format(epoch_timestamp))
return epoch_timestamp
def is_epoch(timestamp):
if timestamp is None:
return False
if isinstance(timestamp, int) or isinstance(timestamp, float):
return True
return timestamp.replace(".", "", 1).isdigit()
def is_iso8601(timestamp):
# https://stackoverflow.com/questions/41129921/validate-an-iso-8601-datetime-string-in-python
if not isinstance(timestamp, str):
return False
try:
if match_iso8601(timestamp) is not None:
return True
except Exception:
pass
return False
def reorder_dataframe(unnested_items_rows, first_elements):
columns = unnested_items_rows.columns.tolist()
for first_element in reversed(first_elements):
if first_element in columns:
columns.remove(first_element)
columns.insert(0, first_element)
unnested_items_rows = unnested_items_rows[columns]
return unnested_items_rows
def fields_selector(data_type):
# specifies the fields to be returned for each data type
if data_type in ["Value", "EndValue"]:
return "Links%3BTimestamp%3BValue%3BType%3BUnitsAbbreviation"
else:
return "Links%3BItems.Timestamp%3BItems.Value%3BItems.Type%3BItems.Value.Value"
def get_next_page_url(json):
if not isinstance(json, dict):
return None
next_page_url = json.get("Links", {}).get("Next", "").replace('&', '&')
if next_page_url:
logger.info("Next page's url is {}".format(next_page_url))
else:
logger.info("No more pages available")
return next_page_url
def change_key_in_dict(input_dictionary, key_to_change, new_key_name):
if key_to_change in input_dictionary:
input_dictionary[new_key_name] = input_dictionary.pop(key_to_change)
return input_dictionary
def get_element_name_from_path(path):
# input: \\osisoft-pi-serv\Well\Assets\TX532|Current
# output: TX532
if not path:
return None
element_name = None
path_tokens = path.split("\\")
if len(path_tokens) > 0:
last_token = path_tokens[-1:][0]
element_name = last_token.split("|")[0]
return element_name
class RecordsLimit():
def __init__(self, records_limit=-1):
self.has_no_limit = (records_limit == -1)
self.records_limit = records_limit
self.counter = 0
def is_reached(self):
if self.has_no_limit:
return False
self.counter += 1
return self.counter > self.records_limit
class PerformanceTimer():
"""
Mesures the time between the calls of the start and stop methods
If start / stop are called several times,
- adds up all start / stop intervals
- count the number of intervals
- compute the average event time
- provides a lists of the NUMBER_OF_SLOWEST_EVENTS_KEPT longest events by event id, for instance url
"""
NUMBER_OF_SLOWEST_EVENTS_KEPT = 5
def __init__(self):
self.slowest_events = []
self.slowest_times = []
self.total_duration = 0
self.number_events = 0
self.current_event_id = None
def start(self, event_id=None):
"""
Args:
event_id (str, optional): name of the event to measure, to be used later on to list the longest events
"""
self.start_time = float(time.time())
self.number_events += 1
self.current_event_id = event_id
def stop(self):
duration = float(time.time()) - self.start_time
self.total_duration += duration
if self.current_event_id:
self._add_to_summary(duration)
def _add_to_summary(self, duration):
if not self.slowest_events:
self.slowest_events.append(self.current_event_id)
self.slowest_times.append(duration)
else:
index = 0
was_inserted = False
for slowest_time in self.slowest_times:
if duration > slowest_time:
self.slowest_times.insert(index, duration)
self.slowest_events.insert(index, self.current_event_id)
was_inserted = True
break
index += 1
if not was_inserted:
self.slowest_times.append(duration)
self.slowest_events.append(self.current_event_id)
self.slowest_times = self.slowest_times[:self.NUMBER_OF_SLOWEST_EVENTS_KEPT]
self.slowest_events = self.slowest_events[:self.NUMBER_OF_SLOWEST_EVENTS_KEPT]
def get_report(self):
"""
Returns:
dict: JSON containing total_duration, number_of_events, average_time, worst_performers list
"""
report = {
"total_duration": self.total_duration,
"number_of_events": self.number_events,
"average_time": self.get_average()
}
if self.slowest_events:
report["worst_performers"] = self.get_worst_performers()
return report
def get_average(self):
if not self.number_events:
return None
return self.total_duration / self.number_events
def get_worst_performers(self):
worst_performers = []
for slowest_event, slowest_time in zip(self.slowest_events, self.slowest_times):
worst_performers.append("{}: {}s".format(slowest_event, slowest_time))
return worst_performers
class BatchTimeCounter(object):
def __init__(self, max_time_to_retrieve_per_batch):
logger.info("BatchTimeCounter:max_time_to_retrieve_per_batch={}s".format(max_time_to_retrieve_per_batch * 60 * 60))
self.max_time_to_retrieve_per_batch = max_time_to_retrieve_per_batch * 60 * 60
self.total_batched_time = 0
def is_batch_full(self):
if self.max_time_to_retrieve_per_batch < 0:
return False
if self.total_batched_time > self.max_time_to_retrieve_per_batch:
logger.warning("batch contains {}s of request, needs to flush now".format(self.total_batched_time))
self.total_batched_time = 0
return True
return False
def add(self, start_time, end_time, interval):
self.total_batched_time += compute_time_spent(start_time, end_time, interval)
def get_item_details(item):
KEYS_TO_CHECK = {
"Name": "title", "TemplateName": "template_name", "CategoryNames": "category_names", "Description": "description",
"HasChildren": "has_children", "Path": "path", "WebId": "id", "checked": "checked", "BaseTemplate": "BaseTemplate"
} # should we stick to python naming convention or keep pi's ones throughout ?
details = {}
for key_to_check in KEYS_TO_CHECK:
value = item.get(key_to_check)
if value:
details[KEYS_TO_CHECK.get(key_to_check)] = value
details["url"] = item.get("Links", {}).get("Self")
details["type"] = "attribute" if "|" in details.get("path", "") else "element"
return details
class Tree():
# Each put
# - stores the data in the index
# - builds a tree based on the data's path, pointing at the right index
def __init__(self, root_tree=None):
self.tree = {}
self.index = []
if root_tree:
self._ingest(root_tree)
def _ingest(self, root_tree, parent_path=None):
parent_path = parent_path or []
if isinstance(root_tree, list):
for item in root_tree:
if not parent_path:
path = item.get("path", "")
parent_path = path.split("\\")[2:][0:2]
item_children = item.pop("children", [])
title = item.get("title")
self._ingest(item_children, parent_path=parent_path + [title])
path = item.get("path", "")
self.put(parent_path + [title], item)
def put(self, path, data):
if isinstance(path, list):
current_level = self.tree
for token in path:
if token not in current_level:
current_level[token] = {}
current_level = current_level.get(token)
index_to_update = current_level.get("_v", None)
if index_to_update is not None:
self.index[index_to_update] = data
else:
last_index = len(self.index)
self.index.append(data)
current_level.update({"_v": last_index})
def get(self, path, default=None):
if isinstance(path, list):
current_level = self.tree
for token in path:
if token not in current_level:
return default
else:
current_level = current_level.get(token)
index = current_level.get("_v")
return self.get_record(index)
def get_tree(self):
return self.tree
def get_record(self, index):
if index is None:
return None
if index < len(self.index):
return self.index[index]
return None
def get_records(self):
return self.index
def exists(self, path):
current = self.tree
if isinstance(path, list):
for token in path:
current = current.get(token, {})
if not current:
return False
return True
return False
def print(self):
print("Tree {}".format(self.tree))
print("Tree content {}".format(self.index))
def size(self):
return len(self.index)
def recursive_tree_rebuild(dictionary, records, counter=None):
counter = counter or -1
output = []
for key in dictionary:
if key == "_v":
continue
sub_dictionary = dictionary.get(key)
context = {}
if "_v" in sub_dictionary:
index_id = sub_dictionary.get("_v")
if isinstance(index_id, int):
context = records[index_id]
counter += 1
if sub_dictionary:
counter += 1
children = recursive_tree_rebuild(sub_dictionary, records, counter + 1)
else:
children = []
# context["id"] = str(counter)
context["title"] = key
context["expanded"] = True
# context["checked"] = False
context["children"] = children
output.append(context)
return output