Exemplo n.º 1
0
    def feedFrom(self, other, ctx=None, suppress=set()):
        """feeds parsed objects from another structure.

		This only works if the other structure is a of the same or a superclass
		of self.
		"""
        from gavo.base import xmlstruct
        if ctx is None:
            ctx = parsecontext.ParseContext()
        evProc = xmlstruct.EventProcessor(None, ctx)
        evProc.setRoot(self)
        for ev in other.iterEvents():
            evProc.feed(*ev)
Exemplo n.º 2
0
def parseFromStream(rootStruct, inputStream, context=None):
    """parses a tree rooted in rootStruct from some file-like object inputStream.

	It returns the root element of the resulting tree.  If rootStruct is
	a type subclass, it will be instanciated to create a root
	element, if it is an instance, this instance will be the root.
	"""
    eventSource = utils.iterparse(inputStream)
    if context is None:
        context = parsecontext.ParseContext()
    context.setEventSource(eventSource)
    res = feedTo(rootStruct, eventSource, context)
    context.runExitFuncs(res)
    return res
Exemplo n.º 3
0
    def change(self, **kwargs):
        """returns a copy of self with all attributes in kwargs overridden with
		the passed values.
		"""
        parent = kwargs.pop("parent_", self.parent)
        attrs = self.getCopyableAttributes(kwargs)
        attrs.update(kwargs)

        newInstance = self.__class__(parent, **attrs).finishElement(
            parsecontext.ParseContext())

        # reparent things without a parent to newInstance.  We don't want to do
        # this unconditionally since we unwisely share some structs.
        for _, value in kwargs.iteritems():
            if not isinstance(value, list):
                value = [value]
            for item in value:
                if hasattr(item, "parent") and item.parent is None:
                    item.parent = newInstance

        return newInstance
Exemplo n.º 4
0
    def copy(self, parent):
        """returns a deep copy of self, reparented to parent.
		"""
        return self.__class__(parent,
                              **self.getCopyableAttributes()).finishElement(
                                  parsecontext.ParseContext())