2008-08-18 07:35:03 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
import glob, os, sys, string, xml.sax, getopt
|
|
|
|
|
|
|
|
|
|
|
|
__doc__ = """\
|
2008-08-19 11:13:07 +00:00
|
|
|
List properties defined in FlightGear's <PropertyList> XML files.
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
Usage:
|
2008-08-19 11:13:07 +00:00
|
|
|
lsprop [-v] [-p] [-i|-I] [-f <format>] [<list-of-xml-files>]
|
2008-08-19 06:36:48 +00:00
|
|
|
lsprop -h
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
Options:
|
2008-08-19 06:36:48 +00:00
|
|
|
-h, --help print this help screen
|
|
|
|
-v, --verbose increase verbosity
|
2008-08-18 07:35:03 +00:00
|
|
|
-i, --all-indices also show null indices in properties
|
|
|
|
-I, --no-indices don't show any indices in properties
|
2008-08-19 11:13:07 +00:00
|
|
|
-p, --raw-paths don't use symbols "$FG_ROOT" and "$FG_HOME" as path prefix
|
2008-08-18 10:15:39 +00:00
|
|
|
-f, --format set output format (default: --format="%f +%l: %p = '%v'")
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
Format:
|
2008-09-23 20:21:19 +00:00
|
|
|
%f file path
|
|
|
|
%l line number
|
|
|
|
%c column number
|
|
|
|
%p property path
|
|
|
|
%t property type
|
|
|
|
%V raw value (unescaped)
|
|
|
|
%v cooked value (carriage return, non printable chars etc. escaped)
|
|
|
|
%q like %v, but single quotes escaped to \\'
|
|
|
|
%Q like %v, but double quotes escaped to \\"
|
|
|
|
%% percent sign
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
Environment:
|
|
|
|
FG_ROOT
|
|
|
|
FG_HOME
|
|
|
|
LSPROP_FORMAT overrides default format
|
|
|
|
|
2008-08-19 06:36:48 +00:00
|
|
|
Arguments:
|
|
|
|
If no file arguments are specified, then the following files are assumed:
|
2008-08-18 07:35:03 +00:00
|
|
|
$FG_ROOT/preferences.xml
|
|
|
|
$FG_ROOT/Aircraft/*/*-set.xml
|
2008-08-19 06:36:48 +00:00
|
|
|
|
|
|
|
Current settings:\
|
2008-08-18 07:35:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class config:
|
|
|
|
root = "/usr/local/share/FlightGear"
|
|
|
|
home = os.environ["HOME"] + "/.fgfs"
|
2008-08-18 10:15:39 +00:00
|
|
|
raw_paths = 0
|
|
|
|
format = "%f +%l: %p = '%v'"
|
2008-08-18 07:35:03 +00:00
|
|
|
verbose = 1
|
|
|
|
indices = 1 # 0: no indices; 1: only indices != [0]; 2: all indices
|
|
|
|
|
|
|
|
|
|
|
|
def errmsg(msg, color = "31;1"):
|
|
|
|
if os.isatty(2):
|
|
|
|
print >>sys.stderr, "\033[%sm%s\033[m" % (color, msg)
|
|
|
|
else:
|
|
|
|
print >>sys.stderr, msg
|
|
|
|
|
|
|
|
|
2008-08-18 10:15:39 +00:00
|
|
|
def cook_path(path, force = 0):
|
2008-08-19 06:36:48 +00:00
|
|
|
path = os.path.normpath(os.path.abspath(path))
|
2008-08-18 10:15:39 +00:00
|
|
|
if config.raw_paths and not force:
|
|
|
|
return path
|
|
|
|
if path.startswith(config.root):
|
|
|
|
path = path.replace(config.root, "$FG_ROOT", 1)
|
|
|
|
elif path.startswith(config.home):
|
|
|
|
path = path.replace(config.home, "$FG_HOME", 1)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2008-08-18 07:35:03 +00:00
|
|
|
class Error(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Abort(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class XMLError(Exception):
|
|
|
|
def __init__(self, locator, msg):
|
2008-08-18 10:15:39 +00:00
|
|
|
msg = "%s in %s +%d:%d" \
|
|
|
|
% (msg.replace("\n", "\\n"), cook_path(locator.getSystemId()), \
|
|
|
|
locator.getLineNumber(), locator.getColumnNumber())
|
2008-08-18 07:35:03 +00:00
|
|
|
raise Error(msg)
|
|
|
|
|
|
|
|
|
|
|
|
class parse_xml_file(xml.sax.handler.ContentHandler):
|
|
|
|
def __init__(self, path, nesting = 0, stack = None):
|
|
|
|
self.level = 0
|
|
|
|
self.path = path
|
|
|
|
self.nesting = nesting
|
|
|
|
self.type = None
|
|
|
|
if stack:
|
|
|
|
self.stack = stack
|
|
|
|
else:
|
2008-08-18 10:15:39 +00:00
|
|
|
self.stack = [[None, None, {}, []]] # name, index, indices, data
|
2008-08-18 07:35:03 +00:00
|
|
|
|
2008-08-18 10:15:39 +00:00
|
|
|
self.pretty_path = cook_path(path)
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
if config.verbose > 1:
|
|
|
|
errmsg("%s (%d)" % (path, nesting), "35")
|
|
|
|
if not os.path.exists(path):
|
2008-08-18 10:15:39 +00:00
|
|
|
raise Error("file doesn't exist: " + self.pretty_path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
xml.sax.parse(path, self, self)
|
|
|
|
except ValueError:
|
|
|
|
pass # FIXME hack arount DTD error
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
def startElement(self, name, attrs):
|
|
|
|
self.level += 1
|
|
|
|
if self.level == 1:
|
|
|
|
if name != "PropertyList":
|
|
|
|
raise XMLError(self.locator, "XML file isn't a <PropertyList>")
|
|
|
|
else:
|
|
|
|
index = 0
|
2008-08-19 06:36:48 +00:00
|
|
|
if attrs.has_key("n"):
|
|
|
|
index = int(attrs["n"])
|
|
|
|
elif name in self.stack[-1][2]:
|
|
|
|
index = self.stack[-1][2][name] + 1
|
|
|
|
self.stack[-1][2][name] = index
|
|
|
|
|
|
|
|
self.type = "unspecified"
|
|
|
|
if attrs.has_key("type"):
|
|
|
|
self.type = attrs["type"]
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
if attrs.has_key("include"):
|
2008-08-19 06:52:47 +00:00
|
|
|
path = os.path.dirname(os.path.abspath(self.path)) + "/" + attrs["include"]
|
2008-08-19 06:36:48 +00:00
|
|
|
if attrs.has_key("omit-node") and attrs["omit-node"] == "y" or self.level == 1:
|
2008-08-18 10:15:39 +00:00
|
|
|
self.stack.append([None, None, self.stack[-1][2], []])
|
2008-08-18 07:35:03 +00:00
|
|
|
else:
|
2008-08-18 10:15:39 +00:00
|
|
|
self.stack.append([name, index, {}, []])
|
2008-08-18 07:35:03 +00:00
|
|
|
parse_xml_file(path, self.nesting + 1, self.stack)
|
2008-08-19 06:36:48 +00:00
|
|
|
elif self.level > 1:
|
2008-08-18 10:15:39 +00:00
|
|
|
self.stack.append([name, index, {}, []])
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
def endElement(self, name):
|
2008-08-18 10:15:39 +00:00
|
|
|
value = string.join(self.stack[-1][3], '')
|
2008-08-19 06:36:48 +00:00
|
|
|
if not len(self.stack[-1][2]) and self.level > 1:
|
2008-08-18 07:35:03 +00:00
|
|
|
path = self.pathname()
|
|
|
|
if path:
|
|
|
|
cooked_value = self.escape(value.encode("iso-8859-15", "backslashreplace"))
|
2008-08-18 10:15:39 +00:00
|
|
|
print config.cooked_format % {
|
|
|
|
"f": self.pretty_path,
|
|
|
|
"l": self.locator.getLineNumber(),
|
|
|
|
"c": self.locator.getColumnNumber(),
|
|
|
|
"p": path,
|
|
|
|
"t": self.type,
|
|
|
|
"V": value,
|
|
|
|
"v": cooked_value,
|
2008-08-19 11:13:07 +00:00
|
|
|
"q": cooked_value.replace("'", "\\'"),
|
|
|
|
'Q': cooked_value.replace('"', '\\"'),
|
2008-08-18 10:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
elif len(string.strip(value)):
|
2008-08-18 13:12:31 +00:00
|
|
|
raise XMLError(self.locator, "garbage found '%s'" % string.strip(value))
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
self.level -= 1
|
|
|
|
if self.level:
|
|
|
|
self.stack.pop()
|
|
|
|
|
|
|
|
def characters(self, data):
|
2008-08-18 10:15:39 +00:00
|
|
|
self.stack[-1][3].append(data)
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
def setDocumentLocator(self, locator):
|
|
|
|
self.locator = locator
|
|
|
|
|
|
|
|
def pathname(self):
|
|
|
|
path = ""
|
|
|
|
for e in self.stack[1:]:
|
|
|
|
if e[0] == None: # omit-node
|
|
|
|
continue
|
|
|
|
path += "/" + e[0]
|
|
|
|
if e[1] and config.indices == 1 or config.indices == 2:
|
|
|
|
path += "[%d]" % e[1]
|
|
|
|
return path
|
|
|
|
|
|
|
|
def escape(self, string):
|
|
|
|
s = ""
|
|
|
|
for c in string:
|
|
|
|
if c == '\n':
|
|
|
|
s += '\\n'
|
|
|
|
elif c == '\r':
|
|
|
|
s += '\\r'
|
|
|
|
elif c == '\v':
|
|
|
|
s += '\\v'
|
|
|
|
elif c == '\\':
|
|
|
|
s += '\\\\'
|
|
|
|
elif not c.isalnum() and " \t!@#$%^&*()_+|~-=\`[]{};':\",./<>?".find(c) < 0:
|
|
|
|
s += "\\x%02x" % ord(c)
|
|
|
|
else:
|
|
|
|
s += c
|
|
|
|
return s
|
|
|
|
|
|
|
|
def warning(self, exception):
|
|
|
|
raise XMLError(self.locator, "WARNING: " + str(exception))
|
2008-08-18 13:12:31 +00:00
|
|
|
|
2008-08-18 07:35:03 +00:00
|
|
|
def error(self, exception):
|
|
|
|
raise XMLError(self.locator, "ERROR: " + str(exception))
|
2008-08-18 13:12:31 +00:00
|
|
|
|
2008-08-18 07:35:03 +00:00
|
|
|
def fatalError(self, exception):
|
|
|
|
raise XMLError(self.locator, "FATAL: " + str(exception))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if 'FG_ROOT' in os.environ:
|
2008-08-18 10:15:39 +00:00
|
|
|
config.root = os.environ['FG_ROOT'].lstrip().rstrip("/\\\t ")
|
2008-08-18 07:35:03 +00:00
|
|
|
if 'FG_HOME' in os.environ:
|
2008-08-18 10:15:39 +00:00
|
|
|
config.home = os.environ['FG_HOME'].lstrip().rstrip("/\\\t ")
|
2008-08-18 07:35:03 +00:00
|
|
|
if 'LSPROP_FORMAT' in os.environ:
|
|
|
|
config.format = os.environ['LSPROP_FORMAT']
|
|
|
|
|
|
|
|
# options
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], \
|
2008-08-18 13:12:31 +00:00
|
|
|
"hviIpf:", \
|
|
|
|
["help", "verbose", "all-indices", "no-indices", "raw-paths", "format="])
|
2008-08-18 07:35:03 +00:00
|
|
|
except getopt.GetoptError, msg:
|
2008-08-19 06:36:48 +00:00
|
|
|
errmsg("Error: %s" % msg)
|
|
|
|
return -1
|
2008-08-18 07:35:03 +00:00
|
|
|
|
|
|
|
for o, a in opts:
|
|
|
|
if o in ("-h", "--help"):
|
|
|
|
print __doc__
|
|
|
|
print '\t--format="%s"' % config.format.replace('"', '\\"')
|
|
|
|
return 0
|
|
|
|
if o in ("-v", "--verbose"):
|
|
|
|
config.verbose += 1
|
|
|
|
if o in ("-i", "--all-indices"):
|
|
|
|
config.indices = 2
|
|
|
|
if o in ("-I", "--no-indices"):
|
|
|
|
config.indices = 0
|
2008-08-18 10:15:39 +00:00
|
|
|
if o in ("-p", "--raw-paths"):
|
|
|
|
config.raw_paths = 1
|
2008-08-18 07:35:03 +00:00
|
|
|
if o in ("-f", "--format"):
|
|
|
|
config.format = a
|
|
|
|
|
|
|
|
# format
|
|
|
|
f = config.format
|
|
|
|
f = f.replace("\\e", "\x1b")
|
|
|
|
f = f.replace("\\033", "\x1b")
|
|
|
|
f = f.replace("\\x1b", "\x1b")
|
|
|
|
f = f.replace("%%", "\x01\x01")
|
|
|
|
f = f.replace("%f", "\x01(f)s")
|
|
|
|
f = f.replace("%l", "\x01(l)d")
|
|
|
|
f = f.replace("%c", "\x01(c)d")
|
|
|
|
f = f.replace("%p", "\x01(p)s")
|
|
|
|
f = f.replace("%t", "\x01(t)s")
|
|
|
|
f = f.replace("%V", "\x01(V)s")
|
|
|
|
f = f.replace("%v", "\x01(v)s")
|
2008-08-19 11:13:07 +00:00
|
|
|
f = f.replace("%q", "\x01(q)s")
|
|
|
|
f = f.replace('%Q', '\x01(Q)s')
|
2008-08-18 07:35:03 +00:00
|
|
|
f = f.replace("%", "%%")
|
|
|
|
f = f.replace("\x01", "%")
|
|
|
|
config.cooked_format = f
|
|
|
|
|
2008-08-18 10:15:39 +00:00
|
|
|
if config.verbose > 2:
|
2008-08-18 07:35:03 +00:00
|
|
|
print >>sys.stderr, "internal format = [%s]" % config.cooked_format
|
|
|
|
|
2008-08-18 13:12:31 +00:00
|
|
|
# arguments
|
2008-08-18 07:35:03 +00:00
|
|
|
if not len(args):
|
|
|
|
args = [config.root + "/preferences.xml"]
|
|
|
|
if not os.path.exists(args[0]):
|
|
|
|
errmsg("Error: environment variable FG_ROOT not set or set wrongly?")
|
|
|
|
return -1
|
|
|
|
for f in glob.glob(config.root + '/Aircraft/*/*-set.xml'):
|
|
|
|
args.append(f)
|
|
|
|
|
|
|
|
for arg in args:
|
|
|
|
try:
|
|
|
|
parse_xml_file(arg)
|
|
|
|
except Abort, e:
|
|
|
|
errmsg("Abort: " + e.args[0])
|
|
|
|
return -1
|
|
|
|
except Error, e:
|
|
|
|
errmsg("Error: " + e.args[0])
|
|
|
|
except IOError, (errno, msg):
|
|
|
|
errmsg("Error: " + msg)
|
|
|
|
return errno
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print >>sys.stderr, "\033[m"
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|
2008-08-18 13:12:31 +00:00
|
|
|
|