Changeset 208 for trunk/python


Ignore:
Timestamp:
09/07/10 23:50:04 (14 years ago)
Author:
tim
Message:

more additions to pyregfi: functional subkey and value lists

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/pyregfi/__init__.py

    r207 r208  
    121121
    122122
     123def _buffer2bytearray(char_pointer, length):
     124    if length == 0 or char_pointer == None:
     125        return None
     126   
     127    ret_val = bytearray(length)
     128    for i in range(0,length):
     129        ret_val[i] = char_pointer[i][0]
     130
     131    return ret_val
     132
     133
     134
     135
    123136class _StructureWrapper():
    124137    "Handles memory management and proxies attribute access to base structures"
     
    144157
    145158
     159class Key(_StructureWrapper):
     160    pass
     161
    146162class Value(_StructureWrapper):
    147163    pass
    148164
    149 
    150165class Data(_StructureWrapper):
    151166    pass
    152167
    153 
    154168class Security(_StructureWrapper):
    155169    pass
    156170
    157171
    158 class _ValueList():
     172class _GenericList():
    159173    hive = None
    160174    key = None
     
    162176    current = None
    163177
     178    # implementation-specific functions
     179    fetch_num = None
     180    find_element = None
     181    get_element = None
     182    constructor = None
     183
    164184    def __init__(self, key):
    165185        self.hive = key.hive
    166186        # XXX: check for NULL here, throw an exception if so.
    167187        self.key = key
    168         self.length = regfi.regfi_fetch_num_values(key.base)
     188        self.length = self.fetch_num(key.base)
    169189   
    170190    def __len__(self):
     
    173193    def __getitem__(self, name):
    174194        index = c_uint32()
    175         # XXX: need to do any funky unicode conversions on name?
    176         if regfi.regfi_find_value(self.hive.file, self.key.base,
    177                                   create_string_buffer(name), byref(index)):
    178             return Value(hive,
    179                          regfi.regfi_get_value(hive.file, key.base, index))
     195        if isinstance(name, str):
     196            name = name.encode('utf-8')
     197
     198        if self.find_element(self.hive.file, self.key.base,
     199                             create_string_buffer(name), byref(index)):
     200            return self.constructor(self.hive, self.get_element(self.hive.file,
     201                                                                self.key.base,
     202                                                                index))
    180203        raise KeyError('')
     204
    181205
    182206    def __iter__(self):
     
    188212            raise StopIteration('')
    189213
    190         vk = regfi.regfi_get_value(self.hive.file, self.key.base,
    191                                    c_uint32(self.current))
     214        elem = self.get_element(self.hive.file, self.key.base,
     215                                c_uint32(self.current))
    192216        self.current += 1
    193         return vk.contents
    194    
     217        return elem.contents
     218   
     219
     220class _SubkeyList(_GenericList):
     221    fetch_num = regfi.regfi_fetch_num_subkeys
     222    find_element = regfi.regfi_find_subkey
     223    get_element = regfi.regfi_get_subkey
     224
     225
     226class _ValueList(_GenericList):
     227    fetch_num = regfi.regfi_fetch_num_values
     228    find_element = regfi.regfi_find_value
     229    get_element = regfi.regfi_get_value
     230
    195231
    196232class Key(_StructureWrapper):
    197233    values = None
     234    subkeys = None
    198235
    199236    def __init__(self, hive, base):
    200237        super(Key, self).__init__(hive, base)
    201238        self.values = _ValueList(self)
     239        self.subkeys = _SubkeyList(self)
     240
     241    def __getattr__(self, name):
     242        ret_val = super(Key, self).__getattr__(name)
     243        if ret_val == None:
     244            return None
     245       
     246        if name == "name":
     247            ret_val = ret_val.decode('utf-8')
     248        elif name == "name_raw":
     249            length = super(Key, self).__getattr__('name_length')
     250            ret_val = _buffer2bytearray(ret_val, length)
     251       
     252        return ret_val
     253
    202254
    203255    def fetch_security(self):
    204256        return Security(self.hive,
    205257                        regfi.regfi_fetch_sk(self.hive.file, self.base))
     258
     259
     260class Value(_StructureWrapper):
     261    def __getattr__(self, name):
     262        ret_val = super(Value, self).__getattr__(name)
     263        if ret_val == None:
     264            return None
     265
     266        if name == "name":
     267            ret_val = ret_val.decode('utf-8')
     268        elif name == "name_raw":
     269            length = super(Value, self).__getattr__('name_length')
     270            ret_val = _buffer2bytearray(ret_val, length)
     271       
     272        return ret_val
     273
     274
     275# Avoids chicken/egg class definitions.
     276# Also makes for convenient code reuse in these lists' parent classes.
     277_SubkeyList.constructor = Key
     278_ValueList.constructor = Value
     279
    206280
    207281
Note: See TracChangeset for help on using the changeset viewer.