|
From: <ba...@us...> - 2007-05-12 00:26:13
|
Revision: 457
http://svn.sourceforge.net/omc/?rev=457&view=rev
Author: bartw
Date: 2007-05-11 17:26:15 -0700 (Fri, 11 May 2007)
Log Message:
-----------
Added ability to generate python provider stubs for CIM classes
Modified Paths:
--------------
tools/trunk/yawn/yawn.py
Modified: tools/trunk/yawn/yawn.py
===================================================================
--- tools/trunk/yawn/yawn.py 2007-05-09 22:56:36 UTC (rev 456)
+++ tools/trunk/yawn/yawn.py 2007-05-12 00:26:15 UTC (rev 457)
@@ -1273,7 +1273,8 @@
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.</div>'
+ ht+= ' of this class. '+_makeHref(req,
+ 'ProviderTemplate', instUrlArgs, 'Provider Template')+'</div>'
ht+= '<table border="1" cellpadding="2">'
if klass.qualifiers.has_key('aggregation'):
titleBGColor = "green"
@@ -1657,6 +1658,7 @@
return '</table></body></html>'
##############################################################################
+##############################################################################
def AssociatedClasses(req, url, ns, className):
conn = _frontMatter(req, url, ns)
classNames = None
@@ -1903,6 +1905,314 @@
return ht
##############################################################################
+##############################################################################
+##############################################################################
+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,
+ }
+ isAssoc = 'association' in cc.qualifiers
+
+ code = '''"""Python Provider for %(classname)s
+
+Instruments the CIM class %(classname)s
+"""
+
+import pywbem
+
+class %(classname)sProvider:
+ """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 in (pywbem.CIM_ERR_NOT_FOUND,
+ pywbem.CIM_ERR_ACCESS_DENIED):
+ pass # EnumerateInstances shouldn't return these
+ else:
+ 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
+
+_prov = %(classname)sProvider() # initialize provider
+py_providers = {'%(classname)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'
+ code+='''
+"""// Provider registration MOF 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";
+};
+"""
+''' % {'classname': cc.classname,
+ 'ptypeNums': ptypes[0],
+ 'ptypeStrs': ptypes[1]}
+
+ return code
+
+def ProviderTemplate(req, url, ns, className):
+ conn = _frontMatter(req, url, ns)
+ req.content_type = 'text/plain'
+ klass = _ex(req, conn.GetClass, ClassName = className, LocalOnly = "false",
+ IncludeClassOrigin = "true")
+ return _providerTemplate(klass)
+
+
+##############################################################################
+##############################################################################
# TODO
"""
- Make GetClass links on REF type fields for properties and parameters.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|