예제 #1
0
	def startElementNS(self, uri_localname, qname, attrs):
		(uri, localname) = uri_localname
		try:
			start_handler = self._startElementHandlers[(uri, localname)]
		except KeyError:
			raise ElementError("unknown element %s for namespace %s" % (localname, uri or NameSpace))
		attrs = AttributesImpl(dict((attrs.getQNameByName(name), value) for name, value in attrs.items()))
		try:
			self.current = self.current.appendChild(start_handler(self.current, attrs))
		except Exception as e:
			raise type(e)("line %d: %s" % (self._locator.getLineNumber(), str(e)))
예제 #2
0
파일: ligolw.py 프로젝트: Solaro/lalsuite
	def __init__(self, attrs = None):
		"""
		Construct an element.  The argument is a
		sax.xmlreader.AttributesImpl object (see the xml.sax
		documentation, but it's basically a dictionary-like thing)
		used to set the element attributes.
		"""
		self.parentNode = None
		if attrs is None:
			self.attributes = AttributesImpl({})
		elif set(attrs.keys()) <= self.validattributes:
			self.attributes = attrs
		else:
			raise ElementError("%s element does not have attribute(s) %s" % (self.tagName, ", ".join("'%s'" % key for key in set(attrs.keys()) - self.validattributes)))
		self.childNodes = []
		self.pcdata = None
예제 #3
0
    def _write_seq(self, record):
        """Write the sequence (PRIVATE).

        Note that SeqXML requires a DNA, RNA or protein alphabet.
        """
        if isinstance(record.seq, UnknownSeq):
            raise TypeError(
                "Sequence type is UnknownSeq but SeqXML requires sequence")

        seq = str(record.seq)

        if not len(seq) > 0:
            raise ValueError("The sequence length should be greater than 0")

        molecule_type = record.annotations.get("molecule_type")
        if molecule_type is not None:
            if "DNA" in molecule_type:
                seqElem = "DNAseq"
            elif "RNA" in molecule_type:
                seqElem = "RNAseq"
            elif molecule_type == "protein":
                seqElem = "AAseq"
            else:
                raise ValueError("unknown molecule_type '%s'" % molecule_type)
        else:
            # Get the base alphabet (underneath any Gapped or StopCodon encoding)
            alpha = Alphabet._get_base_alphabet(record.seq.alphabet)
            if isinstance(alpha, Alphabet.RNAAlphabet):
                seqElem = "RNAseq"
            elif isinstance(alpha, Alphabet.DNAAlphabet):
                seqElem = "DNAseq"
            elif isinstance(alpha, Alphabet.ProteinAlphabet):
                seqElem = "AAseq"
            else:
                raise ValueError("Need a DNA, RNA or Protein alphabet")

        self.xml_generator.startElement(seqElem, AttributesImpl({}))
        self.xml_generator.characters(seq)
        self.xml_generator.endElement(seqElem)
예제 #4
0
def _emit(key, value, content_handler,
          attr_prefix='@',
          cdata_key='#text',
          root=True,
          preprocessor=None):
    if preprocessor is not None:
        result = preprocessor(key, value)
        if result is None:
            return
        key, value = result
    if not isinstance(value, (list, tuple)):
        value = [value]
    if root and len(value) > 1:
        raise ValueError('document with multiple roots')
    for v in value:
        if v is None:
            v = OrderedDict()
        elif not isinstance(v, dict):
            v = _unicode(v)
        if isinstance(v, _basestring):
            v = OrderedDict(((cdata_key, v),))
        cdata = None
        attrs = OrderedDict()
        children = []
        for ik, iv in list(v.items()):
            if ik == cdata_key:
                cdata = iv
                continue
            if ik.startswith(attr_prefix):
                attrs[ik[len(attr_prefix):]] = iv
                continue
            children.append((ik, iv))
        content_handler.startElement(key, AttributesImpl(attrs))
        for child_key, child_value in children:
            _emit(child_key, child_value, content_handler,
                  attr_prefix, cdata_key, False, preprocessor)
        if cdata is not None:
            content_handler.characters(cdata)
        content_handler.endElement(key)
예제 #5
0
    def write_header(self):
        """Write root node with document metadata."""
        SequentialSequenceWriter.write_header(self)

        attrs = {"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
                 "xsi:noNamespaceSchemaLocation": "http://www.seqxml.org/0.4/seqxml.xsd",
                 "seqXMLversion": "0.4"}

        if self.source is not None:
            attrs["source"] = self.source
        if self.source_version is not None:
            attrs["sourceVersion"] = self.source_version
        if self.species is not None:
            if not isinstance(self.species, basestring):
                raise TypeError("species should be of type string")
            attrs["speciesName"] = self.species
        if self.ncbiTaxId is not None:
            if not isinstance(self.ncbiTaxId, (basestring, int)):
                raise TypeError("ncbiTaxID should be of type string or int")
            attrs["ncbiTaxID"] = self.ncbiTaxId

        self.xml_generator.startElement("seqXML", AttributesImpl(attrs))
예제 #6
0
 def start(self, content_handler: 'BoilerpipeBaseParser', tag_name: str, attrs: AttributesImpl) -> bool:
     size_attr = attrs.getValue("size")
     size = None
     if size_attr is not None:
         match = self.PAT_FONT_SIZE.match(size_attr)
         if match is not None:
             rel = match.group(0)
             val = match.group(1)
             # absolute
             if len(rel) == 0:
                 size = val
             # relative
             else:
                 # last non-none element from stack, default 3
                 last_non_none = (s for s in content_handler.font_size_stack[::-1] if s is not None)
                 prev_size = next(last_non_none, 3)
                 if rel[0] == '+':
                     size = prev_size + val
                 else:
                     size = prev_size - val
     content_handler.font_size_stack.append(size)
     return False
예제 #7
0
    def write_record(self, record):
        """Write one record."""
        if not record.id or record.id == "<unknown id>":
            raise ValueError("SeqXML requires identifier")

        if not isinstance(record.id, basestring):
            raise TypeError("Identifier should be of type string")

        attrb = {"id": record.id}

        if "source" in record.annotations and self.source != record.annotations["source"]:
            if not isinstance(record.annotations["source"], basestring):
                raise TypeError("source should be of type string")
            attrb["source"] = record.annotations["source"]

        self.xml_generator.startElement("entry", AttributesImpl(attrb))
        self._write_species(record)
        self._write_description(record)
        self._write_seq(record)
        self._write_dbxrefs(record)
        self._write_properties(record)
        self.xml_generator.endElement("entry")
예제 #8
0
	def appendColumn(self, name):
		"""
		Append a Column element named "name" to the table.  Returns
		the new child.  Raises ValueError if the table already has
		a column by that name, and KeyError if the validcolumns
		attribute of this table does not contain an entry for a
		column by that name.

		Note that the name string is assumed to be "pre-stripped",
		that is it is the significant portion of the elements Name
		attribute.  The Column element's Name attribute will be
		constructed by pre-pending the stripped Table element's
		name and a colon.

		Example:

		>>> import lsctables
		>>> process_table = lsctables.New(lsctables.ProcessTable, [])
		>>> col = process_table.appendColumn("program")
		>>> col.getAttribute("Name")
		u'process:program'
		>>> col.Name
		u'program'
		"""
		try:
			self.getColumnByName(name)
			# if we get here the table already has that column
			raise ValueError("duplicate Column '%s'" % name)
		except KeyError:
			pass
		column = Column(AttributesImpl({u"Name": "%s:%s" % (self.Name, name), u"Type": self.validcolumns[name]}))
		streams = self.getElementsByTagName(ligolw.Stream.tagName)
		if streams:
			self.insertBefore(column, streams[0])
		else:
			self.appendChild(column)
		return column
예제 #9
0
def create_testdoc(_title, _content, _data_count, _data_text):
    xml_doc = BytesIO()
    try:
        xml_generator = XMLGenerator(xml_doc, 'UTF-8')
        start_element = lambda name, attrs: xml_generator.startElement(
            name, attrs)
        end_element = lambda name: xml_generator.endElement(name)
        text = lambda value: xml_generator.characters(value)
        attrs = lambda values: AttributesImpl(values)
        empty_attrs = attrs({})
        xml_generator.startDocument()
        start_element('html', attrs({'xmlns': XHTML_NAMESPACE}))
        start_element('head', empty_attrs)
        start_element('title', empty_attrs)
        text(_title)
        end_element('title')
        end_element('head')
        start_element('body', empty_attrs)
        start_element('h1', empty_attrs)
        text(_title)
        end_element('h1')
        start_element('p', empty_attrs)
        text(_content)
        end_element('p')
        for i in range(_data_count):
            start_element('div', attrs({'data-i': str(i)}))
            for j in range(_data_count):
                start_element('p', attrs({'data-j': str(j)}))
                text(_data_text)
                end_element('p')
            end_element('div')
        end_element('body')
        end_element('html')
        xml_generator.endDocument()
        return xml_doc.getvalue()
    finally:
        xml_doc.close()
예제 #10
0
    def _write_species(self, record):
        """Write the species if given."""

        if "organism" in record.annotations and "ncbi_taxid" in record.annotations:

            if not isinstance(record.annotations["organism"], basestring):
                raise TypeError("organism should be of type string")

            if not isinstance(record.annotations["ncbi_taxid"],
                              (basestring, int)):
                raise TypeError("ncbiTaxID should be of type string or int")

            #The local species definition is only written if it differs from the global species definition
            if record.annotations[
                    "organism"] != self.species or record.annotations[
                        "ncbi_taxid"] != self.ncbiTaxId:

                attr = {
                    "name": record.annotations["organism"],
                    "ncbiTaxID": record.annotations["ncbi_taxid"]
                }
                self.xml_generator.startElement("species",
                                                AttributesImpl(attr))
                self.xml_generator.endElement("species")
예제 #11
0
class FilteringLIGOLWContentHandler(LIGOLWContentHandler):
    """
	LIGO LW content handler that loads everything but those parts of a
	document that match some criteria.  Useful, for example, when one
	wishes to read everything except a single table from a file.

	Example:

	>>> from pycbc_glue.ligolw import utils
	>>> def contenthandler(document):
	...	return FilteringLIGOLWContentHandler(document, lambda name, attrs: name != ligolw.Table.tagName)
	...
	>>> xmldoc = utils.load_filename("test.xml", contenthandler = contenthandler)

	This parses "test.xml" and returns an XML tree with all the Table
	elements and their children removed.
	"""
    def __init__(self, document, element_filter):
        """
		Those elements for which element_filter(name, attrs)
		evaluates to False, and the children of those elements,
		will not be loaded.
		"""
        super(FilteringLIGOLWContentHandler, self).__init__(document)
        self.element_filter = element_filter
        self.depth = 0

    def startElementNS(self, (uri, localname), qname, attrs):
        filter_attrs = AttributesImpl(
            dict((attrs.getQNameByName(name), value)
                 for name, value in attrs.items()))
        if self.depth == 0 and self.element_filter(localname, filter_attrs):
            super(FilteringLIGOLWContentHandler, self).startElementNS(
                (uri, localname), qname, attrs)
        else:
            self.depth += 1
예제 #12
0
def startAtomFeed(filename, date):
    generator = XMLGenerator(open(filename, 'wb'), 'utf-8')

    generator.startDocument()

    generator.startElement(
        u'feed', AttributesImpl({u'xmlns': u'http://www.w3.org/2005/Atom'}))

    generator.startElement(u'id', AttributesImpl({}))
    generator.characters(u'http://www.wine-staging.com/news.xml')
    generator.endElement(u'id')

    generator.startElement(u'title', AttributesImpl({}))
    generator.characters(u'Wine Staging')
    generator.endElement(u'title')

    generator.startElement(u'updated', AttributesImpl({}))
    generator.characters(date.decode('utf8') + u"T00:00:00Z")
    generator.endElement(u'updated')

    generator.startElement(
        u'link', AttributesImpl({
            u'rel': u'self',
            u'href': u'/news.xml'
        }))
    generator.endElement(u'link')

    generator.startElement(
        u'link',
        AttributesImpl({
            u'rel': u'alternate',
            u'type': u'text/html',
            u'href': u'/news.html'
        }))
    generator.endElement(u'link')

    return generator
예제 #13
0
 def handle_start_tag(self, name, attrs):
     self._cont_handler.startElement(name, AttributesImpl(attrs))
예제 #14
0
def AttributesUnion(base, **values):
    baseitems = dict(base)
    baseitems.update(values)
    return AttributesImpl(baseitems)
 def write_deleted_tunable(self, deleted_tunable_name):
     attr_vals = {Attributes.Name: deleted_tunable_name}
     self._writer.startElement(Tags.Deleted,
                               AttributesImpl(attr_vals),
                               can_close=True)
     self._writer.endElement(Tags.Deleted)
예제 #16
0
파일: gensimple.py 프로젝트: zhou0/ibus
 def startDocument(self):
     if self.__downstream:
         self.__downstream.startDocument()
         self.__downstream.startElement('engines', AttributesImpl({}))
예제 #17
0
class Element(object):
	"""
	Base class for all element types.  This class is inspired by the
	class of the same name in the Python standard library's xml.dom
	package.  One important distinction is that the standard DOM
	element is used to represent the structure of a document at a much
	finer level of detail than here.  For example, in the case of the
	standard DOM element, each XML attribute is its own element being a
	child node of its tag, while here they are simply stored as
	attributes of the tag element itself.

	Despite the differences, the documentation for the xml.dom package,
	particularly that of the Element class and it's parent, the Node
	class, is useful as supplementary material in understanding how to
	use this class.
	"""
	# XML tag names are case sensitive:  compare with ==, !=, etc.
	tagName = None
	validchildren = frozenset()

	@classmethod
	def validattributes(cls):
		return frozenset(name for name in dir(cls) if isinstance(getattr(cls, name), attributeproxy))

	def __init__(self, attrs = None):
		"""
		Construct an element.  The argument is a
		sax.xmlreader.AttributesImpl object (see the xml.sax
		documentation, but it's basically a dictionary-like thing)
		used to set the element attributes.
		"""
		self.parentNode = None
		if attrs is None:
			self.attributes = AttributesImpl({})
		elif set(attrs.keys()) <= self.validattributes():
			self.attributes = attrs
		else:
			raise ElementError("%s element: invalid attribute(s) %s" % (self.tagName, ", ".join("'%s'" % key for key in set(attrs.keys()) - self.validattributes())))
		self.childNodes = []
		self.pcdata = None

	def start_tag(self, indent):
		"""
		Generate the string for the element's start tag.
		"""
		return u"%s<%s%s>" % (indent, self.tagName, u"".join(u" %s=\"%s\"" % keyvalue for keyvalue in self.attributes.items()))

	def end_tag(self, indent):
		"""
		Generate the string for the element's end tag.
		"""
		return u"%s</%s>" % (indent, self.tagName)

	def appendChild(self, child):
		"""
		Add a child to this element.  The child's parentNode
		attribute is updated, too.
		"""
		self.childNodes.append(child)
		child.parentNode = self
		self._verifyChildren(len(self.childNodes) - 1)
		return child

	def insertBefore(self, newchild, refchild):
		"""
		Insert a new child node before an existing child. It must
		be the case that refchild is a child of this node; if not,
		ValueError is raised. newchild is returned.
		"""
		for i, childNode in enumerate(self.childNodes):
			if childNode is refchild:
				self.childNodes.insert(i, newchild)
				newchild.parentNode = self
				self._verifyChildren(i)
				return newchild
		raise ValueError(refchild)

	def removeChild(self, child):
		"""
		Remove a child from this element.  The child element is
		returned, and it's parentNode element is reset.  If the
		child will not be used any more, you should call its
		unlink() method to promote garbage collection.
		"""
		for i, childNode in enumerate(self.childNodes):
			if childNode is child:
				del self.childNodes[i]
				child.parentNode = None
				return child
		raise ValueError(child)

	def unlink(self):
		"""
		Break internal references within the document tree rooted
		on this element to promote garbage collection.
		"""
		self.parentNode = None
		for child in self.childNodes:
			child.unlink()
		del self.childNodes[:]

	def replaceChild(self, newchild, oldchild):
		"""
		Replace an existing node with a new node. It must be the
		case that oldchild is a child of this node; if not,
		ValueError is raised. newchild is returned.
		"""
		# .index() would use compare-by-value, we want
		# compare-by-id because we want to find the exact object,
		# not something equivalent to it.
		for i, childNode in enumerate(self.childNodes):
			if childNode is oldchild:
				self.childNodes[i].parentNode = None
				self.childNodes[i] = newchild
				newchild.parentNode = self
				self._verifyChildren(i)
				return newchild
		raise ValueError(oldchild)

	def getElements(self, filter):
		"""
		Return a list of elements below and including this element
		for which filter(element) returns True.
		"""
		l = reduce(lambda l, e: l + e.getElements(filter), self.childNodes, [])
		if filter(self):
			l.append(self)
		return l

	def getElementsByTagName(self, tagName):
		return self.getElements(lambda e: e.tagName == tagName)

	def getChildrenByAttributes(self, attrs):
		l = []
		for c in self.childNodes:
			try:
				if reduce(lambda t, (k, v): t and (c.getAttribute(k) == v), attrs.iteritems(), True):
					l.append(c)
			except KeyError:
				pass
		return l

	def hasAttribute(self, attrname):
		return self.attributes.has_key(attrname)

	def getAttribute(self, attrname):
		return self.attributes[attrname]

	def setAttribute(self, attrname, value):
		# cafeful:  this digs inside an AttributesImpl object and
		# modifies its internal data.  probably not a good idea,
		# but I don't know how else to edit an attribute because
		# the stupid things don't export a method to do it.
		self.attributes._attrs[attrname] = unicode(value)

	def removeAttribute(self, attrname):
		# cafeful:  this digs inside an AttributesImpl object and
		# modifies its internal data.  probably not a good idea,
		# but I don't know how else to edit an attribute because
		# the stupid things don't export a method to do it.
		try:
			del self.attributes._attrs[attrname]
		except KeyError:
			pass

	def appendData(self, content):
		"""
		Add characters to the element's pcdata.
		"""
		if self.pcdata is not None:
			self.pcdata += content
		else:
			self.pcdata = content

	def _verifyChildren(self, i):
		"""
		Method used internally by some elements to verify that
		their children are from the allowed set and in the correct
		order following modifications to their child list.  i is
		the index of the child that has just changed.
		"""
		pass

	def endElement(self):
		"""
		Method invoked by document parser when it encounters the
		end-of-element event.
		"""
		pass

	def write(self, fileobj = sys.stdout, indent = u""):
		"""
		Recursively write an element and it's children to a file.
		"""
		fileobj.write(self.start_tag(indent))
		fileobj.write(u"\n")
		for c in self.childNodes:
			if c.tagName not in self.validchildren:
				raise ElementError("invalid child %s for %s" % (c.tagName, self.tagName))
			c.write(fileobj, indent + Indent)
		if self.pcdata is not None:
			fileobj.write(xmlescape(self.pcdata))
			fileobj.write(u"\n")
		fileobj.write(self.end_tag(indent))
		fileobj.write(u"\n")
예제 #18
0
    def dump_xml(self, output_dir):
        """
        Dumps test result to xml
        """
        self.buffer = False

        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        with open(os.path.join(output_dir, 'junit.xml'), 'w') as output:
            document = XMLGenerator(output, 'utf-8')
            document.startDocument()
            document.startElement('testsuites', AttributesImpl({}))

            suites = groupby(self.testInfos,
                             key=lambda test_info: test_info.case_name)
            for suite_name, suite in suites:
                document.startElement('testsuite',
                                      AttributesImpl({'name': suite_name}))

                for test_info in suite:
                    document.startElement(
                        'testcase',
                        AttributesImpl({
                            'classname':
                            suite_name,
                            'name':
                            test_info.method_name,
                            'time':
                            '%3f' % total_seconds(test_info.end_time -
                                                  test_info.start_time)
                        }))

                    if test_info.result == TestInfo.RESULT.ERROR:
                        document.startElement(
                            'error',
                            AttributesImpl(
                                {'message': smart_text(test_info.err[1])}))
                        document.characters(
                            self._exc_info_to_string(test_info.err, test_info))
                        document.endElement('error')
                    elif test_info.result == TestInfo.RESULT.FAILURE:
                        document.startElement(
                            'failure',
                            AttributesImpl(
                                {'message': smart_text(test_info.err[1])}))
                        document.characters(
                            self._exc_info_to_string(test_info.err, test_info))
                        document.endElement('failure')
                    elif test_info.result == \
                                    TestInfo.RESULT.UNEXPECTED_SUCCESS:
                        document.startElement(
                            'error',
                            AttributesImpl({'message': 'Unexpected success'}))
                        document.endElement('error')
                    elif test_info.result == TestInfo.RESULT.SKIPPED:
                        document.startElement('skipped', AttributesImpl({}))
                        document.characters(test_info.reason)
                        document.endElement('skipped')

                    if test_info.stdout:
                        document.startElement('system-out', AttributesImpl({}))
                        document.characters(test_info.stdout)
                        document.endElement('system-out')

                    if test_info.stderr:
                        document.startElement('system-err', AttributesImpl({}))
                        document.characters(test_info.stderr)
                        document.endElement('system-err')
                    document.endElement('testcase')

                document.endElement('testsuite')

            document.endElement('testsuites')
            document.endDocument()
예제 #19
0
 def output(self, xg):
     xg.startElement("outline", AttributesImpl(self))
     for c in self.children:
         c.output(xg)
     xg.endElement("outline")
예제 #20
0
 def elemWithContent(name, content):
     xg.startElement(name, AttributesImpl({}))
     if content is not None:
         xg.characters(content)
     xg.endElement(name)
예제 #21
0
    def __init__(self, fname, options):
        # The commented code here is to enable write to file options with
        # value same as the default. IMHO it's useless so I've commented it

        #from PM.Gui.Plugins.Engine import PluginEngine

        #orig_dict = {}

        #for plug in PluginEngine().available_plugins:
        #    if plug.audit_type == -1:
        #        continue

        #    for conf_name, conf_dict in plug.configurations:
        #        orig_dict[conf_name] = conf_dict

        output = open(fname, 'w')
        self.depth_idx = -1
        self.writer = XMLGenerator(output, 'utf-8')
        self.writer.startDocument()

        self.startElement('configurations', {}),
        self.writer.characters('\n')

        items = options.keys()
        items.sort()

        trans = {bool: 'bool', int: 'int', float: 'float', str: 'str'}

        for key in items:
            self.startElement('configuration', AttributesImpl({'name': key}))
            self.writer.characters('\n')

            opts = options[key].items()
            opts.sort()

            for opt_id, (opt_val, opt_desc) in opts:
                #if key in orig_dict and opt_id in orig_dict[key] and \
                #   orig_dict[key][opt_id][0] == opt_val:
                #    continue

                try:
                    self.startElement(
                        trans[type(opt_val)],
                        AttributesImpl({
                            'id': opt_id,
                            'description': opt_desc
                        }))

                    if isinstance(opt_val, bool):
                        self.writer.characters(opt_val and '1' or '0')
                    else:
                        self.writer.characters(str(opt_val))

                    self.endElement(trans[type(opt_val)])
                except:
                    continue

            self.writer.characters('  ' * self.depth_idx)
            self.endElement('configuration')

        self.endElement('configurations')
        self.writer.endDocument()
        output.close()
예제 #22
0
파일: ligolw.py 프로젝트: Solaro/lalsuite
class Element(object):
	"""
	Base class for all element types.  This class is inspired by the
	class of the same name in the Python standard library's xml.dom
	package.  One important distinction is that the standard DOM
	element is used to represent the structure of a document at a much
	finer level of detail than here.  For example, in the case of the
	standard DOM element, each XML attribute is its own element being a
	child node of its tag, while here they are simply stored as
	attributes of the tag element itself.

	Despite the differences, the documentation for the xml.dom package,
	particularly that of the Element class and it's parent, the Node
	class, is useful as supplementary material in understanding how to
	use this class.
	"""
	# XML tag names are case sensitive:  compare with ==, !=, etc.
	tagName = None
	validattributes = frozenset()
	validchildren = frozenset()

	def __init__(self, attrs = None):
		"""
		Construct an element.  The argument is a
		sax.xmlreader.AttributesImpl object (see the xml.sax
		documentation, but it's basically a dictionary-like thing)
		used to set the element attributes.
		"""
		self.parentNode = None
		if attrs is None:
			self.attributes = AttributesImpl({})
		elif set(attrs.keys()) <= self.validattributes:
			self.attributes = attrs
		else:
			raise ElementError("%s element does not have attribute(s) %s" % (self.tagName, ", ".join("'%s'" % key for key in set(attrs.keys()) - self.validattributes)))
		self.childNodes = []
		self.pcdata = None

	def start_tag(self, indent):
		"""
		Generate the string for the element's start tag.
		"""
		s = indent + u"<" + self.tagName
		for keyvalue in self.attributes.items():
			s += u" %s=\"%s\"" % keyvalue
		s += u">"
		return s

	def end_tag(self, indent):
		"""
		Generate the string for the element's end tag.
		"""
		return indent + u"</" + self.tagName + u">"

	def appendChild(self, child):
		"""
		Add a child to this element.  The child's parentNode
		attribute is updated, too.
		"""
		self.childNodes.append(child)
		child.parentNode = self
		self._verifyChildren(len(self.childNodes) - 1)
		return child

	def insertBefore(self, newchild, refchild):
		"""
		Insert a new child node before an existing child. It must
		be the case that refchild is a child of this node; if not,
		ValueError is raised. newchild is returned.
		"""
		for i, childNode in enumerate(self.childNodes):
			if childNode is refchild:
				self.childNodes.insert(i, newchild)
				newchild.parentNode = self
				self._verifyChildren(i)
				return newchild
		raise ValueError(refchild)

	def removeChild(self, child):
		"""
		Remove a child from this element.  The child element is
		returned, and it's parentNode element is reset.  If the
		child will not be used any more, you should call its
		unlink() method to promote garbage collection.
		"""
		for i, childNode in enumerate(self.childNodes):
			if childNode is child:
				del self.childNodes[i]
				child.parentNode = None
				return child
		raise ValueError(child)

	def unlink(self):
		"""
		Break internal references within the document tree rooted
		on this element to promote garbage collection.
		"""
		self.parentNode = None
		for child in self.childNodes:
			child.unlink()
		del self.childNodes[:]

	def replaceChild(self, newchild, oldchild):
		"""
		Replace an existing node with a new node. It must be the
		case that oldchild is a child of this node; if not,
		ValueError is raised. newchild is returned.
		"""
		# .index() would use compare-by-value, we want
		# compare-by-id because we want to find the exact object,
		# not something equivalent to it.
		for i, childNode in enumerate(self.childNodes):
			if childNode is oldchild:
				self.childNodes[i].parentNode = None
				self.childNodes[i] = newchild
				newchild.parentNode = self
				self._verifyChildren(i)
				return newchild
		raise ValueError(oldchild)

	def getElements(self, filter):
		"""
		Return a list of elements below and including this element
		for which filter(element) returns True.
		"""
		l = reduce(lambda l, e: l + e.getElements(filter), self.childNodes, [])
		if filter(self):
			l.append(self)
		return l

	def getElementsByTagName(self, tagName):
		return self.getElements(lambda e: e.tagName == tagName)

	def getChildrenByAttributes(self, attrs):
		l = []
		for c in self.childNodes:
			try:
				if reduce(lambda t, (k, v): t and (c.getAttribute(k) == v), attrs.iteritems(), True):
					l.append(c)
			except KeyError:
				pass
		return l

	def hasAttribute(self, attrname):
		return self.attributes.has_key(attrname)

	def getAttribute(self, attrname):
		return self.attributes[attrname]

	def setAttribute(self, attrname, value):
		# cafeful:  this digs inside an AttributesImpl object and
		# modifies its internal data.  probably not a good idea,
		# but I don't know how else to edit an attribute because
		# the stupid things don't export a method to do it.
		self.attributes._attrs[attrname] = unicode(value)

	def removeAttribute(self, attrname):
		# cafeful:  this digs inside an AttributesImpl object and
		# modifies its internal data.  probably not a good idea,
		# but I don't know how else to edit an attribute because
		# the stupid things don't export a method to do it.
		try:
			del self.attributes._attrs[attrname]
		except KeyError:
			pass

	def appendData(self, content):
		"""
		Add characters to the element's pcdata.
		"""
		if self.pcdata is not None:
			self.pcdata += content
		else:
			self.pcdata = content

	def _verifyChildren(self, i):
		"""
		Method used internally by some elements to verify that
		their children are from the allowed set and in the correct
		order following modifications to their child list.  i is
		the index of the child that has just changed.
		"""
		pass

	def endElement(self):
		"""
		Method invoked by document parser when it encounters the
		end-of-element event.
		"""
		pass

	def write(self, fileobj = sys.stdout, indent = u""):
		"""
		Recursively write an element and it's children to a file.
		"""
		fileobj.write(self.start_tag(indent) + u"\n")
		for c in self.childNodes:
			if c.tagName not in self.validchildren:
				raise ElementError("invalid child %s for %s" % (c.tagName, self.tagName))
			c.write(fileobj, indent + Indent)
		if self.pcdata is not None:
			fileobj.write(xmlescape(self.pcdata))
			fileobj.write(u"\n")
		fileobj.write(self.end_tag(indent) + u"\n")
예제 #23
0
파일: gensimple.py 프로젝트: zhou0/ibus
 def save(self):
     if not self.__downstream:
         return
     list_iso639 = []
     if self.__is_variant and self.__is_config_item:
         list_iso639 = self.__list_iso639_for_variant
         if len(list_iso639) == 0:
             list_iso639 = self.__list_iso639
     elif self.__is_layout and self.__is_config_item:
         list_iso639 = self.__list_iso639
     for iso in list_iso639:
         do_deny = False
         for [xkb, layout, variant, lang] in self.__denylist:
             if xkb == 'xkb' \
                and ( layout == self.__layout or layout == '*' ) \
                and ( variant == self.__variant or variant == '*' ) \
                and ( lang == iso or variant == '*' ):
                 do_deny = True
                 break
         if do_deny:
             continue
         self.__downstream.startElement('engine', AttributesImpl({}))
         self.__downstream.startElement('name', AttributesImpl({}))
         name = 'xkb:%s:%s:%s' % (self.__layout, self.__variant, iso)
         self.__downstream.characters(name)
         self.__downstream.endElement('name')
         self.__downstream.startElement('language', AttributesImpl({}))
         iso639_1 = self.__iso639.code2to1(iso)
         if iso639_1 != None:
             iso = iso639_1
         self.__downstream.characters(iso)
         self.__downstream.endElement('language')
         self.__downstream.startElement('license', AttributesImpl({}))
         self.__downstream.characters('GPL')
         self.__downstream.endElement('license')
         if self.__author != None:
             self.__downstream.startElement('author', AttributesImpl({}))
             self.__downstream.characters(self.__author)
             self.__downstream.endElement('author')
         self.__downstream.startElement('layout', AttributesImpl({}))
         self.__downstream.characters(self.__layout)
         self.__downstream.endElement('layout')
         if self.__variant != '':
             self.__downstream.startElement('layout_variant',
                                            AttributesImpl({}))
             self.__downstream.characters(self.__variant)
             self.__downstream.endElement('layout_variant')
         self.__downstream.startElement('longname', AttributesImpl({}))
         self.__downstream.characters(self.__description)
         self.__downstream.endElement('longname')
         self.__downstream.startElement('description', AttributesImpl({}))
         self.__downstream.characters(self.__description)
         self.__downstream.endElement('description')
         self.__downstream.startElement('icon', AttributesImpl({}))
         self.__downstream.characters('ibus-keyboard')
         self.__downstream.endElement('icon')
         self.__downstream.startElement('rank', AttributesImpl({}))
         if self.__variant == '':
             self.__downstream.characters('50')
         else:
             self.__downstream.characters('1')
         self.__downstream.endElement('rank')
         self.__downstream.endElement('engine')
         if self.__first:
             break
예제 #24
0
class LIGOLWContentHandler(sax.handler.ContentHandler, object):
	"""
	ContentHandler class for parsing LIGO Light Weight documents with a
	SAX2-compliant parser.

	Example:

	>>> # initialize empty Document tree into which parsed XML tree
	>>> # will be inserted
	>>> xmldoc = Document()
	>>> # create handler instance attached to Document object
	>>> handler = LIGOLWContentHandler(xmldoc)
	>>> # open file and parse
	>>> make_parser(handler).parse(open("demo.xml"))
	>>> # write XML (default to stdout)
	>>> xmldoc.write()

	NOTE:  this example is for illustration only.  Most users will wish
	to use the .load_*() functions in the glue.ligolw.utils subpackage
	to load documents, and the .write_*() functions to write documents.
	Those functions provide additional features such as support for
	gzip'ed documents, MD5 hash computation, and Condor eviction
	trapping to avoid writing broken documents to disk.

	See also:  PartialLIGOLWContentHandler,
	FilteringLIGOLWContentHandler.
	"""

	def __init__(self, document, start_handlers = {}):
		"""
		Initialize the handler by pointing it to the Document object
		into which the parsed file will be loaded.
		"""
		self.current = self.document = document

		self._startElementHandlers = {
			(None, AdcData.tagName): self.startAdcData,
			(None, AdcInterval.tagName): self.startAdcInterval,
			(None, Array.tagName): self.startArray,
			(None, Column.tagName): self.startColumn,
			(None, Comment.tagName): self.startComment,
			(None, Detector.tagName): self.startDetector,
			(None, Dim.tagName): self.startDim,
			(None, IGWDFrame.tagName): self.startIGWDFrame,
			(None, LIGO_LW.tagName): self.startLIGO_LW,
			(None, Param.tagName): self.startParam,
			(None, Stream.tagName): self.startStream,
			(None, Table.tagName): self.startTable,
			(None, Time.tagName): self.startTime,
		}
		self._startElementHandlers.update(start_handlers)

	def startAdcData(self, parent, attrs):
		return AdcData(attrs)

	def startAdcInterval(self, parent, attrs):
		return AdcInterval(attrs)

	def startArray(self, parent, attrs):
		return Array(attrs)

	def startColumn(self, parent, attrs):
		return Column(attrs)

	def startComment(self, parent, attrs):
		return Comment(attrs)

	def startDetector(self, parent, attrs):
		return Detector(attrs)

	def startDim(self, parent, attrs):
		return Dim(attrs)

	def startIGWDFrame(self, parent, attrs):
		return IGWDFrame(attrs)

	def startLIGO_LW(self, parent, attrs):
		return LIGO_LW(attrs)

	def startParam(self, parent, attrs):
		return Param(attrs)

	def startStream(self, parent, attrs):
		return Stream(attrs)

	def startTable(self, parent, attrs):
		return Table(attrs)

	def startTime(self, parent, attrs):
		return Time(attrs)

	def startElementNS(self, (uri, localname), qname, attrs):
		try:
			start_handler = self._startElementHandlers[(uri, localname)]
		except KeyError:
			raise ElementError("unknown element %s for namespace %s" % (localname, uri or NameSpace))
		attrs = AttributesImpl(dict((attrs.getQNameByName(name), value) for name, value in attrs.items()))
		try:
			self.current = self.current.appendChild(start_handler(self.current, attrs))
		except Exception as e:
			raise type(e)("line %d: %s" % (self._locator.getLineNumber(), str(e)))
예제 #25
0
 def test_attrs_wattr(self):
     self.verify_attrs_wattr(AttributesImpl({"attr": "val"}))
예제 #26
0
 def handle_starttag(self, tag, attributes) -> None:
     self.start_element(tag, AttributesImpl(dict(attributes)))
예제 #27
0
def test_attrs_wattr():
    return verify_attrs_wattr(AttributesImpl({"attr": "val"}))
예제 #28
0
 def test_attrs_empty(self):
     self.verify_empty_attrs(AttributesImpl({}))
 def _write_debugging(self):
     self.write_parser.startElement('debugging',
             AttributesImpl({
                 'level': str(self.debugging_level)})
             )
     self.write_parser.endElement('debugging')
예제 #30
0
def test_attrs_empty():
    return verify_empty_attrs(AttributesImpl({}))
 def _write_verbose(self):
     self.write_parser.startElement('verbose',
             AttributesImpl({
                 'level': str(self.verbose_level)})
             )
     self.write_parser.endElement('verbose')
예제 #32
0
def _emit(key, value, content_handler,
          attr_prefix='@',
          cdata_key='#text',
          depth=0,
          preprocessor=None,
          pretty=False,
          newl='\n',
          indent='\t',
          namespace_separator=':',
          namespaces=None,
          full_document=True):
    key = _process_namespace(key, namespaces, namespace_separator, attr_prefix)
    if preprocessor is not None:
        result = preprocessor(key, value)
        if result is None:
            return
        key, value = result
    if (not hasattr(value, '__iter__')
            or isinstance(value, _basestring)
            or isinstance(value, dict)):
        value = [value]
    for index, v in enumerate(value):
        if full_document and depth == 0 and index > 0:
            raise ValueError('document with multiple roots')
        if v is None:
            v = OrderedDict()
        elif not isinstance(v, dict):
            v = _unicode(v)
        if isinstance(v, _basestring):
            v = OrderedDict(((cdata_key, v),))
        cdata = None
        attrs = OrderedDict()
        children = []
        for ik, iv in v.items():
            if ik == cdata_key:
                cdata = iv
                continue
            if ik.startswith(attr_prefix):
                ik = _process_namespace(ik, namespaces, namespace_separator,
                                        attr_prefix)
                if ik == '@xmlns' and isinstance(iv, dict):
                    for k, v in iv.items():
                        attr = 'xmlns{0}'.format(':{0}'.format(k) if k else '')
                        attrs[attr] = _unicode(v)
                    continue
                if not isinstance(iv, _unicode):
                    iv = _unicode(iv)
                attrs[ik[len(attr_prefix):]] = iv
                continue
            children.append((ik, iv))
        if pretty:
            content_handler.ignorableWhitespace(depth * indent)
        content_handler.startElement(key, AttributesImpl(attrs))
        if pretty and children:
            content_handler.ignorableWhitespace(newl)
        for child_key, child_value in children:
            _emit(child_key, child_value, content_handler,
                  attr_prefix, cdata_key, depth+1, preprocessor,
                  pretty, newl, indent, namespaces=namespaces,
                  namespace_separator=namespace_separator)
        if cdata is not None:
            content_handler.characters(cdata)
        if pretty and children:
            content_handler.ignorableWhitespace(depth * indent)
        content_handler.endElement(key)
        if pretty and depth:
            content_handler.ignorableWhitespace(newl)
    def _write_hosts(self):
        for host in self.hosts:
            # Start host element
            self.write_parser.startElement('host',
                    AttributesImpl({
                        'comment': host.comment})
                    )

            # Status element
            self.write_parser.startElement('status',
                    AttributesImpl({
                        'state': host.status['state']})
                    )
            self.write_parser.endElement('status')


            ##################
            # Address elements
            for address in host.address:
                self.__remove_none(address)
                self.write_parser.startElement('address',
                        AttributesImpl({
                            'addr': address.get('addr', ''),
                            'vendor': address.get('vendor', ''),
                            'addrtype': address.get('addrtype', '')})
                        )
                self.write_parser.endElement('address')
            # End of Address elements
            #########################


            ###################
            # Hostnames element
            self.write_parser.startElement('hostnames', AttributesImpl({}))

            for hname in host.hostnames:
                if not isinstance(hname, dict):
                    continue
                self.write_parser.startElement('hostname',
                        AttributesImpl({
                            'name': hname.get('name', ''),
                            'type': hname.get('type', '')})
                        )
                self.write_parser.endElement('hostname')

            self.write_parser.endElement('hostnames')
            # End of Hostnames element
            ##########################


            ###############
            # Ports element
            self.write_parser.startElement('ports', AttributesImpl({}))

            ## Extraports elements
            for export in host.extraports:
                self.__remove_none(export)
                self.write_parser.startElement('extraports',
                        AttributesImpl({
                            'count': str(export.get('count', '')),
                            'state': export.get('state', '')})
                        )
                self.write_parser.endElement('extraports')

            ## Port elements
            for port in host.ports:
                self.__remove_none(port)
                self.write_parser.startElement('port',
                    AttributesImpl({
                        'portid': port.get('portid', ''),
                        'protocol': port.get('protocol', '')})
                    )

                ### Port state
                self.write_parser.startElement('state',
                        AttributesImpl({
                            'state': port.get('state', '')})
                        )
                self.write_parser.endElement('state')

                ### Port service info
                self.write_parser.startElement('service',
                        AttributesImpl({
                            'conf': port.get('conf', ''),
                            'method': port.get('method', ''),
                            'name': port.get('name', ''),
                            'product': port.get('product', ''),
                            'version': port.get('version', ''),
                            'extrainfo': port.get('extrainfo', '')})
                        )
                self.write_parser.endElement('service')

                self.write_parser.endElement('port')

            self.write_parser.endElement('ports')
            # End of Ports element
            ######################


            ############
            # OS element
            self.write_parser.startElement('os', AttributesImpl({}))

            ## Ports used elements
            for pu in host.portused:
                if not isinstance(pu, dict):
                    continue

                self.__remove_none(pu)
                self.write_parser.startElement('portused',
                        AttributesImpl({
                            'state': pu.get('state', ''),
                            'proto': pu.get('proto', ''),
                            'portid': pu.get('portid', '')})
                        )
                self.write_parser.endElement('portused')

            ## Osclass elements
            for oc in host.osclass:
                if not isinstance(oc, dict):
                    continue

                self.__remove_none(oc)
                self.write_parser.startElement('osclass',
                        AttributesImpl({
                            'vendor': oc.get('vendor', ''),
                            'osfamily': oc.get('osfamily', ''),
                            'type': oc.get('type', ''),
                            'osgen': oc.get('osgen', ''),
                            'accuracy': oc.get('accuracy', '')})
                        )
                self.write_parser.endElement('osclass')

            ## Osmatch elements
            for om in host.osmatch:
                if not isinstance(om, dict):
                    continue

                self.__remove_none(om)
                self.write_parser.startElement('osmatch',
                        AttributesImpl({
                            'name': om.get('name', ''),
                            'accuracy': om.get('accuracy', '')})
                        )
                self.write_parser.endElement('osmatch')

            ## Osfingerprint element
            if isinstance(host.osfingerprint, dict):
                self.__remove_none(host.osfingerprint)
                self.write_parser.startElement('osfingerprint',
                        AttributesImpl({
                            'fingerprint': host.osfingerprint.get(
                                'fingerprint', '')})
                        )
                self.write_parser.endElement('osfingerprint')


            self.write_parser.endElement('os')
            # End of OS element
            ###################

            # Uptime element
            if isinstance(host.uptime, dict):
                self.write_parser.startElement('uptime',
                        AttributesImpl({
                            'seconds': host.uptime.get('seconds', ''),
                            'lastboot': host.uptime.get('lastboot', '')})
                        )
                self.write_parser.endElement('uptime')

            #####################
            # Sequences elementes
            ## TCP Sequence element
            if isinstance(host.tcpsequence, dict):
                self.write_parser.startElement('tcpsequence',
                        AttributesImpl({
                            'index': host.tcpsequence.get('index', ''),
                            'class': host.tcpsequence.get('class', ''),
                            'difficulty': host.tcpsequence.get('difficulty',
                                ''),
                            'values': host.tcpsequence.get('values', '')})
                        )
                self.write_parser.endElement('tcpsequence')

            ## IP ID Sequence element
            if isinstance(host.ipidsequence, dict):
                self.write_parser.startElement('ipidsequence',
                        AttributesImpl({
                            'class': host.ipidsequence.get('class', ''),
                            'values': host.ipidsequence.get('values', '')})
                        )
                self.write_parser.endElement('ipidsequence')

            ## TCP TS Sequence element
            if isinstance(host.tcptssequence, dict):
                self.write_parser.startElement('tcptssequence',
                        AttributesImpl({
                            'class': host.tcptssequence.get('class', ''),
                            'values': host.tcptssequence.get('values', '')})
                        )
                self.write_parser.endElement('tcptssequence')
            # End of sequences elements
            ###########################
            
            # Trace elements
            
            if isinstance(host.trace, dict):
                self.write_parser.startElement('trace',
                        AttributesImpl({
                            'port': host.trace.get('port', ''),
                            'proto': host.trace.get('proto', '')})
                        )
                
                # Write hops:
                for hop in host.trace['hop']:
                    self.write_parser.startElement('hop',
                        AttributesImpl(hop))
                    self.write_parser.endElement('hop')
                
                self.write_parser.endElement('trace')
            # End trace elements

            # End host element
            self.write_parser.endElement('host')