forked from python-smpplib/python-smpplib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
101 lines (79 loc) · 2.89 KB
/
config.py
File metadata and controls
101 lines (79 loc) · 2.89 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
97
98
99
100
101
import os
import json
current_file = os.path.basename(__file__)
current_folder = os.path.dirname(__file__)
default_config_path = os.path.join(current_folder, "config.json")
class Config ():
def __init__ (self, config_path=default_config_path, utf8=False):
"""Contructor of class
Args:
config_path (str/path, optional): Json file for process credentials. Defaults to config.json file.
utf8 (bool, optional): Read or write data in utf8 format. Defaults to False.
"""
self.config_path=config_path
self.utf8=utf8
config_exist = os.path.isfile(self.config_path)
if not config_exist:
print (f"NOT FILE {self.config_path}")
def get (self, credential=""):
"""
Get specific credential from config file
"""
# Read credentials file
if self.utf8:
config_file = open(self.config_path, "r", encoding='utf-8')
else:
config_file = open(self.config_path, "r")
# Get specific credential
try:
config_data = json.loads(config_file.read())
return (config_data[credential])
except Exception as err:
# print (err)
return ""
# Close file
config_file.close()
def get_all (self):
"""
return all crdentials from file
"""
# Read credentials file
if self.utf8:
config_file = open(self.config_path, "r", encoding='utf-8')
else:
config_file = open(self.config_path, "r")
# Get specific credential
try:
config_data = json.loads(config_file.read())
return (config_data)
except Exception as err:
print (err)
return ""
# Close file
config_file.close()
def create_config (self, credentials, rewrite=False):
"""
Create a config file with default credentials
"""
if rewrite:
open_mode = "w"
else:
open_mode = "a"
with open (self.config_path, open_mode) as config_file:
config_file.write(json.dumps(credentials))
def update (self, credential="", value=""):
"""
Update specific credential in config file
"""
with open (self.config_path, "r") as config_file:
config_data = json.loads(config_file.read())
config_data[credential] = value
with open (self.config_path, "w") as config_file:
config_file.write(json.dumps(config_data))
def update_all (self, credentials, values):
"""
Update credentials
"""
for cred_config, cred_gui in credentials.items():
new_credential = values[cred_gui]
self.update (cred_config, new_credential)