Commit d2db72a3 authored by Kipp Cannon's avatar Kipp Cannon

glue.ligolw: load_filename() load_url()

- close files
- see !58
parent 44217192
Pipeline #46638 failed with stages
in 2 minutes and 53 seconds
......@@ -393,17 +393,10 @@ def load_filename(filename, verbose = False, **kwargs):
"""
if verbose:
sys.stderr.write("reading %s ...\n" % (("'%s'" % filename) if filename is not None else "stdin"))
if filename is not None:
fileobj = open(filename, "rb")
else:
fileobj = sys.stdin
# In Python 3, ``sys.stdin`` has an attribute called ``buffer`` that is the
# underyling byte-oriented stream.
try:
fileobj = fileobj.buffer
except AttributeError:
pass
xmldoc, hexdigest = load_fileobj(fileobj, **kwargs)
# In Python 3, ``sys.stdin`` has an attribute called ``buffer``
# that is the underyling byte-oriented stream.
with open(filename, "rb") if filename is not None else sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin as fileobj:
xmldoc, hexdigest = load_fileobj(fileobj, **kwargs)
if verbose:
sys.stderr.write("md5sum: %s %s\n" % (hexdigest, (filename if filename is not None else "")))
return xmldoc
......@@ -430,18 +423,15 @@ def load_url(url, verbose = False, **kwargs):
if url is not None:
scheme, host, path = urllib.parse.urlparse(url)[:3]
if scheme.lower() in ("", "file") and host.lower() in ("", "localhost"):
fileobj = open(path, "rb")
fileobj = lambda: open(path, "rb")
else:
fileobj = urllib.request.urlopen(url)
fileobj = lambda: urllib.request.urlopen(url)
else:
fileobj = sys.stdin
# In Python 3, ``sys.stdin`` has an attribute called ``buffer`` that is the
# underyling byte-oriented stream.
try:
fileobj = fileobj.buffer
except AttributeError:
pass
xmldoc, hexdigest = load_fileobj(fileobj, **kwargs)
# In Python 3, ``sys.stdin`` has an attribute called
# ``buffer`` that is the underyling byte-oriented stream.
fileobj = lambda: sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin
with fileobj() as fileobj:
xmldoc, hexdigest = load_fileobj(fileobj, **kwargs)
if verbose:
sys.stderr.write("md5sum: %s %s\n" % (hexdigest, (url if url is not None else "")))
return xmldoc
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment