global_config.py 3.71 KB
Newer Older
matthmey's avatar
matthmey committed
1
2
'''MIT License

matthmey's avatar
matthmey committed
3
4
Copyright (c) 2019, Swiss Federal Institute of Technology (ETH Zurich), Matthias Meyer

matthmey's avatar
matthmey committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''

24
25
26
import yaml
from os.path import dirname, abspath, join
import os
matthmey's avatar
matthmey committed
27
28
import appdirs
import warnings
29
30
31
32
33
import datetime as dt

# initialize global settings with certain default values
_GLOBAL_CONFIG_DICT = {
    "permasense_server": "http://data.permasense.ch/",
34
    "metadata_directory": join(dirname(abspath(__file__)), "metadata"),
matthmey's avatar
matthmey committed
35
    "reference_time": dt.datetime(2000, 1, 1, 0, 0, 0),
36
37
38
39
40
41
}


def get_global_config():
    return _GLOBAL_CONFIG_DICT

matthmey's avatar
matthmey committed
42

43
44
45
def setting_exists(key):
    return key in _GLOBAL_CONFIG_DICT

matthmey's avatar
matthmey committed
46

47
48
49
50
51
52
53
54
55
56
57
58
59
def get_setting(key):
    """Returns the global setting for the given key
    
    Arguments:
        key {sting} -- setting name
    
    Returns:
        [type] -- setting value
    """

    try:
        return _GLOBAL_CONFIG_DICT[key]
    except KeyError:
matthmey's avatar
matthmey committed
60
61
        print(
            f"""The stuett setting {key} was not found and is required by a function call.
62
63
64
        Set it before your first call to the stuett package.
        This can be done by either providing a settings file via stuett.load_config(),
        updating your user config file or updating the settings directly with
matthmey's avatar
matthmey committed
65
66
67
68
               stuett.set_setting('{key}',value)"""
        )


69
70
71
72
73
74
75
76
77
def get_setting_path(key):
    """Get global setting and make sure it is a valid path
    
    Arguments:
        key {string} -- setting name 

    Returns:
        [type] -- setting value
    """
matthmey's avatar
matthmey committed
78
    path = join(get_setting(key), "")
79
    if not os.path.isdir(path):
matthmey's avatar
matthmey committed
80
        warnings.warn("stuett requested a path which is invalid: {}".format(path))
81
82
    return path

matthmey's avatar
matthmey committed
83
84

def set_setting(key, value):
85
86
    _GLOBAL_CONFIG_DICT[key] = value

matthmey's avatar
matthmey committed
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
def load_config(filename):
    """Load settings from a yaml file.

    This is used to setup the workspace and make the module
    be able to find the necessary permasense directories

    The settings file can for example contain the following items:
    
    ```python
        permasense_vault_dir: '/path/to/permasense_vault/'
        user_dir: '/path/to/user_dir/'
    ```

    """

    if os.path.isfile(filename):
matthmey's avatar
matthmey committed
104
        with open(filename, "r") as f:
105
106
107
108
109
110
111
112
            settings = yaml.safe_load(f)
    else:
        raise IOError("Parameter file not found [%s]" % filename)

    _GLOBAL_CONFIG_DICT.update(settings)


def get_user_config_file():
matthmey's avatar
matthmey committed
113
114
    return join(appdirs.AppDirs("stuett", "stuett").user_config_dir, "config.yml")

115
116
117
118
119
120
121
122
123
124

def load_user_config():
    """Check if a user specific config exists and load it
    """

    user_config = get_user_config_file()

    if os.path.isfile(user_config):
        load_config(user_config)

matthmey's avatar
matthmey committed
125

126
def get_global_time():
matthmey's avatar
matthmey committed
127
128
    return dt.datetime(2000, 1, 1, 0, 0, 0)

129

matthmey's avatar
matthmey committed
130
load_user_config()  # intially load the user settings