Standardisation 2
This commit is contained in:
+35
-8
@@ -2,10 +2,19 @@ from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import uuid
|
||||
from collections.abc import Sequence
|
||||
from pathlib import Path
|
||||
|
||||
from pypdf import PdfReader, PdfWriter
|
||||
|
||||
from copienator import (
|
||||
EvaluationWorkspace,
|
||||
ExitCode,
|
||||
execute,
|
||||
standard_parser,
|
||||
workspace_from_args,
|
||||
)
|
||||
|
||||
|
||||
def copy_pdfs(directory: Path) -> list[Path]:
|
||||
directory = directory.expanduser().resolve()
|
||||
@@ -91,19 +100,37 @@ def rename_all(directory: Path) -> list[tuple[Path, Path]]:
|
||||
return [(source, destination) for source, _temporary, destination in staged]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Prepare scanned PDF copies.")
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = standard_parser("Prepare scanned PDF copies.")
|
||||
subparsers = parser.add_subparsers(dest="operation", required=True)
|
||||
for operation in ("rotate", "rename"):
|
||||
subparser = subparsers.add_parser(operation)
|
||||
subparser.add_argument("directory", type=Path)
|
||||
args = parser.parse_args()
|
||||
subparser.add_argument("evaluation", type=Path, help="Evaluation directory")
|
||||
subparser.add_argument(
|
||||
"--verbose",
|
||||
action="store_true",
|
||||
default=argparse.SUPPRESS,
|
||||
help="Show a traceback when an unexpected error occurs",
|
||||
)
|
||||
return parser
|
||||
|
||||
if args.operation == "rotate":
|
||||
rotate_all(args.directory)
|
||||
|
||||
def run(workspace: EvaluationWorkspace, *, operation: str) -> ExitCode:
|
||||
if operation == "rotate":
|
||||
rotate_all(workspace.root)
|
||||
else:
|
||||
rename_all(args.directory)
|
||||
rename_all(workspace.root)
|
||||
return ExitCode.SUCCESS
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser = build_parser()
|
||||
return execute(
|
||||
parser,
|
||||
argv,
|
||||
lambda args: run(workspace_from_args(args), operation=args.operation),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
raise SystemExit(main())
|
||||
|
||||
Reference in New Issue
Block a user