4
4
import log
5
5
import json
6
6
7
- class Data_container ( object ):
8
- def __init__ ( self , data ):
7
+
8
+ class Data_container (object ):
9
+
10
+ def __init__ (self , data ):
9
11
"""Create a Data_container from a dict."""
10
12
11
13
# Data_container will only represent dicts
12
- if not type ( data ) == dict :
13
- log .write ( "Error in data_container: data is not a dict" )
14
- raise ValueError ( "Error: data is not a dict" )
14
+ if not type ( data ) == dict :
15
+ log .write ( "Error in data_container: data is not a dict" )
16
+ raise ValueError ( "Error: data is not a dict" )
15
17
16
18
# Always use deepcopy to make it thread safe
17
- self ._data = copy .deepcopy ( data )
18
- self ._lock = threading .Lock ()
19
+ self ._data = copy .deepcopy ( data )
20
+ self ._lock = threading .Lock ()
19
21
20
- def get ( self , key ):
22
+ def get ( self , key ):
21
23
"""Return a value or None if not set
22
24
23
25
Keys:
@@ -27,25 +29,25 @@ def get ( self, key ):
27
29
"""
28
30
29
31
with self ._lock :
30
- key_levels = key .split ( "." )
32
+ key_levels = key .split ( "." )
31
33
position = self ._data
32
34
33
35
for key_level in key_levels :
34
36
# Check the current type, so the type of the last iteration's position isn't
35
37
# checked. This will allow getting non-dict values.
36
- if type ( position ) == dict :
38
+ if type ( position ) == dict :
37
39
if key_level in position :
38
40
# Set position to next dict in "tree"
39
- position = position [ key_level ]
41
+ position = position [ key_level ]
40
42
continue
41
43
42
44
# Return None if either one doesn't apply
43
45
return None
44
46
45
47
# Always use deepcopy to make it thread safe
46
- return copy .deepcopy ( position )
48
+ return copy .deepcopy ( position )
47
49
48
- def append ( self , key , value ):
50
+ def append ( self , key , value ):
49
51
"""Append a value to a list responding to a key.
50
52
51
53
Keys:
@@ -55,28 +57,30 @@ def append ( self, key, value ):
55
57
"""
56
58
57
59
with self ._lock :
58
- key_levels = key .split ( "." )
60
+ key_levels = key .split ( "." )
59
61
position = self ._data
60
62
61
63
for key_level in key_levels :
62
64
# Check the current type, so the type of the last iteration's position isn't
63
65
# checked. This will allow getting non-dict values.
64
- if type ( position ) == dict :
66
+ if type ( position ) == dict :
65
67
if key_level in position :
66
68
# Set position to next dict in "tree"
67
- position = position [ key_level ]
69
+ position = position [ key_level ]
68
70
continue
69
71
70
- log .write ( "Error in data_container: Invalid key: %s" % key )
71
- raise ValueError ( "Error: Invalid key: %s" % key )
72
+ log .write ( "Error in data_container: Invalid key: %s" % key )
73
+ raise ValueError ( "Error: Invalid key: %s" % key )
72
74
73
- if not type ( position ) == list :
74
- log .write ( "Error in data_container: Cannot append to %s ( %s ), has to be a list" % type ( position ), key )
75
- raise ValueError ( "Error: Cannot append to %s ( %s ), has to be a list" % type ( position ), key )
75
+ if not type (position ) == list :
76
+ log .write ("Error in data_container: Cannot append to %s ( %s ), has to be a list" % type (
77
+ position ), key )
78
+ raise ValueError (
79
+ "Error: Cannot append to %s ( %s ), has to be a list" % type (position ), key )
76
80
77
- position .append ( value )
81
+ position .append ( value )
78
82
79
- def pop ( self , key , index ):
83
+ def pop ( self , key , index ):
80
84
"""Pop the value at an index off a list responding to a key.
81
85
82
86
Keys:
@@ -86,28 +90,30 @@ def pop ( self, key, index ):
86
90
"""
87
91
88
92
with self ._lock :
89
- key_levels = key .split ( "." )
93
+ key_levels = key .split ( "." )
90
94
position = self ._data
91
95
92
96
for key_level in key_levels :
93
97
# Check the current type, so the type of the last iteration's position isn't
94
98
# checked. This will allow getting non-dict values.
95
- if type ( position ) == dict :
99
+ if type ( position ) == dict :
96
100
if key_level in position :
97
101
# Set position to next dict in "tree"
98
- position = position [ key_level ]
102
+ position = position [ key_level ]
99
103
continue
100
104
101
- log .write ( "Error in data_container: Invalid key: %s" % key )
102
- raise ValueError ( "Error: Invalid key: %s" % key )
105
+ log .write ( "Error in data_container: Invalid key: %s" % key )
106
+ raise ValueError ( "Error: Invalid key: %s" % key )
103
107
104
- if not type ( position ) == list :
105
- log .write ( "Error in data_container: Cannot pop from %s ( %s ), has to be a list" % type ( position ), key )
106
- raise ValueError ( "Error: Cannot pop from %s ( %s ), has to be a list" % type ( position ), key )
108
+ if not type (position ) == list :
109
+ log .write ("Error in data_container: Cannot pop from %s ( %s ), has to be a list" % type (
110
+ position ), key )
111
+ raise ValueError (
112
+ "Error: Cannot pop from %s ( %s ), has to be a list" % type (position ), key )
107
113
108
- position .pop ( index )
114
+ position .pop ( index )
109
115
110
- def remove ( self , key , value ):
116
+ def remove ( self , key , value ):
111
117
"""Remove a value from a list responding to a key.
112
118
113
119
Keys:
@@ -117,28 +123,30 @@ def remove ( self, key, value ):
117
123
"""
118
124
119
125
with self ._lock :
120
- key_levels = key .split ( "." )
126
+ key_levels = key .split ( "." )
121
127
position = self ._data
122
128
123
129
for key_level in key_levels :
124
130
# Check the current type, so the type of the last iteration's position isn't
125
131
# checked. This will allow getting non-dict values.
126
- if type ( position ) == dict :
132
+ if type ( position ) == dict :
127
133
if key_level in position :
128
134
# Set position to next dict in "tree"
129
- position = position [ key_level ]
135
+ position = position [ key_level ]
130
136
continue
131
137
132
- log .write ( "Error in data_container: Invalid key: %s" % key )
133
- raise ValueError ( "Error: Invalid key: %s" % key )
138
+ log .write ( "Error in data_container: Invalid key: %s" % key )
139
+ raise ValueError ( "Error: Invalid key: %s" % key )
134
140
135
- if not type ( position ) == list :
136
- log .write ( "Error in data_container: Cannot remove from %s ( %s ), has to be a list" % type ( position ), key )
137
- raise ValueError ( "Error: Cannot remove from %s ( %s ), has to be a list" % type ( position ), key )
141
+ if not type (position ) == list :
142
+ log .write ("Error in data_container: Cannot remove from %s ( %s ), has to be a list" % type (
143
+ position ), key )
144
+ raise ValueError (
145
+ "Error: Cannot remove from %s ( %s ), has to be a list" % type (position ), key )
138
146
139
- position .remove ( value )
147
+ position .remove ( value )
140
148
141
- def set ( self , key , value ):
149
+ def set ( self , key , value ):
142
150
"""Set a value.
143
151
144
152
Keys:
@@ -148,40 +156,40 @@ def set ( self, key, value ):
148
156
"""
149
157
150
158
with self ._lock :
151
- key_levels = key .split ( "." )
159
+ key_levels = key .split ( "." )
152
160
position = self ._data
153
161
154
162
# Ommit the last key_level from the iteration so position is the dict we want
155
163
# to save the value in.
156
- for key_level in key_levels [ : - 1 ]:
157
- if type ( position ) == dict :
164
+ for key_level in key_levels [ : - 1 ]:
165
+ if type ( position ) == dict :
158
166
if key_level in position :
159
167
# Set position to the nex position
160
- position = position [ key_level ]
168
+ position = position [ key_level ]
161
169
162
170
# Create silently dict if it doesn't exist
163
171
else :
164
- position [ key_level ] = {}
165
- position = position [ key_level ]
172
+ position [ key_level ] = {}
173
+ position = position [ key_level ]
166
174
167
175
else :
168
- log .write ( "Error in data_container: Invalid key: %s" % key )
169
- raise ValueError ( "Error: Invalid key: %s" % key )
176
+ log .write ( "Error in data_container: Invalid key: %s" % key )
177
+ raise ValueError ( "Error: Invalid key: %s" % key )
170
178
171
179
# Actually set the value
172
- position [ key_levels [ - 1 ] ] = value
180
+ position [ key_levels [ - 1 ] ] = value
173
181
174
- def get_data ( self ):
182
+ def get_data ( self ):
175
183
"""Return a deepcopy of the wrapped data.
176
184
177
185
This method is thread safe.
178
186
"""
179
187
180
188
with self ._lock :
181
189
# Always use deepcopy to make it thread safe
182
- return copy .deepcopy ( self ._data )
190
+ return copy .deepcopy ( self ._data )
183
191
184
- def get_data_container ( self , key ):
192
+ def get_data_container ( self , key ):
185
193
"""Return a Data_container object representing a dict identified by a key.
186
194
187
195
Keys:
@@ -192,5 +200,4 @@ def get_data_container ( self, key ):
192
200
193
201
# The locking is already being done in self.get
194
202
# Locking here as well would only cause a deadlock
195
- return Data_container ( self .get ( key ) )
196
-
203
+ return Data_container (self .get (key ))
0 commit comments