From: <eo...@us...> - 2006-12-04 18:00:07
|
Revision: 1301 http://svn.sourceforge.net/pywebsvcs/?rev=1301&view=rev Author: eolson Date: 2006-12-04 10:00:03 -0800 (Mon, 04 Dec 2006) Log Message: ----------- Readd removed code that extends functionality of RegisterType(). RegisterGeneratedTypesWithMapping() Registers python classes so they can be serialized and parsed as "any". Uses _RegisterTypeWithSchemaAndClass() and _DynamicImport() functions. Modified Paths: -------------- trunk/zsi/ZSI/TC.py Modified: trunk/zsi/ZSI/TC.py =================================================================== --- trunk/zsi/ZSI/TC.py 2006-11-16 20:06:01 UTC (rev 1300) +++ trunk/zsi/ZSI/TC.py 2006-12-04 18:00:03 UTC (rev 1301) @@ -1789,7 +1789,67 @@ str(C) + ' duplicating serial registration for ' + str(t)) Any.serialmap[key] = instance +def _DynamicImport(moduleName, className): + ''' + Utility function for RegisterTypeWithSchemaAndClass + ''' + mod = __import__(moduleName) + components = moduleName.split('.') + for comp in components[1:]: + mod = getattr(mod, comp) + return getattr(mod, className) +def _RegisterTypeWithSchemaAndClass(importedSchemaTypes, schemaTypeName, classModuleName, className, generatedClassSuffix="_"): + ''' + Used by RegisterGeneratedTypesWithMapping. + Helps register classes so they can be serialized and parsed as "any". + Register a type by providing its schema and class. This allows + Any and AnyType to reconstruct objects made up of your own classes. + Note: The class module should be able to be imported (by being in your + pythonpath). Your classes __init__ functions shoud have default + arguments for all extra parameters. + Example of use: + import SchemaToPyTypeMap # Mapping written by you. Also used with wsdl2py -m + # mapping = {"SomeDescription":("Descriptions", "SomeDescription"), + # schemaTypeName : moduleName , className + # The module on the next line is generated by wsdl2py + from EchoServer_services_types import urn_ZSI_examples as ExampleTypes + + for key,value in SchemaToPyTypeMap.mapping.items(): + ZSI.TC.RegisterTypeWithSchemaAndClass(importedSchemaTypes = ExampleTypes, schemaTypeName=key, classModuleName=value[0], className=value[1]) + + ''' + # Doing this: (schemaTypeName="ExampleTypes", classModuleName="Description", + # className="SomeDescription") + # sd_instance = ExampleTypes.SomeDescription_(pname="SomeDescription") + # Any.serialmap[Descriptions.SomeDescription] = sd_instance + # Any.parsemap[(None,'SomeDescription')] = sd_instance + classDef = _DynamicImport(classModuleName, className) + interfaceDef = getattr(importedSchemaTypes, schemaTypeName + generatedClassSuffix) + + instance = interfaceDef(pname=className) + Any.serialmap[classDef] = instance + Any.parsemap[(None,schemaTypeName)] = instance + +def RegisterGeneratedTypesWithMapping(generatedTypes, mapping, generatedClassSuffix="_"): + """ + Registers python classes so they can be serialized and parsed as "any". + generatedTypes is a class containing typecode classes generated by zsi. + mapping is a dictionary that maps + {schemaTypeName : moduleName, className} + and is also used with wsdl2py -m + + Example of use: + import SchemaToPyTypeMap # See RegisterTypeWithSchemaAndClass for description + # The module on the next line is generated by wsdl2py and + # contains generated typecodes. + from EchoServer_services_types import urn_ZSI_examples as ExampleTypes + RegisterGeneratedTypesWithMapping(generatedTypes = ExampleTypes, mapping=SchemaToPyTypeMap.mapping) + """ + for key,value in mapping.items(): + _RegisterTypeWithSchemaAndClass(importedSchemaTypes = generatedTypes, schemaTypeName=key, classModuleName=value[0], className=value[1], generatedClassSuffix=generatedClassSuffix) + + from TCnumbers import * from TCtimes import * from schema import GTD, GED, WrapImmutable This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |