Projekt

Obecné

Profil

Stáhnout (2.83 KB) Statistiky
| Větev: | Revize:
1
import yaml
2
import os
3
from typing import Dict, Set
4
from shared_types import StringSetType
5
from Utilities.Database import database_record_logs
6
from Utilities.helpers import should_skip
7

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

    
13

    
14
def load_configuration(dataset_name: str) -> Dict[str, any]:
15
    """
16
    Loads yaml configuration file into memory
17

    
18
    Args:
19
        dataset_name: name of dataset that has existing configuration file
20

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

    
27
    devices_dic = {}
28

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

    
33
    data["devices"] = devices_dic
34

    
35
    return data
36

    
37

    
38
def update_configuration(dataset_name: str,
39
                         new_devices: StringSetType) -> None:
40
    """
41
    Open dataset and appends new_devices to the end
42

    
43
    Args:
44
        dataset_name: name of dataset that has existing configuration file
45
        new_devices: list or set of new devices for dataset
46
    """
47

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

    
58

    
59
def check_if_there_is_a_config_file(dataset_name: str) -> bool:
60
    """
61
    Goes trough all config files (represeting valid dataset in database)
62
    and checks if dataset_name is there
63

    
64
    Args:
65
        dataset_name: name of dataset that has existing configuration file
66

    
67
    Returns:   
68
        True - if contains
69
        False - if not
70
    """
71
    datasets = os.listdir(CONFIG_FILES_PATH)
72

    
73
    for dataset in datasets:
74
        name = dataset.split('.')
75
        if name[0] == dataset_name:
76
            return True
77

    
78
    return False
79

    
80

    
81
def return_dictionary_of_valid_devices(
82
        devices: Dict[str, any]) -> Dict[str, Dict[str, any]]:
83
    """
84
    Iterates over all devices specified in config file
85

    
86
    Extracts only valid one (have both specified coordinates no UNKOWN! OR SKIP)
87

    
88
    Args:
89
        devices: dictionary of devices contained in config file
90

    
91
    Returns:   
92
        Dictonary containing only valid devices
93
    """
94
    valid_devices = {}
95

    
96
    for device in devices.keys():
97
        if not should_skip(devices[device]):
98
            valid_devices[device] = {
99
                'name': device,
100
                'x': devices[device]['x'],
101
                'y': devices[device]['y']
102
            }
103

    
104
    return valid_devices
(1-1/4)