Skip to content

Commit

Permalink
stdin: fix false no file test and add tests for stdin
Browse files Browse the repository at this point in the history
Commit 5a14efc broke input from stdin, with the check added for
actually existing files. Add an exception when the file input is "-" so
reading from stdin works again. The original commit that introduced this
feature did not include documentation. Add documentation to "--help",
manpages and the README. Add testing for reading from stdin with python3
so future commits will not break the feature unintentionally.

Fixes: dilshod#270
  • Loading branch information
ferdinandyb committed Dec 13, 2023
1 parent c676f08 commit 3fe49b1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pip install xlsx2csv
```
**positional arguments:**
```
xlsxfile xlsx file path
xlsxfile xlsx file path, use '-' to read from STDIN
outfile output csv file path, or directory if -s 0 is specified
```
**optional arguments:**
Expand Down
4 changes: 4 additions & 0 deletions man/xlsx2csv.1.pod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ xlsx2csv - Convert xlsx xml files to csv format

The conversion uses Expat SAX parser for xml processing.

=head1 STDIN

Use "-" for INFILE to read from the STDIN.

=head1 OPTIONS

=over 4
Expand Down
26 changes: 23 additions & 3 deletions test/run
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def compare(case, arguments=[]):
ext = "xlsx"
if os.path.exists("test/%s.xlsm" % case):
ext = "xlsm"

if os.name == 'posix':# in case of Linux
left = subprocess.check_output(["python%s" %pyver, "./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','')
command = ["python%s" %pyver]
elif os.name == 'nt':# in case of Windows
# Use py.exe http://blog.python.org/2011/07/python-launcher-for-windows_11.html on Windows
left = subprocess.check_output(["py", "-%s" %pyver, "./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','')
command = ["py", "-%s" %pyver]
else:
print("os.name is unexpected: "+os.name)
sys.exit(1)
left = subprocess.check_output(command + ["./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','')

f = open("test/%s.csv" %case, "r", encoding="utf-8", newline="")
right = f.read().replace('\r','')
Expand All @@ -41,9 +42,28 @@ def compare(case, arguments=[]):
failed = True
else:
print("OK: %s %s" %(case, pyver))

# test STDIN, only works for python3
if pyver == "2":
continue

xfile = open("test/%s.%s" %(case,ext), "rb")
stdin = xfile.read()
xfile.close()

pipe = subprocess.run(command + ["./xlsx2csv.py"] + arguments + ["-"], input = stdin, capture_output = True)
stdinleft = pipe.stdout.decode("utf-8").replace('\r','').replace('\r','')

if stdinleft != right:
print("FAILED (STDIN): %s %s" %(case, pyver))
failed = True
else:
print("OK (STDIN): %s %s" %(case, pyver))

if failed:
sys.exit(1)


compare("datetime", ["--dateformat=%Y-%m-%d %H:%M:%S"])
compare("empty_row")
compare("junk-small")
Expand Down
4 changes: 2 additions & 2 deletions xlsx2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ def main():

if "ArgumentParser" in globals():
parser = ArgumentParser(description="xlsx to csv converter")
parser.add_argument('infile', metavar='xlsxfile', help="xlsx file path")
parser.add_argument('infile', metavar='xlsxfile', help="xlsx file path, use '-' to read from STDIN")
parser.add_argument('outfile', metavar='outfile', nargs='?', help="output csv file path")
parser.add_argument('-v', '--version', action='version', version=__version__)
nargs_plus = "+"
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def main():
try:
if os.path.isdir(options.infile):
convert_recursive(options.infile, sheetid, outfile, kwargs)
elif not os.path.exists(options.infile):
elif not os.path.exists(options.infile) and options.infile != "-":
raise InvalidXlsxFileException("Input file not found!")
else:
xlsx2csv = Xlsx2csv(options.infile, **kwargs)
Expand Down

0 comments on commit 3fe49b1

Please sign in to comment.