forked from avico78/total_compare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
96 lines (73 loc) · 2.94 KB
/
settings.py
File metadata and controls
96 lines (73 loc) · 2.94 KB
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
import yaml
class SettingsLoader:
def __init__(self, config_file_path):
self.config_file_path = config_file_path
self.settings = self._load_settings()
def _load_settings(self):
with open(self.config_file_path, 'r') as config_file:
return yaml.safe_load(config_file) or {}
def __call__(self, parameter=None):
if parameter is None:
return self.settings
return self._get_nested_setting(parameter)
def _get_nested_setting(self, parameter):
keys = parameter.split('.')
current_dict = self.settings
for key in keys:
current_dict = current_dict.get(key, {})
return current_dict
def __str__(self):
return "\n".join(f"{key}: {value}" for key, value in vars(self).items())
#
#
#
# class Settings:
# _instance = None # Class variable to store the instance
#
# def __new__(cls, config_path):
# if cls._instance is None:
# cls._instance = super(Settings, cls).__new__(cls)
# cls._instance.config_path = config_path
# cls._instance.excel_settings = {}
# cls._instance.database_settings = {}
# cls._instance.data_sources = {}
# cls._instance._read_config()
# return cls._instance
#
# def _read_config(self):
# # Read configuration from YAML file
# with open(self.config_path, 'r') as config_file:
# config_data = yaml.safe_load(config_file)
# self.excel_settings = config_data.get('excel_settings')
# self.database_settings = config_data.get('databases', {})
# self.data_sources = config_data.get('data_sources', {})
#
# def get_settings(self, parameter=None):
# if parameter is None:
# return self.settings
# return self._get_nested_setting(parameter)
#
# def _get_nested_setting(self, parameter):
# keys = parameter.split('.')
# current_dict = self.settings
# for key in keys:
# current_dict = current_dict.get(key, {})
# return current_dict
#
# def __str__(self):
# return "\n".join(f"{key}: {value}" for key, value in vars(self).items())
if __name__ == "__main__":
# Example usage
config_file_path = "config/config.yml"
loader1 = SettingsLoader(config_file_path)
# Accessing and printing nested settings using the __call__ method
excel_file_path = loader1('excel_settings.file_path')
print("Excel file path:", excel_file_path)
# Creating another instance with the same config file path
loader2 = SettingsLoader(config_file_path)
# Check if they are the same instance using id()
assert id(loader1) != id(loader2) ,"both id as SettingsLoader is singeltone"
excel_sheets = loader2('excel_settings.sheets')
print("Excel sheets:", excel_sheets)
sqlite_table_name = loader2('databases.sqlite.table_name')
print("SQLite table name:", sqlite_table_name)