- Timestamp:
- 03/25/11 18:31:16 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/pyregfi/__init__.py
r213 r214 6 6 7 7 import sys 8 import weakref 8 9 from pyregfi.structures import * 9 10 … … 150 151 # access to base structures 151 152 class _StructureWrapper(object): 152 hive = None153 base = None153 _hive = None 154 _base = None 154 155 155 156 def __init__(self, hive, base): 156 self. hive = hive157 self._hive = hive 157 158 # XXX: check for NULL here, throw an exception if so. 158 self. base = base159 self._base = base 159 160 160 161 def __del__(self): 161 regfi.regfi_free_record(self. base)162 regfi.regfi_free_record(self._base) 162 163 hive = None 163 164 164 165 def __getattr__(self, name): 165 return getattr(self. base.contents, name)166 return getattr(self._base.contents, name) 166 167 167 168 def __eq__(self, other): … … 188 189 189 190 class _GenericList(object): 190 hive = None191 key = None192 length = None193 current = None194 195 # implementation-specific functions 196 fetch_num = None197 find_element = None198 get_element = None199 constructor = None191 _hive = None 192 _key = None 193 _length = None 194 _current = None 195 196 # implementation-specific functions for _SubkeyList and _ValueList 197 _fetch_num = None 198 _find_element = None 199 _get_element = None 200 _constructor = None 200 201 201 202 def __init__(self, key): 202 self.hive = key.hive 203 self._hive = key._hive 204 205 # Normally it's good to avoid cyclic references like this 206 # (key.list.key...) but in this case it makes ctypes memory 207 # management easier to reference the Key instead of the base 208 # structure. We use a weak reference in order to allow for garbage 209 # collection, since value/subkey lists should not be usable if their 210 # parent Key is freed anyway. 211 203 212 # XXX: check for NULL here, throw an exception if so. 204 self.key = key 205 self.length = self.fetch_num(key.base) 206 207 def __del__(self): 208 self.key = None 209 213 self._key = weakref.proxy(key) 214 self._length = self._fetch_num(key._base) 215 216 210 217 def __len__(self): 211 return self. length218 return self._length 212 219 213 220 def __getitem__(self, name): … … 219 226 name = create_string_buffer(bytes(name)) 220 227 221 if self.find_element(self.hive.file, self.key.base, name, byref(index)): 222 return self.constructor(self.hive, self.get_element(self.hive.file, 223 self.key.base, 224 index)) 228 if self._find_element(self._hive.file, self._key.base, name, byref(index)): 229 return self._constructor(self._hive, 230 self._get_element(self._hive.file, 231 self._key.base, 232 index)) 225 233 raise KeyError('') 226 234 … … 232 240 233 241 def __iter__(self): 234 self. current = 0242 self._current = 0 235 243 return self 236 244 237 245 def __next__(self): 238 if self. current >= self.length:246 if self._current >= self._length: 239 247 raise StopIteration('') 240 248 241 elem = self. get_element(self.hive.file, self.key.base,242 c_uint32(self. current))243 self. current += 1244 return self. constructor(self.hive, elem)249 elem = self._get_element(self._hive.file, self._key._base, 250 c_uint32(self._current)) 251 self._current += 1 252 return self._constructor(self._hive, elem) 245 253 246 254 # For Python 2.x 247 def next(self): 248 return self.__next__() 255 next = __next__ 249 256 250 257 251 258 class _SubkeyList(_GenericList): 252 fetch_num = regfi.regfi_fetch_num_subkeys253 find_element = regfi.regfi_find_subkey254 get_element = regfi.regfi_get_subkey259 _fetch_num = regfi.regfi_fetch_num_subkeys 260 _find_element = regfi.regfi_find_subkey 261 _get_element = regfi.regfi_get_subkey 255 262 256 263 257 264 class _ValueList(_GenericList): 258 fetch_num = regfi.regfi_fetch_num_values259 find_element = regfi.regfi_find_value260 get_element = regfi.regfi_get_value265 _fetch_num = regfi.regfi_fetch_num_values 266 _find_element = regfi.regfi_find_value 267 _get_element = regfi.regfi_get_value 261 268 262 269 … … 287 294 288 295 def fetch_security(self): 289 return Security(self. hive,290 regfi.regfi_fetch_sk(self. hive.file, self.base))296 return Security(self._hive, 297 regfi.regfi_fetch_sk(self._hive.file, self.base)) 291 298 292 299 … … 300 307 ret_val = None 301 308 if name == "data": 302 data_p = regfi.regfi_fetch_data(self. hive.file, self.base)309 data_p = regfi.regfi_fetch_data(self._hive.file, self.base) 303 310 try: 304 311 data_struct = data_p.contents … … 330 337 elif name == "data_raw": 331 338 # XXX: should we load the data without interpretation instead? 332 data_p = regfi.regfi_fetch_data(self. hive.file, self.base)339 data_p = regfi.regfi_fetch_data(self._hive.file, self.base) 333 340 try: 334 341 data_struct = data_p.contents … … 357 364 # Avoids chicken/egg class definitions. 358 365 # Also makes for convenient code reuse in these lists' parent classes. 359 _SubkeyList. constructor = Key360 _ValueList. constructor = Value366 _SubkeyList._constructor = Key 367 _ValueList._constructor = Value 361 368 362 369 … … 427 434 raise Exception("Could not create iterator. Current log:\n" 428 435 + GetLogMessages()) 429 self. hive = hive436 self._hive = hive 430 437 431 438 def __getattr__(self, name): … … 463 470 464 471 # For Python 2.x 465 def next(self): 466 return self.__next__() 472 next = __next__ 467 473 468 474 def down(self): … … 484 490 485 491 def current_key(self): 486 return Key(self. hive, regfi.regfi_iterator_cur_key(self.iter))492 return Key(self._hive, regfi.regfi_iterator_cur_key(self.iter)) 487 493 488 494 #XXX Add subkey/value search accessor functions (?)
Note: See TracChangeset
for help on using the changeset viewer.