home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / ZSI / generate / wsdl2python.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  14.5 KB  |  423 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import os
  5. import sys
  6. import warnings
  7. from ZSI import _get_idstr
  8. from ZSI.wstools.logging import getLogger as _GetLogger
  9. from ZSI.wstools import WSDLTools
  10. from ZSI.wstools.WSDLTools import SoapAddressBinding, SoapBodyBinding, SoapBinding, MimeContentBinding, HttpUrlEncodedBinding
  11. from ZSI.wstools.XMLSchema import SchemaReader, ElementDeclaration, SchemaError
  12. from ZSI.typeinterpreter import BaseTypeInterpreter
  13. from ZSI.generate import WsdlGeneratorError, Wsdl2PythonError
  14. from containers import *
  15. from ZSI.generate import utility
  16. from ZSI.generate.utility import NamespaceAliasDict as NAD
  17. from ZSI.generate.utility import GetModuleBaseNameFromWSDL
  18.  
  19. class WriteServiceModule:
  20.     client_module_suffix = '_client'
  21.     messages_module_suffix = '_messages'
  22.     types_module_suffix = '_types'
  23.     logger = _GetLogger('WriteServiceModule')
  24.     
  25.     def __init__(self, wsdl, addressing = False, notification = False, do_extended = False, extPyClasses = None, configParser = None):
  26.         self._wsdl = wsdl
  27.         self._addressing = addressing
  28.         self._notification = notification
  29.         self._configParser = configParser
  30.         self.usedNamespaces = None
  31.         self.services = []
  32.         self.client_module_path = None
  33.         self.types_module_name = None
  34.         self.types_module_path = None
  35.         self.messages_module_path = None
  36.         self.do_extended = do_extended
  37.         self.extPyClasses = extPyClasses
  38.  
  39.     
  40.     def getClientModuleName(self):
  41.         name = GetModuleBaseNameFromWSDL(self._wsdl)
  42.         if not name:
  43.             raise WsdlGeneratorError, 'could not determine a service name'
  44.         name
  45.         if self.client_module_suffix is None:
  46.             return name
  47.         return '%s%s' % (name, self.client_module_suffix)
  48.  
  49.     
  50.     def setTypesModuleName(self, name):
  51.         self.types_module_name = name
  52.  
  53.     
  54.     def getTypesModuleName(self):
  55.         if self.types_module_name is not None:
  56.             return self.types_module_name
  57.         name = GetModuleBaseNameFromWSDL(self._wsdl)
  58.         if not name:
  59.             raise WsdlGeneratorError, 'could not determine a service name'
  60.         name
  61.         if self.types_module_suffix is None:
  62.             return name
  63.         return '%s%s' % (name, self.types_module_suffix)
  64.  
  65.     
  66.     def setClientModulePath(self, path):
  67.         self.client_module_path = path
  68.  
  69.     
  70.     def getTypesModulePath(self):
  71.         return self.types_module_path
  72.  
  73.     
  74.     def setTypesModulePath(self, path):
  75.         self.types_module_path = path
  76.  
  77.     
  78.     def gatherNamespaces(self):
  79.         if self.usedNamespaces is not None:
  80.             return None
  81.         self.logger.debug('gatherNamespaces')
  82.         self.usedNamespaces = { }
  83.         
  84.         def recommended_ns(s):
  85.             tns = s.getTargetNamespace()
  86.             recommended = None
  87.             for k, v in s.attributes.get('xmlns', { }).items():
  88.                 if k == '':
  89.                     continue
  90.                 
  91.                 if v == tns:
  92.                     recommended = k
  93.                     break
  94.                     continue
  95.             
  96.             return recommended
  97.  
  98.         for schema in self._wsdl.types.values():
  99.             tns = schema.getTargetNamespace()
  100.             self.logger.debug('Register schema(%s) -- TNS(%s)' % (_get_idstr(schema), tns))
  101.             if self.usedNamespaces.has_key(tns) is False:
  102.                 self.usedNamespaces[tns] = []
  103.             
  104.             self.usedNamespaces[tns].append(schema)
  105.             NAD.add(tns, recommended_ns(schema))
  106.         
  107.         for k, v in SchemaReader.namespaceToSchema.items():
  108.             self.logger.debug('Register schema(%s) -- TNS(%s)' % (_get_idstr(v), k))
  109.             if self.usedNamespaces.has_key(k) is False:
  110.                 self.usedNamespaces[k] = []
  111.             
  112.             self.usedNamespaces[k].append(v)
  113.             NAD.add(k, recommended_ns(v))
  114.         
  115.  
  116.     
  117.     def writeClient(self, fd, sdClass = None, **kw):
  118.         if not sdClass:
  119.             pass
  120.         sdClass = ServiceDescription
  121.         print >>fd, '#' * 50
  122.         print >>fd, '# file: %s.py' % self.getClientModuleName()
  123.         print >>fd, '# '
  124.         print >>fd, '# client stubs generated by "%s"' % self.__class__
  125.         print >>fd, '#     %s' % ' '.join(sys.argv)
  126.         print >>fd, '# '
  127.         print >>fd, '#' * 50
  128.         self.services = []
  129.         for service in self._wsdl.services:
  130.             sd = sdClass(self._addressing, do_extended = self.do_extended, wsdl = self._wsdl)
  131.             if len(self._wsdl.types) > 0:
  132.                 sd.setTypesModuleName(self.getTypesModuleName(), self.getTypesModulePath())
  133.             
  134.             self.gatherNamespaces()
  135.             sd.fromWsdl(service, **kw)
  136.             sd.write(fd)
  137.             self.services.append(sd)
  138.         
  139.  
  140.     
  141.     def writeTypes(self, fd):
  142.         print >>fd, '#' * 50
  143.         print >>fd, '# file: %s.py' % self.getTypesModuleName()
  144.         print >>fd, '#'
  145.         print >>fd, '# schema types generated by "%s"' % self.__class__
  146.         print >>fd, '#    %s' % ' '.join(sys.argv)
  147.         print >>fd, '#'
  148.         print >>fd, '#' * 50
  149.         print >>fd, TypesHeaderContainer()
  150.         self.gatherNamespaces()
  151.         for l in self.usedNamespaces.values():
  152.             sd = SchemaDescription(do_extended = self.do_extended, extPyClasses = self.extPyClasses)
  153.             for schema in l:
  154.                 sd.fromSchema(schema)
  155.             
  156.             sd.write(fd)
  157.         
  158.  
  159.  
  160.  
  161. class ServiceDescription:
  162.     separate_messages = False
  163.     logger = _GetLogger('ServiceDescription')
  164.     
  165.     def __init__(self, addressing = False, do_extended = False, wsdl = None):
  166.         self.typesModuleName = None
  167.         self.messagesModuleName = None
  168.         self.wsAddressing = addressing
  169.         self.imports = ServiceHeaderContainer()
  170.         self.messagesImports = ServiceHeaderContainer()
  171.         self.locator = ServiceLocatorContainer()
  172.         self.bindings = []
  173.         self.messages = []
  174.         self.do_extended = do_extended
  175.         self._wsdl = wsdl
  176.  
  177.     
  178.     def setTypesModuleName(self, name, modulePath = None):
  179.         self.typesModuleName = '%s' % name
  180.         if modulePath is not None:
  181.             self.typesModuleName = '%s.%s' % (modulePath, name)
  182.         
  183.  
  184.     
  185.     def fromWsdl(self, service, **kw):
  186.         self.imports.setTypesModuleName(self.typesModuleName)
  187.         self.imports.appendImport(kw.get('imports', []))
  188.         self.locator.setUp(service)
  189.         
  190.         try:
  191.             bindings = map((lambda p: p.binding), service.ports)
  192.         except:
  193.             warnings.warn('not all ports have binding declared,')
  194.             bindings = ()
  195.  
  196.         for port in service.ports:
  197.             if port.binding not in bindings:
  198.                 continue
  199.             
  200.             while port.binding in bindings:
  201.                 bindings.remove(port.binding)
  202.             desc = BindingDescription(useWSA = self.wsAddressing, do_extended = self.do_extended, wsdl = self._wsdl)
  203.             
  204.             try:
  205.                 desc.setUp(port.getBinding())
  206.             except Wsdl2PythonError:
  207.                 ex = None
  208.                 self.logger.warning('Skipping port(%s)' % port.name)
  209.                 if len(ex.args):
  210.                     self.logger.warning(ex.args[0])
  211.                     continue
  212.                 continue
  213.  
  214.             desc.setReaderClass(kw.get('readerclass'))
  215.             desc.setWriterClass(kw.get('writerclass'))
  216.             for soc in desc.operations:
  217.                 if soc.hasInput() is True:
  218.                     mw = MessageWriter(do_extended = self.do_extended)
  219.                     mw.setUp(soc, port, input = True)
  220.                     self.messages.append(mw)
  221.                     if soc.hasOutput() is True:
  222.                         mw = MessageWriter(do_extended = self.do_extended)
  223.                         mw.setUp(soc, port, input = False)
  224.                         self.messages.append(mw)
  225.                     
  226.                 soc.hasOutput() is True
  227.             
  228.             self.bindings.append(desc)
  229.         
  230.  
  231.     
  232.     def write(self, fd, msg_fd = None):
  233.         print >>fd, self.imports
  234.         print >>fd, self.locator
  235.         for m in self.bindings:
  236.             print >>fd, m
  237.         
  238.         for m in self.messages:
  239.             print >>fd, m
  240.         
  241.  
  242.  
  243.  
  244. class MessageWriter:
  245.     logger = _GetLogger('MessageWriter')
  246.     
  247.     def __init__(self, do_extended = False):
  248.         self.content = None
  249.         self.do_extended = do_extended
  250.  
  251.     
  252.     def __str__(self):
  253.         if not self.content:
  254.             raise Wsdl2PythonError, 'Must call setUp.'
  255.         self.content
  256.         return self.content.getvalue()
  257.  
  258.     
  259.     def setUp(self, soc, port, input = False):
  260.         rpc = soc.isRPC()
  261.         literal = soc.isLiteral(input)
  262.         kw = { }
  263.         klass = None
  264.         if rpc and literal:
  265.             klass = ServiceRPCLiteralMessageContainer
  266.         elif not rpc and literal:
  267.             kw['do_extended'] = self.do_extended
  268.             klass = ServiceDocumentLiteralMessageContainer
  269.         elif rpc and not literal:
  270.             klass = ServiceRPCEncodedMessageContainer
  271.         else:
  272.             raise WsdlGeneratorError, 'doc/enc not supported.'
  273.         self.content = not literal(**kw)
  274.         self.content.setUp(port, soc, input)
  275.  
  276.  
  277.  
  278. class SchemaDescription:
  279.     logger = _GetLogger('SchemaDescription')
  280.     
  281.     def __init__(self, do_extended = False, extPyClasses = None):
  282.         self.classHead = NamespaceClassHeaderContainer()
  283.         self.classFoot = NamespaceClassFooterContainer()
  284.         self.items = []
  285.         self._SchemaDescription__types = []
  286.         self._SchemaDescription__elements = []
  287.         self.targetNamespace = None
  288.         self.do_extended = do_extended
  289.         self.extPyClasses = extPyClasses
  290.  
  291.     
  292.     def fromSchema(self, schema):
  293.         ns = schema.getTargetNamespace()
  294.         if self.targetNamespace is None:
  295.             self.targetNamespace = ns
  296.         
  297.         self.classHead.ns = self.classFoot.ns = ns
  298.         for t in schema.types:
  299.             if t.getAttributeName() not in self._SchemaDescription__types:
  300.                 continue
  301.             _[1][t]
  302.         
  303.         for e in schema.elements:
  304.             if e.getAttributeName() not in self._SchemaDescription__elements:
  305.                 continue
  306.             _[2][e]
  307.         
  308.  
  309.     
  310.     def getTypes(self):
  311.         return self._SchemaDescription__types
  312.  
  313.     
  314.     def getElements(self):
  315.         return self._SchemaDescription__elements
  316.  
  317.     
  318.     def write(self, fd):
  319.         print >>fd, self.classHead
  320.         for t in self.items:
  321.             print >>fd, t
  322.         
  323.         print >>fd, self.classFoot
  324.  
  325.  
  326.  
  327. class SchemaItemWriter:
  328.     logger = _GetLogger('SchemaItemWriter')
  329.     
  330.     def __init__(self, do_extended = False, extPyClasses = None):
  331.         self.content = None
  332.         self.do_extended = do_extended
  333.         self.extPyClasses = extPyClasses
  334.  
  335.     
  336.     def __str__(self):
  337.         return str(self.content)
  338.  
  339.     
  340.     def fromSchemaItem(self, item):
  341.         raise NotImplementedError, ''
  342.  
  343.  
  344.  
  345. class ElementWriter(SchemaItemWriter):
  346.     logger = _GetLogger('ElementWriter')
  347.     
  348.     def fromSchemaItem(self, item):
  349.         if item.isElement() is False or item.isLocal() is True:
  350.             raise TypeError, 'expecting global element declaration: %s' % item.getItemTrace()
  351.         item.isLocal() is True
  352.         local = False
  353.         qName = item.getAttribute('type')
  354.         if not qName:
  355.             etp = item.content
  356.             local = True
  357.         else:
  358.             etp = item.getTypeDefinition('type')
  359.         if etp is None:
  360.             if local is True:
  361.                 self.content = ElementLocalComplexTypeContainer(do_extended = self.do_extended)
  362.             else:
  363.                 self.content = ElementSimpleTypeContainer()
  364.         elif etp.isLocal() is False:
  365.             self.content = ElementGlobalDefContainer()
  366.         elif etp.isSimple() is True:
  367.             self.content = ElementLocalSimpleTypeContainer()
  368.         elif etp.isComplex():
  369.             self.content = ElementLocalComplexTypeContainer(do_extended = self.do_extended)
  370.         else:
  371.             raise Wsdl2PythonError, 'Unknown element declaration: %s' % item.getItemTrace()
  372.         (etp is None).logger.debug('ElementWriter setUp container "%r", Schema Item "%s"' % (self.content, item.getItemTrace()))
  373.         self.content.setUp(item)
  374.  
  375.  
  376.  
  377. class TypeWriter(SchemaItemWriter):
  378.     logger = _GetLogger('TypeWriter')
  379.     
  380.     def fromSchemaItem(self, item):
  381.         if item.isDefinition() is False or item.isLocal() is True:
  382.             raise TypeError, 'expecting global type definition not: %s' % item.getItemTrace()
  383.         item.isLocal() is True
  384.         self.content = None
  385.         if item.isSimple():
  386.             if item.content.isRestriction():
  387.                 self.content = RestrictionContainer()
  388.             elif item.content.isUnion():
  389.                 self.content = UnionContainer()
  390.             elif item.content.isList():
  391.                 self.content = ListContainer()
  392.             else:
  393.                 raise Wsdl2PythonError, 'unknown simple type definition: %s' % item.getItemTrace()
  394.             item.content.isRestriction().content.setUp(item)
  395.             return None
  396.         if item.isComplex():
  397.             kw = { }
  398.             if item.content is None or item.content.isModelGroup():
  399.                 self.content = ComplexTypeContainer(do_extended = self.do_extended, extPyClasses = self.extPyClasses)
  400.                 kw['empty'] = item.content is None
  401.             elif item.content.isSimple():
  402.                 self.content = ComplexTypeSimpleContentContainer()
  403.             elif item.content.isComplex():
  404.                 self.content = ComplexTypeComplexContentContainer(do_extended = self.do_extended)
  405.             else:
  406.                 raise Wsdl2PythonError, 'unknown complex type definition: %s' % item.getItemTrace()
  407.             item.isSimple().logger.debug('TypeWriter setUp container "%r", Schema Item "%s"' % (self.content, item.getItemTrace()))
  408.             
  409.             try:
  410.                 self.content.setUp(item, **kw)
  411.             except Exception:
  412.                 ex = None
  413.                 args = [
  414.                     'Failure in setUp: %s' % item.getItemTrace()]
  415.                 args += ex.args
  416.                 ex.args = tuple(args)
  417.                 raise 
  418.  
  419.             return None
  420.         raise TypeError, 'expecting SimpleType or ComplexType: %s' % item.getItemTrace()
  421.  
  422.  
  423.