1
0
mirror of https://github.com/moparisthebest/xeps synced 2024-11-21 08:45:04 -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
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():
import argparse
import sys
@ -170,35 +179,31 @@ def main():
except ValueError:
continue
try:
with xepfile.open("rb") as f:
parsed = open_xml(f)
tree.append(make_metadata_element(
number,
extract_xep_metadata(parsed),
True,
))
except Exception as exc:
parsed = parse_checked_and_print_error(xepfile)
if parsed is None:
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"):
protoname = xepfile.name.rsplit(".", 1)[0]
try:
with xepfile.open("rb") as f:
parsed = open_xml(f)
tree.append(make_metadata_element(
"xxxx",
extract_xep_metadata(parsed),
False,
protoname=protoname
))
except Exception as exc:
parsed = parse_checked_and_print_error(xepfile)
if parsed is None:
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:
sys.exit(2)