winziprint: add -v and -2 options
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from typing import Union
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
@ -11,23 +12,41 @@ import weasyprint
|
|||||||
import pypdf
|
import pypdf
|
||||||
|
|
||||||
|
|
||||||
|
VERSION = __version__ = '0.1.0'
|
||||||
|
|
||||||
BATCH_SIZE = 10
|
BATCH_SIZE = 10
|
||||||
|
|
||||||
|
|
||||||
def convert(input_files: list[str], output_files: str, encoding: str = None, progress: bool = False) -> list[int]:
|
def _get_blank_page() -> weasyprint.Page:
|
||||||
|
html = weasyprint.HTML(string='')
|
||||||
|
doc = html.render()
|
||||||
|
blank_page = doc.pages[0]
|
||||||
|
del html
|
||||||
|
del doc
|
||||||
|
return blank_page
|
||||||
|
|
||||||
|
|
||||||
|
def convert(input_files: list[str],
|
||||||
|
output_files: str,
|
||||||
|
encoding: str = None,
|
||||||
|
padding: bool = False,
|
||||||
|
progress: bool = False) -> list[int]:
|
||||||
# it takes roughly 100ms to generate one document
|
# it takes roughly 100ms to generate one document
|
||||||
page_nums = []
|
page_nums = []
|
||||||
tmp_file_names = []
|
tmp_file_names = []
|
||||||
|
|
||||||
steps = len(input_files) + len(input_files) // BATCH_SIZE + 1
|
steps = len(input_files) + len(input_files) // BATCH_SIZE + 1
|
||||||
|
blank_page = _get_blank_page() if padding else None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for i in range(0, len(input_files), BATCH_SIZE):
|
for i in range(0, len(input_files), BATCH_SIZE):
|
||||||
batch = input_files[i:i + BATCH_SIZE]
|
batch = input_files[i:i + BATCH_SIZE]
|
||||||
documents = []
|
documents = []
|
||||||
for n, file_name in enumerate(batch):
|
for n, file_name in enumerate(batch):
|
||||||
html = weasyprint.HTML(file_name, encoding=encoding)
|
html = weasyprint.HTML(filename=file_name, encoding=encoding)
|
||||||
doc = html.render()
|
doc = html.render()
|
||||||
|
if padding and len(doc.pages) % 2 != 0:
|
||||||
|
doc.pages.append(blank_page)
|
||||||
documents.append(doc)
|
documents.append(doc)
|
||||||
del html
|
del html
|
||||||
if progress:
|
if progress:
|
||||||
@ -60,10 +79,18 @@ def convert(input_files: list[str], output_files: str, encoding: str = None, pro
|
|||||||
return page_nums
|
return page_nums
|
||||||
|
|
||||||
|
|
||||||
def _wrapper_convert(args: list[str], encoding: str = None, progress: bool = False) -> None:
|
def _wrapper_convert(args: list[str], encoding: str = None, padding: bool = False, progress: bool = False) -> None:
|
||||||
try:
|
try:
|
||||||
|
if len(args) < 2:
|
||||||
|
print(f'error: Too few arguments', flush=True)
|
||||||
|
return
|
||||||
|
inputs = args[:-1]
|
||||||
|
output = args[-1]
|
||||||
|
if inputs[0] == '-2':
|
||||||
|
inputs.pop(0)
|
||||||
|
padding = True
|
||||||
t0 = time.process_time()
|
t0 = time.process_time()
|
||||||
pages = convert(args[:-1], args[-1], encoding=encoding, progress=progress)
|
pages = convert(inputs, output, encoding=encoding, padding=padding, progress=progress)
|
||||||
t1 = time.process_time()
|
t1 = time.process_time()
|
||||||
print(f'success: '
|
print(f'success: '
|
||||||
f'{len(args) - 1} documents, '
|
f'{len(args) - 1} documents, '
|
||||||
@ -79,11 +106,13 @@ def _wrapper_convert(args: list[str], encoding: str = None, progress: bool = Fal
|
|||||||
|
|
||||||
|
|
||||||
def usage() -> None:
|
def usage() -> None:
|
||||||
print(f'usage: {sys.argv[0]} [-h] [-p] [-d DIR] [-e ENCODING] [ - | INPUT [INPUT...] OUTPUT ]\n\n'
|
print(f'usage: {sys.argv[0]} [-h] [-v] [-p] [-2] [-d DIR] [-e ENCODING] [ - | INPUT [INPUT...] OUTPUT ]\n\n'
|
||||||
'options:\n'
|
'options:\n'
|
||||||
' -h, --help show this help message and exit\n'
|
' -h, --help show this help message and exit\n'
|
||||||
|
' -v, --version show version and exit\n'
|
||||||
' -d, --directory set the working directory\n'
|
' -d, --directory set the working directory\n'
|
||||||
' -e, --encoding encoding of the input files\n'
|
' -e, --encoding encoding of the input files\n'
|
||||||
|
' -2, --double-paged pad documents to an even number of pages\n'
|
||||||
' -p, --progress show progress updates\n'
|
' -p, --progress show progress updates\n'
|
||||||
'\n'
|
'\n'
|
||||||
' - use stdin for retrieving input and output file names (semi-colon-seperated)\n'
|
' - use stdin for retrieving input and output file names (semi-colon-seperated)\n'
|
||||||
@ -92,9 +121,21 @@ def usage() -> None:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def _get_arg(args: list[str], n1: str, n2: str = None) -> str:
|
def version() -> None:
|
||||||
|
print(f'WinziPrint: {__version__}\n'
|
||||||
|
f'WeasyPrint: {weasyprint.__version__}',
|
||||||
|
file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_arg(args: list[str], n1: str, n2: str = None, flag: bool = False) -> Union[None, str, bool]:
|
||||||
v = None
|
v = None
|
||||||
for n in [n1] + (n2 and [n2] or []):
|
for n in [n1] + (n2 and [n2] or []):
|
||||||
|
if flag:
|
||||||
|
if n in args:
|
||||||
|
v = True
|
||||||
|
args.remove(n)
|
||||||
|
else:
|
||||||
if n in args:
|
if n in args:
|
||||||
i = args.index(n)
|
i = args.index(n)
|
||||||
if i + 1 >= len(args):
|
if i + 1 >= len(args):
|
||||||
@ -102,34 +143,30 @@ def _get_arg(args: list[str], n1: str, n2: str = None) -> str:
|
|||||||
v = args[i + 1]
|
v = args[i + 1]
|
||||||
args.pop(i)
|
args.pop(i)
|
||||||
args.pop(i)
|
args.pop(i)
|
||||||
return v
|
return v if not flag else v or False
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
args = sys.argv[1:]
|
args = sys.argv[1:]
|
||||||
if len(args) == 0 or '-h' in args or '--help' in args:
|
if len(args) == 0 or '-h' in args or '--help' in args:
|
||||||
usage()
|
usage()
|
||||||
|
elif '-v' in args or '--version' in args:
|
||||||
|
version()
|
||||||
|
|
||||||
working_dir = _get_arg(args, '-d', '--directory')
|
working_dir = _get_arg(args, '-d', '--directory')
|
||||||
if working_dir:
|
if working_dir:
|
||||||
os.chdir(working_dir)
|
os.chdir(working_dir)
|
||||||
encoding = _get_arg(args, '-e', '--encoding')
|
encoding = _get_arg(args, '-e', '--encoding')
|
||||||
|
progress = _get_arg(args, '-p', '--progress', flag=True)
|
||||||
progress = False
|
double_paged = _get_arg(args, '-2', '--double-paged', flag=True)
|
||||||
if '-p' in args:
|
|
||||||
args.remove('-p')
|
|
||||||
progress = True
|
|
||||||
if '--progress' in args:
|
|
||||||
args.remove('--progress')
|
|
||||||
progress = True
|
|
||||||
|
|
||||||
if args == ['-']:
|
if args == ['-']:
|
||||||
for line in sys.stdin:
|
for line in sys.stdin:
|
||||||
_wrapper_convert(line.strip().split(';'), encoding=encoding, progress=progress)
|
_wrapper_convert(line.strip().split(';'), encoding=encoding, padding=double_paged, progress=progress)
|
||||||
elif len(args) < 2:
|
elif len(args) < 2:
|
||||||
usage()
|
usage()
|
||||||
else:
|
else:
|
||||||
_wrapper_convert(args, encoding=encoding, progress=progress)
|
_wrapper_convert(args, encoding=encoding, padding=double_paged, progress=progress)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Reference in New Issue
Block a user