Projekt

Obecné

Profil

Stáhnout (2.63 KB) Statistiky
| Větev: | Revize:
1
import yaml
2
import os
3
from Utilities.Database import database_record_logs
4
from Utilities.helpers import should_skip
5

    
6
# Path to dataset configuration files
7
CONFIG_FILES_PATH = "DatasetConfigs/"
8
# Config file type
9
CONFIG_FILE_TYPE = ".yaml"
10

    
11

    
12
def load_configuration(dataset_name):
13
    """
14
    Loads yaml configuration file into memory
15

    
16
    Args:
17
        dataset_name: name of dataset that has existing configuration file
18

    
19
    Returns:
20
        yaml configuration file as dictionary
21
    """
22
    with open(CONFIG_FILES_PATH + dataset_name + CONFIG_FILE_TYPE, "r") as f:
23
        data = yaml.load(f, Loader=yaml.FullLoader)
24

    
25
    devices_dic = dict()
26

    
27
    if data["devices"] is not None:
28
        for item in data["devices"]:
29
            devices_dic.update(item)
30

    
31
    data["devices"] = devices_dic
32

    
33
    return data
34

    
35

    
36
def update_configuration(dataset_name, new_devices):
37
    """
38
    Open dataset and appends new_devices to the end
39

    
40
    Args:
41
        dataset_name: name of dataset that has existing configuration file
42
        new_devices: list or set of new devices for dataset
43
    """
44

    
45
    with open(CONFIG_FILES_PATH + dataset_name + CONFIG_FILE_TYPE,
46
              "a") as file:
47
        for device in new_devices:
48
            if device == "":
49
                continue
50
            file.write("  - " + device + ":\n")
51
            file.write("      x: UNKNOWN!\n")
52
            file.write("      y: UNKNOWN!\n")
53
            file.write("\n")
54

    
55

    
56
def check_if_there_is_a_config_file(dataset_name):
57
    """
58
    Goes trough all config files (represeting valid dataset in database)
59
    and checks if dataset_name is there
60

    
61
    Args:
62
        dataset_name: name of dataset that has existing configuration file
63

    
64
    Returns:   
65
        True - if contains
66
        False - if not
67
    """
68
    datasets = os.listdir(CONFIG_FILES_PATH)
69

    
70
    for dataset in datasets:
71
        name = dataset.split('.')
72
        if name[0] == dataset_name:
73
            return True
74

    
75
    return False
76

    
77

    
78
def return_dictionary_of_valid_devices(devices):
79
    """
80
    Iterates over all devices specified in config file
81

    
82
    Extracts only valid one (have both specified coordinates no UNKOWN! OR SKIP)
83

    
84
    Args:
85
        devices: dictionary of devices contained in config file
86

    
87
    Returns:   
88
        Dictonary containing only valid devices
89
    """
90
    valid_devices = dict()
91

    
92
    for device in devices.keys():
93
        if not should_skip(devices[device]):
94
            valid_devices[device] = {
95
                'name': device,
96
                'x': devices[device]['x'],
97
                'y': devices[device]['y']
98
            }
99

    
100
    return valid_devices
(1-1/4)