本文整理汇总了Python中lxml.etree.Comment方法的典型用法代码示例。如果您正苦于以下问题:Python etree.Comment方法的具体用法?Python etree.Comment怎么用?Python etree.Comment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree
的用法示例。
在下文中一共展示了etree.Comment方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: collect_inserts_aux
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def collect_inserts_aux(child, params, inserts, options):
roots = []
save_base_url = params.base_url
string_content = resolve_ref(child, params, options)
if string_content is not None:
root = etree.fromstring(string_content, base_url=params.base_url)
roots.append(root)
for child1 in root:
if not isinstance(child1, etree._Comment):
namespace = child1.nsmap[child1.prefix]
if (child1.tag != '{%s}include' % (namespace, ) and
child1.tag != '{%s' % (namespace, )):
comment = etree.Comment(etree.tostring(child))
comment.tail = '\n'
inserts.append(comment)
inserts.append(child1)
insert_roots = collect_inserts(root, params, inserts, options)
roots.extend(insert_roots)
params.base_url = save_base_url
return roots
示例2: _extract_composition
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def _extract_composition(element, spec):
"""
Parse XML spec to extract the composition of a nested structure (Component, Group or MsgType)
"""
returned = []
for elem in list(element):
if elem.tag == "field":
returned.append((spec.tags.by_name(elem.get('name')),
elem.get('required') == "Y"))
elif elem.tag == 'component':
returned.append((Component(elem, spec), elem.get('required') == "Y"))
elif elem.tag == 'group':
returned.append((Group.from_element(elem, spec), elem.get('required') == "Y"))
elif (parse.__module__ == 'lxml.etree') and (elem.tag == Comment):
pass
else:
raise ValueError("Could not process element '{}'".format(elem.tag))
return returned
示例3: getNodeDetails
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def getNodeDetails(self, node):
if isinstance(node, tuple): # Text node
node, key = node
assert key in ("text", "tail"), "Text nodes are text or tail, found %s" % key
return base.TEXT, ensure_str(getattr(node, key))
elif isinstance(node, Root):
return (base.DOCUMENT,)
elif isinstance(node, Doctype):
return base.DOCTYPE, node.name, node.public_id, node.system_id
elif isinstance(node, FragmentWrapper) and not hasattr(node, "tag"):
return base.TEXT, ensure_str(node.obj)
elif node.tag == etree.Comment:
return base.COMMENT, ensure_str(node.text)
elif node.tag == etree.Entity:
return base.ENTITY, ensure_str(node.text)[1:-1] # strip &;
else:
# This is assumed to be an ordinary element
match = tag_regexp.match(ensure_str(node.tag))
if match:
namespace, tag = match.groups()
else:
namespace = None
tag = ensure_str(node.tag)
attrs = {}
for name, value in list(node.attrib.items()):
name = ensure_str(name)
value = ensure_str(value)
match = tag_regexp.match(name)
if match:
attrs[(match.group(1), match.group(2))] = value
else:
attrs[(None, name)] = value
return (base.ELEMENT, namespace, self.filter.fromXmlName(tag),
attrs, len(node) > 0 or node.text)
示例4: export_xml_to_directory
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def export_xml_to_directory(self, elem):
'''
Do this recursively. If an element is a descriptor, then put that in its own
subdirectory.
'''
def write_xml(x):
un = x.get('url_name')
if un is None:
self.errlog("Oops! error in export_xml_to_directory, missing url_name:")
self.errlog(x)
elem.attrib.pop('url_name')
if 'url_name_orig' in elem.attrib and self.keep_urls:
elem.attrib.pop('url_name_orig')
edir = self.mkdir(self.dir / x.tag)
open(edir/un + '.xml','w').write(self.pp_xml(x))
return un
#print elem
if elem.tag=='descriptor':
# print "--> %s" % list(elem)
self.export_xml_to_directory(elem[0]) # recurse on children, depth first
elem.tag = elem.get('tag')
elem.set('url_name',elem.get('url_name'))
elem.attrib.pop('tag')
# self.export_xml_to_directory(elem) # recurse on this tag
elif elem.tag==etree.Comment: # comment <!-- foo -->
pass
elif elem.get('url_name') is None:
pass
else:
if elem.findall('.//descriptor'):
for k in elem:
self.export_xml_to_directory(k) # recurse on children
write_xml(elem) # write to file and remove from parent
elem.getparent().remove(elem)
示例5: getNodeDetails
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def getNodeDetails(self, node):
if isinstance(node, tuple): # Text node
node, key = node
assert key in ("text", "tail"), "Text nodes are text or tail, found %s" % key
return _base.TEXT, ensure_str(getattr(node, key))
elif isinstance(node, Root):
return (_base.DOCUMENT,)
elif isinstance(node, Doctype):
return _base.DOCTYPE, node.name, node.public_id, node.system_id
elif isinstance(node, FragmentWrapper) and not hasattr(node, "tag"):
return _base.TEXT, node.obj
elif node.tag == etree.Comment:
return _base.COMMENT, ensure_str(node.text)
elif node.tag == etree.Entity:
return _base.ENTITY, ensure_str(node.text)[1:-1] # strip &;
else:
# This is assumed to be an ordinary element
match = tag_regexp.match(ensure_str(node.tag))
if match:
namespace, tag = match.groups()
else:
namespace = None
tag = ensure_str(node.tag)
attrs = {}
for name, value in list(node.attrib.items()):
name = ensure_str(name)
value = ensure_str(value)
match = tag_regexp.match(name)
if match:
attrs[(match.group(1), match.group(2))] = value
else:
attrs[(None, name)] = value
return (_base.ELEMENT, namespace, self.filter.fromXmlName(tag),
attrs, len(node) > 0 or node.text)
示例6: getNodeDetails
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def getNodeDetails(self, node):
if isinstance(node, tuple): # Text node
node, key = node
assert key in ("text", "tail"), _("Text nodes are text or tail, found %s") % key
return _base.TEXT, ensure_str(getattr(node, key))
elif isinstance(node, Root):
return (_base.DOCUMENT,)
elif isinstance(node, Doctype):
return _base.DOCTYPE, node.name, node.public_id, node.system_id
elif isinstance(node, FragmentWrapper) and node.isstring:
return _base.TEXT, node.obj
elif node.tag == etree.Comment:
return _base.COMMENT, ensure_str(node.text)
elif node.tag == etree.Entity:
return _base.ENTITY, ensure_str(node.text)[1:-1] # strip &;
else:
# This is assumed to be an ordinary element
match = tag_regexp.match(ensure_str(node.tag))
if match:
namespace, tag = match.groups()
else:
namespace = None
tag = ensure_str(node.tag)
attrs = {}
for name, value in list(node.attrib.items()):
name = ensure_str(name)
value = ensure_str(value)
match = tag_regexp.match(name)
if match:
attrs[(match.group(1), match.group(2))] = value
else:
attrs[(None, name)] = value
return (_base.ELEMENT, namespace, self.filter.fromXmlName(tag),
attrs, len(node) > 0 or node.text)
示例7: clear_includes_and_imports
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def clear_includes_and_imports(node):
namespace = node.nsmap[node.prefix]
child_iter1 = node.iterfind('{%s}include' % (namespace, ))
child_iter2 = node.iterfind('{%s}import' % (namespace, ))
for child in itertools.chain(child_iter1, child_iter2):
repl = etree.Comment(etree.tostring(child))
repl.tail = '\n'
node.replace(child, repl)
示例8: getNodeDetails
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def getNodeDetails(self, node):
if isinstance(node, tuple): # Text node
node, key = node
assert key in ("text", "tail"), _("Text nodes are text or tail, found %s") % key
return _base.TEXT, getattr(node, key)
elif isinstance(node, Root):
return (_base.DOCUMENT,)
elif isinstance(node, Doctype):
return _base.DOCTYPE, node.name, node.public_id, node.system_id
elif isinstance(node, FragmentWrapper) and node.isstring:
return _base.TEXT, node
elif node.tag == etree.Comment:
return _base.COMMENT, node.text
elif node.tag == etree.Entity:
return _base.ENTITY, node.text[1:-1] # strip &;
else:
#This is assumed to be an ordinary element
match = tag_regexp.match(node.tag)
if match:
namespace, tag = match.groups()
else:
namespace = None
tag = node.tag
attrs = {}
for name, value in node.attrib.items():
match = tag_regexp.match(name)
if match:
attrs[(match.group(1),match.group(2))] = value
else:
attrs[(None,name)] = value
return (_base.ELEMENT, namespace, self.filter.fromXmlName(tag),
attrs, len(node) > 0 or node.text)
示例9: container
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def container(self, node, elem, module, path):
"""Create a sample container element and proceed with its children."""
nel, newm, path = self.sample_element(node, elem, module, path)
if path is None:
return
if self.annots:
pres = node.search_one("presence")
if pres is not None:
nel.append(etree.Comment(" presence: %s " % pres.arg))
self.process_children(node, nel, newm, path)
示例10: leaf
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def leaf(self, node, elem, module, path):
"""Create a sample leaf element."""
if node.i_default is None:
nel, newm, path = self.sample_element(node, elem, module, path)
if path is None:
return
if self.annots:
nel.append(etree.Comment(
" type: %s " % node.search_one("type").arg))
elif self.defaults:
nel, newm, path = self.sample_element(node, elem, module, path)
if path is None:
return
nel.text = str(node.i_default_str)
示例11: anyxml
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def anyxml(self, node, elem, module, path):
"""Create a sample anyxml element."""
nel, newm, path = self.sample_element(node, elem, module, path)
if path is None:
return
if self.annots:
nel.append(etree.Comment(" anyxml "))
示例12: list_comment
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def list_comment(self, node, elem, minel):
"""Add list annotation to `elem`."""
lo = "0" if minel is None else minel.arg
maxel = node.search_one("max-elements")
hi = "" if maxel is None else maxel.arg
elem.insert(0, etree.Comment(
" # entries: %s..%s " % (lo, hi)))
if node.keyword == 'list':
elem.insert(0, etree.Comment(
" # keys: " + ",".join([k.arg for k in node.i_key])))
示例13: _handle_InsertComment
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def _handle_InsertComment(self, action, tree):
target = tree.xpath(action.target)[0]
target.insert(action.position, etree.Comment(action.text))
示例14: node_ratio
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import Comment [as 别名]
def node_ratio(self, left, right):
if left.tag is etree.Comment or right.tag is etree.Comment:
if left.tag is etree.Comment and right.tag is etree.Comment:
# comments
self._sequencematcher.set_seqs(left.text, right.text)
return self._sequence_ratio()
# One is a comment the other is not:
return 0
for attr in self.uniqueattrs:
if not isinstance(attr, str):
# If it's actually a sequence of (tag, attr), the tags must
# match first.
tag, attr = attr
if tag != left.tag or tag != right.tag:
continue
if attr in left.attrib or attr in right.attrib:
# One of the nodes have a unique attribute, we check only that.
# If only one node has it, it means they are not the same.
return int(left.attrib.get(attr) == right.attrib.get(attr))
match = self.leaf_ratio(left, right)
child_ratio = self.child_ratio(left, right)
if child_ratio is not None:
match = (match + child_ratio) / 2
return match