opensipscli 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
opensipscli/config.py ADDED
@@ -0,0 +1,162 @@
1
+ #!/usr/bin/env python
2
+ ##
3
+ ## This file is part of OpenSIPS CLI
4
+ ## (see https://github.com/OpenSIPS/opensips-cli).
5
+ ##
6
+ ## This program is free software: you can redistribute it and/or modify
7
+ ## it under the terms of the GNU General Public License as published by
8
+ ## the Free Software Foundation, either version 3 of the License, or
9
+ ## (at your option) any later version.
10
+ ##
11
+ ## This program is distributed in the hope that it will be useful,
12
+ ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ ## GNU General Public License for more details.
15
+ ##
16
+ ## You should have received a copy of the GNU General Public License
17
+ ## along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ ##
19
+
20
+ import os
21
+ import configparser
22
+ from opensipscli import defaults
23
+ from opensipscli.logger import logger
24
+
25
+ class OpenSIPSCLIConfig:
26
+
27
+ current_instance = defaults.DEFAULT_SECTION
28
+
29
+ def __init__(self):
30
+ self.config = configparser.ConfigParser(
31
+ defaults=defaults.DEFAULT_VALUES,
32
+ default_section=defaults.DEFAULT_SECTION)
33
+ self.dynamic_options = {}
34
+ self.custom_options = {}
35
+
36
+ # Read the file given as parameter in order to parse it
37
+ def parse(self, in_file):
38
+ if not in_file:
39
+ logger.info("no config file used!")
40
+ elif os.path.isfile(in_file) and os.access(in_file, os.R_OK):
41
+ self.config.read(in_file)
42
+ else:
43
+ logger.error("Either file is missing or is not readable.")
44
+
45
+ def set_option(self, option, value = None):
46
+ if value:
47
+ self.custom_options[option] = value
48
+ else:
49
+ del self.custom_options[option]
50
+
51
+ def set_custom_options(self, options):
52
+ if options is None:
53
+ return
54
+ if isinstance(options, dict):
55
+ for k in options.keys():
56
+ self.set_option(k, options[k])
57
+ else:
58
+ for arg in options:
59
+ parsed = arg.split('=')
60
+ key = parsed[0]
61
+ val = '='.join(parsed[1:])
62
+ self.set_option(key, val)
63
+
64
+ # Function to get the value from a section.value
65
+ def get(self, key):
66
+ if self.dynamic_options and key in self.dynamic_options:
67
+ return self.dynamic_options[key]
68
+ if self.custom_options and key in self.custom_options:
69
+ return self.custom_options[key]
70
+ elif self.current_instance not in self.config:
71
+ return defaults.DEFAULT_VALUES[key]
72
+ else:
73
+ return self.config[self.current_instance][key]
74
+
75
+ # Function to set a dynamic value
76
+ def set(self, key, value):
77
+ self.dynamic_options[key] = value
78
+ logger.debug("set {}={}".format(key, value))
79
+
80
+ def mkBool(self, val):
81
+ return val.lower() in ['yes', '1', 'true']
82
+
83
+ def getBool(self, key):
84
+ return self.mkBool(self.get(key))
85
+
86
+ # checks if a configuration exists
87
+ def exists(self, key):
88
+ if self.dynamic_options and key in self.dynamic_options:
89
+ return True
90
+ if self.custom_options and key in self.custom_options:
91
+ return True
92
+ elif self.current_instance not in self.config:
93
+ return key in defaults.DEFAULT_VALUES
94
+ else:
95
+ return key in self.config[self.current_instance]
96
+
97
+ def set_instance(self, instance):
98
+ self.current_instance = instance
99
+ self.dynamic_options = {}
100
+
101
+ def has_instance(self, instance):
102
+ return instance in self.config
103
+
104
+ def get_default_instance(self):
105
+ return defaults.DEFAULT_SECTION
106
+
107
+ # reads a param or returns a default
108
+ def read_param(self, param, prompt, default=None, yes_no=False,
109
+ isbool=False, allow_empty=False):
110
+ if param:
111
+ if type(param) != list:
112
+ param = [param]
113
+ for p in param:
114
+ if self.exists(p):
115
+ return self.mkBool(self.get(p)) if isbool else self.get(p)
116
+ val = ""
117
+ if yes_no:
118
+ prompt = prompt + " [y/n]"
119
+ if default is not None:
120
+ prompt = prompt + " (default: '{}')".format("y" if default else "n")
121
+ elif default is not None:
122
+ prompt = prompt + " (default: '{}')".format(default)
123
+ prompt = prompt + ": "
124
+ while val == "":
125
+ try:
126
+ val = input(prompt).strip()
127
+ except Exception as e:
128
+ return None
129
+ if val == "":
130
+ if allow_empty:
131
+ return ""
132
+
133
+ if default is not None:
134
+ return default
135
+ elif yes_no:
136
+ if val.lower() in ['y', 'yes']:
137
+ return True
138
+ elif val.lower() in ['n', 'no']:
139
+ return False
140
+ else:
141
+ prompt = "Please choose 'y' or 'n': "
142
+ else:
143
+ return val
144
+
145
+ def to_dict(self):
146
+ temp = defaults.DEFAULT_VALUES.copy()
147
+ temp.update(self.config.defaults())
148
+
149
+ if not self.config.has_section(self.current_instance):
150
+ temp.update(self.custom_options)
151
+ temp.update(self.dynamic_options)
152
+ return temp
153
+
154
+ for option in self.config.options(self.current_instance):
155
+ temp[option] = self.config.get(self.current_instance, option)
156
+
157
+ temp.update(self.custom_options)
158
+ temp.update(self.dynamic_options)
159
+ return temp
160
+
161
+
162
+ cfg = OpenSIPSCLIConfig()