-
Notifications
You must be signed in to change notification settings - Fork 40
/
Configberry.py
134 lines (105 loc) · 4.07 KB
/
Configberry.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import configparser
import os
import logging
import shutil
class Configberry:
config = configparser.ConfigParser()
def __init__(self):
self.logger = logging.getLogger("Configberry")
self.config.read( self.getConfigFIle() )
self.__create_config_if_not_exists()
def getConfigFIle(self):
FILE_PATH = os.getenv('CONFIG_FILE_PATH', './')
CONFIG_FILE_NAME = os.path.join(FILE_PATH, 'config.ini')
self.logger.debug("Config file path: %s" % CONFIG_FILE_NAME)
return CONFIG_FILE_NAME
def getJSON(self):
jsondata = {}
for s in self.sections():
jsondata.setdefault(s, {})
for (k, data) in self.config.items(s):
jsondata[s].setdefault(k, data)
return jsondata
def items(self):
self.config.read( self.getConfigFIle() )
return self.config.items()
def sections(self):
self.config.read( self.getConfigFIle() )
return self.config.sections()
def findByMac(self, mac):
"Busca entre todas las sections por la mac"
for s in self.sections()[1:]:
if self.config.has_option(s, 'mac'):
mymac = self.config.get(s, 'mac')
self.logger.debug("mymac %s y la otra es mac %s" % (mymac, mac))
if mymac == mac:
self.logger.debug("encontre la mac %s" % mac)
return (s, self.get_config_for_printer(s))
return False
def writeSectionWithKwargs(self, printerName, kwargs):
self.config = configparser.RawConfigParser()
self.config.read( self.getConfigFIle() )
if not self.config.has_section(printerName):
self.config.add_section(printerName)
for param in kwargs:
self.config.set(printerName, param, kwargs[param])
with open(self.getConfigFIle(), 'w') as configfile:
self.config.write(configfile)
configfile.close()
return 1
def __create_config_if_not_exists(self):
CONFIG_FILE_NAME = self.getConfigFIle()
if not os.path.isfile(CONFIG_FILE_NAME):
curpath = os.path.dirname(os.path.realpath(__file__))
configIniInstallFile = os.path.join(curpath, 'config.ini.install')
shutil.copy(configIniInstallFile, CONFIG_FILE_NAME)
def get_config_for_printer(self, printerName):
'''
printerName: string
'''
# if printerName is an IP address, extract IP and PORT.
# e.g.
# printerName = "192.168.0.25:9100"
# host is 192.168.0.25
# port is 9100
# e.g. 2
# printerName = "192.168.0.25"
# host is 192.168.0.25
# port is 9100
# e.g. 3
# printerName = "192.168.0.25:6100"
# host is 192.168.0.25
# port is 6100
if ":" in printerName:
host, port = printerName.split(":")
ret = {
"marca": "EscP",
"driver": "ReceiptDirectJet",
"host": host,
"port": port
}
return ret
elif "&" in printerName:
params = printerName.split('&')
dictConf = {}
for param in params:
key, value = param.split('=')
dictConf[key] = value
return dictConf
else:
printerName = printer
dictConf = {s: dict(self.config.items(s)) for s in self.config.sections()}
return dictConf[printerName]
def get_actual_config(self):
dictConf = {s: dict(self.config.items(s)) for s in self.config.sections()}
return dictConf
def delete_printer_from_config(self, printerName):
self.config = configparser.RawConfigParser()
CONFIG_FILE_NAME = self.getConfigFIle()
self.config.read(CONFIG_FILE_NAME)
if self.config.has_section(printerName):
self.config.remove_section(printerName)
with open(CONFIG_FILE_NAME, 'w') as configfile:
self.config.write(configfile)
configfile.close()
return 1