XMLErrors on one line
This commit is contained in:
parent
1c4e485d12
commit
9959b08698
1 changed files with 51 additions and 39 deletions
|
@ -14,7 +14,8 @@ Options:
|
||||||
-v, --verbose increase verbosity (can be used multiple times)
|
-v, --verbose increase verbosity (can be used multiple times)
|
||||||
-i, --all-indices also show null indices in properties
|
-i, --all-indices also show null indices in properties
|
||||||
-I, --no-indices don't show any indices in properties
|
-I, --no-indices don't show any indices in properties
|
||||||
-f, --format set output format (default: --format="%f +%l: %p = '%'v'")
|
-p, --raw-paths don't replace FG_ROOT/FG_HOME prefix by environment variables
|
||||||
|
-f, --format set output format (default: --format="%f +%l: %p = '%v'")
|
||||||
|
|
||||||
Format:
|
Format:
|
||||||
%f file path
|
%f file path
|
||||||
|
@ -42,7 +43,8 @@ If no file arguments are specified, then the following files are assumed:
|
||||||
class config:
|
class config:
|
||||||
root = "/usr/local/share/FlightGear"
|
root = "/usr/local/share/FlightGear"
|
||||||
home = os.environ["HOME"] + "/.fgfs"
|
home = os.environ["HOME"] + "/.fgfs"
|
||||||
format = "%f +%l: %p = '%'v'"
|
raw_paths = 0
|
||||||
|
format = "%f +%l: %p = '%v'"
|
||||||
verbose = 1
|
verbose = 1
|
||||||
indices = 1 # 0: no indices; 1: only indices != [0]; 2: all indices
|
indices = 1 # 0: no indices; 1: only indices != [0]; 2: all indices
|
||||||
|
|
||||||
|
@ -54,6 +56,17 @@ def errmsg(msg, color = "31;1"):
|
||||||
print >>sys.stderr, msg
|
print >>sys.stderr, msg
|
||||||
|
|
||||||
|
|
||||||
|
def cook_path(path, force = 0):
|
||||||
|
path = os.path.normpath(path)
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
class Error(Exception):
|
class Error(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -64,9 +77,9 @@ class Abort(Exception):
|
||||||
|
|
||||||
class XMLError(Exception):
|
class XMLError(Exception):
|
||||||
def __init__(self, locator, msg):
|
def __init__(self, locator, msg):
|
||||||
msg = "%s\n\tin %s, line %d, column %d" \
|
msg = "%s in %s +%d:%d" \
|
||||||
% (msg, locator.getSystemId(), locator.getLineNumber(), \
|
% (msg.replace("\n", "\\n"), cook_path(locator.getSystemId()), \
|
||||||
locator.getColumnNumber())
|
locator.getLineNumber(), locator.getColumnNumber())
|
||||||
raise Error(msg)
|
raise Error(msg)
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,19 +92,19 @@ class parse_xml_file(xml.sax.handler.ContentHandler):
|
||||||
if stack:
|
if stack:
|
||||||
self.stack = stack
|
self.stack = stack
|
||||||
else:
|
else:
|
||||||
self.stack = [[None, None, {}, ""]] # name, index, indices, data
|
self.stack = [[None, None, {}, []]] # name, index, indices, data
|
||||||
|
|
||||||
self.pretty_path = os.path.normpath(path)
|
self.pretty_path = cook_path(path)
|
||||||
if path.startswith(config.root):
|
|
||||||
self.pretty_path = self.pretty_path.replace(config.root, "$FG_ROOT", 1)
|
|
||||||
elif path.startswith(config.home):
|
|
||||||
self.pretty_path = self.pretty_path.replace(config.home, "$FG_HOME", 1)
|
|
||||||
|
|
||||||
if config.verbose > 1:
|
if config.verbose > 1:
|
||||||
errmsg("%s (%d)" % (path, nesting), "35")
|
errmsg("%s (%d)" % (path, nesting), "35")
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
raise Error("file doesn't exist: " + path)
|
raise Error("file doesn't exist: " + self.pretty_path)
|
||||||
|
|
||||||
|
try:
|
||||||
xml.sax.parse(path, self, self)
|
xml.sax.parse(path, self, self)
|
||||||
|
except ValueError:
|
||||||
|
pass # FIXME hack arount DTD error
|
||||||
|
|
||||||
def startElement(self, name, attrs):
|
def startElement(self, name, attrs):
|
||||||
self.level += 1
|
self.level += 1
|
||||||
|
@ -111,24 +124,23 @@ class parse_xml_file(xml.sax.handler.ContentHandler):
|
||||||
if attrs.has_key("include"):
|
if attrs.has_key("include"):
|
||||||
path = os.path.dirname(self.path) + "/" + attrs["include"]
|
path = os.path.dirname(self.path) + "/" + attrs["include"]
|
||||||
if attrs.has_key("omit-node") and attrs["omit-node"] == "y":
|
if attrs.has_key("omit-node") and attrs["omit-node"] == "y":
|
||||||
self.stack.append([None, None, self.stack[-1][2], ""])
|
self.stack.append([None, None, self.stack[-1][2], []])
|
||||||
else:
|
else:
|
||||||
self.stack.append([name, index, {}, ""])
|
self.stack.append([name, index, {}, []])
|
||||||
parse_xml_file(path, self.nesting + 1, self.stack)
|
parse_xml_file(path, self.nesting + 1, self.stack)
|
||||||
else:
|
else:
|
||||||
self.stack.append([name, index, {}, ""])
|
self.stack.append([name, index, {}, []])
|
||||||
|
|
||||||
self.type = "unspecified"
|
self.type = "unspecified"
|
||||||
if attrs.has_key("type"):
|
if attrs.has_key("type"):
|
||||||
self.type = attrs["type"]
|
self.type = attrs["type"]
|
||||||
|
|
||||||
def endElement(self, name):
|
def endElement(self, name):
|
||||||
|
value = string.join(self.stack[-1][3], '')
|
||||||
if not len(self.stack[-1][2]):
|
if not len(self.stack[-1][2]):
|
||||||
path = self.pathname()
|
path = self.pathname()
|
||||||
if path:
|
if path:
|
||||||
value = self.stack[-1][3]
|
|
||||||
cooked_value = self.escape(value.encode("iso-8859-15", "backslashreplace"))
|
cooked_value = self.escape(value.encode("iso-8859-15", "backslashreplace"))
|
||||||
try:
|
|
||||||
print config.cooked_format % {
|
print config.cooked_format % {
|
||||||
"f": self.pretty_path,
|
"f": self.pretty_path,
|
||||||
"l": self.locator.getLineNumber(),
|
"l": self.locator.getLineNumber(),
|
||||||
|
@ -140,18 +152,16 @@ class parse_xml_file(xml.sax.handler.ContentHandler):
|
||||||
"v'": cooked_value.replace("'", "\\'"),
|
"v'": cooked_value.replace("'", "\\'"),
|
||||||
'v"': cooked_value.replace('"', '\\"'),
|
'v"': cooked_value.replace('"', '\\"'),
|
||||||
}
|
}
|
||||||
except TypeError, e:
|
|
||||||
raise Abort("invalid --format value")
|
|
||||||
|
|
||||||
elif len(string.strip(self.stack[-1][3])):
|
elif len(string.strip(value)):
|
||||||
raise XMLError(self.locator, "child data '" + string.strip(self.stack[-1][3]) + "'")
|
raise XMLError(self.locator, "garbage found '" + string.strip(value) + "'")
|
||||||
|
|
||||||
self.level -= 1
|
self.level -= 1
|
||||||
if self.level:
|
if self.level:
|
||||||
self.stack.pop()
|
self.stack.pop()
|
||||||
|
|
||||||
def characters(self, data):
|
def characters(self, data):
|
||||||
self.stack[-1][3] += data
|
self.stack[-1][3].append(data)
|
||||||
|
|
||||||
def setDocumentLocator(self, locator):
|
def setDocumentLocator(self, locator):
|
||||||
self.locator = locator
|
self.locator = locator
|
||||||
|
@ -193,17 +203,17 @@ class parse_xml_file(xml.sax.handler.ContentHandler):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if 'FG_ROOT' in os.environ:
|
if 'FG_ROOT' in os.environ:
|
||||||
config.root = os.environ['FG_ROOT'].rstrip("/\\\t ").lstrip()
|
config.root = os.environ['FG_ROOT'].lstrip().rstrip("/\\\t ")
|
||||||
if 'FG_HOME' in os.environ:
|
if 'FG_HOME' in os.environ:
|
||||||
config.home = os.environ['FG_HOME'].rstrip("/\\\t ").lstrip()
|
config.home = os.environ['FG_HOME'].lstrip().rstrip("/\\\t ")
|
||||||
if 'LSPROP_FORMAT' in os.environ:
|
if 'LSPROP_FORMAT' in os.environ:
|
||||||
config.format = os.environ['LSPROP_FORMAT']
|
config.format = os.environ['LSPROP_FORMAT']
|
||||||
|
|
||||||
# options
|
# options
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], \
|
opts, args = getopt.getopt(sys.argv[1:], \
|
||||||
"hviIf:", \
|
"hviIf:p", \
|
||||||
["help", "verbose", "all-indices", "no-indices", "format="])
|
["help", "verbose", "all-indices", "no-indices", "format=", "unify-paths"])
|
||||||
except getopt.GetoptError, msg:
|
except getopt.GetoptError, msg:
|
||||||
print >>sys.stderr, str(msg)
|
print >>sys.stderr, str(msg)
|
||||||
return 0
|
return 0
|
||||||
|
@ -220,6 +230,8 @@ def main():
|
||||||
config.indices = 2
|
config.indices = 2
|
||||||
if o in ("-I", "--no-indices"):
|
if o in ("-I", "--no-indices"):
|
||||||
config.indices = 0
|
config.indices = 0
|
||||||
|
if o in ("-p", "--raw-paths"):
|
||||||
|
config.raw_paths = 1
|
||||||
if o in ("-f", "--format"):
|
if o in ("-f", "--format"):
|
||||||
config.format = a
|
config.format = a
|
||||||
|
|
||||||
|
@ -243,7 +255,7 @@ def main():
|
||||||
f = f.replace("\x01", "%")
|
f = f.replace("\x01", "%")
|
||||||
config.cooked_format = f
|
config.cooked_format = f
|
||||||
|
|
||||||
if config.verbose > 1:
|
if config.verbose > 2:
|
||||||
print >>sys.stderr, "internal format = [%s]" % config.cooked_format
|
print >>sys.stderr, "internal format = [%s]" % config.cooked_format
|
||||||
|
|
||||||
if not len(args):
|
if not len(args):
|
||||||
|
|
Loading…
Add table
Reference in a new issue