[Opensrf-commits] r1062 - trunk/src/python/osrf

svn at svn.open-ils.org svn at svn.open-ils.org
Sat Jul 28 11:33:43 EDT 2007


Author: erickson
Date: 2007-07-28 11:32:18 -0400 (Sat, 28 Jul 2007)
New Revision: 1062

Modified:
   trunk/src/python/osrf/net_obj.py
Log:
re-factored and cleaned up the registered class base-class object

Modified: trunk/src/python/osrf/net_obj.py
===================================================================
--- trunk/src/python/osrf/net_obj.py	2007-07-26 20:31:41 UTC (rev 1061)
+++ trunk/src/python/osrf/net_obj.py	2007-07-28 15:32:18 UTC (rev 1062)
@@ -2,20 +2,11 @@
 from xml.sax import saxutils
 
 
-class osrfNetworkObject(object):
-    ''' Base class for all network serializable objects '''
-    pass
+# -----------------------------------------------------------
+# Define the global network-class registry
+# -----------------------------------------------------------
 
-def osrfNewObjectFromHint(hint):
-    try:
-        obj = None
-        exec('obj = osrfNetworkObject.%s()' % hint)
-        return obj
-    except AttributeError:
-        return osrfNetworkObject.__unknown()
-
-
-''' Global object registry '''
+# Global object registry 
 objectRegistry = {}
 
 class osrfNetworkRegistry(object):
@@ -37,59 +28,79 @@
     getRegistry = staticmethod(getRegistry)
 
 
-def __makeNetworkAccessor(cls, key):
-    '''  Creates and accessor/mutator method for the given class.  
+# -----------------------------------------------------------
+# Define the base class for all network-serializable objects
+# -----------------------------------------------------------
 
-        'key' is the name the method will have and represents
-        the field on the object whose data we are accessing
-        ''' 
-    def accessor(self, *args):
-        if len(args) != 0:
-            self.__data[key] = args[0]
-        return self.__data.get(key)
-    setattr(cls, key, accessor)
+class osrfNetworkObject(object):
+    ''' Base class for all network serializable objects '''
 
+    # link to our registry object for this registered class
+    registry = None
 
+    def __init__(self, data=None):
+        ''' If this is an array, we pull data out of the data array
+            (if there is any) and translate that into a hash internally '''
 
-def __makeGetRegistry(cls, registry):
-    ''' Wraps the registry for this class inside an accessor method '''
-    def get(self):
-        return registry
-    setattr(cls, 'getRegistry', get)
+        self._data = data
+        if not data: self._data = {}
+        if isinstance(data, list):
+            self.importArrayData(list)
 
-def __makeGetData(cls):
-    ''' Wraps the stored data in an accessor method '''
-    def get(self):
-        return self.__data
-    setattr(cls, 'getData', get)
+    def importArrayData(self, data):
+        ''' If an array-based object is created with an array
+            of data, cycle through and load the data '''
 
-def __makeSetField(cls):
-    ''' Creates a generic mutator for fields by fieldname '''
-    def set(self, field, value):
-        self.__data[field] = value
-    setattr(cls, 'setField', set)
-        
+        self._data = {}
+        if len(data) == 0: return
 
-def __osrfNetworkObjectInit(self, data=None):
-    ''' __init__ method for osrNetworkObjects.
-        If this is an array, we pull data out of the data array
-        (if there is any) and translate that into a hash internally
-        '''
-    self.__data = data
-    if not data: self.__data = {}
+        reg = self.getRegistry()
+        if reg.wireProtocol == 'array':
+            for i in range(len(reg.keys)):
+                if len(data) > i: break
+                self.setField(reg.keys[i], data[i])
 
-    if isinstance(data, list):
-        self.__data = {}
-        if len(data) > 0:
-            reg = self.getRegistry()
-            if reg.wireProtocol == 'array':
-                for i in range(len(reg.keys)):
-                    try:
-                        self.__data[reg.keys[i]] = data[i]
-                    except:
-                        self.__data[reg.keys[i]] = None
+    def getData(self):
+        ''' Returns the full dataset for this object as a dict '''
+        return self._data
 
+    def setField(self, field, value):
+        self._data[field] = value
 
+    def getField(self, field):
+        return self._data.get(field)
+
+    def getRegistry(cls):
+        ''' Returns the registry object for this registered class '''
+        return cls.registry
+    getRegistry = classmethod(getRegistry)
+
+
+def osrfNewObjectFromHint(hint):
+    ''' Given a hint, this will create a new object of that 
+        type and return it.  If this hint is not registered,
+        an object of type osrfNetworkObject.__unknown is returned'''
+    try:
+        obj = None
+        exec('obj = osrfNetworkObject.%s()' % hint)
+        return obj
+    except AttributeError:
+        return osrfNetworkObject.__unknown()
+
+
+
+
+def __makeNetworkAccessor(cls, key):
+    ''' Creates and accessor/mutator method for the given class.  
+        'key' is the name the method will have and represents
+        the field on the object whose data we are accessing ''' 
+    def accessor(self, *args):
+        if len(args) != 0:
+            self.setField(key, args[0])
+        return self.getField(key)
+    setattr(cls, key, accessor)
+
+
 def osrfNetworkRegisterHint(hint, keys, type='hash'):
     ''' Registers a new network-serializable object class.
 
@@ -114,16 +125,10 @@
     for k in keys:
         __makeNetworkAccessor(cls, k)
 
-    # assign our custom init function
-    setattr(cls, '__init__', __osrfNetworkObjectInit)
-    __makeGetRegistry(cls, registry)
-    __makeGetData(cls)
-    __makeSetField(cls)
-
-
     # attach our new class to the osrfNetworkObject 
     # class so others can access it
     setattr(osrfNetworkObject, hint , cls)
+    cls.registry = registry
 
 
 



More information about the opensrf-commits mailing list