home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / ZSI / generate / commands.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  13.8 KB  |  405 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import exceptions
  5. import sys
  6. import optparse
  7. import os
  8. import warnings
  9. import traceback
  10. from os.path import isfile, join, split
  11. import ZSI
  12. from ConfigParser import ConfigParser
  13. from ZSI.generate.wsdl2python import WriteServiceModule, ServiceDescription as wsdl2pyServiceDescription
  14. from ZSI.wstools import WSDLTools, XMLSchema
  15. from ZSI.wstools.logging import setBasicLoggerDEBUG
  16. from ZSI.generate import containers, utility
  17. from ZSI.generate.utility import NCName_to_ClassName as NC_to_CN, TextProtect
  18. from ZSI.generate.wsdl2dispatch import ServiceModuleWriter as ServiceDescription
  19. from ZSI.generate.wsdl2dispatch import WSAServiceModuleWriter as ServiceDescriptionWSA
  20. warnings.filterwarnings('ignore', '', exceptions.UserWarning)
  21.  
  22. def SetDebugCallback(option, opt, value, parser, *args, **kwargs):
  23.     setBasicLoggerDEBUG()
  24.     warnings.resetwarnings()
  25.  
  26.  
  27. def SetPyclassMetaclass(option, opt, value, parser, *args, **kwargs):
  28.     ServiceHeaderContainer = ServiceHeaderContainer
  29.     TypecodeContainerBase = TypecodeContainerBase
  30.     TypesHeaderContainer = TypesHeaderContainer
  31.     import ZSI.generate.containers
  32.     TypecodeContainerBase.metaclass = kwargs['metaclass']
  33.     TypesHeaderContainer.imports.append('from %(module)s import %(metaclass)s' % kwargs)
  34.     ServiceHeaderContainer.imports.append('from %(module)s import %(metaclass)s' % kwargs)
  35.  
  36.  
  37. def SetUpLazyEvaluation(option, opt, value, parser, *args, **kwargs):
  38.     TypecodeContainerBase = TypecodeContainerBase
  39.     import ZSI.generate.containers
  40.     TypecodeContainerBase.lazy = True
  41.  
  42.  
  43. def wsdl2py(args = None):
  44.     op = optparse.OptionParser(usage = 'USAGE: %wsdl2py [options] WSDL', description = wsdl2py.__doc__)
  45.     op.add_option('-x', '--schema', action = 'store_true', dest = 'schema', default = False, help = 'process just the schema from an xsd file [no services]')
  46.     op.add_option('-d', '--debug', action = 'callback', callback = SetDebugCallback, help = 'debug output')
  47.     op.add_option('-a', '--address', action = 'store_true', dest = 'address', default = False, help = 'ws-addressing support, must include WS-Addressing schema.')
  48.     op.add_option('-b', '--complexType', action = 'callback', callback = SetPyclassMetaclass, callback_kwargs = {
  49.         'module': 'ZSI.generate.pyclass',
  50.         'metaclass': 'pyclass_type' }, help = 'add convenience functions for complexTypes, including Getters, Setters, factory methods, and properties (via metaclass). *** DONT USE WITH --simple-naming ***')
  51.     op.add_option('-l', '--lazy', action = 'callback', callback = SetUpLazyEvaluation, callback_kwargs = { }, help = 'EXPERIMENTAL: recursion error solution, lazy evalution of typecodes')
  52.     op.add_option('-w', '--twisted', action = 'store_true', dest = 'twisted', default = False, help = 'generate a twisted.web client/server, dependencies python>=2.4, Twisted>=2.0.0, TwistedWeb>=0.5.0')
  53.     op.add_option('-o', '--output-dir', action = 'store', dest = 'output_dir', default = '.', type = 'string', help = 'save files in directory')
  54.     op.add_option('-s', '--simple-naming', action = 'store_true', dest = 'simple_naming', default = False, help = 'map element names directly to python attributes')
  55.     op.add_option('-p', '--pydoc', action = 'store_true', dest = 'pydoc', default = False, help = 'top-level directory for pydoc documentation.')
  56.     is_cmdline = args is None
  57.     if is_cmdline:
  58.         (options, args) = op.parse_args()
  59.     else:
  60.         (options, args) = op.parse_args(args)
  61.     if len(args) != 1:
  62.         print >>sys.stderr, 'Expecting a file/url as argument (WSDL).'
  63.         sys.exit(os.EX_USAGE)
  64.     
  65.     location = args[0]
  66.     if options.schema is True:
  67.         reader = XMLSchema.SchemaReader(base_url = location)
  68.     else:
  69.         reader = WSDLTools.WSDLReader()
  70.     load = reader.loadFromFile
  71.     if not isfile(location):
  72.         load = reader.loadFromURL
  73.     
  74.     
  75.     try:
  76.         wsdl = load(location)
  77.     except Exception:
  78.         e = None
  79.         print >>sys.stderr, 'Error loading %s: \n\t%s' % (location, e)
  80.         traceback.print_exc(sys.stderr)
  81.         if hasattr(os, 'EX_NOINPUT'):
  82.             sys.exit(os.EX_NOINPUT)
  83.         
  84.         sys.exit('error loading %s' % location)
  85.  
  86.     if isinstance(wsdl, XMLSchema.XMLSchema):
  87.         wsdl.location = location
  88.         files = _wsdl2py(options, wsdl)
  89.     else:
  90.         files = _wsdl2py(options, wsdl)
  91.         files.append(_wsdl2dispatch(options, wsdl))
  92.     if getattr(options, 'pydoc', False):
  93.         _writepydoc(os.path.join('docs', 'API'), *files)
  94.     
  95.     if is_cmdline:
  96.         return None
  97.     return files
  98.  
  99.  
  100. def _wsdl2py(options, wsdl):
  101.     if options.twisted:
  102.         ServiceHeaderContainer = ServiceHeaderContainer
  103.         import ZSI.generate.containers
  104.         
  105.         try:
  106.             ServiceHeaderContainer.imports.remove('from ZSI import client')
  107.         except ValueError:
  108.             pass
  109.  
  110.         ServiceHeaderContainer.imports.append('from ZSI.twisted import client')
  111.     
  112.     if options.simple_naming:
  113.         containers.SetTypeNameFunc((lambda n: '%s_' % NC_to_CN(n)))
  114.         containers.SetElementNameFunc((lambda n: '%s' % NC_to_CN(n)))
  115.         
  116.         containers.ContainerBase.func_aname = lambda instnc, n: TextProtect(str(n))
  117.         
  118.         utility.namespace_name = lambda cls, ns: utility.Namespace2ModuleName(ns)
  119.     
  120.     files = []
  121.     append = files.append
  122.     if isinstance(wsdl, XMLSchema.XMLSchema):
  123.         wsm = WriteServiceModule(_XMLSchemaAdapter(wsdl.location, wsdl), addressing = options.address)
  124.     else:
  125.         wsm = WriteServiceModule(wsdl, addressing = options.address)
  126.         client_mod = wsm.getClientModuleName()
  127.         client_file = join(options.output_dir, '%s.py' % client_mod)
  128.         append(client_file)
  129.         fd = open(client_file, 'w+')
  130.         wsm.writeClient(fd)
  131.         fd.close()
  132.     types_mod = wsm.getTypesModuleName()
  133.     types_file = join(options.output_dir, '%s.py' % types_mod)
  134.     append(types_file)
  135.     fd = open(types_file, 'w+')
  136.     wsm.writeTypes(fd)
  137.     fd.close()
  138.     return files
  139.  
  140.  
  141. def _wsdl2dispatch(options, wsdl):
  142.     kw = dict()
  143.     if options.twisted:
  144.         WSResource = WSResource
  145.         import ZSI.twisted.WSresource
  146.         kw['base'] = WSResource
  147.         ss = ServiceDescription(**kw)
  148.         if options.address is True:
  149.             raise RuntimeError, 'WS-Address w/twisted currently unsupported, edit the "factory" attribute by hand'
  150.         options.address is True
  151.     elif options.address is True:
  152.         ss = ServiceDescriptionWSA()
  153.     else:
  154.         ss = ServiceDescription(**kw)
  155.     ss.fromWSDL(wsdl)
  156.     file_name = ss.getServiceModuleName() + '.py'
  157.     fd = open(join(options.output_dir, file_name), 'w+')
  158.     ss.write(fd)
  159.     fd.close()
  160.     return file_name
  161.  
  162.  
  163. class _XMLSchemaAdapter:
  164.     
  165.     def __init__(self, location, schema):
  166.         self.name = '_'.join(split(location)[-1].split('.'))
  167.         self.types = {
  168.             schema.targetNamespace: schema }
  169.  
  170.  
  171. import os
  172. import pydoc
  173. import sys
  174. import warnings
  175. import inspect
  176. import os.path as os
  177. from distutils import log
  178. from distutils.command.build_py import build_py
  179. from distutils.util import convert_path
  180. from ZSI.schema import ElementDeclaration, TypeDefinition
  181.  
  182. def _writedoc(doc, thing, forceload = 0):
  183.     
  184.     try:
  185.         (object, name) = pydoc.resolve(thing, forceload)
  186.         page = pydoc.html.page(pydoc.describe(object), pydoc.html.document(object, name))
  187.         fname = os.path.join(doc, name + '.html')
  188.         file = open(fname, 'w')
  189.         file.write(page)
  190.         file.close()
  191.     except (ImportError, pydoc.ErrorDuringImport):
  192.         value = None
  193.         traceback.print_exc(sys.stderr)
  194.  
  195.     return name + '.html'
  196.  
  197.  
  198. def _writeclientdoc(doc, thing, forceload = 0):
  199.     docmodule = pydoc.HTMLDoc.docmodule
  200.     
  201.     def strongarm(self, object, name = None, mod = (None,), *ignored):
  202.         result = docmodule(self, object, name, mod, *ignored)
  203.         nonmembers = []
  204.         push = nonmembers.append
  205.         for k, v in inspect.getmembers(object, inspect.isclass):
  206.             if inspect.getmodule(v) is not object and getattr(v, 'typecode', None) is not None:
  207.                 push('<a href="%s.html">%s</a>: pyclass alias<br/>' % (v.__name__, k))
  208.                 continue
  209.         
  210.         result += self.bigsection('Aliases', '#ffffff', '#eeaa77', ''.join(nonmembers))
  211.         return result
  212.  
  213.     pydoc.HTMLDoc.docmodule = strongarm
  214.     
  215.     try:
  216.         (object, name) = pydoc.resolve(thing, forceload)
  217.         page = pydoc.html.page(pydoc.describe(object), pydoc.html.document(object, name))
  218.         name = os.path.join(doc, name + '.html')
  219.         file = open(name, 'w')
  220.         file.write(page)
  221.         file.close()
  222.     except (ImportError, pydoc.ErrorDuringImport):
  223.         value = None
  224.         log.debug(str(value))
  225.  
  226.     pydoc.HTMLDoc.docmodule = docmodule
  227.  
  228.  
  229. def _writetypesdoc(doc, thing, forceload = 0):
  230.     
  231.     try:
  232.         (object, name) = pydoc.resolve(thing, forceload)
  233.         name = os.path.join(doc, name + '.html')
  234.     except (ImportError, pydoc.ErrorDuringImport):
  235.         value = None
  236.         log.debug(str(value))
  237.         return None
  238.  
  239.     cdict = { }
  240.     fdict = { }
  241.     elements_dict = { }
  242.     types_dict = { }
  243.     for kname, klass in inspect.getmembers(thing, inspect.isclass):
  244.         if thing is not inspect.getmodule(klass):
  245.             continue
  246.         
  247.         cdict[kname] = inspect.getmembers(klass, inspect.isclass)
  248.         for iname, iklass in cdict[kname]:
  249.             key = (kname, iname)
  250.             fdict[key] = _writedoc(doc, iklass)
  251.             if issubclass(iklass, ElementDeclaration):
  252.                 
  253.                 try:
  254.                     typecode = iklass()
  255.                 except (AttributeError, RuntimeError):
  256.                     ex = None
  257.                     elements_dict[iname] = _writebrokedoc(doc, ex, iname)
  258.                     continue
  259.  
  260.                 elements_dict[iname] = None
  261.                 if typecode.pyclass is not None:
  262.                     elements_dict[iname] = _writedoc(doc, typecode.pyclass)
  263.                     continue
  264.                 continue
  265.             
  266.             if issubclass(iklass, TypeDefinition):
  267.                 
  268.                 try:
  269.                     typecode = iklass(None)
  270.                 except (AttributeError, RuntimeError):
  271.                     ex = None
  272.                     types_dict[iname] = _writebrokedoc(doc, ex, iname)
  273.                     continue
  274.  
  275.                 types_dict[iname] = None
  276.                 if typecode.pyclass is not None:
  277.                     types_dict[iname] = _writedoc(doc, typecode.pyclass)
  278.                     continue
  279.                 continue
  280.                 continue
  281.         
  282.     
  283.     
  284.     def strongarm(self, object, name = None, mod = None, funcs = None, classes = (None, None, None, { }, { }), *ignored):
  285.         realname = object.__name__
  286.         if not name:
  287.             pass
  288.         name = realname
  289.         bases = object.__bases__
  290.         (object, name) = pydoc.resolve(object, forceload)
  291.         contents = []
  292.         push = contents.append
  293.         if name == realname:
  294.             title = '<a name="%s">class <strong>%s</strong></a>' % (name, realname)
  295.         else:
  296.             title = '<strong>%s</strong> = <a name="%s">class %s</a>' % (name, name, realname)
  297.         mdict = { }
  298.         if bases:
  299.             parents = []
  300.             for base in bases:
  301.                 parents.append(self.classlink(base, object.__module__))
  302.             
  303.             title = title + '(%s)' % pydoc.join(parents, ', ')
  304.         
  305.         doc = self.markup(pydoc.getdoc(object), self.preformat, funcs, classes, mdict)
  306.         if doc:
  307.             pass
  308.         doc = '<tt>%s<br> </tt>' % doc
  309.         for iname, iclass in cdict[name]:
  310.             fname = fdict[(name, iname)]
  311.             if elements_dict.has_key(iname):
  312.                 push('class <a href="%s">%s</a>: element declaration typecode<br/>' % (fname, iname))
  313.                 pyclass = elements_dict[iname]
  314.                 if pyclass is not None:
  315.                     push('<ul>instance attributes:')
  316.                     push('<li><a href="%s">pyclass</a>: instances serializable to XML<br/></li>' % elements_dict[iname])
  317.                     push('</ul>')
  318.                 
  319.             pyclass is not None
  320.             if types_dict.has_key(iname):
  321.                 push('class <a href="%s">%s</a>: type definition typecode<br/>' % (fname, iname))
  322.                 pyclass = types_dict[iname]
  323.                 if pyclass is not None:
  324.                     push('<ul>instance attributes:')
  325.                     push('<li><a href="%s">pyclass</a>: instances serializable to XML<br/></li>' % types_dict[iname])
  326.                     push('</ul>')
  327.                 
  328.             pyclass is not None
  329.             push('class <a href="%s">%s</a>: TODO not sure what this is<br/>' % (fname, iname))
  330.         
  331.         contents = ''.join(contents)
  332.         return self.section(title, '#000000', '#ffc8d8', contents, 3, doc)
  333.  
  334.     doclass = pydoc.HTMLDoc.docclass
  335.     pydoc.HTMLDoc.docclass = strongarm
  336.     
  337.     try:
  338.         page = pydoc.html.page(pydoc.describe(object), pydoc.html.document(object, name))
  339.         file = open(name, 'w')
  340.         file.write(page)
  341.         file.close()
  342.     except (ImportError, pydoc.ErrorDuringImport):
  343.         value = None
  344.         log.debug(str(value))
  345.  
  346.     pydoc.HTMLDoc.docclass = doclass
  347.  
  348.  
  349. def _writebrokedoc(doc, ex, name, forceload = 0):
  350.     
  351.     try:
  352.         fname = os.path.join(doc, name + '.html')
  353.         page = pydoc.html.page(pydoc.describe(ex), pydoc.html.document(str(ex), fname))
  354.         file = open(fname, 'w')
  355.         file.write(page)
  356.         file.close()
  357.     except (ImportError, pydoc.ErrorDuringImport):
  358.         value = None
  359.         log.debug(str(value))
  360.  
  361.     return name + '.html'
  362.  
  363.  
  364. def _writepydoc(doc, *args):
  365.     ok = True
  366.     if not os.path.isdir(doc):
  367.         os.makedirs(doc)
  368.     
  369.     if os.path.curdir not in sys.path:
  370.         sys.path.append(os.path.curdir)
  371.     
  372.     for f in args:
  373.         if f.startswith('./'):
  374.             f = f[2:]
  375.         
  376.         name = os.path.sep.join(f.strip('.py').split(os.path.sep))
  377.         
  378.         try:
  379.             e = __import__(name)
  380.         except Exception:
  381.             ex = None
  382.             raise 
  383.  
  384.         if name.endswith('_client'):
  385.             _writeclientdoc(doc, e)
  386.             continue
  387.         
  388.         if name.endswith('_types'):
  389.             _writetypesdoc(doc, e)
  390.             continue
  391.         
  392.         
  393.         try:
  394.             _writedoc(doc, e)
  395.         continue
  396.         except IndexError:
  397.             ex = None
  398.             _writebrokedoc(doc, ex, name)
  399.             continue
  400.             continue
  401.         
  402.  
  403.     
  404.  
  405.