コード例 #1
0
    def ParseHeaderElements(self, ofwhat):
        '''Returns a dictionary of pyobjs.
        ofhow -- list of typecodes w/matching nspname/pname to the header_elements.
        '''
        d = {}
        lenofwhat = len(ofwhat)
        c, crange = self.header_elements[:], list(range(len(self.header_elements)))
        for i,what in [ (i, ofwhat[i]) for i in range(lenofwhat) ]:
            if isinstance(what, AnyElement): 
                raise EvaluateException('not supporting <any> as child of SOAP-ENC:Header')

            v = []
            occurs = 0
            namespaceURI,tagName = what.nspname,what.pname
            for j,c_elt in [ (j, c[j]) for j in crange if c[j] ]:
                prefix,name = SplitQName(c_elt.tagName)
                nsuri = _resolve_prefix(c_elt, prefix)
                if tagName == name and namespaceURI == nsuri:
                    pyobj = what.parse(c_elt, self)
                else:
                    continue
                v.append(pyobj)
                c[j] = None
            if what.minOccurs > len(v) > what.maxOccurs:
               raise EvaluateException('number of occurances(%d) doesnt fit constraints (%d,%s)'\
                   %(len(v),what.minOccurs,what.maxOccurs))
            if what.maxOccurs == 1:
                if len(v) == 0: v = None
                else: v = v[0]
            d[(what.nspname,what.pname)] = v
        return d
コード例 #2
0
ファイル: schema.py プロジェクト: whatzgnu/pysphere
    def getSubstituteType(self, elt, ps):
        """if xsi:type does not match the instance type attr,
        check to see if it is a derived type substitution.

        DONT Return the element's type.

        Parameters:
            elt -- the DOM element being parsed
            ps -- the ParsedSoap object.
        """
        pyclass = SchemaInstanceType.getTypeDefinition(*self.type)
        if pyclass is None:
            raise EvaluateException(
                    'No Type registed for xsi:type=(%s, %s)' %
                    (self.type[0], self.type[1]), ps.Backtrace(elt))

        typeName = _find_type(elt)
        prefix,typeName = SplitQName(typeName)
        uri = ps.GetElementNSdict(elt).get(prefix)
        subclass = SchemaInstanceType.getTypeDefinition(uri, typeName)
        if subclass is None:
            raise EvaluateException(
                    'No registered xsi:type=(%s, %s), substitute for xsi:type=(%s, %s)' %
                    (uri, typeName, self.type[0], self.type[1]), ps.Backtrace(elt))

        if not issubclass(subclass, pyclass) and subclass(None) and not issubclass(subclass, pyclass):
            raise TypeError(
                    'Substitute Type (%s, %s) is not derived from %s' %
                    (self.type[0], self.type[1], pyclass), ps.Backtrace(elt))

        return subclass((self.nspname, self.pname))
コード例 #3
0
 def FindLocalHREF(self, href, elt, headers=1):
     '''Find a local HREF in the data elements.
     '''
     if href[0] != '#':
         raise EvaluateException(
             'Absolute HREF ("%s") not implemented' % href,
             self.Backtrace(elt))
     frag = href[1:]
     # Already found?
     e = self.id_cache.get(frag)
     if e: return e
     # Do a breadth-first search, in the data first.  Most likely
     # to find multi-ref targets shallow in the data area.
     list = self.data_elements[:] + [self.body_root]
     if headers: list.extend(self.header_elements)
     while list:
         e = list.pop()
         if e.nodeType == _Node.ELEMENT_NODE:
             nodeid = _find_id(e)
             if nodeid:
                 self.id_cache[nodeid] = e
                 if nodeid == frag: return e
         list += _children(e)
     raise EvaluateException('''Can't find node for HREF "%s"''' % href,
             self.Backtrace(elt))
コード例 #4
0
ファイル: TCcompound.py プロジェクト: zenky41/pySphere
    def parse(self, elt, ps):
        debug = self.logger.debugOn()
        debug and self.logger.debug('parse')

        xtype = self.checkname(elt, ps)
        if self.type and xtype not in [self.type, (None, None)]:
            if not isinstance(self, TypeDefinition):
                raise EvaluateException(\
                    'ComplexType for %s has wrong type(%s), looking for %s' %
                        (self.pname, self.checktype(elt,ps), self.type),
                                        ps.Backtrace(elt))
            else:
                #TODO: mabye change MRO to handle this
                debug and self.logger.debug('delegate to substitute type')
                what = TypeDefinition.getSubstituteType(self, elt, ps)
                return what.parse(elt, ps)

        href = _find_href(elt)
        if href:
            if _children(elt):
                raise EvaluateException('Struct has content and HREF',
                                        ps.Backtrace(elt))
            elt = ps.FindLocalHREF(href, elt)
        c = _child_elements(elt)
        if self.nilled(elt, ps): return Nilled

        # Create the object.
        if self.pyclass:
            # type definition must be informed of element tag (nspname,pname),
            # element declaration is initialized with a tag.
            try:
                pyobj = self.pyclass()
            except Exception, e:
                raise TypeError("Constructing element (%s,%s) with pyclass(%s), %s" \
                    %(self.nspname, self.pname, self.pyclass.__name__, str(e)))
コード例 #5
0
ファイル: TCcompound.py プロジェクト: dhtech/pysphere
 def parse(self, elt, ps):
     href = _find_href(elt)
     if href:
         if _children(elt):
             raise EvaluateException('Array has content and HREF',
                     ps.Backtrace(elt))
         elt = ps.FindLocalHREF(href, elt)
     if self.nilled(elt, ps): return Nilled
     if not _find_arraytype(elt) and self.undeclared is False:
         raise EvaluateException('Array expected', ps.Backtrace(elt))
     t = _find_type(elt)
     if t:
         pass # XXX should check the type, but parsing that is hairy.
     offset = self.parse_offset(elt, ps)
     v, vlen = [], 0
     if offset and not self.sparse:
         while vlen < offset:
             vlen += 1
             v.append(self.fill)
     for c in _child_elements(elt):
         item = self.ofwhat.parse(c, ps)
         position = self.parse_position(c, ps) or offset
         if self.sparse:
             v.append((position, item))
         else:
             while offset < position:
                 offset += 1
                 v.append(self.fill)
             v.append(item)
         offset += 1
     return v
コード例 #6
0
ファイル: TCcompound.py プロジェクト: dhtech/pysphere
 def parse_position(self, elt, ps):
     o = _find_arrayposition(elt)
     if not o: return None
     if o.find(',') > -1:
         raise EvaluateException('Sorry, no multi-dimensional arrays',
                 ps.Backtrace(elt))
     if not _position_pat.match(o):
         raise EvaluateException('Bad array position "' + o + '"',
                 ps.Backtrace(elt))
     return int(o[1:-1])
コード例 #7
0
 def ResolveHREF(self, uri, tc, **keywords):
     r = getattr(tc, 'resolver', self.resolver)
     if not r:
         raise EvaluateException('No resolver for "' + uri + '"')
     try:
         if isinstance(uri, unicode): uri = str(uri)
         retval = r(uri, tc, self, **keywords)
     except Exception, e:
         raise EvaluateException("""Can't resolve '""" + uri + "' (" + \
             str(e.__class__) + "): " + str(e))
コード例 #8
0
 def ResolveHREF(self, uri, tc, **keywords):
     r = getattr(tc, 'resolver', self.resolver)
     if not r:
         raise EvaluateException('No resolver for "' + uri + '"')
     try:
         if type(uri) == str: uri = str(uri)
         retval = r(uri, tc, self, **keywords)
     except Exception as e:
         raise EvaluateException('''Can't resolve "''' + uri + '" (' + \
             str(e.__class__) + "): " + str(e))
     return retval
コード例 #9
0
ファイル: address.py プロジェクト: zenky41/pySphere
    def setRequest(self, endPointReference, action):
        '''Call For Request
        '''
        self._action = action
        self.header_pyobjs = None
        pyobjs = []
        namespaceURI = self.wsAddressURI
        addressTo = self._addressTo
        messageID = self._messageID = "uuid:%s" %time.time()

        # Set Message Information Headers
        # MessageID
        typecode = GED(namespaceURI, "MessageID")
        pyobjs.append(typecode.pyclass(messageID))

        # Action
        typecode = GED(namespaceURI, "Action")
        pyobjs.append(typecode.pyclass(action))

        # To
        typecode = GED(namespaceURI, "To")
        pyobjs.append(typecode.pyclass(addressTo))

        # From
        typecode = GED(namespaceURI, "From")
        mihFrom = typecode.pyclass()
        mihFrom._Address = self.anonymousURI
        pyobjs.append(mihFrom)

        if endPointReference:
            if hasattr(endPointReference, 'typecode') is False:
                raise EvaluateException('endPointReference must have a typecode attribute')

            if isinstance(endPointReference.typecode, \
                GTD(namespaceURI ,'EndpointReferenceType')) is False:
                raise EvaluateException('endPointReference must be of type %s'
                    %GTD(namespaceURI ,'EndpointReferenceType'))

            ReferenceProperties = getattr(endPointReference, '_ReferenceProperties', None)
            if ReferenceProperties is None: # In recent WS-A attribute name changed
                ReferenceProperties = getattr(endPointReference, '_ReferenceParameters', None)
            if ReferenceProperties is not None:
                for v in getattr(ReferenceProperties, '_any', ()):
                    if not hasattr(v,'typecode'):
                        raise EvaluateException('<any> element, instance missing typecode attribute')

                    pyobjs.append(v)

        self.header_pyobjs = tuple(pyobjs)
コード例 #10
0
ファイル: TCtimes.py プロジェクト: trb116/pythonanalyzer
 def text_to_data(self, text, elt, ps):
     '''convert text into typecode specific data.
     '''
     if text is None:
         return None
     m = Duration.lex_pattern.match(text)
     if m is None:
         raise EvaluateException('Illegal duration', ps.Backtrace(elt))
     d = m.groupdict()
     if d['T'] and (d['h'] is None and d['m'] is None and d['s'] is None):
         raise EvaluateException('Duration has T without time')
     try:
         retval = _dict_to_tuple(d)
     except ValueError, e:
         raise EvaluateException(str(e))
コード例 #11
0
ファイル: TCtimes.py プロジェクト: trb116/pythonanalyzer
    def text_to_data(self, text, elt, ps):
        '''convert text into typecode specific data.
        '''
        if text is None:
            return None

        m = self.lex_pattern.match(text)
        if not m:
            raise EvaluateException('Bad Gregorian: %s' % text,
                                    ps.Backtrace(elt))
        try:
            retval = _dict_to_tuple(m.groupdict())
        except ValueError:
            raise

        if self.fix_timezone:
            retval = _fix_timezone(retval,
                                   tz_from=m.groupdict().get('tz'),
                                   tz_to=None)

        retval = _fix_none_fields(retval)

        if self.pyclass is not None:
            return self.pyclass(retval)
        return retval
コード例 #12
0
ファイル: TCcompound.py プロジェクト: dhtech/pysphere
 def parse_offset(self, elt, ps):
     o = _find_arrayoffset(elt)
     if not o: return 0
     if not _offset_pat.match(o):
         raise EvaluateException('Bad offset "' + o + '"',
                     ps.Backtrace(elt))
     return int(o[1:-1])
コード例 #13
0
 def parse(self, elt, ps):
     val = Decimal.parse(self, elt, ps)
     if val not in self.choices:
         raise EvaluateException('Value "' + str(val) + \
                     '" not in enumeration list',
                 ps.Backtrace(elt))
     return val
コード例 #14
0
 def XML(self, uri, tc, ps, **keywords):
     content = self.get(uri)
     if content:
         dom = ps.readerclass().fromStream(content)
         return _child_elements(dom)[0]
     if not self.next: raise EvaluateException("Unresolvable URI " + uri)
     return self.next.XML(uri, tc, ps, **keywords)
コード例 #15
0
ファイル: address.py プロジェクト: dhtech/pysphere
    def setUp(self):
        '''Look for WS-Address
        '''
        toplist = [wsa for wsa in WSA_LIST if wsa.ADDRESS==self.wsAddressURI]
        epr = 'EndpointReferenceType'
        for WSA in toplist+WSA_LIST:
            if (self.wsAddressURI is not None and self.wsAddressURI != WSA.ADDRESS) or \
                _has_type_definition(WSA.ADDRESS, epr) is True:
                break
        else:
            raise EvaluateException('enabling wsAddressing requires the inclusion of that namespace')

        self.wsAddressURI = WSA.ADDRESS
        self.anonymousURI = WSA.ANONYMOUS
        self._replyTo = WSA.ANONYMOUS
コード例 #16
0
ファイル: address.py プロジェクト: dhtech/pysphere
    def _getWSAddressTypeCodes(self, **kw):
        '''kw -- namespaceURI keys with sequence of element names.
        '''
        typecodes = []
        try:
            for nsuri,elements in list(kw.items()):
                for el in elements:
                    typecode = GED(nsuri, el)
                    if typecode is None:
                        raise WSActionException('Missing namespace, import "%s"' %nsuri)

                    typecodes.append(typecode)
            else:
                pass
        except EvaluateException as ex:
            raise EvaluateException('To use ws-addressing register typecodes for namespace(%s)' %self.wsAddressURI)
        return typecodes
コード例 #17
0
ファイル: TCtimes.py プロジェクト: dhtech/pysphere
    def text_to_data(self, text, elt, ps):
        '''convert text into typecode specific data.
        '''
        if text is None:
            return None

        m = self.lex_pattern.match(text)
        if not m:
            raise EvaluateException('Bad Gregorian: %s' % text,
                                    ps.Backtrace(elt))
        try:
            retval = _dict_to_tuple(m.groupdict())
        except ValueError as e:
            #raise EvaluateException(str(e))
            raise

        if self.pyclass is not None:
            return self.pyclass(retval)
        return retval
コード例 #18
0
 def get_pythontype(self, msg_type, targetNamespace, typeclass=None):
     if not typeclass:
         tc = self.get_typeclass(msg_type, targetNamespace)
     else:
         tc = typeclass
     if tc in self._tc_to_int:
         return 'int'
     elif tc in self._tc_to_float:
         return 'float'
     elif tc in self._tc_to_string:
         return 'str'
     elif tc in self._tc_to_tuple:
         return 'tuple'
     elif tc in [TCcompound.Array]:
         return 'list'
     elif tc in [TC.Boolean]:
         return 'bool'
     elif isinstance(tc, TypeCode):
         raise EvaluateException(
             'failed to map zsi typecode to a python type')
     return None
コード例 #19
0
 def _check_allowed(self, uri):
     for a in self.allowed:
         if uri.startswith(a): return
     raise EvaluateException("Disallowed URI prefix")
コード例 #20
0
 def serialize(self, elt, sw, pyobj, name=None, orig=None, **kw):
     if pyobj not in self.choices:
         raise EvaluateException('Value not in int enumeration list',
                                 ps.Backtrace(elt))
     Integer.serialize(self, elt, sw, pyobj, name=name, orig=orig, **kw)
コード例 #21
0
 def Opaque(self, uri, tc, ps, **keywords):
     content = self.get(uri)
     if content: return content.getvalue()
     if not self.next: raise EvaluateException("Unresolvable URI " + uri)
     return self.next.Opaque(uri, tc, ps, **keywords)
コード例 #22
0
ファイル: TCcompound.py プロジェクト: zenky41/pySphere
                        else:
                            setattr(pyobj, what.aname, [value])
                        c[j] = None
                        continue
                    else:
                        setattr(pyobj, what.aname, value)
                    c[j] = None
                    break

                if debug:
                    self.logger.debug("no element (%s,%s)", what.nspname,
                                      what.pname)

                # No match; if it was supposed to be here, that's an error.
                if self.inorder is True and i == j:
                    raise EvaluateException('Out of order complexType',
                                            ps.Backtrace(c_elt))
                else:
                    if hasattr(what, 'default'):
                        setattr(pyobj, what.aname, what.default)
                    # elif what.minOccurs > 0 and not hasattr(pyobj, what.aname):
                    #     raise EvaluateException('Element "' + what.aname + '" missing from complexType', ps.Backtrace(elt))

        if isinstance(pyobj, ComplexType._DictHolder):
            return pyobj.__dict__

        return pyobj

    def serialize(self, elt, sw, pyobj, inline=False, name=None, **kw):
        if inline or self.inline:
            self.cb(elt, sw, pyobj, name=name, **kw)
        else:
コード例 #23
0
ファイル: TCcompound.py プロジェクト: dlopes7/pysphere
    def parse(self, elt, ps):
        debug = self.logger.debugOn()
        debug and self.logger.debug('parse')

        xtype = self.checkname(elt, ps)
        if self.type and xtype not in [self.type, (None, None)]:
            if not isinstance(self, TypeDefinition):
                raise EvaluateException(\
                    'ComplexType for %s has wrong type(%s), looking for %s' %
                        (self.pname, self.checktype(elt,ps), self.type),
                                        ps.Backtrace(elt))
            else:
                #TODO: mabye change MRO to handle this
                debug and self.logger.debug('delegate to substitute type')
                what = TypeDefinition.getSubstituteType(self, elt, ps)
                return what.parse(elt, ps)

        href = _find_href(elt)
        if href:
            if _children(elt):
                raise EvaluateException('Struct has content and HREF',
                                        ps.Backtrace(elt))
            elt = ps.FindLocalHREF(href, elt)
        c = _child_elements(elt)
        if self.nilled(elt, ps): return Nilled

        # Create the object.
        if self.pyclass:
            # type definition must be informed of element tag (nspname,pname),
            # element declaration is initialized with a tag.
            try:
                pyobj = self.pyclass()
            except Exception as e:
                raise TypeError("Constructing element (%s,%s) with pyclass(%s), %s" \
                    %(self.nspname, self.pname, self.pyclass.__name__, str(e)))
        else:
            pyobj = ComplexType._DictHolder()

        # parse all attributes contained in attribute_typecode_dict (user-defined attributes),
        # the values (if not None) will be keyed in self.attributes dictionary.
        attributes = self.parse_attributes(elt, ps)
        if attributes:
            setattr(pyobj, self.attrs_aname, attributes)

        #MIXED
        if self.mixed is True:
            setattr(pyobj, self.mixed_aname,
                    self.simple_value(elt, ps, mixed=True))

        # Clone list of kids (we null it out as we process)
        c, crange = c[:], list(range(len(c)))
        # Loop over all items we're expecting

        for j, c_elt in [(j, c[j]) for j in crange if c[j]]:

            for i, what in [(i, self.ofwhat[i])
                            for i in range(len(self.ofwhat))]:

                # retrieve typecode if it is hidden
                if isinstance(what, collections.Callable): what = what()

                # Loop over all available kids
                # if debug:
                #     self.logger.debug("what: (%s,%s)", what.nspname, what.pname)

                # Parse value, and mark this one done.
                # if debug:
                #     self.logger.debug("child node: (%s,%s)", c_elt.namespaceURI, c_elt.tagName)
                match = False
                if what.name_match(c_elt):
                    match = True
                    value = what.parse(c_elt, ps)
                elif isinstance(what, AnyElement):
                    match = True
                    value = what.parse(c_elt, ps)
                else:
                    # substitutionGroup head must be a global element declaration
                    # if successful delegate to matching GED
                    subwhat = _get_substitute_element(what, c_elt, ps)
                    if subwhat:
                        match = True
                        value = subwhat.parse(c_elt, ps)

                    if debug:
                        self.logger.debug("substitutionGroup: %s", subwhat)

                if match:
                    if what.maxOccurs > 1:
                        attr = getattr(pyobj, what.aname, None)
                        if attr is not None:
                            attr.append(value)
                        else:
                            setattr(pyobj, what.aname, [value])
                        c[j] = None
                        continue
                    else:
                        setattr(pyobj, what.aname, value)
                    c[j] = None
                    break

                if debug:
                    self.logger.debug("no element (%s,%s)", what.nspname,
                                      what.pname)

                # No match; if it was supposed to be here, that's an error.
                if self.inorder is True and i == j:
                    raise EvaluateException('Out of order complexType',
                                            ps.Backtrace(c_elt))
                else:
                    if hasattr(what, 'default'):
                        setattr(pyobj, what.aname, what.default)
                    # elif what.minOccurs > 0 and not hasattr(pyobj, what.aname):
                    #     raise EvaluateException('Element "' + what.aname + '" missing from complexType', ps.Backtrace(elt))

        if isinstance(pyobj, ComplexType._DictHolder):
            return pyobj.__dict__

        return pyobj
コード例 #24
0
 def serialize(self, elt, sw, pyobj, name=None, orig=None, **kw):
     if pyobj not in self.choices:
         raise EvaluateException('Value not in int enumeration list')
     Decimal.serialize(self, elt, sw, pyobj, name=name, orig=orig, **kw)
コード例 #25
0
ファイル: TCcompound.py プロジェクト: dhtech/pysphere
    def cb(self, elt, sw, pyobj, name=None, **kw):
        debug = self.logger.debugOn()
        if debug:
            self.logger.debug("cb: %s" %str(self.ofwhat))

        objid = _get_idstr(pyobj)
        ns,n = self.get_name(name, objid)
        if pyobj is None:
            if self.nillable is True:
                elem = elt.createAppendElement(ns, n)
                self.serialize_as_nil(elem)
                return
            raise EvaluateException('element(%s,%s) is not nillable(%s)' %(
                self.nspname,self.pname,self.nillable))

        if self.mutable is False and sw.Known(pyobj): 
            return
        
        if debug:
            self.logger.debug("element: (%s, %s)", str(ns), n)
            
        if n is not None:
            elem = elt.createAppendElement(ns, n)
            self.set_attributes(elem, pyobj)
            if kw.get('typed', self.typed) is True:
                self.set_attribute_xsi_type(elem)

            #MIXED For now just stick it in front.
            if self.mixed is True and self.mixed_aname is not None:
               if hasattr(pyobj, self.mixed_aname):
                   textContent = getattr(pyobj, self.mixed_aname)
                   if hasattr(textContent, 'typecode'):
                       textContent.typecode.serialize_text_node(elem, sw, textContent)
                   elif type(textContent) in _stringtypes:
                       if debug:
                           self.logger.debug("mixed text content:\n\t%s", 
                                             textContent)
                       elem.createAppendTextNode(textContent)
                   else:
                       raise EvaluateException('mixed test content in element (%s,%s) must be a string type' %(
                           self.nspname,self.pname), sw.Backtrace(elt))
               else:
                   if debug:
                       self.logger.debug("mixed NO text content in %s", 
                                         self.mixed_aname)
        else:
            #For information items w/o tagNames 
            #  ie. model groups,SOAP-ENC:Header
            elem = elt

        if self.inline:
            pass
        elif not self.inline and self.unique:
            raise EvaluateException('Not inline, but unique makes no sense. No href/id.',
                sw.Backtrace(elt))
        elif n is not None:
            self.set_attribute_id(elem, objid)

        if self.pyclass and type(self.pyclass) is type:
            f = lambda attr: getattr(pyobj, attr, None)
        elif self.pyclass:
            d = pyobj.__dict__
            f = lambda attr: d.get(attr)
        else:
            d = pyobj
            f = lambda attr: pyobj.get(attr)
            if TypeCode.typechecks and type(d) != dict:
                raise TypeError("Classless complexType didn't get dictionary")

        indx, lenofwhat = 0, len(self.ofwhat)
        if debug:
            self.logger.debug('element declaration (%s,%s)', self.nspname, 
                              self.pname)
            if self.type:
                self.logger.debug('xsi:type definition (%s,%s)', self.type[0], 
                                  self.type[1])
            else:
                self.logger.warning('NO xsi:type')

        while indx < lenofwhat:
            occurs = 0
            what = self.ofwhat[indx]
            
            # retrieve typecode if hidden
            if isinstance(what, collections.Callable): what = what()
            
            if debug:
                self.logger.debug('serialize what -- %s', 
                                  what.__class__.__name__)

            # No way to order <any> instances, so just grab any unmatched
            # anames and serialize them.  Only support one <any> in all content.
            # Must be self-describing instances

            # Regular handling of declared elements
            aname = what.aname
            v = f(aname)
            indx += 1
            if what.minOccurs == 0 and v is None: 
                continue

            # Default to typecode, if self-describing instance, and check 
            # to make sure it is derived from what.
            whatTC = what
            if (whatTC.maxOccurs == UNBOUNDED or whatTC.maxOccurs > 1) and v is not None:
                if type(v) not in _seqtypes:
                    raise EvaluateException('pyobj (%s,%s), aname "%s": maxOccurs %s, expecting a %s' %(
                         self.nspname,self.pname,what.aname,whatTC.maxOccurs,_seqtypes), 
                         sw.Backtrace(elt))

                for v2 in v: 
                    occurs += 1
                    if whatTC.maxOccurs != UNBOUNDED and occurs > whatTC.maxOccurs:
                        raise EvaluateException('occurances (%d) exceeded maxOccurs(%d) for <%s>' %(
                                occurs, whatTC.maxOccurs, what.pname), 
                                sw.Backtrace(elt))
                        
                    what = _get_type_or_substitute(whatTC, v2, sw, elt)
                    if debug and what is not whatTC:
                        self.logger.debug('substitute derived type: %s' %
                                          what.__class__)
                        
                    what.serialize(elem, sw, v2, **kw)
#                    try:
#                        what.serialize(elem, sw, v2, **kw)
#                    except Exception, e:
#                        raise EvaluateException('Serializing %s.%s, %s %s' %
#                            (n, whatTC.aname or '?', e.__class__.__name__, str(e)))

                if occurs < whatTC.minOccurs:
                    raise EvaluateException(\
                        'occurances(%d) less than minOccurs(%d) for <%s>' %
                        (occurs, whatTC.minOccurs, what.pname), sw.Backtrace(elt))
                    
                continue

            if v is not None or what.nillable is True:
                what = _get_type_or_substitute(whatTC, v, sw, elt)
                if debug and what is not whatTC:
                    self.logger.debug('substitute derived type: %s' %
                                      what.__class__)
                what.serialize(elem, sw, v, **kw)
#                try:
#                    what.serialize(elem, sw, v, **kw)
#                except (ParseException, EvaluateException), e:
#                    raise
#                except Exception, e:
#                    raise EvaluateException('Serializing %s.%s, %s %s' %
#                        (n, whatTC.aname or '?', e.__class__.__name__, str(e)),
#                        sw.Backtrace(elt))
                continue

            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))
コード例 #26
0
ファイル: TCcompound.py プロジェクト: dhtech/pysphere
    def parse(self, elt, ps):
        debug = self.logger.debugOn()
        debug and self.logger.debug('parse')
       
        xtype = self.checkname(elt, ps)
        if self.type and xtype not in [ self.type, (None,None) ]:
            if not isinstance(self, TypeDefinition):
                raise EvaluateException(\
                    'ComplexType for %s has wrong type(%s), looking for %s' %
                        (self.pname, self.checktype(elt,ps), self.type), 
                                        ps.Backtrace(elt))
            else:
                #TODO: mabye change MRO to handle this 
                debug and self.logger.debug('delegate to substitute type')
                what = TypeDefinition.getSubstituteType(self, elt, ps)
                return what.parse(elt, ps)
            
        href = _find_href(elt)
        if href:
            if _children(elt):
                raise EvaluateException('Struct has content and HREF',
                        ps.Backtrace(elt))
            elt = ps.FindLocalHREF(href, elt)
        c = _child_elements(elt)
        count = len(c)
        if self.nilled(elt, ps): return Nilled

        # Create the object.
        v = {}

        # parse all attributes contained in attribute_typecode_dict (user-defined attributes),
        # the values (if not None) will be keyed in self.attributes dictionary.
        attributes = self.parse_attributes(elt, ps)
        if attributes:
            v[self.attrs_aname] = attributes

        #MIXED
        if self.mixed is True:
            v[self.mixed_aname] = self.simple_value(elt,ps, mixed=True)

        # Clone list of kids (we null it out as we process)
        c, crange = c[:], list(range(len(c)))
        # Loop over all items we're expecting
        
        if debug:
            self.logger.debug("ofwhat: %s",str(self.ofwhat))
            
        any = None
        for i,what in [ (i, self.ofwhat[i]) for i in range(len(self.ofwhat)) ]:
            
            # retrieve typecode if it is hidden
            if isinstance(what, collections.Callable): what = what()
            
            # Loop over all available kids
            if debug: 
                self.logger.debug("what: (%s,%s)", what.nspname, what.pname)
                
            for j,c_elt in [ (j, c[j]) for j in crange if c[j] ]:
                # Parse value, and mark this one done. 
                if debug:
                    self.logger.debug("child node: (%s,%s)", c_elt.namespaceURI, c_elt.tagName)

                match = False
                if what.name_match(c_elt):
                    match = True
                    value = what.parse(c_elt, ps)
                else:
                    # substitutionGroup head must be a global element declaration
                    # if successful delegate to matching GED
                    subwhat = _get_substitute_element(what, c_elt, ps)
                    if subwhat:
                        match = True
                        value = subwhat.parse(c_elt, ps)

                    if debug: 
                        self.logger.debug("substitutionGroup: %s", subwhat)

                if match:
                    if what.maxOccurs == UNBOUNDED or what.maxOccurs > 1:
                        if what.aname in v:
                            v[what.aname].append(value)
                        else:
                            v[what.aname] = [value]
                        c[j] = None
                        continue
                    else:
                        v[what.aname] = value
                    c[j] = None
                    break

                if debug:
                    self.logger.debug("no element (%s,%s)", what.nspname, what.pname)

                # No match; if it was supposed to be here, that's an error.
                if self.inorder is True and i == j:
                    raise EvaluateException('Out of order complexType',
                            ps.Backtrace(c_elt))
            else:
                # only supporting 1 <any> declaration in content.
                if isinstance(what,AnyElement):
                    any = what
                elif hasattr(what, 'default'):
                    v[what.aname] = what.default
                elif what.minOccurs > 0 and what.aname not in v:
                    raise EvaluateException('Element "' + what.aname + \
                        '" missing from complexType', ps.Backtrace(elt))

        # Look for wildcards and unprocessed children
        # XXX Stick all this stuff in "any", hope for no collisions
        if any is not None:
            occurs = 0
            v[any.aname] = []
            for j,c_elt in [ (j, c[j]) for j in crange if c[j] ]:
                value = any.parse(c_elt, ps)
                if any.maxOccurs == UNBOUNDED or any.maxOccurs > 1:
                    v[any.aname].append(value)
                else:
                    v[any.aname] = value

                occurs += 1

            # No such thing as nillable <any>
            if any.maxOccurs == 1 and occurs == 0:
                v[any.aname] = None
            elif occurs < any.minOccurs or (any.maxOccurs!=UNBOUNDED and any.maxOccurs<occurs):
                raise EvaluateException('occurances of <any> elements(#%d) bound by (%d,%s)' %(
                    occurs, any.minOccurs,str(any.maxOccurs)), ps.Backtrace(elt))

        if not self.pyclass: 
            return v

        # type definition must be informed of element tag (nspname,pname),
        # element declaration is initialized with a tag.
        try:
            pyobj = self.pyclass()
        except Exception as e:
            raise TypeError("Constructing element (%s,%s) with pyclass(%s), %s" \
                %(self.nspname, self.pname, self.pyclass.__name__, str(e)))
        for key in list(v.keys()):
            setattr(pyobj, key, v[key])
        return pyobj