Blame view

src/core/pyros_django/dashboard/config_pyros.py 4.03 KB
e5f122b6   Alexis Koralewski   Adding env monito...
1
2
import yaml
import os
77d4b621   Alexis Koralewski   Add Filenames to ...
3
import sys
e5f122b6   Alexis Koralewski   Adding env monito...
4
import time
a2dbbde1   Alexis Koralewski   Adding new config...
5
6
import pykwalify.core
from pykwalify.errors import SchemaError
b42745c3   Alexis Koralewski   Fix config_pyros ...
7

00d8c70e   Alexis Koralewski   fix default path ...
8
sys.path.append(os.environ.get("PROJECT_ROOT_PATH", os.path.join(os.getcwd(),"../../../")))
77d4b621   Alexis Koralewski   Add Filenames to ...
9
from vendor.guitastro.src.guitastro import FileNames
b42745c3   Alexis Koralewski   Fix config_pyros ...
10
11


42bf33a2   Alexis Koralewski   renaming configpy...
12
class ConfigPyros:
77d4b621   Alexis Koralewski   Add Filenames to ...
13

e5f122b6   Alexis Koralewski   Adding env monito...
14
    def check_and_return_config(self, yaml_file: str, schema_file: str) -> dict:
a2dbbde1   Alexis Koralewski   Adding new config...
15
16
17
18
19
20
21
22
23
24
25
        """
        Check if yaml_file is valid for the schema_file and return an dictionary of the config file

        Args:
            yaml_file (str): Path to the config_file to be validated
            schema_file (str): Path to the schema file 

        Returns:
            dict: dictionary of the config file (with values)
        """
        # disable pykwalify error to clean the output
e5f122b6   Alexis Koralewski   Adding env monito...
26
        # logging.disable(logging.ERROR)
a2dbbde1   Alexis Koralewski   Adding new config...
27
28
29
30
31
32
        try:
            can_yaml_file_be_read = False
            while can_yaml_file_be_read != True:
                if os.access(yaml_file, os.R_OK):
                    can_yaml_file_be_read = True
                else:
e5f122b6   Alexis Koralewski   Adding env monito...
33
34
                    print(
                        f"{yaml_file} can't be accessed, waiting for availability")
a2dbbde1   Alexis Koralewski   Adding new config...
35
36
                    time.sleep(0.5)

e5f122b6   Alexis Koralewski   Adding env monito...
37
            c = pykwalify.core.Core(source_file=yaml_file, schema_files=[
b42745c3   Alexis Koralewski   Fix config_pyros ...
38
                                    os.path.join(self.config_path, schema_file)])
a2dbbde1   Alexis Koralewski   Adding new config...
39
40
41
            return c.validate(raise_exception=True)
        except SchemaError:
            for error in c.errors:
e5f122b6   Alexis Koralewski   Adding env monito...
42
43
                print("Error :", str(error).split(". Path")[0])
                print("Path to error :", error.path)
a2dbbde1   Alexis Koralewski   Adding new config...
44
45
46
47
            return None
        except IOError:
            print("Error when reading the observatory config file")

e5f122b6   Alexis Koralewski   Adding env monito...
48
49
50
51
    def read_and_check_config_file(self, yaml_file: str) -> dict:
        """
        Read the schema key of the config file to retrieve schema name and proceed to the checking of that config file
        Call check_and_return_config function and print its return.
a2dbbde1   Alexis Koralewski   Adding new config...
52

e5f122b6   Alexis Koralewski   Adding env monito...
53
54
55
56
57
58
59
60
        Args:
            yaml_file (str): path to the config file
        Returns:
            dict: Dictionary of the config file (with values)
        """
        try:
            can_config_file_be_read = False
            while can_config_file_be_read != True:
a2dbbde1   Alexis Koralewski   Adding new config...
61

e5f122b6   Alexis Koralewski   Adding env monito...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                if os.access(yaml_file, os.R_OK):
                    can_config_file_be_read = True
                else:
                    print(
                        f"{yaml_file} can't be accessed, waiting for availability")
                    time.sleep(0.5)
            with open(yaml_file, 'r') as stream:
                print(f"Reading {yaml_file}")
                config_file = yaml.safe_load(stream)
            result = self.check_and_return_config(
                yaml_file, config_file["schema"])
            if result == None:
                print(
                    "Error when reading and validating config file, please check the errors right above")
                exit(1)
            return result
a2dbbde1   Alexis Koralewski   Adding new config...
78

e5f122b6   Alexis Koralewski   Adding env monito...
79
80
81
82
83
        except yaml.YAMLError as exc:
            print(exc)
        except Exception as e:
            print(e)
            return None
a2dbbde1   Alexis Koralewski   Adding new config...
84

e5f122b6   Alexis Koralewski   Adding env monito...
85
    def __init__(self, pyros_config_file):
b42745c3   Alexis Koralewski   Fix config_pyros ...
86
87
        self.config_path = os.path.join(
            os.environ.get("PROJECT_ROOT_PATH", "../../../") , "config/")
23a61124   Alexis Koralewski   Change obsconfig ...
88
89
        if pyros_config_file is None:
            pyros_config_file = "config_pyros_default.yml"
a2dbbde1   Alexis Koralewski   Adding new config...
90
        self.pyros_config = self.read_and_check_config_file(pyros_config_file)
d3e71677   Alexis Koralewski   Put seq & eph fn_...
91
        
77d4b621   Alexis Koralewski   Add Filenames to ...
92
93
def main():
    os.environ["DJANGO_PATH"] = "../../src/core/pyros_django"
b42745c3   Alexis Koralewski   Fix config_pyros ...
94
95
    os.environ["PROJECT_ROOT_PATH"] = "../../../../"
    cp = ConfigPyros(os.path.join(os.environ["PROJECT_ROOT_PATH"], "config", "config_pyros_default.yml"))
77d4b621   Alexis Koralewski   Add Filenames to ...
96
97
    cp.fn.fcontext = "pyros_seq"
    print(cp.fn)
b42745c3   Alexis Koralewski   Fix config_pyros ...
98
99
100
101
102
103
104
105
106
107
    # file_fullname = "../../data/sequences_pickle/P1/20230319/16.p"
    # param = cp.fn.naming_get(file_fullname)
    # print(param)
    # cp.fn.fname = cp.fn.naming_set(param)
    # print(cp.fn.fname)
    # #cp.fn.subdir = cp.fn.subdir_from_filename(file_fullname)
    # print(type(cp.fn.fname))
    # out_fullname = cp.fn.join()
    # print(os.getcwd())
    # print(out_fullname)
77d4b621   Alexis Koralewski   Add Filenames to ...
108
109
110
if __name__ == "__main__":

    main()