-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaseobject.py
92 lines (77 loc) · 2.87 KB
/
baseobject.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
"""
Base object that all sites inherit off of.
"""
try:
import ConfigParser
except ImportError:
import configparser as ConfigParser
import gevent, traceback, httplib2, socket, logging
import node, copy, threading, time
class Base_Object(object):
"""
Base object for all website classes to inherit off of
"""
def __init__(self, config_file, objects):
self.config = ConfigParser.RawConfigParser()
if not self.config.read(config_file):
raise LookupError("No such config file")
self.namespace = node.Node_NameSpace()
self.write_nodes = set()
self.file_name = config_file
self.lock = threading.Lock()
self.lock.acquire()
thread = threading.Thread(target=self.write_thread)
thread.daemon = True
thread.start()
for section in self.config.sections():
if section != 'general':
item = node.Node(section, dict(self.config.items(section)), self.namespace, objects)
self.write_nodes.add(item)
else:
values = dict(self.config.items(section))
for item in values:
setattr(self, item, values[item])
self.lock.release()
gevent.spawn(self.write_poll)
def write_poll(self):
while True:
while not self.lock.acquire(False):
gevent.sleep(0)
for node in self.write_nodes:
section = node.name
for k,v in node.get_dict().items():
self.config.set(section, k,v)
self.lock.release()
gevent.sleep(60)
def write_thread(self):
mode = 'wb'
while True:
try:
with self.lock:
with open(self.file_name, mode) as fd:
self.config.write(fd)
except IOError, e:
return
time.sleep(60)
def __getattr__(self, name):
node = self.namespace.get_node(name)
if node and 'value' in node.dict:
return node.dict['value']
elif '.' in name:
node, index = name.split('.', 1)
node = self.namespace.get_node(node)
if node and index in node.dict:
return node.dict[index]
return None
def __getitem__(self, name):
node = self.namespace.get_node(name)
if node and 'value' in node.dict:
return node.dict['value']
elif '.' in name:
node, index = name.split('.', 1)
node = self.namespace.get_node(node)
if node and index in node.dict:
return node.dict[index]
elif node:
return node.dict
return None