winziprint: add -v and -2 options
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Union
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
@ -11,23 +12,41 @@ import weasyprint
|
||||
import pypdf
|
||||
|
||||
|
||||
VERSION = __version__ = '0.1.0'
|
||||
|
||||
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
|
||||
page_nums = []
|
||||
tmp_file_names = []
|
||||
|
||||
steps = len(input_files) + len(input_files) // BATCH_SIZE + 1
|
||||
blank_page = _get_blank_page() if padding else None
|
||||
|
||||
try:
|
||||
for i in range(0, len(input_files), BATCH_SIZE):
|
||||
batch = input_files[i:i + BATCH_SIZE]
|
||||
documents = []
|
||||
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()
|
||||
if padding and len(doc.pages) % 2 != 0:
|
||||
doc.pages.append(blank_page)
|
||||
documents.append(doc)
|
||||
del html
|
||||
if progress:
|
||||
@ -60,10 +79,18 @@ def convert(input_files: list[str], output_files: str, encoding: str = None, pro
|
||||
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:
|
||||
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()
|
||||
pages = convert(args[:-1], args[-1], encoding=encoding, progress=progress)
|
||||
pages = convert(inputs, output, encoding=encoding, padding=padding, progress=progress)
|
||||
t1 = time.process_time()
|
||||
print(f'success: '
|
||||
f'{len(args) - 1} documents, '
|
||||
@ -79,57 +106,67 @@ def _wrapper_convert(args: list[str], encoding: str = None, progress: bool = Fal
|
||||
|
||||
|
||||
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'
|
||||
' -h, --help show this help message and exit\n'
|
||||
' -d, --directory set the working directory\n'
|
||||
' -e, --encoding encoding of the input files\n'
|
||||
' -p, --progress show progress updates\n'
|
||||
' -h, --help show this help message and exit\n'
|
||||
' -v, --version show version and exit\n'
|
||||
' -d, --directory set the working directory\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'
|
||||
'\n'
|
||||
' - use stdin for retrieving input and output file names (semi-colon-seperated)\n'
|
||||
' INPUT name of an html input file\n'
|
||||
' OUTPUT name of an pdf output file', file=sys.stderr)
|
||||
' - use stdin for retrieving input and output file names (semi-colon-seperated)\n'
|
||||
' INPUT name of an html input file\n'
|
||||
' OUTPUT name of an pdf output file', file=sys.stderr)
|
||||
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
|
||||
for n in [n1] + (n2 and [n2] or []):
|
||||
if n in args:
|
||||
i = args.index(n)
|
||||
if i + 1 >= len(args):
|
||||
usage()
|
||||
v = args[i + 1]
|
||||
args.pop(i)
|
||||
args.pop(i)
|
||||
return v
|
||||
if flag:
|
||||
if n in args:
|
||||
v = True
|
||||
args.remove(n)
|
||||
else:
|
||||
if n in args:
|
||||
i = args.index(n)
|
||||
if i + 1 >= len(args):
|
||||
usage()
|
||||
v = args[i + 1]
|
||||
args.pop(i)
|
||||
args.pop(i)
|
||||
return v if not flag else v or False
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = sys.argv[1:]
|
||||
if len(args) == 0 or '-h' in args or '--help' in args:
|
||||
usage()
|
||||
elif '-v' in args or '--version' in args:
|
||||
version()
|
||||
|
||||
working_dir = _get_arg(args, '-d', '--directory')
|
||||
if working_dir:
|
||||
os.chdir(working_dir)
|
||||
encoding = _get_arg(args, '-e', '--encoding')
|
||||
|
||||
progress = False
|
||||
if '-p' in args:
|
||||
args.remove('-p')
|
||||
progress = True
|
||||
if '--progress' in args:
|
||||
args.remove('--progress')
|
||||
progress = True
|
||||
progress = _get_arg(args, '-p', '--progress', flag=True)
|
||||
double_paged = _get_arg(args, '-2', '--double-paged', flag=True)
|
||||
|
||||
if args == ['-']:
|
||||
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:
|
||||
usage()
|
||||
else:
|
||||
_wrapper_convert(args, encoding=encoding, progress=progress)
|
||||
_wrapper_convert(args, encoding=encoding, padding=double_paged, progress=progress)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Reference in New Issue
Block a user