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. Attributes, text nodes and comments are
18 represented as files. The filesystem is read-only.
20 The VFS was inspired by a FUSE xmlfs: https://github.com/halhen/xmlfs
25 __author__ = "Oleg Broytman <phd@phdru.name>"
26 __copyright__ = "Copyright (C) 2013 PhiloSoft Design"
31 import xml.dom.minidom
40 # Get the default charset.
42 lcAll = locale.getdefaultlocale()
43 except locale.Error, err:
44 print >>sys.stderr, "WARNING:", err
48 default_encoding = lcAll[1]
51 default_encoding = locale.getpreferredencoding()
52 except locale.Error, err:
53 print >>sys.stderr, "WARNING:", err
54 default_encoding = sys.getdefaultencoding()
56 default_encoding = sys.getdefaultencoding()
59 logger = logging.getLogger('xml-mcextfs')
60 log_err_handler = logging.StreamHandler(sys.stderr)
61 logger.addHandler(log_err_handler)
62 logger.setLevel(logging.INFO)
66 XML Virtual FileSystem for Midnight Commander version %s
70 This is not a program. Put the script in $HOME/.mc/extfs.d or
71 /usr/[local/][lib|share]/mc/extfs. For more information read the source!""",
72 __version__, __author__, __copyright__
76 locale.setlocale(locale.LC_ALL, '')
79 def _list(node, path=''):
80 childNodes = node.childNodes
82 for element in childNodes:
86 width = int(math.log10(n))+1
87 template = "%%0%dd" % width
91 for element in childNodes:
95 subpath = '%s/%s %s' % (path, template % n, element.localName)
97 subpath = '%s %s' % (template % n, element.localName)
98 subpath_encoded = subpath.encode(default_encoding, "replace")
99 print "dr--r--r-- 1 user group 0 Jan 1 00:00 %s" % subpath_encoded
100 _list(element, subpath)
103 """List the entire VFS"""
105 dom = xml.dom.minidom.parse(sys.argv[2])
110 """Extract a file from the VFS"""
114 """Put a file to the VFS"""
115 sys.exit("XML VFS doesn't support adding files (read-only filesystem)")
118 """Remove a file from the VFS"""
119 sys.exit("XML VFS doesn't support removing files/directories (read-only filesystem)")
121 mcxml_rmdir = mcxml_rm
124 """Create a directory in the VFS"""
125 sys.exit("XML VFS doesn't support creating directories (read-only filesystem)")
128 command = sys.argv[1]
129 procname = "mcxml_" + command
132 if not g.has_key(procname):
133 logger.critical("Unknown command %s", command)
141 logger.exception("Error during run")