1
0
mirror of https://github.com/moparisthebest/xeps synced 2024-11-28 12:12:22 -05:00

extract-metadata: make error handling cleaner

This commit is contained in:
Jonas Wielicki 2017-08-23 17:00:55 +02:00
parent 340ab90216
commit d0bbbb2d5c

View File

@ -141,6 +141,15 @@ def make_metadata_element(number, metadata, accepted, *, protoname=None):
return result return result
def parse_checked_and_print_error(xepfile):
try:
with xepfile.open("rb") as f:
return open_xml(f)
except xml.parsers.expat.ExpatError as exc:
print("{}: {}".format(xepfile, exc), file=sys.stderr)
return None
def main(): def main():
import argparse import argparse
import sys import sys
@ -170,35 +179,31 @@ def main():
except ValueError: except ValueError:
continue continue
try: parsed = parse_checked_and_print_error(xepfile)
with xepfile.open("rb") as f: if parsed is None:
parsed = open_xml(f)
tree.append(make_metadata_element(
number,
extract_xep_metadata(parsed),
True,
))
except Exception as exc:
has_error = True has_error = True
print("{}: {}".format(xepfile, exc), file=sys.stderr) continue
tree.append(make_metadata_element(
number,
extract_xep_metadata(parsed),
True,
))
for xepfile in (args.xepdir / "inbox").glob("*.xml"): for xepfile in (args.xepdir / "inbox").glob("*.xml"):
protoname = xepfile.name.rsplit(".", 1)[0] protoname = xepfile.name.rsplit(".", 1)[0]
try: parsed = parse_checked_and_print_error(xepfile)
with xepfile.open("rb") as f: if parsed is None:
parsed = open_xml(f)
tree.append(make_metadata_element(
"xxxx",
extract_xep_metadata(parsed),
False,
protoname=protoname
))
except Exception as exc:
has_error = True has_error = True
print("{}: {}".format(xepfile, exc), file=sys.stderr) continue
tree.append(make_metadata_element(
"xxxx",
extract_xep_metadata(parsed),
False,
protoname=protoname
))
if has_error: if has_error:
sys.exit(2) sys.exit(2)