-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparameters.py
More file actions
74 lines (63 loc) · 2.36 KB
/
Copy pathparameters.py
File metadata and controls
74 lines (63 loc) · 2.36 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
#parameters.py
"""
Handle application parameters stored in a ini style configuration file.
"""
import os,ConfigParser
import logging
# Reference the global logger
logger = logging.getLogger('MyLogger')
debug=logger.debug
info=logger.info
class Section(object):
def __init__(self,name,):
self.name=name
class Parameters(object):
def __init__(self,iniFile,sectionName='main'):
self.iniFile=iniFile
self.sectionName=sectionName
self._readIniFile(self.iniFile)
## # parameter:(default,cast function)
## self.paramMap={
## 'host':('127.0.0.1',str)
## ,'port':(1448,int)
## ,'init_interval':(60,int) #seconds between reads of the ini file
## ,'poll_interval':(2000,int) #milliseconds between socket scans
## ,'username':('KE7FXL',str)
## ,'password':('22445',str)
## ,'filter':('a/47/-127/41/-116',str)
## ,'aprsis_login':(False,lambda x:x!='0')
## ,'log_packets':(False,lambda x:x!='0')
## }
## for k in self.paramMap.keys():
## self.__dict__[k]=self.paramMap[k][0]
def get(self,attr):
#print self.__dict__[self.sectionName].__dict__[attr]
return self.__dict__[self.sectionName].__getattribute__(attr)
def _readIniFile(self,iniFile=None):
if iniFile:
oldIniFile=self.iniFile
self.iniFile=iniFile
else:
oldIniFile=self.iniFile
cfg=ConfigParser.ConfigParser()
oldParams=self.__dict__.copy()
debug('Reading inifile: %s' % self.iniFile)
## try:
if not os.path.exists(iniFile):
debug('Config file does not exist: %s' % iniFile)
return False
cfg.read(iniFile)
for section in cfg.sections():
self.__dict__[section]=Section(section)
items=cfg.items(section)
for item in items:
self.__dict__[section].__setattr__(item[0],item[1])
debug('Init parmeters: %s' % self.__dict__[self.sectionName].__dict__)
return True
## except:
## debug('Error reading config file: %s' % self.iniFile)
## self.__dict__=oldParams.copy()
## self.iniFile=oldIniFile
## return False
if __name__=='__main__':
p=Parameters('aprsmonitor.ini')