]> git.phdru.name Git - extfs.d.git/blob - xml
486106dd2789e956d4409d8c5e3de7d1ce06b530
[extfs.d.git] / xml
1 #! /usr/bin/env python
2 """XML Virtual FileSystem for Midnight Commander
3
4 The script requires Midnight Commander 3.1+
5 (http://www.midnight-commander.org/), Python 2.4+ (http://www.python.org/).
6
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.
11
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
14 file.
15
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.
19
20 The VFS was inspired by a FUSE xmlfs: https://github.com/halhen/xmlfs
21
22 """
23
24 __version__ = "0.1.0"
25 __author__ = "Oleg Broytman <phd@phdru.name>"
26 __copyright__ = "Copyright (C) 2013 PhiloSoft Design"
27 __license__ = "GPL"
28
29 import math
30 import sys
31 import xml.dom.minidom
32
33 try:
34    import locale
35    use_locale = True
36 except ImportError:
37    use_locale = False
38
39 if use_locale:
40    # Get the default charset.
41    try:
42       lcAll = locale.getdefaultlocale()
43    except locale.Error, err:
44       print >>sys.stderr, "WARNING:", err
45       lcAll = []
46
47    if len(lcAll) == 2:
48       default_encoding = lcAll[1]
49    else:
50       try:
51          default_encoding = locale.getpreferredencoding()
52       except locale.Error, err:
53          print >>sys.stderr, "WARNING:", err
54          default_encoding = sys.getdefaultencoding()
55 else:
56    default_encoding = sys.getdefaultencoding()
57
58 import logging
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)
63
64 if len(sys.argv) < 3:
65     logger.critical("""\
66 XML Virtual FileSystem for Midnight Commander version %s
67 Author: %s
68 %s
69
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__
73 )
74     sys.exit(1)
75
76 locale.setlocale(locale.LC_ALL, '')
77
78
79 def _list(node, path=''):
80     childNodes = node.childNodes
81     n = 0
82     for element in childNodes:
83         if element.localName:
84             n += 1
85     if n:
86         width = int(math.log10(n))+1
87         template = "%%0%dd" % width
88     else:
89         template = "%d"
90     n = 0
91     for element in childNodes:
92         if element.localName:
93             n += 1
94             if path:
95                 subpath = '%s/%s %s' % (path, template % n, element.localName)
96             else:
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)
101
102 def mcxml_list():
103     """List the entire VFS"""
104
105     dom = xml.dom.minidom.parse(sys.argv[2])
106     _list(dom)
107
108
109 def mcxml_copyout():
110     """Extract a file from the VFS"""
111
112
113 def mcxml_copyin():
114     """Put a file to the VFS"""
115     sys.exit("XML VFS doesn't support adding files (read-only filesystem)")
116
117 def mcxml_rm():
118     """Remove a file from the VFS"""
119     sys.exit("XML VFS doesn't support removing files/directories (read-only filesystem)")
120
121 mcxml_rmdir = mcxml_rm
122
123 def mcxml_mkdir():
124     """Create a directory in the VFS"""
125     sys.exit("XML VFS doesn't support creating directories (read-only filesystem)")
126
127
128 command = sys.argv[1]
129 procname = "mcxml_" + command
130
131 g = globals()
132 if not g.has_key(procname):
133     logger.critical("Unknown command %s", command)
134     sys.exit(1)
135
136 try:
137     g[procname]()
138 except SystemExit:
139     raise
140 except:
141     logger.exception("Error during run")