/usr/share/pyshared/ZSI/generate/commands.py is in python-zsi 2.1~a1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | ############################################################################
# Joshua Boverhof<JRBoverhof@lbl.gov>, LBNL
# Monte Goode <MMGoode@lbl.gov>, LBNL
# See Copyright for copyright notice!
############################################################################
import exceptions, sys, optparse, os, warnings, traceback
from os.path import isfile, join, split
#from operator import xor
import ZSI
from ConfigParser import ConfigParser
from ZSI.generate.wsdl2python import WriteServiceModule, ServiceDescription as wsdl2pyServiceDescription
from ZSI.wstools import WSDLTools, XMLSchema
from ZSI.wstools.logging import setBasicLoggerDEBUG
from ZSI.generate import containers, utility
from ZSI.generate.utility import NCName_to_ClassName as NC_to_CN, TextProtect
from ZSI.generate.wsdl2dispatch import ServiceModuleWriter as ServiceDescription
from ZSI.generate.wsdl2dispatch import WSAServiceModuleWriter as ServiceDescriptionWSA
warnings.filterwarnings('ignore', '', exceptions.UserWarning)
def SetDebugCallback(option, opt, value, parser, *args, **kwargs):
setBasicLoggerDEBUG()
warnings.resetwarnings()
def SetPyclassMetaclass(option, opt, value, parser, *args, **kwargs):
"""set up pyclass metaclass for complexTypes"""
from ZSI.generate.containers import ServiceHeaderContainer,\
TypecodeContainerBase, TypesHeaderContainer
TypecodeContainerBase.metaclass = kwargs['metaclass']
TypesHeaderContainer.imports.append(\
'from %(module)s import %(metaclass)s' %kwargs
)
ServiceHeaderContainer.imports.append(\
'from %(module)s import %(metaclass)s' %kwargs
)
def SetUpLazyEvaluation(option, opt, value, parser, *args, **kwargs):
from ZSI.generate.containers import TypecodeContainerBase
TypecodeContainerBase.lazy = True
def wsdl2py(args=None):
"""Utility for automatically generating client/service interface code from
a wsdl definition, and a set of classes representing element declarations
and type definitions. By default invoking this script produces three files,
each named after the wsdl definition name, in the current working directory.
Generated Modules Suffix:
_client.py -- client locator, rpc proxy port, messages
_types.py -- typecodes representing
_server.py -- server-side bindings
Parameters:
args -- optional can provide arguments, rather than parsing
command-line.
return:
Default behavior is to return None, if args are provided then
return names of the generated files.
"""
op = optparse.OptionParser(usage="USAGE: %wsdl2py [options] WSDL",
description=wsdl2py.__doc__)
# Basic options
op.add_option("-x", "--schema",
action="store_true", dest="schema", default=False,
help="process just the schema from an xsd file [no services]")
op.add_option("-d", "--debug",
action="callback", callback=SetDebugCallback,
help="debug output")
# WS Options
op.add_option("-a", "--address",
action="store_true", dest="address", default=False,
help="ws-addressing support, must include WS-Addressing schema.")
# pyclass Metaclass
op.add_option("-b", "--complexType",
action="callback", callback=SetPyclassMetaclass,
callback_kwargs={'module':'ZSI.generate.pyclass',
'metaclass':'pyclass_type'},
help="add convenience functions for complexTypes, including Getters, Setters, factory methods, and properties (via metaclass). *** DONT USE WITH --simple-naming ***")
# Lazy Evaluation of Typecodes (done at serialization/parsing when needed).
op.add_option("-l", "--lazy",
action="callback", callback=SetUpLazyEvaluation,
callback_kwargs={},
help="EXPERIMENTAL: recursion error solution, lazy evalution of typecodes")
# Use Twisted
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")
op.add_option("-o", "--output-dir",
action="store", dest="output_dir", default=".", type="string",
help="save files in directory")
op.add_option("-s", "--simple-naming",
action="store_true", dest="simple_naming", default=False,
help="map element names directly to python attributes")
op.add_option("-p", "--pydoc",
action="store_true", dest="pydoc", default=False,
help="top-level directory for pydoc documentation.")
is_cmdline = args is None
if is_cmdline:
(options, args) = op.parse_args()
else:
(options, args) = op.parse_args(args)
if len(args) != 1:
print>>sys.stderr, 'Expecting a file/url as argument (WSDL).'
sys.exit(os.EX_USAGE)
location = args[0]
if options.schema is True:
reader = XMLSchema.SchemaReader(base_url=location)
else:
reader = WSDLTools.WSDLReader()
load = reader.loadFromFile
if not isfile(location):
load = reader.loadFromURL
try:
wsdl = load(location)
except Exception, e:
print >> sys.stderr, "Error loading %s: \n\t%s" % (location, e)
traceback.print_exc(sys.stderr)
# exit code UNIX specific, Windows?
if hasattr(os, 'EX_NOINPUT'): sys.exit(os.EX_NOINPUT)
sys.exit("error loading %s" %location)
if isinstance(wsdl, XMLSchema.XMLSchema):
wsdl.location = location
files = _wsdl2py(options, wsdl)
else:
files = _wsdl2py(options, wsdl)
files.append(_wsdl2dispatch(options, wsdl))
if getattr(options, 'pydoc', False):
_writepydoc(os.path.join('docs', 'API'), *files)
if is_cmdline:
return
return files
#def wsdl2dispatch(args=None):
# """Deprecated: wsdl2py now generates everything
# A utility for automatically generating service skeleton code from a wsdl
# definition.
# """
# op = optparse.OptionParser()
# op.add_option("-a", "--address",
# action="store_true", dest="address", default=False,
# help="ws-addressing support, must include WS-Addressing schema.")
# op.add_option("-d", "--debug",
# action="callback", callback=SetDebugCallback,
# help="debug output")
# op.add_option("-t", "--types",
# action="store", dest="types", default=None, type="string",
# help="Write generated files to OUTPUT_DIR")
# op.add_option("-o", "--output-dir",
# action="store", dest="output_dir", default=".", type="string",
# help="file to load types from")
# op.add_option("-s", "--simple-naming",
# action="store_true", dest="simple_naming", default=False,
# help="Simplify generated naming.")
#
# if args is None:
# (options, args) = op.parse_args()
# else:
# (options, args) = op.parse_args(args)
#
# if len(args) != 1:
# print>>sys.stderr, 'Expecting a file/url as argument (WSDL).'
# sys.exit(os.EX_USAGE)
#
# reader = WSDLTools.WSDLReader()
# if isfile(args[0]):
# _wsdl2dispatch(options, reader.loadFromFile(args[0]))
# return
#
# _wsdl2dispatch(options, reader.loadFromURL(args[0]))
def _wsdl2py(options, wsdl):
if options.twisted:
from ZSI.generate.containers import ServiceHeaderContainer
try:
ServiceHeaderContainer.imports.remove('from ZSI import client')
except ValueError:
pass
ServiceHeaderContainer.imports.append('from ZSI.twisted import client')
if options.simple_naming:
# Use a different client suffix
# WriteServiceModule.client_module_suffix = "_client"
# Write messages definitions to a separate file.
#wsdl2pyServiceDescription.separate_messages = True
# Use more simple type and element class names
containers.SetTypeNameFunc( lambda n: '%s_' %(NC_to_CN(n)) )
containers.SetElementNameFunc( lambda n: '%s' %(NC_to_CN(n)) )
# Don't add "_" to the attribute name (remove when --aname works well)
containers.ContainerBase.func_aname = lambda instnc,n: TextProtect(str(n))
# write out the modules with their names rather than their number.
utility.namespace_name = lambda cls, ns: utility.Namespace2ModuleName(ns)
files = []
append = files.append
if isinstance(wsdl, XMLSchema.XMLSchema):
wsm = WriteServiceModule(_XMLSchemaAdapter(wsdl.location, wsdl),
addressing=options.address)
else:
wsm = WriteServiceModule(wsdl, addressing=options.address)
client_mod = wsm.getClientModuleName()
client_file = join(options.output_dir, '%s.py' %client_mod)
append(client_file)
fd = open(client_file, 'w+')
wsm.writeClient(fd)
fd.close()
types_mod = wsm.getTypesModuleName()
types_file = join(options.output_dir, '%s.py' %types_mod)
append(types_file)
fd = open(types_file, 'w+' )
wsm.writeTypes(fd)
fd.close()
return files
def _wsdl2dispatch(options, wsdl):
"""TOOD: Remove ServiceContainer stuff, and replace with WSGI.
"""
kw = dict()
if options.twisted:
from ZSI.twisted.WSresource import WSResource
kw['base'] = WSResource
ss = ServiceDescription(**kw)
if options.address is True:
raise RuntimeError, 'WS-Address w/twisted currently unsupported, edit the "factory" attribute by hand'
else:
# TODO: make all this handler arch
if options.address is True:
ss = ServiceDescriptionWSA()
else:
ss = ServiceDescription(**kw)
ss.fromWSDL(wsdl)
file_name = ss.getServiceModuleName()+'.py'
fd = open( join(options.output_dir, file_name), 'w+')
ss.write(fd)
fd.close()
return file_name
class _XMLSchemaAdapter:
"""Adapts an obj XMLSchema.XMLSchema to look like a WSDLTools.WSDL,
just setting a couple attributes code expects to see.
"""
def __init__(self, location, schema):
"""Parameters:
location -- base location, file path
schema -- XMLSchema instance
"""
self.name = '_'.join(split(location)[-1].split('.'))
self.types = {schema.targetNamespace:schema}
import os, pydoc, sys, warnings, inspect
import os.path
from distutils import log
from distutils.command.build_py import build_py
from distutils.util import convert_path
#from setuptools import find_packages
#from setuptools import Command
from ZSI.schema import ElementDeclaration, TypeDefinition
#from pyGridWare.utility.generate.Modules import NR
#from pyGridWare.utility.generate.Modules import CLIENT, TYPES
#def find_packages_modules(where='.'):
# #pack,mod,mod_file
# """Return a list all Python packages found within directory 'where'
# """
# out = []
# stack=[(convert_path(where), '')]
# while stack:
# where,prefix = stack.pop(0)
# for name in os.listdir(where):
# fn = os.path.join(where,name)
# #if (os.path.isdir(fn) and
# # os.path.isfile(os.path.join(fn,'__init__.py'))
# #):
# # out.append(prefix+name); stack.append((fn,prefix+name+'.'))
# if (os.path.isdir(fn) and
# os.path.isfile(os.path.join(fn,'__init__.py'))):
# stack.append((fn,prefix+name+'.'))
# continue
#
# if name == '__init__.py' or not name.endswith('.py'):
# continue
#
# out.append((prefix, name.split('.py')[0]))
#
# return out
def _writedoc(doc, thing, forceload=0):
"""Write HTML documentation to a file in the current directory.
"""
try:
object, name = pydoc.resolve(thing, forceload)
page = pydoc.html.page(pydoc.describe(object), pydoc.html.document(object, name))
fname = os.path.join(doc, name + '.html')
file = open(fname, 'w')
file.write(page)
file.close()
except (ImportError, pydoc.ErrorDuringImport), value:
traceback.print_exc(sys.stderr)
else:
return name + '.html'
def _writeclientdoc(doc, thing, forceload=0):
"""Write HTML documentation to a file in the current directory.
"""
docmodule = pydoc.HTMLDoc.docmodule
def strongarm(self, object, name=None, mod=None, *ignored):
result = docmodule(self, object, name, mod, *ignored)
# Grab all the aliases to pyclasses and create links.
nonmembers = []
push = nonmembers.append
for k,v in inspect.getmembers(object, inspect.isclass):
if inspect.getmodule(v) is not object and getattr(v,'typecode',None) is not None:
push('<a href="%s.html">%s</a>: pyclass alias<br/>' %(v.__name__,k))
result += self.bigsection('Aliases', '#ffffff', '#eeaa77', ''.join(nonmembers))
return result
pydoc.HTMLDoc.docmodule = strongarm
try:
object, name = pydoc.resolve(thing, forceload)
page = pydoc.html.page(pydoc.describe(object), pydoc.html.document(object, name))
name = os.path.join(doc, name + '.html')
file = open(name, 'w')
file.write(page)
file.close()
except (ImportError, pydoc.ErrorDuringImport), value:
log.debug(str(value))
pydoc.HTMLDoc.docmodule = docmodule
def _writetypesdoc(doc, thing, forceload=0):
"""Write HTML documentation to a file in the current directory.
"""
try:
object, name = pydoc.resolve(thing, forceload)
name = os.path.join(doc, name + '.html')
except (ImportError, pydoc.ErrorDuringImport), value:
log.debug(str(value))
return
# inner classes
cdict = {}
fdict = {}
elements_dict = {}
types_dict = {}
for kname,klass in inspect.getmembers(thing, inspect.isclass):
if thing is not inspect.getmodule(klass):
continue
cdict[kname] = inspect.getmembers(klass, inspect.isclass)
for iname,iklass in cdict[kname]:
key = (kname,iname)
fdict[key] = _writedoc(doc, iklass)
if issubclass(iklass, ElementDeclaration):
try:
typecode = iklass()
except (AttributeError,RuntimeError), ex:
elements_dict[iname] = _writebrokedoc(doc, ex, iname)
continue
elements_dict[iname] = None
if typecode.pyclass is not None:
elements_dict[iname] = _writedoc(doc, typecode.pyclass)
continue
if issubclass(iklass, TypeDefinition):
try:
typecode = iklass(None)
except (AttributeError,RuntimeError), ex:
types_dict[iname] = _writebrokedoc(doc, ex, iname)
continue
types_dict[iname] = None
if typecode.pyclass is not None:
types_dict[iname] = _writedoc(doc, typecode.pyclass)
continue
def strongarm(self, object, name=None, mod=None, funcs={}, classes={}, *ignored):
"""Produce HTML documentation for a class object."""
realname = object.__name__
name = name or realname
bases = object.__bases__
object, name = pydoc.resolve(object, forceload)
contents = []
push = contents.append
if name == realname:
title = '<a name="%s">class <strong>%s</strong></a>' % (
name, realname)
else:
title = '<strong>%s</strong> = <a name="%s">class %s</a>' % (
name, name, realname)
mdict = {}
if bases:
parents = []
for base in bases:
parents.append(self.classlink(base, object.__module__))
title = title + '(%s)' % pydoc.join(parents, ', ')
doc = self.markup(pydoc.getdoc(object), self.preformat, funcs, classes, mdict)
doc = doc and '<tt>%s<br> </tt>' % doc
for iname,iclass in cdict[name]:
fname = fdict[(name,iname)]
if elements_dict.has_key(iname):
push('class <a href="%s">%s</a>: element declaration typecode<br/>'\
%(fname,iname))
pyclass = elements_dict[iname]
if pyclass is not None:
push('<ul>instance attributes:')
push('<li><a href="%s">pyclass</a>: instances serializable to XML<br/></li>'\
%elements_dict[iname])
push('</ul>')
elif types_dict.has_key(iname):
push('class <a href="%s">%s</a>: type definition typecode<br/>' %(fname,iname))
pyclass = types_dict[iname]
if pyclass is not None:
push('<ul>instance attributes:')
push('<li><a href="%s">pyclass</a>: instances serializable to XML<br/></li>'\
%types_dict[iname])
push('</ul>')
else:
push('class <a href="%s">%s</a>: TODO not sure what this is<br/>' %(fname,iname))
contents = ''.join(contents)
return self.section(title, '#000000', '#ffc8d8', contents, 3, doc)
doclass = pydoc.HTMLDoc.docclass
pydoc.HTMLDoc.docclass = strongarm
try:
page = pydoc.html.page(pydoc.describe(object), pydoc.html.document(object, name))
file = open(name, 'w')
file.write(page)
file.close()
except (ImportError, pydoc.ErrorDuringImport), value:
log.debug(str(value))
pydoc.HTMLDoc.docclass = doclass
def _writebrokedoc(doc, ex, name, forceload=0):
try:
fname = os.path.join(doc, name + '.html')
page = pydoc.html.page(pydoc.describe(ex), pydoc.html.document(str(ex), fname))
file = open(fname, 'w')
file.write(page)
file.close()
except (ImportError, pydoc.ErrorDuringImport), value:
log.debug(str(value))
return name + '.html'
def _writepydoc(doc, *args):
"""create pydoc html pages
doc -- destination directory for documents
*args -- modules run thru pydoc
"""
ok = True
if not os.path.isdir(doc):
os.makedirs(doc)
if os.path.curdir not in sys.path:
sys.path.append(os.path.curdir)
for f in args:
if f.startswith('./'): f = f[2:]
name = os.path.sep.join(f.strip('.py').split(os.path.sep))
try:
e = __import__(name)
except Exception,ex:
raise
# _writebrokedoc(doc, ex, name)
# continue
if name.endswith('_client'):
_writeclientdoc(doc, e)
continue
if name.endswith('_types'):
_writetypesdoc(doc, e)
continue
try:
_writedoc(doc, e)
except IndexError,ex:
_writebrokedoc(doc, ex, name)
continue
|