-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBTUtils.py
52 lines (46 loc) · 1.68 KB
/
BTUtils.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
import inspect
import time
class BTObject(object):
def __init__(self, theName, theApp):
self.name = theName
self.app = theApp
if self.app.debugObject: self.app.log(self.name, "created")
class BTApp(object):
def __init__(self, configFileName, logFileName, config):
self.running = True # True until something terminates the program
self.configFileName = configFileName
self.logFileName = logFileName
self.setConfig(config)
self.readConfig()
def setConfig(self, config, override=True):
for item in config.keys():
try:
attr = getattr(self, item)
if override:
setattr(self, item, config[item])
except AttributeError:
setattr(self, item, config[item])
def readConfig(self):
try:
configFile = open(self.configFileName)
config = {}
for line in configFile:
try:
line = line[:line.find("#")].strip()
if line != "":
param = line.split("=")
config[param[0].strip()] = eval(param[1].strip())
except:
print "Bad configuration parameter"
print line
self.setConfig(config)
configFile.close()
except:
pass
def log(self, *args):
message = "%-16s: "%args[0]
for arg in args[1:]:
message += arg.__str__()+" "
logFile = open(self.logFileName, "a")
logFile.write(time.strftime("%Y-%m-%d %H:%M:%S")+" - "+message+"\n")
logFile.close()