refactor for readability

This commit is contained in:
2020-06-12 11:34:08 +02:00
parent a4d2ac5c5b
commit 1fae4f49a0
4 changed files with 49 additions and 38 deletions

View File

@@ -1,10 +1,8 @@
import sys
import click
from click import ClickException
from app.generate import generate_answers, generate_problems
from app.config import BEGIN_ANSWERS_TOKEN
from app.generate import generate_answers, generate_problems
from app.pdf import make_pdf
@@ -24,6 +22,26 @@ def main(
output_filepath: str = None,
) -> None:
validate_args(pdf, silent, output_filepath)
answers, problems = make_problems_and_answers(bits, num_problems)
if pdf:
make_pdf(
problems=problems,
answers=answers,
output_path=output_filepath or "problems.pdf",
include_answers=include_answers,
)
if not silent:
if include_answers:
click.echo(BEGIN_ANSWERS_TOKEN.join((problems, answers)))
else:
click.echo(problems)
def validate_args(pdf, silent, output_filepath):
if pdf and silent:
raise ClickException(
"please specify either `pdf` or `silent`, not both (otherwise there "
@@ -33,24 +51,12 @@ def main(
if pdf and output_filepath and not output_filepath.endswith("pdf"):
raise ClickException("Please include an output filepath ending in '.pdf'")
def make_problems_and_answers(bits, num_problems):
problems = generate_problems(bits, num_problems)
answers = generate_answers(problems)
problems_string = "\n\n".join(problems)
answers_string = "\n\n".join(
[f"{problem} | {answer} " for problem, answer in zip(problems, answers)]
)
if pdf:
make_pdf(
problems=problems_string,
answers=answers_string,
output_path=output_filepath or "problems.pdf",
include_answers=include_answers,
)
if not silent:
if include_answers:
click.echo(BEGIN_ANSWERS_TOKEN.join((problems_string, answers_string)))
else:
click.echo(problems_string)
return answers_string, problems_string

View File

@@ -2,6 +2,8 @@ import os
import subprocess
import tempfile
POSTSCRIPT_FILEPATH = "tempfile.ps"
def make_pdf(
problems: str, answers: str, output_path: str, include_answers: bool
@@ -11,29 +13,32 @@ def make_pdf(
else:
pdf_jobs = ("problems", problems)
for job_name, job in pdf_jobs:
for job_name, text in pdf_jobs:
if job_name == "answers":
path, filename = os.path.split(output_path)
new_filename = f"{filename.split('.pdf')[0]}-answers.pdf"
output_path = os.path.join(path, new_filename)
output_path = make_answers_path(output_path)
with tempfile.NamedTemporaryFile(mode="w") as txt_file:
txt_file.write(job)
txt_file.flush()
write_to_txtfile(txt_file, text)
make_postscript_file(txt_file)
make_pdf_file(output_path)
os.unlink(POSTSCRIPT_FILEPATH)
command = (
f"enscript --columns=4 --no-header --output=tempfile.ps {txt_file.name}"
)
print(f"command: '{command}'")
output = subprocess.check_output(command, shell=True)
print(output)
def make_answers_path(path):
path, filename = os.path.split(path)
new_filename = f"{filename.split('.pdf')[0]}-answers.pdf"
return os.path.join(path, new_filename)
command = f"ps2pdf tempfile.ps {output_path}"
print(f"command: '{command}'")
output = subprocess.check_output(command, shell=True)
print(output)
print("made pdf", output_path)
os.unlink("tempfile.ps")
def write_to_txtfile(txt_file, text):
txt_file.write(text)
txt_file.flush()
def make_pdf_file(output_path):
command = f"ps2pdf {POSTSCRIPT_FILEPATH} {output_path}"
subprocess.call(command, shell=True)
def make_postscript_file(txt_file):
command = f"enscript --columns=4 --no-header --output={POSTSCRIPT_FILEPATH} {txt_file.name}"
subprocess.call(command, shell=True)

BIN
problems-answers.pdf Normal file

Binary file not shown.

BIN
problems.pdf Normal file

Binary file not shown.