forked from AirBreak-UIA/SensorThingsAPI_QGIS-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_things_config.py
97 lines (71 loc) · 2.36 KB
/
sensor_things_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
"""SensorThings API config class
Description
-----------
Utility class for the plugin configuration.
Libraries/Modules
-----------------
- None.
Notes
-----
- None.
Author(s)
---------
- Created by Sandro Moretti on 09/02/2022.
Dedagroup Spa.
Members
-------
"""
import os
import yaml
from qgis.PyQt.QtCore import QObject
#
#-----------------------------------------------------------
class SensorThingsConfig(QObject):
""" Plugin configuration class. """
CFG_PLUGIN_FILE_NAME = 'config.yaml'
def __init__(self, cfg_file: str=None, parent: QObject=None):
"""Constructor"""
super().__init__(parent=parent)
self.__config = {}
self.__config_file = None
# read config file
self.__initialize(cfg_file)
def __initialize(self, cfg_file: str=None):
"""Initialization method to import YAML config file"""
# init
cfg_file = cfg_file or self.plugin_config_file
# load yaml file
with open(cfg_file, 'r') as stream:
self.__config = yaml.load( stream, Loader=yaml.loader.Loader )
# set internal members
self.__config_file = cfg_file
@property
def initialized(self) -> bool:
"""Returns true if inizialized """
return self.__config_file is not None
@property
def plugin_config_file(self) -> bool:
"""Returns config file path"""
return os.path.join(os.path.dirname(__file__), 'conf', self.CFG_PLUGIN_FILE_NAME)
def get_value(self, cfg_path: str, default=None):
"""Returns a config value by a key path, with slash as separator"""
# find value from path
cfg_path = cfg_path or ''
value = None
dt = self.__config
for key in cfg_path.split( '/' ):
if dt is None:
break
value = dt = dt.get(key)
# check if found value
if value is not None:
# return found value
return value
elif default is not None:
# return default value
return default
# raise exception if value not found
raise KeyError( "{0}: '{1}'".format(
self.tr( "Key path not found in the configuration file" ),
cfg_path ) )