--- /dev/null
+#! /usr/bin/env python
+"""XML Virtual FileSystem for Midnight Commander
+
+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/.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.
+
+Run this "cd" command in the Midnight Commander (in the "bindings" file the
+command is "%cd"): cd file.xml#xml, where "file.xml" is the name of your xml
+file.
+
+The VFS represents tags as directories; the directories are numbered to
+distinguish tags with the same name. Attributes, text nodes and comments are
+represented as files. The filesystem is read-only.
+
+The VFS was inspired by a FUSE xmlfs: https://github.com/halhen/xmlfs
+
+"""
+
+__version__ = "0.1.0"
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2013 PhiloSoft Design"
+__license__ = "GPL"
+
+import math
+import sys
+import xml.dom.minidom
+
+try:
+ import locale
+ use_locale = True
+except ImportError:
+ use_locale = False
+
+if use_locale:
+ # Get the default charset.
+ try:
+ lcAll = locale.getdefaultlocale()
+ except locale.Error, err:
+ print >>sys.stderr, "WARNING:", err
+ lcAll = []
+
+ if len(lcAll) == 2:
+ default_encoding = lcAll[1]
+ else:
+ try:
+ default_encoding = locale.getpreferredencoding()
+ except locale.Error, err:
+ print >>sys.stderr, "WARNING:", err
+ default_encoding = sys.getdefaultencoding()
+else:
+ default_encoding = sys.getdefaultencoding()
+
+import logging
+logger = logging.getLogger('xml-mcextfs')
+log_err_handler = logging.StreamHandler(sys.stderr)
+logger.addHandler(log_err_handler)
+logger.setLevel(logging.INFO)
+
+if len(sys.argv) < 3:
+ logger.critical("""\
+XML Virtual FileSystem for Midnight Commander version %s
+Author: %s
+%s
+
+This is not a program. Put the script in $HOME/.mc/extfs.d or
+/usr/[local/][lib|share]/mc/extfs. For more information read the source!""",
+ __version__, __author__, __copyright__
+)
+ sys.exit(1)
+
+locale.setlocale(locale.LC_ALL, '')
+
+
+def _list(node, path=''):
+ childNodes = node.childNodes
+ n = 0
+ for element in childNodes:
+ if element.localName:
+ n += 1
+ if n:
+ width = int(math.log10(n))+1
+ template = "%%0%dd" % width
+ else:
+ template = "%d"
+ n = 0
+ for element in childNodes:
+ if element.localName:
+ n += 1
+ if path:
+ subpath = '%s/%s %s' % (path, template % n, element.localName)
+ else:
+ subpath = '%s %s' % (template % n, element.localName)
+ subpath_encoded = subpath.encode(default_encoding, "replace")
+ print "dr--r--r-- 1 user group 0 Jan 1 00:00 %s" % subpath_encoded
+ _list(element, subpath)
+
+def mcxml_list():
+ """List the entire VFS"""
+
+ dom = xml.dom.minidom.parse(sys.argv[2])
+ _list(dom)
+
+
+def mcxml_copyout():
+ """Extract a file from the VFS"""
+
+
+def mcxml_copyin():
+ """Put a file to the VFS"""
+ sys.exit("XML VFS doesn't support adding files (read-only filesystem)")
+
+def mcxml_rm():
+ """Remove a file from the VFS"""
+ sys.exit("XML VFS doesn't support removing files/directories (read-only filesystem)")
+
+mcxml_rmdir = mcxml_rm
+
+def mcxml_mkdir():
+ """Create a directory in the VFS"""
+ sys.exit("XML VFS doesn't support creating directories (read-only filesystem)")
+
+
+command = sys.argv[1]
+procname = "mcxml_" + command
+
+g = globals()
+if not g.has_key(procname):
+ logger.critical("Unknown command %s", command)
+ sys.exit(1)
+
+try:
+ g[procname]()
+except SystemExit:
+ raise
+except:
+ logger.exception("Error during run")