[rtems-tools commit] Refactor subcommands

Chris Johns chrisj at rtems.org
Sun Aug 24 23:45:34 UTC 2014


Module:    rtems-tools
Branch:    master
Commit:    ddbc5306fa4f92048a8b1e94a35c25227adcf4fb
Changeset: http://git.rtems.org/rtems-tools/commit/?id=ddbc5306fa4f92048a8b1e94a35c25227adcf4fb

Author:    Dhananjay Balan <mb.dhananjay at gmail.com>
Date:      Wed Aug 21 18:30:36 2013 +0530

Refactor subcommands
 - index commands inherit from a parent class.

---

 tools/gdb/python/rtems.py |   88 +++++++++++++++++---------------------------
 1 files changed, 34 insertions(+), 54 deletions(-)

diff --git a/tools/gdb/python/rtems.py b/tools/gdb/python/rtems.py
index 718701c..7d26542 100644
--- a/tools/gdb/python/rtems.py
+++ b/tools/gdb/python/rtems.py
@@ -65,16 +65,20 @@ class rtems_object(gdb.Command):
                 object.show(from_tty)
         objects.information.invalidate()
 
-class rtems_semaphore(gdb.Command):
-    '''Semaphore subcommand for rtems'''
+class rtems_index(gdb.Command):
+    '''Print object by index'''
 
     api = 'classic'
-    _class = 'semaphores'
+    _class = ''
 
-    def __init__(self):
-        self.__doc__ = 'Display the RTEMS semaphores by index'
-        super(rtems_semaphore, self).__init__( 'rtems semaphore',
-                                           gdb.COMMAND_DATA, gdb.COMPLETE_NONE )
+    def __init__(self,command):
+        super(rtems_index, self).__init__( command,
+                                           gdb.COMMAND_DATA,
+                                           gdb.COMPLETE_NONE)
+
+    def instance(self,obj):
+        '''Returns a n instance of corresponding object, the child should extend this'''
+        return obj
 
     def invoke(self, arg, from_tty):
         for val in arg.split():
@@ -91,68 +95,44 @@ class rtems_semaphore(gdb.Command):
                 print "error: index %s is invalid" % (index)
                 return
 
-            instance = classic.semaphore(obj)
+            instance = self.instance(obj)
             instance.show(from_tty)
         objects.information.invalidate()
 
-class rtems_task(gdb.Command):
+
+class rtems_semaphore(rtems_index):
+    '''semaphore subcommand'''
+    _class = 'semaphores'
+
+    def __init__(self):
+        self.__doc__ = 'Display RTEMS semaphore(s) by index(es)'
+        super(rtems_semaphore, self).__init__('rtems semaphore')
+
+    def instance(self,obj):
+        return classic.semaphore(obj)
+
+class rtems_task(rtems_index):
     '''tasks subcommand for rtems'''
 
-    api = 'classic'
     _class = 'tasks'
 
     def __init__(self):
-        self.__doc__ = 'Display the RTEMS tasks by index(s)'
-        super(rtems_task,self).__init__('rtems task',
-                                        gdb.COMMAND_DATA, gdb.COMPLETE_NONE)
+        self.__doc__ = 'Display RTEMS task(s) by index(es)'
+        super(rtems_task,self).__init__('rtems task')
 
-    def invoke(self, arg, from_tty):
-        for val in arg.split():
-            try:
-                index = int(val)
-            except ValueError:
-                raise gdb.GdbError( "Value is not an integer")
+    def instance(self,obj):
+        return classic.task(obj)
 
-            try:
-                obj = objects.information.object_return(self.api,
-                                                        self._class,
-                                                        index).dereference()
-            except IndexError:
-                print "error: index %s is invalid" % (index)
-                return
 
-            instance = classic.task(obj)
-            instance.show(from_tty)
-        objects.information.invalidate()
-
-class rtems_message_queue(gdb.Command):
+class rtems_message_queue(rtems_index):
     '''Message Queue subcommand'''
 
-    api = 'classic'
     _class = 'message_queues'
 
     def __init__(self):
-        self.__doc__ = 'Display the RTEMS message_queue by index(s)'
-        super(rtems_message_queue,self).__init__('rtems mqueue',
-                                                gdb.COMMAND_DATA,
-                                                gdb.COMPLETE_NONE)
-
-    def invoke(self, arg, from_tty):
-        for val in arg.split():
-            try:
-                index = int(val)
-            except ValueError:
-                print "error: %s is not an index" % (val)
-                return
+        self.__doc__ = 'Display RTEMS message_queue(s) by index(es)'
+        super(rtems_message_queue,self).__init__('rtems mqueue')
 
-            try:
-                obj = objects.information.object_return(self.api,
-                                                        self._class,
-                                                        index).dereference()
-            except IndexError:
-                print "error: index %s is invalid" % (index)
-                return
+    def instance(self,obj):
+        return classic.message_queue(obj)
 
-            instance = classic.message_queue(obj)
-            instance.show(from_tty)
-        objects.information.invalidate()
\ No newline at end of file



More information about the vc mailing list