X-Git-Url: https://git.phdru.name/?p=extfs.d.git;a=blobdiff_plain;f=xml;h=5ed82db406ae9a061a733b046c0f050102adc9df;hp=084a78483553f2f6cfed759bcce5b5efdfc85904;hb=7333a3485fd782d8f7b3850e88f649d75afd505d;hpb=d4a65630f87d5732b003a33b75bc1bed430f43b6 diff --git a/xml b/xml index 084a784..5ed82db 100755 --- a/xml +++ b/xml @@ -4,13 +4,13 @@ The script requires Midnight Commander 3.1+ (http://www.midnight-commander.org/), Python 2.4+ (http://www.python.org/). -For mc 4.7+ put the script in $HOME/[.local/share/].mc/extfs.d. +For mc 4.7+ just put the script in $HOME/[.local/share/].mc/extfs.d. For older versions put it in /usr/[local/][lib|share]/mc/extfs and add a line "xml" to the /usr/[local/][lib|share]/mc/extfs/extfs.ini. Make the script executable. For mc 4.7+ run this "cd" command in the Midnight Commander (in the "bindings" -file the command is "%cd"): cd file/xml://; In older versions it is +file the command is "%cd"): cd file/xml://; in older versions it is cd file#xml, where "file" is the name of your XML file. The VFS represents tags as directories; the directories are numbered to @@ -21,8 +21,12 @@ comments are represented as text files; attributes are shown in a file named deliberately ignore a small chance of newline characters in values); names and values are reencoded to the console encoding. Text nodes and comments are collected in a file named "text", stripped and reencoded. The filesystem is -read-only. ElementTree- and lxml.etree-based implementations don't show -namespaces as attributes. +read-only. + +Implementation based on minidom doesn't understand namespaces, it just shows +them among other attributes. ElementTree-based implementation doesn't show +namespaces at all. Implementation based on lxml.etree shows namespaces in a +separate file "namespaces"; every child tag includes its parent's namespaces. It is useful to have a top-down view on an XML structure but it's especially convenient to extract text values from tags. One can get, for example, a @@ -33,13 +37,13 @@ The VFS was inspired by a FUSE xmlfs: https://github.com/halhen/xmlfs """ -__version__ = "0.5.0" +__version__ = "0.5.1" __author__ = "Oleg Broytman " __copyright__ = "Copyright (C) 2013 PhiloSoft Design" __license__ = "GPL" -default_implementation = None # Can be None for default choice, - # 'lxml', 'elementtree' or 'minidom' +default_implementation = None # Can be None for default choice, + # 'lxml', 'elementtree' or 'minidom' use_minidom = True use_elementtree = False @@ -117,6 +121,9 @@ class XmlVfs(object): def list(self): self._list(self.getroot()) + def has_ns(self, node): + return False + def get_child_node(self, node, i): n = 0 for element in self.getchildren(node): @@ -136,7 +143,7 @@ class MiniDOMXmlVfs(XmlVfs): def attrs2text(self, node): attrs = node.attributes - attrs = [attrs.item(i) for i in range (attrs.length)] + attrs = [attrs.item(i) for i in range(attrs.length)] return '\n'.join(["%s=%s" % (a.name.encode(default_encoding, "replace"), a.value.encode(default_encoding, "replace")) @@ -163,7 +170,7 @@ class MiniDOMXmlVfs(XmlVfs): if element.localName: n += 1 if n: - width = int(math.log10(n))+1 + width = int(math.log10(n)) + 1 template = "%%0%dd" % width else: template = "%d" @@ -209,7 +216,7 @@ if use_elementtree or use_lxml: if text: text_accumulator.append(text) for element in node: if not self.istag(element): - text = u"" % text + text = u"" % element.text text_accumulator.append(text) if node.tail: text = node.tail.strip() @@ -233,26 +240,26 @@ if use_elementtree: class PIParser(ET.XMLTreeBuilder): - def __init__(self): - ET.XMLTreeBuilder.__init__(self) - # assumes ElementTree 1.2.X - self._parser.CommentHandler = self.handle_comment - self._parser.ProcessingInstructionHandler = self.handle_pi - self._target.start("document", {}) + def __init__(self): + ET.XMLTreeBuilder.__init__(self) + # assumes ElementTree 1.2.X + self._parser.CommentHandler = self.handle_comment + self._parser.ProcessingInstructionHandler = self.handle_pi + self._target.start("document", {}) - def close(self): - self._target.end("document") - return ET.XMLTreeBuilder.close(self) + def close(self): + self._target.end("document") + return ET.XMLTreeBuilder.close(self) - def handle_comment(self, data): - self._target.start(ET.Comment, {}) - self._target.data(data) - self._target.end(ET.Comment) + def handle_comment(self, data): + self._target.start(ET.Comment, {}) + self._target.data(data) + self._target.end(ET.Comment) - def handle_pi(self, target, data): - self._target.start(ET.PI, {}) - self._target.data(target + " " + data) - self._target.end(ET.PI) + def handle_pi(self, target, data): + self._target.start(ET.PI, {}) + self._target.data(target + " " + data) + self._target.end(ET.PI) self.document = ET.parse(sys.argv[2], PIParser()) @@ -311,6 +318,17 @@ if use_lxml: attr_accumulator.append("%s=%s" % (name, value)) return '\n'.join(attr_accumulator) + def has_ns(self, node): + return bool(node.nsmap) + + def ns2text(self, node): + ns_accumulator = [] + for name, value in node.nsmap.items(): + name = name.encode(default_encoding, "replace") + value = value.encode(default_encoding, "replace") + ns_accumulator.append("%s=%s" % (name, value)) + return '\n'.join(ns_accumulator) + def list(self): self._list(self.getroot()) @@ -337,6 +355,10 @@ if use_lxml: attr_text = self.attrs2text(element) print "-r--r--r-- 1 user group %d Jan 1 00:00 %s/attributes" % ( len(attr_text), subpath_encoded) + if element.nsmap: + ns_text = self.ns2text(element) + print "-r--r--r-- 1 user group %d Jan 1 00:00 %s/namespaces" % ( + len(ns_text), subpath_encoded) text = self.collect_text(element) if text: print "-r--r--r-- 1 user group %d Jan 1 00:00 %s/text" % ( @@ -382,7 +404,7 @@ def mcxml_copyout(): if ' ' in path_comp: i = int(path_comp.split(' ', 1)[0]) node = xmlvfs.get_child_node(node, i) - elif path_comp in ('attributes', 'text'): + elif path_comp in ('attributes', 'namespaces', 'text'): break else: xml_error('Unknown file') @@ -393,9 +415,18 @@ def mcxml_copyout(): else: xml_error('There are no attributes') - if path_comp == 'text': + elif path_comp == 'namespaces': + if xmlvfs.has_ns(node): + text = xmlvfs.ns2text(node) + else: + xml_error('There are no attributes') + + elif path_comp == 'text': text = xmlvfs.collect_text(node) + else: + xml_error('Unknown file') + outfile = open(real_filename, 'w') outfile.write(text) outfile.close()