1
0
mirror of https://github.com/moparisthebest/xeps synced 2024-12-01 05:32:15 -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,25 +179,24 @@ 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) has_error = True
continue
tree.append(make_metadata_element( tree.append(make_metadata_element(
number, number,
extract_xep_metadata(parsed), extract_xep_metadata(parsed),
True, True,
)) ))
except Exception as exc:
has_error = True
print("{}: {}".format(xepfile, exc), file=sys.stderr)
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) has_error = True
continue
tree.append(make_metadata_element( tree.append(make_metadata_element(
"xxxx", "xxxx",
@ -196,9 +204,6 @@ def main():
False, False,
protoname=protoname protoname=protoname
)) ))
except Exception as exc:
has_error = True
print("{}: {}".format(xepfile, exc), file=sys.stderr)
if has_error: if has_error:
sys.exit(2) sys.exit(2)