2 """XML Virtual FileSystem for Midnight Commander
4 The script requires Midnight Commander 3.1+
5 (http://www.midnight-commander.org/), Python 2.4+ (http://www.python.org/).
7 For mc 4.7+ put the script in $HOME/.mc/extfs.d.
8 For older versions put it in /usr/[local/][lib|share]/mc/extfs
9 and add a line "xml" to the /usr/[local/][lib|share]/mc/extfs/extfs.ini.
10 Make the script executable.
12 Run this "cd" command in the Midnight Commander (in the "bindings" file the
13 command is "%cd"): cd file.xml#xml, where "file.xml" is the name of your xml
16 The VFS represents tags as directories; the directories are numbered to
17 distinguish tags with the same name; also numbering helps to sort tags by their
18 order in XML instead of sorting them by name. Attributes, text nodes and
19 comments are represented as text files; attributes are shown in a file named
20 "attributes", attributes are listed in the file as name=value lines (I
21 deliberately ignore a small chance there is a newline character in values). The
22 filesystem is read-only.
24 The VFS was inspired by a FUSE xmlfs: https://github.com/halhen/xmlfs
29 __author__ = "Oleg Broytman <phd@phdru.name>"
30 __copyright__ = "Copyright (C) 2013 PhiloSoft Design"
35 import xml.dom.minidom
44 # Get the default charset.
46 lcAll = locale.getdefaultlocale()
47 except locale.Error, err:
48 print >>sys.stderr, "WARNING:", err
52 default_encoding = lcAll[1]
55 default_encoding = locale.getpreferredencoding()
56 except locale.Error, err:
57 print >>sys.stderr, "WARNING:", err
58 default_encoding = sys.getdefaultencoding()
60 default_encoding = sys.getdefaultencoding()
63 logger = logging.getLogger('xml-mcextfs')
64 log_err_handler = logging.StreamHandler(sys.stderr)
65 logger.addHandler(log_err_handler)
66 logger.setLevel(logging.INFO)
70 XML Virtual FileSystem for Midnight Commander version %s
74 This is not a program. Put the script in $HOME/.mc/extfs.d or
75 /usr/[local/][lib|share]/mc/extfs. For more information read the source!""",
76 __version__, __author__, __copyright__
81 locale.setlocale(locale.LC_ALL, '')
83 def _attrs2text(attrs):
84 attrs = [attrs.item(i) for i in range (attrs.length)]
85 return '\n'.join(["%s=%s" %
86 (a.name.encode(default_encoding, "replace"),
87 a.value.encode(default_encoding, "replace"))
90 def _list(node, path=''):
91 childNodes = node.childNodes
93 for element in childNodes:
97 width = int(math.log10(n))+1
98 template = "%%0%dd" % width
102 for element in childNodes:
103 if element.localName:
106 subpath = '%s/%s %s' % (path, template % n, element.localName)
108 subpath = '%s %s' % (template % n, element.localName)
109 subpath_encoded = subpath.encode(default_encoding, "replace")
110 print "dr--r--r-- 1 user group 0 Jan 1 00:00 %s" % subpath_encoded
111 attrs = element.attributes
113 attr_text = _attrs2text(attrs)
114 print "-r--r--r-- 1 user group %d Jan 1 00:00 %s/attributes" % (
115 len(attr_text), subpath_encoded)
116 _list(element, subpath)
119 """List the entire VFS"""
121 dom = xml.dom.minidom.parse(sys.argv[2])
125 def _get_child_node(node, i):
127 for element in node.childNodes:
128 if element.localName:
132 xml_error('There are less than %d nodes' % i)
135 """Extract a file from the VFS"""
137 node = xml.dom.minidom.parse(sys.argv[2])
138 xml_filename = sys.argv[3]
139 real_filename = sys.argv[4]
141 for path_comp in xml_filename.split('/'):
143 i = int(path_comp.split(' ', 1)[0])
144 node = _get_child_node(node, i)
145 elif path_comp == 'attributes':
148 xml_error('Unknown file')
150 if path_comp == 'attributes':
151 attrs = node.attributes
153 text = _attrs2text(attrs)
155 xml_error('There are no attributes')
157 outfile = open(real_filename, 'w')
163 """Put a file to the VFS"""
164 sys.exit("XML VFS doesn't support adding files (read-only filesystem)")
167 """Remove a file from the VFS"""
168 sys.exit("XML VFS doesn't support removing files/directories (read-only filesystem)")
170 mcxml_rmdir = mcxml_rm
173 """Create a directory in the VFS"""
174 sys.exit("XML VFS doesn't support creating directories (read-only filesystem)")
177 def xml_error(error_str):
178 logger.critical("Error walking XML file: %s", error_str)
181 command = sys.argv[1]
182 procname = "mcxml_" + command
185 if not g.has_key(procname):
186 logger.critical("Unknown command %s", command)
194 logger.exception("Error during run")