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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from ZSI import _copyright, _children, _child_elements, _inttypes, _stringtypes, _seqtypes, _find_arraytype, _find_href, _find_type, _find_xmlns_prefix, _get_idstr, EvaluateException, ParseException
  5. from TC import _get_element_nsuri_name, _get_xsitype, TypeCode, Any, AnyElement, AnyType, Nilled, UNBOUNDED
  6. from schema import GED, ElementDeclaration, TypeDefinition, _get_substitute_element, _get_type_definition, _is_substitute_element
  7. from ZSI.wstools.Namespaces import SCHEMA, SOAP
  8. from ZSI.wstools.Utility import SplitQName
  9. from ZSI.wstools.logging import getLogger as _GetLogger
  10. import re
  11. import types
  12. from copy import copy as _copy
  13.  
  14. _find_arrayoffset = lambda E: if not E.getAttributeNS(SOAP.ENC, 'offset'):
  15. passE.getAttributeNS(SOAP.ENC12, 'offset')
  16.  
  17. _find_arrayposition = lambda E: if not E.getAttributeNS(SOAP.ENC, 'position'):
  18. passE.getAttributeNS(SOAP.ENC12, 'position')
  19. _offset_pat = re.compile('\\[[0-9]+\\]')
  20. _position_pat = _offset_pat
  21.  
  22. def _check_typecode_list(ofwhat, tcname):
  23.     for o in ofwhat:
  24.         if callable(o):
  25.             continue
  26.         
  27.         if not isinstance(o, TypeCode):
  28.             raise TypeError(tcname + ' ofwhat outside the TypeCode hierarchy, ' + str(o.__class__))
  29.         isinstance(o, TypeCode)
  30.         if o.pname is None and not isinstance(o, AnyElement):
  31.             raise TypeError(tcname + ' element ' + str(o) + ' has no name')
  32.         not isinstance(o, AnyElement)
  33.     
  34.  
  35.  
  36. def _get_type_or_substitute(typecode, pyobj, sw, elt):
  37.     sub = getattr(pyobj, 'typecode', typecode)
  38.     if sub is typecode or sub is None:
  39.         return typecode
  40.     if isinstance(typecode, AnyElement):
  41.         return sub
  42.     if isinstance(sub, ElementDeclaration):
  43.         if (typecode.nspname, typecode.pname) == (sub.nspname, sub.pname):
  44.             raise TypeError('bad usage, failed to serialize element reference (%s, %s), in: %s' % (typecode.nspname, typecode.pname, sw.Backtrace(elt)))
  45.         (typecode.nspname, typecode.pname) == (sub.nspname, sub.pname)
  46.         if _is_substitute_element(typecode, sub):
  47.             return sub
  48.         raise TypeError('failed to serialize (%s, %s) illegal sub GED (%s,%s): %s' % (typecode.nspname, typecode.pname, sub.nspname, sub.pname, sw.Backtrace(elt)))
  49.     isinstance(sub, ElementDeclaration)
  50.     if not isinstance(typecode, AnyType) and not isinstance(sub, typecode.__class__):
  51.         raise TypeError('failed to serialize substitute %s for %s,  not derivation: %s' % (sub, typecode, sw.Backtrace(elt)))
  52.     not isinstance(sub, typecode.__class__)
  53.     sub = _copy(sub)
  54.     sub.nspname = typecode.nspname
  55.     sub.pname = typecode.pname
  56.     sub.aname = typecode.aname
  57.     return sub
  58.  
  59.  
  60. class ComplexType(TypeCode):
  61.     logger = _GetLogger('ZSI.TCcompound.ComplexType')
  62.     
  63.     def __init__(self, pyclass, ofwhat, pname = None, inorder = False, inline = False, mutable = True, mixed = False, mixed_aname = '_text', **kw):
  64.         TypeCode.__init__(self, pname, pyclass = pyclass, **kw)
  65.         self.inorder = inorder
  66.         self.inline = inline
  67.         self.mutable = mutable
  68.         self.mixed = mixed
  69.         self.mixed_aname = None
  70.         if mixed is True:
  71.             self.mixed_aname = mixed_aname
  72.         
  73.         if self.mutable is True:
  74.             self.inline = True
  75.         
  76.         if not kw.get('type'):
  77.             pass
  78.         self.type = _get_xsitype(self)
  79.         t = type(ofwhat)
  80.         if t not in _seqtypes:
  81.             raise TypeError('Struct ofwhat must be list or sequence, not ' + str(t))
  82.         t not in _seqtypes
  83.         self.ofwhat = tuple(ofwhat)
  84.         if TypeCode.typechecks:
  85.             if self.pyclass is not None and type(self.pyclass) is not types.ClassType and not isinstance(self.pyclass, object):
  86.                 raise TypeError('pyclass must be None or an old-style/new-style class, not ' + str(type(self.pyclass)))
  87.             not isinstance(self.pyclass, object)
  88.             _check_typecode_list(self.ofwhat, 'ComplexType')
  89.         
  90.  
  91.     
  92.     def parse(self, elt, ps):
  93.         debug = self.logger.debugOn()
  94.         if debug:
  95.             pass
  96.         self.logger.debug('parse')
  97.         xtype = self.checkname(elt, ps)
  98.         if self.type and xtype not in [
  99.             self.type,
  100.             (None, None)]:
  101.             if not isinstance(self, TypeDefinition):
  102.                 raise EvaluateException('ComplexType for %s has wrong type(%s), looking for %s' % (self.pname, self.checktype(elt, ps), self.type), ps.Backtrace(elt))
  103.             isinstance(self, TypeDefinition)
  104.             if debug:
  105.                 pass
  106.             self.logger.debug('delegate to substitute type')
  107.             what = TypeDefinition.getSubstituteType(self, elt, ps)
  108.             return what.parse(elt, ps)
  109.         xtype not in [
  110.             self.type,
  111.             (None, None)]
  112.         href = _find_href(elt)
  113.         if href:
  114.             if _children(elt):
  115.                 raise EvaluateException('Struct has content and HREF', ps.Backtrace(elt))
  116.             _children(elt)
  117.             elt = ps.FindLocalHREF(href, elt)
  118.         
  119.         c = _child_elements(elt)
  120.         count = len(c)
  121.         if self.nilled(elt, ps):
  122.             return Nilled
  123.         v = { }
  124.         attributes = self.parse_attributes(elt, ps)
  125.         if attributes:
  126.             v[self.attrs_aname] = attributes
  127.         
  128.         if self.mixed is True:
  129.             v[self.mixed_aname] = self.simple_value(elt, ps, mixed = True)
  130.         
  131.         c = c[:]
  132.         crange = range(len(c))
  133.         if debug:
  134.             self.logger.debug('ofwhat: %s', str(self.ofwhat))
  135.         
  136.         any = None
  137.         for i in range(len(self.ofwhat)):
  138.             pass
  139.         
  140.         if any is not None:
  141.             occurs = 0
  142.             v[any.aname] = []
  143.             for j in crange:
  144.                 if c[j]:
  145.                     continue
  146.                 _[3][(j, c[j])]
  147.             
  148.             if any.maxOccurs == 1 and occurs == 0:
  149.                 v[any.aname] = None
  150.             elif (occurs < any.minOccurs or any.maxOccurs != UNBOUNDED) and any.maxOccurs < occurs:
  151.                 raise EvaluateException('occurances of <any> elements(#%d) bound by (%d,%s)' % (occurs, any.minOccurs, str(any.maxOccurs)), ps.Backtrace(elt))
  152.             
  153.         
  154.         if not self.pyclass:
  155.             return v
  156.         
  157.         try:
  158.             pyobj = self.pyclass()
  159.         except Exception:
  160.             self.pyclass
  161.             e = self.pyclass
  162.             raise TypeError('Constructing element (%s,%s) with pyclass(%s), %s' % (self.nspname, self.pname, self.pyclass.__name__, str(e)))
  163.         except:
  164.             self.pyclass
  165.  
  166.         for key in v.keys():
  167.             setattr(pyobj, key, v[key])
  168.         
  169.         return pyobj
  170.  
  171.     
  172.     def serialize(self, elt, sw, pyobj, inline = False, name = None, **kw):
  173.         if inline or self.inline:
  174.             self.cb(elt, sw, pyobj, name = name, **kw)
  175.         else:
  176.             objid = _get_idstr(pyobj)
  177.             (ns, n) = self.get_name(name, objid)
  178.             el = elt.createAppendElement(ns, n)
  179.             el.setAttributeNS(None, 'href', '#%s' % objid)
  180.             sw.AddCallback(self.cb, elt, sw, pyobj)
  181.  
  182.     
  183.     def cb(self, elt, sw, pyobj, name = None, **kw):
  184.         debug = self.logger.debugOn()
  185.         if debug:
  186.             self.logger.debug('cb: %s' % str(self.ofwhat))
  187.         
  188.         objid = _get_idstr(pyobj)
  189.         (ns, n) = self.get_name(name, objid)
  190.         if pyobj is None:
  191.             if self.nillable is True:
  192.                 elem = elt.createAppendElement(ns, n)
  193.                 self.serialize_as_nil(elem)
  194.                 return None
  195.             raise EvaluateException, 'element(%s,%s) is not nillable(%s)' % (self.nspname, self.pname, self.nillable)
  196.         pyobj is None
  197.         if self.mutable is False and sw.Known(pyobj):
  198.             return None
  199.         if debug:
  200.             self.logger.debug('element: (%s, %s)', str(ns), n)
  201.         
  202.         if n is not None:
  203.             elem = elt.createAppendElement(ns, n)
  204.             self.set_attributes(elem, pyobj)
  205.             if kw.get('typed', self.typed) is True:
  206.                 self.set_attribute_xsi_type(elem)
  207.             
  208.             if self.mixed is True and self.mixed_aname is not None:
  209.                 if hasattr(pyobj, self.mixed_aname):
  210.                     textContent = getattr(pyobj, self.mixed_aname)
  211.                     if hasattr(textContent, 'typecode'):
  212.                         textContent.typecode.serialize_text_node(elem, sw, textContent)
  213.                     elif type(textContent) in _stringtypes:
  214.                         if debug:
  215.                             self.logger.debug('mixed text content:\n\t%s', textContent)
  216.                         
  217.                         elem.createAppendTextNode(textContent)
  218.                     else:
  219.                         raise EvaluateException('mixed test content in element (%s,%s) must be a string type' % (self.nspname, self.pname), sw.Backtrace(elt))
  220.                 hasattr(textContent, 'typecode')
  221.                 if debug:
  222.                     self.logger.debug('mixed NO text content in %s', self.mixed_aname)
  223.                 
  224.             
  225.         else:
  226.             elem = elt
  227.         if self.inline:
  228.             pass
  229.         elif not (self.inline) and self.unique:
  230.             raise EvaluateException('Not inline, but unique makes no sense. No href/id.', sw.Backtrace(elt))
  231.         elif n is not None:
  232.             self.set_attribute_id(elem, objid)
  233.         
  234.         if self.pyclass and type(self.pyclass) is type:
  235.             
  236.             f = lambda attr: getattr(pyobj, attr, None)
  237.         elif self.pyclass:
  238.             d = pyobj.__dict__
  239.             
  240.             f = lambda attr: d.get(attr)
  241.         else:
  242.             d = pyobj
  243.             
  244.             f = lambda attr: pyobj.get(attr)
  245.             if TypeCode.typechecks and type(d) != types.DictType:
  246.                 raise TypeError("Classless complexType didn't get dictionary")
  247.             type(d) != types.DictType
  248.         indx = 0
  249.         lenofwhat = len(self.ofwhat)
  250.         if debug:
  251.             self.logger.debug('element declaration (%s,%s)', self.nspname, self.pname)
  252.             if self.type:
  253.                 self.logger.debug('xsi:type definition (%s,%s)', self.type[0], self.type[1])
  254.             else:
  255.                 self.logger.warning('NO xsi:type')
  256.         
  257.         while indx < lenofwhat:
  258.             occurs = 0
  259.             what = self.ofwhat[indx]
  260.             if callable(what):
  261.                 what = what()
  262.             
  263.             if debug:
  264.                 self.logger.debug('serialize what -- %s', what.__class__.__name__)
  265.             
  266.             aname = what.aname
  267.             v = f(aname)
  268.             indx += 1
  269.             if what.minOccurs == 0 and v is None:
  270.                 continue
  271.             
  272.             whatTC = what
  273.             if whatTC.maxOccurs > 1 and v is not None:
  274.                 if type(v) not in _seqtypes:
  275.                     raise EvaluateException('pyobj (%s,%s), aname "%s": maxOccurs %s, expecting a %s' % (self.nspname, self.pname, what.aname, whatTC.maxOccurs, _seqtypes), sw.Backtrace(elt))
  276.                 type(v) not in _seqtypes
  277.                 for v2 in v:
  278.                     occurs += 1
  279.                     if occurs > whatTC.maxOccurs:
  280.                         raise EvaluateException('occurances (%d) exceeded maxOccurs(%d) for <%s>' % (occurs, whatTC.maxOccurs, what.pname), sw.Backtrace(elt))
  281.                     occurs > whatTC.maxOccurs
  282.                     what = _get_type_or_substitute(whatTC, v2, sw, elt)
  283.                     if debug and what is not whatTC:
  284.                         self.logger.debug('substitute derived type: %s' % what.__class__)
  285.                     
  286.                     what.serialize(elem, sw, v2, **kw)
  287.                 
  288.                 if occurs < whatTC.minOccurs:
  289.                     raise EvaluateException('occurances(%d) less than minOccurs(%d) for <%s>' % (occurs, whatTC.minOccurs, what.pname), sw.Backtrace(elt))
  290.                 occurs < whatTC.minOccurs
  291.                 continue
  292.             
  293.             if v is not None or what.nillable is True:
  294.                 what = _get_type_or_substitute(whatTC, v, sw, elt)
  295.                 if debug and what is not whatTC:
  296.                     self.logger.debug('substitute derived type: %s' % what.__class__)
  297.                 
  298.                 what.serialize(elem, sw, v, **kw)
  299.                 continue
  300.             
  301.             raise EvaluateException('Got None for nillable(%s), minOccurs(%d) element (%s,%s), %s' % (what.nillable, what.minOccurs, what.nspname, what.pname, elem), sw.Backtrace(elt))
  302.  
  303.     
  304.     def setDerivedTypeContents(self, extensions = None, restrictions = None):
  305.         if extensions:
  306.             ofwhat = list(self.ofwhat)
  307.             if type(extensions) in _seqtypes:
  308.                 ofwhat += list(extensions)
  309.             else:
  310.                 ofwhat.append(extensions)
  311.         elif restrictions:
  312.             if type(restrictions) in _seqtypes:
  313.                 ofwhat = restrictions
  314.             else:
  315.                 ofwhat = (restrictions,)
  316.         else:
  317.             return None
  318.         self.ofwhat = extensions(ofwhat)
  319.         self.lenofwhat = len(self.ofwhat)
  320.  
  321.  
  322.  
  323. class Struct(ComplexType):
  324.     logger = _GetLogger('ZSI.TCcompound.Struct')
  325.     
  326.     def __init__(self, pyclass, ofwhat, pname = None, inorder = False, inline = False, mutable = True, **kw):
  327.         ComplexType.__init__(self, pyclass, ofwhat, pname = pname, inorder = inorder, inline = inline, mutable = mutable, **kw)
  328.         whats = map((lambda what: (what.nspname, what.pname)), self.ofwhat)
  329.         for idx in range(len(self.ofwhat)):
  330.             what = self.ofwhat[idx]
  331.             key = (what.nspname, what.pname)
  332.             if not isinstance(what, AnyElement) and what.maxOccurs > 1:
  333.                 raise TypeError, 'Constraint: no element can have a maxOccurs>1'
  334.             what.maxOccurs > 1
  335.             if key in whats[idx + 1:]:
  336.                 raise TypeError, 'Constraint: No element may have the same name as any other'
  337.             key in whats[idx + 1:]
  338.         
  339.  
  340.  
  341.  
  342. class Array(TypeCode):
  343.     logger = _GetLogger('ZSI.TCcompound.Array')
  344.     
  345.     def __init__(self, atype, ofwhat, pname = None, dimensions = 1, fill = None, sparse = False, mutable = False, size = None, nooffset = 0, undeclared = False, childnames = None, **kw):
  346.         TypeCode.__init__(self, pname, **kw)
  347.         self.dimensions = dimensions
  348.         self.atype = atype
  349.         if undeclared is False and self.atype[1].endswith(']') is False:
  350.             self.atype = (self.atype[0], '%s[]' % self.atype[1])
  351.         
  352.         if self.dimensions != 1:
  353.             raise TypeError('Only single-dimensioned arrays supported')
  354.         self.dimensions != 1
  355.         self.fill = fill
  356.         self.sparse = sparse
  357.         self.mutable = mutable
  358.         self.size = size
  359.         self.nooffset = nooffset
  360.         self.undeclared = undeclared
  361.         self.childnames = childnames
  362.         if self.size:
  363.             t = type(self.size)
  364.             if t in _inttypes:
  365.                 self.size = (self.size,)
  366.             elif t in _seqtypes:
  367.                 self.size = tuple(self.size)
  368.             elif TypeCode.typechecks:
  369.                 raise TypeError('Size must be integer or list, not ' + str(t))
  370.             
  371.         
  372.         if not ofwhat:
  373.             pass
  374.         ofwhat = Any()
  375.         if TypeCode.typechecks:
  376.             if self.undeclared is False and type(atype) not in _seqtypes and len(atype) == 2:
  377.                 raise TypeError('Array type must be a sequence of len 2.')
  378.             len(atype) == 2
  379.             t = type(ofwhat)
  380.             if not isinstance(ofwhat, TypeCode):
  381.                 raise TypeError('Array ofwhat outside the TypeCode hierarchy, ' + str(ofwhat.__class__))
  382.             isinstance(ofwhat, TypeCode)
  383.             if self.size:
  384.                 if len(self.size) != self.dimensions:
  385.                     raise TypeError('Array dimension/size mismatch')
  386.                 len(self.size) != self.dimensions
  387.                 for s in self.size:
  388.                     if type(s) not in _inttypes:
  389.                         raise TypeError('Array size "' + str(s) + '" is not an integer.')
  390.                     type(s) not in _inttypes
  391.                 
  392.             
  393.         
  394.         self.ofwhat = ofwhat
  395.  
  396.     
  397.     def parse_offset(self, elt, ps):
  398.         o = _find_arrayoffset(elt)
  399.         if not o:
  400.             return 0
  401.         if not _offset_pat.match(o):
  402.             raise EvaluateException('Bad offset "' + o + '"', ps.Backtrace(elt))
  403.         _offset_pat.match(o)
  404.         return int(o[1:-1])
  405.  
  406.     
  407.     def parse_position(self, elt, ps):
  408.         o = _find_arrayposition(elt)
  409.         if not o:
  410.             return None
  411.         if o.find(',') > -1:
  412.             raise EvaluateException('Sorry, no multi-dimensional arrays', ps.Backtrace(elt))
  413.         o.find(',') > -1
  414.         if not _position_pat.match(o):
  415.             raise EvaluateException('Bad array position "' + o + '"', ps.Backtrace(elt))
  416.         _position_pat.match(o)
  417.         return int(o[1:-1])
  418.  
  419.     
  420.     def parse(self, elt, ps):
  421.         href = _find_href(elt)
  422.         if href:
  423.             if _children(elt):
  424.                 raise EvaluateException('Array has content and HREF', ps.Backtrace(elt))
  425.             _children(elt)
  426.             elt = ps.FindLocalHREF(href, elt)
  427.         
  428.         if self.nilled(elt, ps):
  429.             return Nilled
  430.         if not _find_arraytype(elt) and self.undeclared is False:
  431.             raise EvaluateException('Array expected', ps.Backtrace(elt))
  432.         self.undeclared is False
  433.         t = _find_type(elt)
  434.         if t:
  435.             pass
  436.         
  437.         offset = self.parse_offset(elt, ps)
  438.         v = []
  439.         vlen = 0
  440.         if offset and not (self.sparse):
  441.             while vlen < offset:
  442.                 vlen += 1
  443.                 v.append(self.fill)
  444.         
  445.         for c in _child_elements(elt):
  446.             item = self.ofwhat.parse(c, ps)
  447.             if not self.parse_position(c, ps):
  448.                 pass
  449.             position = offset
  450.             if self.sparse:
  451.                 v.append((position, item))
  452.             else:
  453.                 while offset < position:
  454.                     offset += 1
  455.                     v.append(self.fill)
  456.                 v.append(item)
  457.             offset += 1
  458.         
  459.         return v
  460.  
  461.     
  462.     def serialize(self, elt, sw, pyobj, name = None, childnames = None, **kw):
  463.         debug = self.logger.debugOn()
  464.         if debug:
  465.             self.logger.debug('serialize: %r' % pyobj)
  466.         
  467.         if self.mutable is False and sw.Known(pyobj):
  468.             return None
  469.         objid = _get_idstr(pyobj)
  470.         (ns, n) = self.get_name(name, objid)
  471.         el = elt.createAppendElement(ns, n)
  472.         if self.nillable is True and pyobj is None:
  473.             self.serialize_as_nil(el)
  474.             return None
  475.         self.set_attributes(el, pyobj)
  476.         if not self.unique:
  477.             pass
  478.         unique = kw.get('unique', False)
  479.         if unique is False and sw.Known(pyobj):
  480.             self.set_attribute_href(el, objid)
  481.             return None
  482.         if self.unique is False:
  483.             self.set_attribute_id(el, objid)
  484.         
  485.         offset = 0
  486.         if self.sparse is False and self.nooffset is False:
  487.             offset = 0
  488.             end = len(pyobj)
  489.             while offset < end and pyobj[offset] == self.fill:
  490.                 offset += 1
  491.             if offset:
  492.                 el.setAttributeNS(SOAP.ENC, 'offset', '[%d]' % offset)
  493.             
  494.         
  495.         if self.undeclared is False:
  496.             el.setAttributeNS(SOAP.ENC, 'arrayType', '%s:%s' % (el.getPrefix(self.atype[0]), self.atype[1]))
  497.         
  498.         if debug:
  499.             self.logger.debug('ofwhat: %r' % self.ofwhat)
  500.         
  501.         d = { }
  502.         if not childnames:
  503.             pass
  504.         kn = self.childnames
  505.         if kn:
  506.             d['name'] = kn
  507.         elif not self.ofwhat.aname:
  508.             d['name'] = 'element'
  509.         
  510.         if self.sparse is False:
  511.             for e in pyobj[offset:]:
  512.                 self.ofwhat.serialize(el, sw, e, **d)
  513.             
  514.         else:
  515.             position = 0
  516.             for pos, v in pyobj:
  517.                 if pos != position:
  518.                     el.setAttributeNS(SOAP.ENC, 'position', '[%d]' % pos)
  519.                     position = pos
  520.                 
  521.                 self.ofwhat.serialize(el, sw, v, **d)
  522.                 position += 1
  523.             
  524.  
  525.  
  526. if __name__ == '__main__':
  527.     print _copyright
  528.  
  529.