-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.py
More file actions
255 lines (185 loc) · 5.76 KB
/
Copy pathcontainer.py
File metadata and controls
255 lines (185 loc) · 5.76 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
###########################################################
#
# Copyright (c) 2005, Southpaw Technology
# All Rights Reserved
#
# PROPRIETARY INFORMATION. This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
__all__ = ["Container"]
import thread
# Get the container instance
INSTANCES = {} # containers separated by thread
#session_instances = {} # containers separated by ticket
def _get_instance():
# get the current thread
instance = INSTANCES.get(thread.get_ident())
if instance == None:
# create a new container
instance = Container()
INSTANCES[thread.get_ident()] = instance
return instance
def _set_instance(instance):
INSTANCES[thread.get_ident()] = instance
def _create_instance():
instance = Container()
INSTANCES[thread.get_ident()] = instance
return instance
def _delete_instance():
# delete old one for this thread
container = INSTANCES[thread.get_ident()]
container.clear_data()
del(container)
del(INSTANCES[thread.get_ident()])
class Container(object):
'''general low level container object to store global information
to the entire application'''
def __init__(my):
#print "INITIALIZING CONTAINER: ", thread.get_ident(), my
my.info = dict()
def get_data(my):
return my.info
def clear_data(my):
my.info = {}
def get_instance():
return _get_instance()
get_instance = staticmethod(get_instance)
def set_instance(instance):
return _set_instance(instance)
set_instance = staticmethod(set_instance)
def get_all_instances():
return INSTANCES
get_all_instances = staticmethod(get_all_instances)
def put(key, value):
_get_instance().info[key] = value
put = staticmethod(put)
stats = {}
def get(key):
#try:
# return _get_instance().info[key]
#except:
# return None
return _get_instance().info.get(key)
#instance = _get_instance()
#return instance.info.get(key)
get = staticmethod(get)
def has(key):
return _get_instance().info.has_key(key)
has = staticmethod(has)
def remove(key):
instance = _get_instance()
if instance.info.has_key(key):
del(instance.info[key])
remove = staticmethod(remove)
# convenience methods for managing a sequence
#
def clear_seq(key):
seq = Container.get(key)
if seq == None:
seq = []
Container.put(key, seq)
else:
del seq[:] # clear the sequence
clear_seq = staticmethod(clear_seq)
def append_seq(key, value):
seq = Container.get(key)
if seq == None:
seq = []
Container.put(key, seq)
seq.append(value)
return seq
append_seq = staticmethod(append_seq)
def get_seq(key):
seq = Container.get(key)
if seq == None:
seq = []
Container.put(key, seq)
return seq
get_seq = staticmethod(get_seq)
# convenience methods for managing a dictionary
def _get_dict(dict_name):
data = Container.get(dict_name)
if data == None:
data = {}
Container.put(dict_name, data)
return data
_get_dict = staticmethod(_get_dict)
def put_dict(dict_name, key, value):
dict = Container._get_dict(dict_name)
dict[key] = value
put_dict = staticmethod(put_dict)
def get_dict(dict_name, key):
dict = Container._get_dict(dict_name)
return dict.get(key)
get_dict = staticmethod(get_dict)
def get_full_dict(dict_name):
dict = Container._get_dict(dict_name)
return dict
get_full_dict = staticmethod(get_full_dict)
def clear_dict(dict_name):
Container.put(dict_name, {})
clear_dict = staticmethod(clear_dict)
###############################
# Counter methods
#
def start_counter(key):
Container.put(key, 0)
return 0
start_counter = staticmethod(start_counter)
def get_counter(key):
counter = Container.get(key)
if counter == None:
counter = Container.start_counter(key)
return counter
get_counter = staticmethod(get_counter)
def increment(key):
counter = Container.get(key)
if counter == None:
counter = 1
else:
counter += 1
Container.put(key, counter)
return counter
increment = staticmethod(increment)
def decrement(key):
counter = Container.get(key)
if counter == None:
counter = -1
else:
counter -= 1
Container.put(key, counter)
return counter
decrement = staticmethod(decrement)
def clear():
'''Clears the container. Should be called at the initialization
of the application'''
#print "clearing container!!!"
instance = _get_instance()
#del(instance.info)
instance.info = {}
clear = staticmethod(clear)
def create():
'''Creates the container. Should be called at the initialization
of the application'''
instance = _create_instance()
return instance
create = staticmethod(create)
def delete():
'''Removes the container.'''
#print "deleting container"
_delete_instance()
delete = staticmethod(delete)
def clear_all():
'''clears all the instances for all threads'''
INSTANCES = {}
clear_all = staticmethod(clear_all)
# use psyco if present
try:
import psyco
psyco.bind(Container.get)
psyco.bind(_get_instance)
except ImportError:
pass