Create the <body> etree element of an RS3 file (contains segments
        and groups) given a DGParentedTree.
164154
def gen_body(self):
        
        # We need to sort the treepositions, as RSTTool relies on <segment>
        # elements to be in linear order of the EDUs in the text.
        for treepos in sorted(self.treepositions):
            node = self.dgtree[treepos]
            node_id = self.get_node_id(treepos)
            node_type = get_node_type(node)

            if node_type in (TreeNodeTypes.leaf_node,
                             TreeNodeTypes.relation_node):
                relname, parent_id = self.get_relname_and_parent(treepos)

                attrib_list = [('id', node_id)]
                if parent_id is not None:
                    attrib_list.extend([('parent', parent_id), ('relname', relname)])

                if node_type == TreeNodeTypes.leaf_node:
                    self.body['segments'].append(E('segment', node, OrderedDict(attrib_list)))

                else:  # node_type == TreeNodeTypes.relation_node:
                    group_type = self.get_group_type(treepos)
                    # insert 'type' attrib between 'id' and 'parent'
                    attrib_list.insert(1, ('type', group_type))
                    self.body['groups'].append(E('group', OrderedDict(attrib_list)))
821957
function buildElementsTree(contexts) {

	contexts = contexts || [document];

	var queriesArray = Object.keys(queries).map(function(key) {
		return queries[key];
	});

	var selector = queriesArray.map(function(query) {
		return query._selector;
	}).join(',');

	var elements = [];
	contexts.forEach(function(context) {
		for (var node = context.parentNode; node; node = node.parentNode) {
			// Skip nested contexts
			if (contexts.indexOf(node) !== -1) {
				return;
			}
		}
		if (context !== document && elementMatchesSelector(context, selector)) {
			elements.push(context);
		}
		elements.push.apply(elements, arrayFrom(context.querySelectorAll(selector)));
	});

	var tree = [];
	var treeCache = createCacheMap();

	elements.forEach(function(element) {

		if (element === documentElement) {
			return;
		}

		var treeNode = {
			_element: element,
			_children: [],
			_queries: [],
			_changes: [],
			_done: false,
		};

		var children = tree;
		for (var node = element.parentNode; node; node = node.parentNode) {
			if (treeCache.get(node)) {
				children = treeCache.get(node)._children;
				break;
			}
		}

		treeCache.set(element, treeNode);

		children.push(treeNode);

		queriesArray.forEach(function(query) {
			if (elementMatchesSelector(element, query._selector)) {
				treeNode._queries.push(query);
			}
		});

	});

	return tree;

}
417244
def paula_etree_to_string(tree, dtd_filename):
    """"""
    return etree.tostring(
        tree, pretty_print=True, xml_declaration=True,
        encoding="UTF-8", standalone='no',
        doctype='<!DOCTYPE paula SYSTEM "{0}">'.format(dtd_filename))
483742
def from_etree(cls, etree_element):
        
        ins = SaltNode.from_etree(etree_element)
        # TODO: this looks dangerous, ask Stackoverflow about it!
        # convert SaltNode into PrimaryTextNode
        ins.__class__ = PrimaryTextNode.mro()[0]
        ins.text = extract_primary_text(etree_element)
        return ins
587597
def etree_to_string(tree):
        
        buff = BytesIO()
        tree.write(buff, xml_declaration=True, encoding='UTF-8')
        return buff.getvalue()
571236
def getTreesFor(self, document, content_type):
        

        # Relative path without potential leading path separator
        # otherwise os.path.join doesn't work
        for rel_path in self.overrides[content_type]:
            if rel_path[0] in ('/', '\\'):
                rel_path = rel_path[1:]
            file_path = os.path.join(document._cache_dir, rel_path)
            yield etree.parse(utils.xmlFile(file_path, 'rb'))
        return
1131741
def md_from_etree(self, md_element, context=''):
        
        md = {}
        # grab all understood attributes into md dict
        for att in ('capability', 'change', 'hash', 'length', 'path', 'mime_type',
                    'md_at', 'md_completed', 'md_from', 'md_until'):
            xml_att = self._xml_att_name(att)
            val = md_element.attrib.get(xml_att, None)
            if (val is not None):
                md[att] = val
        # capability. Allow this to be missing but do a very simple syntax
        # check on plausible values if present
        if ('capability' in md):
            if (re.match(r"^[\w\-]+$", md['capability']) is None):
                raise SitemapParseError(
                    "Bad capability name '%s' in %s" %
                    (capability, context))
        # change should be one of defined values
        if ('change' in md):
            if (md['change'] not in ['created', 'updated', 'deleted']):
                self.logger.warning(
                    "Bad change attribute in <rs:md> for %s" %
                    (context))
        # length should be an integer
        if ('length' in md):
            try:
                md['length'] = int(md['length'])
            except ValueError as e:
                raise SitemapParseError(
                    "Invalid length element in <rs:md> for %s" %
                    (context))
        return(md)
139952
def _buildElementTree(self,):
        
        t_elt = ctree.Element(self.name)

        for k,v in [ (key,value) for key,value in self.__dict__.items() if key != 'name']: # Excluding name from list of items
            if v and v != 'false' :
                t_elt.set(k if k != 'like' else 'as', str(v).lower())

        self._etree = t_elt
        return t_elt
289810
def _build_tree(self):
        

        groups = self._groups or self.get_children_paths(self.root_path)
        for group in groups:
            node = Node(name=group, parent=self.root)
            self.root.children.append(node)
            self._init_sub_groups(node)
347891
def get_rendered_toctree(builder, docname, prune=False, collapse=True):
    
    fulltoc = build_full_toctree(builder,
                                 docname,
                                 prune=prune,
                                 collapse=collapse,
                                 )
    rendered_toc = builder.render_partial(fulltoc)['fragment']
    return rendered_toc
1333409
def ln_from_etree(self, ln_element, context=''):
        
        ln = {}
        # grab all understood attributes into ln dict
        for att in ('hash', 'href', 'length', 'modified',
                    'path', 'rel', 'pri', 'mime_type'):
            xml_att = self._xml_att_name(att)
            val = ln_element.attrib.get(xml_att, None)
            if (val is not None):
                ln[att] = val
        # now do some checks and conversions...
        # href (MANDATORY)
        if ('href' not in ln):
            raise SitemapParseError(
                "Missing href in <rs:ln> in %s" %
                (context))
        # rel (MANDATORY)
        if ('rel' not in ln):
            raise SitemapParseError("Missing rel in <rs:ln> in %s" % (context))
        # length in bytes
        if ('length' in ln):
            try:
                ln['length'] = int(ln['length'])
            except ValueError as e:
                raise SitemapParseError(
                    "Invalid length attribute value in <rs:ln> for %s" %
                    (context))
        # pri - priority, must be a number between 1 and 999999
        if ('pri' in ln):
            try:
                ln['pri'] = int(ln['pri'])
            except ValueError as e:
                raise SitemapParseError(
                    "Invalid pri attribute in <rs:ln> for %s" %
                    (context))
            if (ln['pri'] < 1 or ln['pri'] > 999999):
                raise SitemapParseError(
                    "Bad pri attribute value in <rs:ln> for %s" %
                    (context))
        return(ln)
171271
def build_tree(self):
        
        if self.built:
            return
        self.doc_root = self.root.element()
        for key in self.sorted_fields():
            if key not in self._fields:
                continue
            field = self._fields[key]
            if field != self.root:
                if isinstance(field, XmlModel):
                    field.build_tree()
                    if (self.drop_empty and field.drop_empty
                        and len(field.doc_root) == 0):
                        continue
                    self.doc_root.append(field.doc_root)
                elif isinstance(field, list):
                    # we just allow XmlFields and XmlModels
                    # Also xml as str for memory management
                    for item in field:
                        if isinstance(item, XmlField):
                            ele = item.element()
                            if self.drop_empty and len(ele) == 0:
                                continue
                            self.doc_root.append(ele)
                        elif isinstance(item, XmlModel):
                            item.build_tree()
                            if self.drop_empty and len(item.doc_root) == 0:
                                continue
                            self.doc_root.append(item.doc_root)
                        elif isinstance(item, (six.text_type, six.string_types)):
                            ele = etree.fromstring(clean_xml(item))
                            self.doc_root.append(ele)
                        item = None
                elif (field.parent or self.root.name) == self.root.name:
                    ele = field.element()
                    if self.drop_empty and len(ele) == 0 and not ele.text:
                        continue
                    ele = field.element(parent=self.doc_root)
                else:
                    nodes = [n for n in self.doc_root.iterdescendants(
                                            tag=field.parent)]
                    if nodes:
                        ele = field.element()
                        if (self.drop_empty and len(ele) == 0 and not ele.text):
                            continue
                        ele = field.element(parent=nodes[0])
                    #else:
                    #    raise RuntimeError("No parent found!")
        self.built = True
1092612
def to_xml(body_element):
    
    doctype = b"""<?xml version="1.0" encoding="UTF-8"?>\n"""
    if body_element is None:
        return succeed(b"")
    d = flattenString(None, body_element)
    d.addCallback(lambda flattened: doctype + flattened)
    return d
295280
def from_file(cls, jss, filename):
        
        tree = ElementTree.parse(filename)
        root = tree.getroot()
        return cls(jss, root)
123537
def add_path(self, path, attribs=None, group=None):
        """"""

        # If not given a parent, assume that the path does not have a group
        if group is None:
            group = self.tree.getroot()

        # If given a list of strings (one or more), assume it represents
        # a sequence of nested group names
        elif all(isinstance(elem, str) for elem in group):
            group = self.get_or_add_group(group)

        elif not isinstance(group, Element):
            raise TypeError(
                'Must provide a list of strings or an xml.etree.Element '
                'object. Instead you provided {0}'.format(group))

        else:
            # Make sure that the group belongs to this Document object
            if not self.contains_group(group):
                warnings.warn('The requested group does not belong to '
                              'this Document')

        # TODO: It might be better to use duck-typing here with a try-except
        if isinstance(path, Path):
            path_svg = path.d()
        elif is_path_segment(path):
            path_svg = Path(path).d()
        elif isinstance(path, str):
            # Assume this is a valid d-string.
            # TODO: Should we sanity check the input string?
            path_svg = path
        else:
            raise TypeError(
                'Must provide a Path, a path segment type, or a valid '
                'SVG path d-string. Instead you provided {0}'.format(path))

        if attribs is None:
            attribs = {}
        else:
            attribs = attribs.copy()

        attribs['d'] = path_svg

        return SubElement(group, 'path', attribs)
1047999
def from_etree(cls, etree_element):
        
        label_elements = get_subelements(etree_element, 'labels')
        labels = [SaltLabel.from_etree(elem) for elem in label_elements]
        return cls(name=get_element_name(etree_element),
                   element_id=get_graph_element_id(etree_element),
                   xsi_type=get_xsi_type(etree_element),
                   labels=labels,
                   xml=etree_element)
1037596
def _extract_body(self):
        

        def is_descendant_node(parent, node):
            node = node.getparent()
            while node is not None:
                if node == parent:
                    return True
                node = node.getparent()
            return False

        for pattern in self.config.body:
            items = self.parsed_tree.xpath(pattern)

            if len(items) == 1:
                if self.config.prune:
                    self.body = Document(etree.tostring(items[0])).summary()

                else:
                    self.body = etree.tostring(items[0])

                # We've got a body now.
                break

            else:
                appended_something = False
                body = etree.Element("root")

                for item in items:
                    if item.getparent() is None:
                        continue

                    is_descendant = False

                    for parent in body:
                        if (is_descendant_node(parent, item)):
                            is_descendant = True
                            break

                    if not is_descendant:

                        if self.config.prune:

                            # Clean with readability. Needs
                            # to-string conversion first.
                            pruned_string = Document(
                                etree.tostring(item)).summary()

                            # Re-parse the readability string
                            # output and include it in our body.
                            new_tree = etree.parse(
                                StringIO(pruned_string), self.parser)

                            failed = False

                            try:
                                body.append(
                                    new_tree.xpath('//html/body/div/div')[0]
                                )
                            except IndexError:

                                if 'id="readabilityBody"' in pruned_string:
                                    try:
                                        body.append(
                                            new_tree.xpath('//body')
                                        )
                                    except:
                                        failed = True

                                else:
                                    failed = True

                            if failed:
                                LOGGER.error(u'Pruning item failed:'
                                             u'\n\n%s\n\nWe got: “%s” '
                                             u'and skipped it.',
                                             etree.tostring(
                                                 item).replace(u'\n', u''),
                                             pruned_string.replace(u'\n', u''),
                                             extra={'siteconfig':
                                                    self.config.host})
                                pass

                        else:
                            body.append(item)

                        appended_something = True

                if appended_something:
                    self.body = etree.tostring(body)

                    # We've got a body now.
                    break