Lines Matching refs:self
48 def __init__(self, filebasename, mode, flag='c'):
49 filebasename = self._os.fsencode(filebasename)
50 self._mode = mode
51 self._readonly = (flag == 'r')
58 self._dirfile = filebasename + b'.dir'
64 self._datfile = filebasename + b'.dat'
65 self._bakfile = filebasename + b'.bak'
68 self._index = None # maps keys to (pos, siz) pairs
71 self._create(flag)
72 self._update(flag)
74 def _create(self, flag):
76 for filename in (self._datfile, self._bakfile, self._dirfile):
83 f = _io.open(self._datfile, 'r', encoding="Latin-1")
87 with _io.open(self._datfile, 'w', encoding="Latin-1") as f:
88 self._chmod(self._datfile)
93 def _update(self, flag):
94 self._modified = False
95 self._index = {}
97 f = _io.open(self._dirfile, 'r', encoding="Latin-1")
101 self._modified = True
108 self._index[key] = pos_and_siz_pair
113 def _commit(self):
117 if self._index is None or not self._modified:
121 self._os.unlink(self._bakfile)
126 self._os.rename(self._dirfile, self._bakfile)
130 with self._io.open(self._dirfile, 'w', encoding="Latin-1") as f:
131 self._chmod(self._dirfile)
132 for key, pos_and_siz_pair in self._index.items():
140 def _verify_open(self):
141 if self._index is None:
144 def __getitem__(self, key):
147 self._verify_open()
148 pos, siz = self._index[key] # may raise KeyError
149 with _io.open(self._datfile, 'rb') as f:
158 def _addval(self, val):
159 with _io.open(self._datfile, 'rb+') as f:
172 def _setval(self, pos, val):
173 with _io.open(self._datfile, 'rb+') as f:
181 def _addkey(self, key, pos_and_siz_pair):
182 self._index[key] = pos_and_siz_pair
183 with _io.open(self._dirfile, 'a', encoding="Latin-1") as f:
184 self._chmod(self._dirfile)
187 def __setitem__(self, key, val):
188 if self._readonly:
198 self._verify_open()
199 self._modified = True
200 if key not in self._index:
201 self._addkey(key, self._addval(val))
205 pos, siz = self._index[key]
209 self._index[key] = self._setval(pos, val)
214 self._index[key] = self._addval(val)
224 def __delitem__(self, key):
225 if self._readonly:
229 self._verify_open()
230 self._modified = True
232 del self._index[key]
237 self._commit()
239 def keys(self):
241 return list(self._index)
245 def items(self):
246 self._verify_open()
247 return [(key, self[key]) for key in self._index.keys()]
249 def __contains__(self, key):
253 return key in self._index
255 if self._index is None:
260 def iterkeys(self):
262 return iter(self._index)
267 def __len__(self):
269 return len(self._index)
273 def close(self):
275 self._commit()
277 self._index = self._datfile = self._dirfile = self._bakfile = None
281 def _chmod(self, file):
282 self._os.chmod(file, self._mode)
284 def __enter__(self):
285 return self
287 def __exit__(self, *args):
288 self.close()