changeset 7:1d776ec496e5

Migrate all to find_map_item_name()
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 24 May 2011 10:28:30 +0800
parents 917f0a9b9d06
children 59dd10bf60f2
files paraspace/dexfile.py
diffstat 1 files changed, 21 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/paraspace/dexfile.py	Tue May 24 10:14:05 2011 +0800
+++ b/paraspace/dexfile.py	Tue May 24 10:28:30 2011 +0800
@@ -730,7 +730,10 @@
 
     def find_map_item(self, type_value):
         maps = self._maps
-        codeItem_map = [map for map in maps if map.type == type_value][0]
+        try:
+            codeItem_map = [map for map in maps if map.type == type_value][0]
+        except IndexError:
+            return None
         
         return codeItem_map
 
@@ -868,20 +871,15 @@
     def _parse_classDatas(self):
         header = self._header
         data = self._data
-        maps = self._maps
         
-        kDexTypeClassDataItem = [type
-                                 for type, name in _DEX_MapItem.types.items()
-                                 if name == 'kDexTypeClassDataItem'][0]
-        class_data_map = [map
-                          for map in maps
-                          if map.type == kDexTypeClassDataItem][0]
+        class_data_map = self.find_map_item_name('kDexTypeClassDataItem')
 
-        off = [class_data_map.offset]
+        moff = man_off(class_data_map.offset)
+
         def parse_class_data():
             class_data = _DEX_ClassData()
-            class_data.parse(data, off[0])
-            off[0] = off[0] + class_data.data_size
+            class_data.parse(data, moff())
+            moff(class_data.data_size)
             return class_data
         class_datas = [parse_class_data() for i in range(class_data_map.size)]
 
@@ -889,13 +887,9 @@
         pass
 
     def _parse_typeLists(self):
-        maps = self._maps
         data = self._data
 
-        kDexTypeTypeList = [type 
-                            for type, name in _DEX_MapItem.types.items()
-                            if name == 'kDexTypeTypeList'][0]
-        typeList_map = [map for map in maps if map.type == kDexTypeTypeList][0]
+        typeList_map = self.find_map_item_name('kDexTypeTypeList')
         num_typeLists = typeList_map.size
 
         typeLists = _DEX_TypeLists()
@@ -905,21 +899,19 @@
         pass
 
     def _parse_codeItems(self):
-        maps = self._maps
         data = self._data
 
-        kDexTypeCodeItem = [type 
-                            for type, name in _DEX_MapItem.types.items()
-                            if name == 'kDexTypeCodeItem'][0]
-        codeItem_map = [map for map in maps if map.type == kDexTypeCodeItem][0]
+        codeItem_map = self.find_map_item_name('kDexTypeCodeItem')
+        if codeItem_map is None:
+            return
         num_codeItems = codeItem_map.size
         
-        cur_off = [codeItem_map.offset]
+        moff = man_off(codeItem_map.offset)
+        
         def parse_code():
-            off = cur_off[0]
             code = _DEX_Code()
-            code.parse(data, off)
-            cur_off[0] = off + code.data_size
+            code.parse(data, moff())
+            moff(code.data_size)
             return code
 
         codeItems = [parse_code() for i in range(num_codeItems)]
@@ -931,6 +923,8 @@
         data = self._data
         
         annoset_map = self.find_map_item_name('kDexTypeAnnotationSetItem')
+        if annoset_map is None:
+            return
         
         moff = man_off(annoset_map.offset)
         
@@ -949,6 +943,8 @@
         
         annodir_map = \
             self.find_map_item_name('kDexTypeAnnotationsDirectoryItem')
+        if annodir_map is None:
+            return
         
         moff = man_off(annodir_map.offset)