| 
     
      
      
      From: <ba...@us...> - 2007-05-23 05:03:32
      
     
   | 
Revision: 467
          http://svn.sourceforge.net/omc/?rev=467&view=rev
Author:   bartw
Date:     2007-05-22 22:03:33 -0700 (Tue, 22 May 2007)
Log Message:
-----------
moved python provider generator to cimprovider
Modified Paths:
--------------
    tools/trunk/yawn/yawn.py
Modified: tools/trunk/yawn/yawn.py
===================================================================
--- tools/trunk/yawn/yawn.py	2007-05-22 15:26:08 UTC (rev 466)
+++ tools/trunk/yawn/yawn.py	2007-05-23 05:03:33 UTC (rev 467)
@@ -1302,8 +1302,15 @@
     ht+= '<div align=center>View '+_makeHref(req, 'EnumInstanceNames', instUrlArgs, 'Instance Names')
     ht+= ' or '+_makeHref(req, 'EnumInstances', instUrlArgs, 'Instances')
     ht+= ' or '+_makeHref(req, 'AssociatedClasses', instUrlArgs, 'Associated Classes')
-    ht+= ' of this class.  '+_makeHref(req, 
-               'Provider', instUrlArgs, 'Python Provider')+'</div>'
+    ht+= ' of this class.'
+    try:
+        # see if code generator is available
+        from cimprovider import provider_template 
+        ht+= '  '+ _makeHref(req, 
+                   'Provider', instUrlArgs, 'Python Provider')
+    except ImportError:
+        pass
+    ht+= '</div>'
     ht+= '<table border="1" cellpadding="2">'
     if klass.qualifiers.has_key('aggregation'):
         titleBGColor = "green"
@@ -1945,305 +1952,13 @@
 ##############################################################################
 ##############################################################################
 ##############################################################################
-def _providerTemplate (cc):
-    #################
-    def format_desc (obj):
-        txt = ''
-        try:
-            raw = obj.qualifiers['description'].value
-            beg = 0
-            end = 0
-            while beg < len(raw):
-                beg = end
-                end += 65
-                while beg < len(raw) and raw[beg] in string.whitespace:
-                    beg = beg+1
-                while end < len(raw) and end > beg \
-                   and raw[end] not in string.whitespace:
-                    end = end-1
-                line = raw[beg:end]
-                line = line.replace('\n',' ')
-                txt +='''
-            %s''' % line
-        except KeyError:
-            pass
-        return txt
 
-    #################
-    def type_str (obj):
-        tx = obj.type
-        if tx == 'reference':
-            tx = 'REF %s (CIMInstanceName)' % obj.reference_class
-        if obj.is_array:
-            tx += '[]'
-        return tx
-    #################
-    def is_required (obj):
-        try:
-            if obj.qualifiers['required'].value:
-                return '(Required)'
-            else:
-                return ''
-        except KeyError:
-            return ''
-    #################
-
-    mappings = {'classname':cc.classname,
-                'classname_l':cc.classname.lower()}
-    isAssoc = 'association' in cc.qualifiers
-
-    code = '''"""Python Provider for %(classname)s
-
-Instruments the CIM class %(classname)s
-"""
-
-import pywbem
-from cimprovider import CIMProvider
-
-class %(classname)sProvider(CIMProvider):
-    """Instrument the CIM class %(classname)s 
-''' % mappings
-    code+= format_desc(cc)
-    code+= '''
-    """
-
-    #########################################################################
-    def __init__ (self):
-        pass
-
-    #########################################################################
-    def get_instance (self, env, model, cim_class):
-        """Return an instance of %(classname)s
-
-        Keyword arguments:
-        env -- Provider Environment
-        model -- A template of the CIMInstance to be returned.  The key 
-            properties are set on this instance to correspond to the 
-            instanceName that was requested.  The properties of the model
-            are already filtered according to the PropertyList from the 
-            request.
-        cim_class -- The CIMClass %(classname)s
-
-        """
-''' % mappings
-    keyProps = [p for p in cc.properties.values() \
-                if 'key' in p.qualifiers]
-    for prop in cc.properties.values():
-        if 'key' in prop.qualifiers:
-            continue
-        code+= '''
-        if '%(pname)s' in model.properties:
-            model['%(pname)s'] = # TODO (type = %(ptype)s) %(required)s''' \
-                % { 'pname': prop.name, 'ptype': type_str(prop),
-                    'required':is_required(prop) }
-
-    code+= '''
-        return model
-
-    #########################################################################
-    def enum_instances(self, env, model, cim_class, keys_only):
-        """ Enumerate instances of %(classname)s
-        The WBEM operations EnumerateInstances and EnumerateInstanceNames
-        are both mapped to this method. 
-        This method is a python generator
-
-        Keyword arguments:
-        env -- Provider Environment
-        model -- A template of the CIMInstances to be generated.  The 
-            properties of the model are already filtered according to the 
-            PropertyList from the request.
-        cim_class -- The CIMClass %(classname)s
-        keys_only -- A boolean.  True if only the key properties should be
-            set on the generated instances.
-
-        """
-
-        while False: # TODO more instances?
-            # TODO fetch system resource
-            # Key properties''' % mappings
-    for kp in keyProps:
-        code+='''    
-            model['%(pname)s'] = # TODO (type = %(ptype)s)''' \
-                % { 'pname':kp.name, 'ptype':type_str(kp) }
-    code+='''
-            if keys_only:
-                yield model
-            else:
-                try:
-                    yield self.get_instance(env, model, cim_class)
-                except pywbem.CIMError, (num, msg):
-                    if num not in (pywbem.CIM_ERR_NOT_FOUND, 
-                                   pywbem.CIM_ERR_ACCESS_DENIED):
-                        raise
-'''
-
-    code+='''
-    #########################################################################
-    def set_instance(self, env, instance, previous_instance, cim_class):
-        """ Return a newly created or modified instance of %(classname)s
-
-        Keyword arguments:
-        env -- Provider Environment
-        instance -- The new CIMInstance.  If modifying an existing instance,
-            the properties on this instance have been filtered by the 
-            PropertyList from the request.
-        previous_instance -- The previous instance if modifying an existing 
-            instance.  None if creating a new instance. 
-        cim_class -- The CIMClass %(classname)s
-
-        Return the new instance.  The keys must be set on the new instance. 
-
-        """
-
-        # TODO create or modify the instance
-        return instance
-
-    #########################################################################
-    def delete_instance(self, env, instance_name):
-        """ Delete an instance of %(classname)s
-
-        Keyword arguments:
-        env -- Provider Environment
-        instance_name -- A CIMInstanceName specifying the instance of 
-            %(classname)s to delete.
-
-        """ ''' % mappings
-                
-    for method in cc.methods.values():
-        inParms = [ p for p in method.parameters.values() if \
-                    'in' in p.qualifiers and p.qualifiers['in'].value ]
-        outParms = [ p for p in method.parameters.values() if \
-                    'out' in p.qualifiers and p.qualifiers['out'].value ]
-        code+= '''
-    #########################################################################
-    def cim_method_%s(self, env, object_name, method''' % method.name.lower()
-        for p in inParms:
-            code+= ''',
-                                  param_%s''' % p.name.lower()
-        code+= '''):
-        """Implements %s.%s()
-''' % (cc.classname, method.name)
-        code+= format_desc(method)
-
-        code+= '''
-        Keyword arguments:
-        env -- Provider Environment
-        object_name -- A CIMInstanceName or CIMCLassName specifying the 
-            object on which the method %(mname)s should be invoked
-        method -- A CIMMethod representing the method meta-data'''
-
-        for p in inParms:
-            code+= '''
-        param_%s --  The input parameter %s (type %s) %s''' \
-                    % (p.name.lower(), p.name, type_str(p), is_required(p))
-            code+= format_desc(p)
-
-        code+='''
-
-        Returns a two-tuple containing the return value (type %s)
-        and a dictionary with the out-parameters
-
-        Output parameters:''' %(method.return_type)
-
-        if not outParms:
-            code+= ' none'
-        else:
-            for p in outParms:
-                code+='''
-        %s -- (type %s) %s''' % (p.name, type_str(p), is_required(p))
-                code+= format_desc(p)
-
-
-        code+='''
-
-        """
-
-        # TODO do something
-        out_params = {}'''
-
-        for p in outParms:
-            code+='''
-        out_params['%s'] = # TODO (type %s)''' % (p.name.lower(), type_str(p))
-
-        code+='''
-        rval = # TODO (type %s)
-        return (rval, out_params)
-        ''' % method.return_type
-
-    if isAssoc:
-        code+= '''
-    #########################################################################
-    def references(self, env, object_name, model, assoc_class, 
-                   result_class_name, role, result_role):
-        """Instrument %(classname)s Associations.
-        All four association-related operations (Associators, AssociatorNames, 
-        References, ReferenceNames) are mapped to this method. 
-        This method is a python generator
-
-        Keyword arguments:
-        env -- Provider Environment
-        object_name -- A CIMInstanceName that defines the source CIM Object
-            whose associated Objects are to be returned.
-        model -- A template CIMInstance of %(classname)s to serve as a model
-            of the objects to be returned.  Only properties present on this
-            model need to be returned. 
-        assoc_class -- The CIMClass %(classname)s
-        result_class_name -- If not None, acts as a filter on the returned set 
-            of Objects by mandating that each returned Object MUST be either 
-            an Instance of this Class (or one of its subclasses) or be this 
-            Class (or one of its subclasses).
-        role -- If not None, acts as a filter on the returned set of Objects 
-            by mandating that each returned Object MUST be associated to the 
-            source Object via an Association in which the source Object plays 
-            the specified role (i.e. the name of the Property in the 
-            Association Class that refers to the source Object MUST match 
-            the value of this parameter).
-        result_role -- If not None, acts as a filter on the returned set of 
-            Objects by mandating that each returned Object MUST be associated 
-            to the source Object via an Association in which the returned 
-            Object plays the specified role (i.e. the name of the Property in 
-            the Association Class that refers to the returned Object MUST 
-            match the value of this parameter).
-        """
-
-''' % mappings
-
-    code+= '''
-
-## end of class %(classname)sProvider
-
-_%(classname_l)s_prov = %(classname)sProvider()          # initialize provider
-py_providers = {'%(classname)s': _%(classname_l)s_prov}   # register provider
-''' % mappings
-
-    ptypes = ['1', 'Instance']
-    if isAssoc:
-        ptypes[0]+= ',3'
-        ptypes[1]+= ', Associator'
-    if cc.methods:
-        ptypes[0]+= ',6'
-        ptypes[1]+= ', Method'
-    mof ='''
-// Provider registration for %(classname)s
-instance of OpenWBEM_PyProviderRegistration
-{
-        InstanceID = "<org:product:unique_id>"; // TODO
-        NamespaceNames = {"root/cimv2"}; 
-        ClassName = "%(classname)s"; 
-        ProviderTypes = {%(ptypeNums)s};  // %(ptypeStrs)s
-        ModulePath = "/some/path/%(classname)sProvider.py";  // TODO
-}; 
-''' % {'classname': cc.classname, 
-       'ptypeNums': ptypes[0], 
-       'ptypeStrs': ptypes[1]}
-                
-    return code, mof
-
 def Provider(req, url, ns, className):
     conn = _frontMatter(req, url, ns)
     klass = _ex(req, conn.GetClass, ClassName = className, LocalOnly = "false", 
                 IncludeClassOrigin = "true")
-    code, mof = _providerTemplate(klass)
+    from cimprovider import provider_template
+    code, mof = provider_template(klass)
     title = 'Python Provider for %s' % className
     ht = _printHead(title, req)
     ht+= '<font size=+1><b>%s</b></font>' % title
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |