1
|
import yaml
|
2
|
import os
|
3
|
from Utilities.Database import database_record_logs
|
4
|
|
5
|
# Path to dataset configuration files
|
6
|
CONFIG_FILES_PATH = "DatasetConfigs/"
|
7
|
# Config file type
|
8
|
CONFIG_FILE_TYPE = ".yaml"
|
9
|
|
10
|
|
11
|
def load_configuration(dataset_name):
|
12
|
"""
|
13
|
Loads yaml configuration file into memory
|
14
|
|
15
|
Args:
|
16
|
dataset_name: name of dataset that has existing configuration file
|
17
|
|
18
|
Returns:
|
19
|
yaml configuration file as dictionary
|
20
|
"""
|
21
|
with open(CONFIG_FILES_PATH + dataset_name + CONFIG_FILE_TYPE, "r") as f:
|
22
|
data = yaml.load(f, Loader=yaml.FullLoader)
|
23
|
|
24
|
devices_dic = dict()
|
25
|
|
26
|
if data["devices"] is not None:
|
27
|
for item in data["devices"]:
|
28
|
devices_dic.update(item)
|
29
|
|
30
|
data["devices"] = devices_dic
|
31
|
|
32
|
return data
|
33
|
|
34
|
|
35
|
def update_configuration(dataset_name, new_devices):
|
36
|
"""
|
37
|
Open dataset and appends new_devices to the end
|
38
|
|
39
|
Args:
|
40
|
dataset_name: name of dataset that has existing configuration file
|
41
|
new_devices: list or set of new devices for dataset
|
42
|
"""
|
43
|
|
44
|
with open(CONFIG_FILES_PATH + dataset_name + CONFIG_FILE_TYPE, "a") as file:
|
45
|
for device in new_devices:
|
46
|
file.write(" - "+device+":\n")
|
47
|
file.write(" x: UNKNOWN!\n")
|
48
|
file.write(" y: UNKNOWN!\n")
|
49
|
file.write("\n")
|
50
|
|
51
|
|
52
|
def check_if_there_is_a_config_file(dataset_name):
|
53
|
"""
|
54
|
Goes trough all config files (represeting valid dataset in database)
|
55
|
and checks if dataset_name is there
|
56
|
|
57
|
Args:
|
58
|
dataset_name: name of dataset that has existing configuration file
|
59
|
|
60
|
Returns:
|
61
|
True - if contains
|
62
|
False - if not
|
63
|
"""
|
64
|
datasets = os.listdir(CONFIG_FILES_PATH)
|
65
|
|
66
|
for dataset in datasets:
|
67
|
name = dataset.split('.')
|
68
|
if name[0] == dataset_name :
|
69
|
return True
|
70
|
|
71
|
return False
|
72
|
|
73
|
def return_dictionary_of_valid_devices(devices):
|
74
|
"""
|
75
|
Iterates over all devices specified in config file
|
76
|
|
77
|
Extracts only valid one (have both specified coordinates no UNKOWN! OR SKIP)
|
78
|
|
79
|
Args:
|
80
|
devices: dictionary of devices contained in config file
|
81
|
|
82
|
Returns:
|
83
|
Dictonary containing only valid devices
|
84
|
"""
|
85
|
valid_devices = dict()
|
86
|
|
87
|
for device in devices.keys():
|
88
|
x = devices[device]['x']
|
89
|
y = devices[device]['y']
|
90
|
if not (x == "SKIP" or x == "UNKNOWN!" or y == "SKIP" or y == "UNKNOWN!"):
|
91
|
valid_devices[device] = {'name': device, 'x': x , 'y': y}
|
92
|
|
93
|
return valid_devices
|