Rev 92: A couple of helper functions on ObjectManager. in http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

John Arbash Meinel john at arbash-meinel.com
Thu Oct 8 21:52:41 BST 2009


At http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

------------------------------------------------------------
revno: 92
revision-id: john at arbash-meinel.com-20091008205220-t256dj4mxahkzch7
parent: john at arbash-meinel.com-20091007221506-jsn9uj5awx487p1b
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Thu 2009-10-08 15:52:20 -0500
message:
  A couple of helper functions on ObjectManager.
  
  When I'm manually probing, I find I often want to grab a collection of a given type,
  or I want to get a specific object.
  This just means a little less typing. Ideally we'd have a gui to do
  this sort of probing. We'll get there eventually, I guess.
-------------- next part --------------
=== modified file 'meliae/loader.py'
--- a/meliae/loader.py	2009-10-07 21:39:53 +0000
+++ b/meliae/loader.py	2009-10-08 20:52:20 +0000
@@ -195,6 +195,9 @@
         self.objs = objs
         self.show_progress = show_progress
 
+    def __getitem__(self, address):
+        return self.objs[address]
+
     def compute_referrers(self):
         """For each object, figure out who is referencing it."""
         referrers = {} # From address => [referred from]
@@ -283,6 +286,10 @@
             summary._add(obj)
         return summary
 
+    def get_all(self, type_str):
+        """Return all objects that match a given type."""
+        return [o for o in self.objs.itervalues() if o.type_str == type_str]
+
 
 def load(source, using_json=False, show_prog=True):
     """Load objects from the given source.

=== modified file 'meliae/tests/test_loader.py'
--- a/meliae/tests/test_loader.py	2009-09-18 17:00:34 +0000
+++ b/meliae/tests/test_loader.py	2009-10-08 20:52:20 +0000
@@ -1,14 +1,14 @@
 # Copyright (C) 2009 Canonical Ltd
-# 
+#
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 3 as
 # published by the Free Software Foundation.
-# 
+#
 # This program is distributed in the hope that it will be useful, but
 # WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
@@ -65,7 +65,7 @@
             # re-evaluated for 64-bit versions of python
             test_dict_id = int(test_dict_id - 2 * (sys.maxint + 1))
         self.assertTrue(test_dict_id in manager.objs)
-        
+
     def test_load_one(self):
         objs = loader.load([
             '{"address": 1234, "type": "int", "size": 12, "value": 10'
@@ -94,7 +94,18 @@
         finally:
             f.close()
             os.remove(name)
-            
+
+    def test_get_all(self):
+        om = loader.load(_example_dump, show_prog=False)
+        the_ints = om.get_all('int')
+        self.assertEqual(2, len(the_ints))
+        self.assertEqual([4, 5], sorted([i.address for i in the_ints]))
+
+    def test_one(self):
+        om = loader.load(_example_dump, show_prog=False)
+        an_int = om[5]
+        self.assertEqual(5, an_int.address)
+        self.assertEqual('int', an_int.type_str)
 
 
 class TestRemoveExpensiveReferences(tests.TestCase):



More information about the bazaar-commits mailing list