commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r5042 - trunk/gnue-common/src/datasources/drivers/appserver/Schema/Disco


From: johannes
Subject: r5042 - trunk/gnue-common/src/datasources/drivers/appserver/Schema/Discovery
Date: Tue, 27 Jan 2004 03:44:16 -0600 (CST)

Author: johannes
Date: 2004-01-27 03:44:15 -0600 (Tue, 27 Jan 2004)
New Revision: 5042

Modified:
   
trunk/gnue-common/src/datasources/drivers/appserver/Schema/Discovery/Introspection.py
Log:
Merged all depreciated methods into find (). findone () expects to be called 
with a fully qualified classname (module and class).


Modified: 
trunk/gnue-common/src/datasources/drivers/appserver/Schema/Discovery/Introspection.py
===================================================================
--- 
trunk/gnue-common/src/datasources/drivers/appserver/Schema/Discovery/Introspection.py
       2004-01-27 00:02:25 UTC (rev 5041)
+++ 
trunk/gnue-common/src/datasources/drivers/appserver/Schema/Discovery/Introspection.py
       2004-01-27 09:44:15 UTC (rev 5042)
@@ -25,7 +25,7 @@
 #
 # NOTES:
 #
-# $Id$
+# $Id:$
 
 __all__ = ['Introspection']
 
@@ -37,211 +37,162 @@
 from gnue.common.datasources import GIntrospection
 
 # =============================================================================
-# Introspection support for application server
+# Exceptions
 # =============================================================================
-class Introspection (GIntrospection.Introspection):
 
-  # list of the types of Schema objects this driver provides
-  types = [('object',_('Business Object Class'),1)]
+# -----------------------------------------------------------------------------
+# Base class for all Introspection exceptions
+# -----------------------------------------------------------------------------
+class EIntrospection (Exception):
+  def __init__ (self, text):
+    self._msg = text
+    Exception.__init__ (self, self._msg)
 
-  #
-  # TODO: This is a quick hack to get this class
-  # TODO: into the new-style schema format.
-  # TODO: getSchema* should be merged into find()
-  #
-  def find(self, name=None, type=None):
-    if name is None:
-      return self.getSchemaList (type)
-    else:
-      rs = self.getSchemaByName(name, type)
-      if rs:
-        return [rs]
-      else:
-        return None
+  def __repr__ (self):
+    return self._msg
 
+# -----------------------------------------------------------------------------
+# Requested module does not exist in class repository
+# -----------------------------------------------------------------------------
+class EModuleNotFound (EIntrospection):
+  def __init__ (self, module):
+    msg = "Introspection: Module '%s' not found." % module
+    EIntrospection.__init__ (self, msg)
 
-  # -------------------------------------------------------------------------
-  # Get a dictionary with all modules available
-  # -------------------------------------------------------------------------
-  def _getModuleDict (self):
-    res = {}
 
-    try:
-      cursor = self._connection.request ('gnue_module', [], ['gnue_id'], 
-                                         ['gnue_id', 'gnue_name'])
-      try:
-        data = [1]
-        while len (data):
-          data = cursor.fetch ()
 
-          for module in data:
-            res [module ['gnue_id']] = module ['gnue_name']
+# =============================================================================
+# Introspection support for application server
+# =============================================================================
+class Introspection (GIntrospection.Introspection):
 
-      finally:
-        cursor.close ()
+  # list of the types of Schema objects this driver provides
+  types = [('object',_('Business Object Class'),1)]
 
-    except Exception, msg:
-      print "Error %s" % msg
-      GDebug.printMesg (1, _("Error creating introspection module list\n\n " +
-                             "--- %s ---") % msg)
-      raise
 
-    return res
-
   # -------------------------------------------------------------------------
-  # Get a list of all business classes
+  # Get a list of schema objects for name (and type)
   # -------------------------------------------------------------------------
-  def getSchemaList (self, type = None):
-    if not (type in ('object', None)):
-      return []
-
-    self._modules = self._getModuleDict ()
+  def find (self, name = None, type = None):
+    self._modules = self._getModuleDictionary ()
     self._classes = {}
+    result        = []
 
-    # First we build a module dictionary
+    if name is None:
+      if not type in ['object', None]:
+        return []
+
+      cond = []
+    else:
+      cond = self._getNameCondition (name)
+
+    fields = ["gnue_module", "gnue_name"]
+    cursor = self._connection.request ("gnue_class", cond, fields, fields)
+
     try:
-      cursor = self._connection.request ('gnue_module', [], ['gnue_id'], 
-                                         ['gnue_id', 'gnue_name'])
       data = [1]
+
       while len (data):
         data = cursor.fetch ()
 
-        for module in data:
-          self._modules [module ['gnue_id']] = module ['gnue_name']
+        for classdef in data:
+          classid = classdef ["gnue_id"]
+          modname = self._modules [classdef ["gnue_module"]]
 
+          self._classes [classid] = "%s_%s" % (modname, classdef ["gnue_name"])
 
+      for classid in self._classes.keys ():
+        attrs = {"id"     : string.lower (self._classes [classid]),
+                 "name"   : self._classes [classid],
+                 "type"   : "object",
+                 "gnue_id": classid}
+      
+        result.append (GIntrospection.Schema (attrs, 
+                                  getChildSchema = self._getChildSchema))
+
+    finally:
       cursor.close ()
 
-    except Exception, msg:
-      print "Error %s" % msg
-      GDebug.printMesg (1, _("Error creating introspection module list\n\n " +
-                             "--- %s ---") % msg)
-      return []
+    if len (result):
+      return result
+    else:
+      return None
 
-    # Next we build a class dictionary
+  # -------------------------------------------------------------------------
+  # Create a dictionary with all available modules
+  # -------------------------------------------------------------------------
+  def _getModuleDictionary (self):
+    res    = {}
+    cursor = self._connection.request ('gnue_module', [], ['gnue_id'], 
+                                       ['gnue_id', 'gnue_name'])
     try:
-      cursor = self._connection.request ('gnue_class', [], ['gnue_module'],
-                                         ['gnue_name', 'gnue_module'])
-
       data = [1]
-
       while len (data):
         data = cursor.fetch ()
 
-        for classdef in data:
-          classid = classdef ['gnue_id']
-          modname = self._modules [classdef ['gnue_module']]
+        for module in data:
+          res [module ['gnue_id']] = module ['gnue_name']
 
-          self._classes [classid] = modname + '_' + classdef ['gnue_name']
-
+    finally:
       cursor.close ()
 
-      # Now we build the result
-      res = []
-      classes = self._classes.keys ()
-      classes.sort ()
-
-      for key in classes:
-        name  = self._classes [key]
-        attrs = {'id'     : string.lower (name),
-                 'name'   : name,
-                 'type'   : 'object',
-                 'gnue_id': key}
-      
-        res.append (GIntrospection.Schema (attrs, 
-                                  getChildSchema = self.__getChildSchema))
-
-    except Exception, msg:
-      print "Error %s" % msg
-      GDebug.printMesg (1, _("Error creating introspection class list\n\n " +
-                             "--- %s ---") % msg)
-      return []
-
     return res
 
-  
   # -------------------------------------------------------------------------
-  # Get a schema for a single business class
-  # FIXME: 'gnue_name' is not a fully qualified identifier in the
-  #        classrepository. There might be more modules introducing the same 
-  #        classname.
+  # Create a condition sequence for a given classname
   # -------------------------------------------------------------------------
-  def getSchemaByName (self, name, type = None): 
-    res = None
-    self._modules = self._getModuleDict ()
+  def _getNameCondition (self, name):
+    (module, classname) = string.split (name, '_')
 
-    try:
-      cursor = self._connection.request ('gnue_class',
-          [["eq", ""], ["field", "gnue_name"], ["const", name]],
-          ['gnue_id'],
-          ['gnue_module', 'gnue_name'])
+    if not module in self._modules.values ():
+      raise EModuleNotFound (module)
 
-      try:
-        data = cursor.fetch ()
-        if len (data):
-          cdef = data [0]
+    # Find the Module-Id
+    for moduleId in self._modules.keys ():
+      if self._modules [moduleId] == module:
+        break
 
-          name  = "%s_%s" % (self._modules [cdef ['gnue_module']], 
-                             cdef ['gnue_name'])
+    return [["eq", ""], ["field", "gnue_module"], ["const", moduleId],
+            ["eq", ""], ["field", "gnue_name"]  , ["const", classname]]
 
-          attrs = {'id'     : string.lower (name),
-                   'name'   : name,
-                   'type'   : 'object',
-                   'gnue_id': cdef ['gnue_id']}
-      
-          res = GIntrospection.Schema (attrs, 
-                                       getChildSchema = self.__getChildSchema)
-      finally:
-        cursor.close ()
-
-    except:
-      raise
-
-    return res
-
   # -------------------------------------------------------------------------
   # Get a list of all properties owned by a given class
   # -------------------------------------------------------------------------
-  def __getChildSchema(self, parent):
-    res = []
-    self._modules = self._getModuleDict ()
+  def _getChildSchema (self, parent):
+    result = []
+    self._modules = self._getModuleDictionary ()
 
-    try:
-      cursor = self._connection.request ('gnue_property', 
+    cursor = self._connection.request ("gnue_property", 
           [["eq", ""], ["field", "gnue_class"], ["const", parent.gnue_id]],
-          ['gnue_id'], 
-          ['gnue_module', 'gnue_name', 'gnue_type', 'gnue_length',
-          'gnue_scale'])
-      try:
-        data = [1]
-        while len (data):
-          data = cursor.fetch ()
+          ["gnue_module", "gnue_name"], 
+          ["gnue_module", "gnue_name", "gnue_type", "gnue_length",
+           "gnue_scale"])
+    try:
+      data = [1]
+      while len (data):
+        data = cursor.fetch ()
 
-          for property in data:
-            name  = "%s_%s" % (self._modules [property ['gnue_module']],
-                               string.lower (property ['gnue_name']))
+        for property in data:
+          name  = "%s_%s" % (self._modules [property ['gnue_module']],
+                             string.lower (property ['gnue_name']))
 
-            # TODO: check if scale and precision is the same
-            # TODO: classrepository doesn't define 'required'
-            attrs = { 'id'        : "%s.%s" % (parent.id, name),
-                      'name'      : name,
-                      'datatype'  : property ['gnue_type'],
-                      'precision' : property ['gnue_scale'] ,
-                      'nativetype': 'unknown',
-                      'required'  : 0}
+          # TODO: check if scale and precision is the same
+          attrs = {'id'        : "%s.%s" % (parent.id, name),
+                   'name'      : name,
+                   'datatype'  : property ['gnue_type'],
+                   'precision' : property ['gnue_scale'] ,
+                   'nativetype': 'unknown',
+                   'required'  : 0}
 
-            if property ['gnue_type'] in ['boolean', 'date', 'datetime', 
-                                          'number', 'string', 'time']:
-              attrs ['length'] = property ['gnue_length']
-            else:
-              attrs ['length'] = 32
+          if property ['gnue_type'] in ['boolean', 'date', 'datetime', 
+                                        'number', 'string', 'time']:
+            attrs ['length'] = property ['gnue_length']
+          else:
+            attrs ['length'] = 32
 
-            res.append (GIntrospection.Schema (attrs))
+          result.append (GIntrospection.Schema (attrs))
 
-      finally:
-        cursor.close ()
+    finally:
+      cursor.close ()
 
-    except:
-      raise
-
-    return res
+    return result





reply via email to

[Prev in Thread] Current Thread [Next in Thread]