forked from josch/img2pdf
fixing a slew of python 2 issues
This commit is contained in:
parent
725462462b
commit
0e76a5bd97
2 changed files with 61 additions and 22 deletions
|
@ -1236,26 +1236,42 @@ def parse_borderarg(string):
|
|||
def input_images(path):
|
||||
if path == '-':
|
||||
# we slurp in all data from stdin because we need to seek in it later
|
||||
result = sys.stdin.buffer.read()
|
||||
if PY3:
|
||||
result = sys.stdin.buffer.read()
|
||||
else:
|
||||
result = sys.stdin.read()
|
||||
if len(result) == 0:
|
||||
raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
|
||||
else:
|
||||
try:
|
||||
if os.path.getsize(path) == 0:
|
||||
raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
|
||||
# test-read a byte from it so that we can abort early in case
|
||||
# we cannot read data from the file
|
||||
with open(path, "rb") as im:
|
||||
im.read(1)
|
||||
except IsADirectoryError:
|
||||
raise argparse.ArgumentTypeError(
|
||||
"\"%s\" is a directory" % path)
|
||||
except PermissionError:
|
||||
raise argparse.ArgumentTypeError(
|
||||
"\"%s\" permission denied" % path)
|
||||
except FileNotFoundError:
|
||||
raise argparse.ArgumentTypeError(
|
||||
"\"%s\" does not exist" % path)
|
||||
if PY3:
|
||||
try:
|
||||
if os.path.getsize(path) == 0:
|
||||
raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
|
||||
# test-read a byte from it so that we can abort early in case
|
||||
# we cannot read data from the file
|
||||
with open(path, "rb") as im:
|
||||
im.read(1)
|
||||
except IsADirectoryError:
|
||||
raise argparse.ArgumentTypeError(
|
||||
"\"%s\" is a directory" % path)
|
||||
except PermissionError:
|
||||
raise argparse.ArgumentTypeError(
|
||||
"\"%s\" permission denied" % path)
|
||||
except FileNotFoundError:
|
||||
raise argparse.ArgumentTypeError(
|
||||
"\"%s\" does not exist" % path)
|
||||
else:
|
||||
try:
|
||||
if os.path.getsize(path) == 0:
|
||||
raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
|
||||
# test-read a byte from it so that we can abort early in case
|
||||
# we cannot read data from the file
|
||||
with open(path, "rb") as im:
|
||||
im.read(1)
|
||||
except IOError as err:
|
||||
raise argparse.ArgumentTypeError(str(err))
|
||||
except OSError as err:
|
||||
raise argparse.ArgumentTypeError(str(err))
|
||||
result = path
|
||||
return result
|
||||
|
||||
|
@ -1338,7 +1354,7 @@ def valid_date(string):
|
|||
raise argparse.ArgumentTypeError("cannot parse date: %s" % string)
|
||||
|
||||
|
||||
def main():
|
||||
def main(argv=sys.argv):
|
||||
rendered_papersizes = ""
|
||||
for k, v in sorted(papersizes.items()):
|
||||
rendered_papersizes += " %-8s %s\n" % (papernames[k], v)
|
||||
|
@ -1493,7 +1509,7 @@ Report bugs at https://gitlab.mister-muffin.de/josch/img2pdf/issues
|
|||
|
||||
outargs.add_argument(
|
||||
'-o', '--output', metavar='out', type=argparse.FileType('wb'),
|
||||
default=sys.stdout.buffer,
|
||||
default=sys.stdout.buffer if PY3 else sys.stdout,
|
||||
help='Makes the program output to a file instead of standard output.')
|
||||
outargs.add_argument(
|
||||
'-C', '--colorspace', metavar='colorspace', type=parse_colorspacearg,
|
||||
|
@ -1697,7 +1713,7 @@ values set via the --border option.
|
|||
'--viewer-fullscreen', action="store_true",
|
||||
help='Instruct the PDF viewer to open the PDF in fullscreen mode')
|
||||
|
||||
args = parser.parse_args()
|
||||
args = parser.parse_args(argv[1:])
|
||||
|
||||
if args.verbose:
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
@ -1710,7 +1726,10 @@ values set via the --border option.
|
|||
if len(args.images) == 0:
|
||||
logging.info("reading image from standard input")
|
||||
try:
|
||||
args.images = [sys.stdin.buffer.read()]
|
||||
if PY3:
|
||||
args.images = [sys.stdin.buffer.read()]
|
||||
else:
|
||||
args.images = [sys.stdin.read()]
|
||||
except KeyboardInterrupt:
|
||||
exit(0)
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import struct
|
|||
import sys
|
||||
import zlib
|
||||
from PIL import Image
|
||||
from io import StringIO, BytesIO
|
||||
from io import StringIO, BytesIO, TextIOWrapper
|
||||
|
||||
HERE = os.path.dirname(__file__)
|
||||
|
||||
|
@ -430,6 +430,25 @@ def tiff_header_for_ccitt(width, height, img_size, ccitt_group=4):
|
|||
)
|
||||
|
||||
|
||||
class CommandLineTests(unittest.TestCase):
|
||||
def test_main_help(self):
|
||||
# silence output
|
||||
sys_stdout = sys.stdout
|
||||
if PY3:
|
||||
sys.stdout = TextIOWrapper(BytesIO())
|
||||
else:
|
||||
sys.stdout = BytesIO()
|
||||
|
||||
try:
|
||||
img2pdf.main(['img2pdf', '--help'])
|
||||
except SystemExit:
|
||||
# argparse does sys.exit(0) on --help
|
||||
res = sys.stdout.getvalue()
|
||||
self.assertIn('img2pdf', res)
|
||||
finally:
|
||||
sys.stdout = sys_stdout
|
||||
|
||||
|
||||
def test_suite():
|
||||
class TestImg2Pdf(unittest.TestCase):
|
||||
pass
|
||||
|
@ -636,4 +655,5 @@ def test_suite():
|
|||
|
||||
return unittest.TestSuite((
|
||||
unittest.makeSuite(TestImg2Pdf),
|
||||
unittest.makeSuite(CommandLineTests),
|
||||
))
|
||||
|
|
Loading…
Reference in a new issue