Skip to content

Commit 7b55313

Browse files
author
Kazuki Suzuki Przyborowski
committed
Update pyneofile.py
1 parent 132decc commit 7b55313

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

pyneofile.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ def _wrap_outfile(outfile):
175175
return outfile, False, False
176176
return _iopen(outfile, 'wb'), True, False
177177

178+
def _open_in(infile):
179+
if hasattr(infile, 'read'): return infile, False
180+
if isinstance(infile, (bytes, bytearray)): return io.BytesIO(infile), True
181+
if infile is None: raise ValueError('infile is None')
182+
return io.open(u(infile), 'rb'), True
183+
184+
def _open_out(outfile):
185+
if outfile in (None, '-', b'-'): return True, None, bytearray()
186+
if hasattr(outfile, 'write'): return False, outfile, None
187+
return False, io.open(u(outfile), 'wb'), None
188+
178189
def _normalize_pack_inputs(infiles):
179190
"""Normalize in-memory inputs into items for pack_iter_neo.
180191
Supported forms:
@@ -288,7 +299,7 @@ def __init__(self, fp): self.fp = fp
288299
def write(self, data): self.fp.write(data if isinstance(data, (bytes, bytearray)) else b(data))
289300

290301
dst = _Dst(fp)
291-
_write_global_header(dst, fs, 0, encoding, checksumtype)
302+
_write_global_header(fp, 0, encoding, checksumtype, extradata=[], formatspecs=fs)
292303
fp.write(_append_nulls(['0', '0'], d))
293304
try:
294305
fp.flush()
@@ -314,12 +325,12 @@ def make_empty_file_neo(outfile=None, fmttype=None, checksumtype='crc32', format
314325
fs = _select_formatspecs_neo(formatspecs, fmttype, outfile)
315326
d = fs['format_delimiter']
316327

317-
bufmode, fp, buf = _open_out(outfile)
328+
bufmode, fp, buf = _wrap_outfile(outfile)
318329
class _Dst(object):
319330
def write(self, data): _write(bufmode, fp, buf, data)
320331
dst = _Dst()
321-
_write_global_header(dst, fs, 0, encoding, checksumtype)
322-
_write(bufmode, fp, buf, _append_nulls(['0', '0'], d))
332+
_write_global_header(fp, 0, encoding, checksumtype, extradata=[], formatspecs=fs)
333+
fp.write(_append_nulls(['0', '0'], d))
323334

324335
# return policy
325336
if bufmode:

0 commit comments

Comments
 (0)