Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keiko output context data #671

Merged
merged 3 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions keiko/keiko/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def health() -> ServiceHealth:
return get_health()

# mount reports as static files
if not Path(settings.reports_folder).exists():
Path(settings.reports_folder).mkdir(parents=True)
Path(settings.reports_folder).mkdir(parents=True, exist_ok=True)
app.mount("/reports", StaticFiles(directory=settings.reports_folder), name="reports")

return app
46 changes: 25 additions & 21 deletions keiko/keiko/keiko.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,42 +87,47 @@ def generate_report(
logger.info("Template rendered. [report_id=%s] [template=%s]", report_id, template_name)

# create temp folder
with tempfile.TemporaryDirectory() as tmp_dirname:
with tempfile.TemporaryDirectory() as directory:
logger.info(
"Temporary folder created. [report_id=%s] [template=%s] [tmp_dirname=%s]",
"Temporary folder created. [report_id=%s] [template=%s] [directory=%s]",
report_id,
template_name,
tmp_dirname,
directory,
)

# copy assets
shutil.copytree(settings.assets_folder, tmp_dirname, dirs_exist_ok=True)
shutil.copytree(settings.assets_folder, directory, dirs_exist_ok=True)
logger.info("Assets copied. [report_id=%s] [template=%s]", report_id, template_name)

output_file = settings.reports_folder / report_id

# tex file
tex_output_file_name = report_id + ".keiko.tex"
pdf_output_file_name = report_id + ".keiko.pdf"
tex_output_file_path = output_file.with_suffix(".keiko.tex")
pdf_output_file_path = output_file.with_suffix(".keiko.pdf")

preprocessed_tex = Path(tmp_dirname) / tex_output_file_name
preprocessed_tex.write_text(out_document)
preprocessed_tex_path = Path(directory).joinpath(f"{report_id}.keiko.tex")
preprocessed_tex_path.write_text(out_document)

# copy preprocessed tex file if debug is enabled
# if debug is enabled copy preprocessed tex file and input data
if debug or settings.debug:
shutil.copyfile(
Path(tmp_dirname) / tex_output_file_name,
Path(settings.reports_folder) / tex_output_file_name,
preprocessed_tex_path,
tex_output_file_path,
)

json_output_file_path = output_file.with_suffix(".keiko.json")
json_output_file_path.write_text(report_data.json(indent=4))

# run pdflatex
cmd = [
"pdflatex",
"-synctex=1",
"-interaction=nonstopmode",
tex_output_file_name,
preprocessed_tex_path.as_posix(),
]
env = {**os.environ, "TEXMFVAR": tmp_dirname}
env = {**os.environ, "TEXMFVAR": directory}
for i in (1, 2):
output = subprocess.run(cmd, cwd=tmp_dirname, env=env, capture_output=True, check=False)
output = subprocess.run(cmd, cwd=directory, env=env, capture_output=True, check=False)
logger.info(
"pdflatex [run=%d] [report_id=%s] [template=%s] [command=%s]",
i,
Expand All @@ -139,16 +144,15 @@ def generate_report(
logger.debug(output.stderr.decode("utf-8"))

# copy result back to output folder
Path(settings.reports_folder).mkdir(parents=True, exist_ok=True)
shutil.copyfile(
Path(tmp_dirname) / pdf_output_file_name,
Path(settings.reports_folder) / pdf_output_file_name,
preprocessed_tex_path.with_suffix(".pdf"),
pdf_output_file_path,
)
logger.info(
"Report copied to reports folder. [report_id=%s] [template=%s] [output_file=%s]",
report_id,
template_name,
Path(settings.reports_folder) / pdf_output_file_name,
pdf_output_file_path,
)

# ...tempfiles are deleted automatically when leaving the context
Expand All @@ -157,14 +161,14 @@ def generate_report(
def read_glossary(glossary: str, settings: Settings) -> Dict[str, Tuple[str, str]]:
"""Read a glossary CSV file and return a dictionary of entries."""
glossary_entries = {}
glossary_file_path = Path(settings.glossaries_folder) / glossary
with open(glossary_file_path, encoding="utf-8") as glossary_file:
glossary_file_path = settings.glossaries_folder / glossary
with glossary_file_path.open(encoding="utf-8") as glossary_file:
csvreader = csv.reader(glossary_file)
# skip header
_ = next(csvreader)
for row in csvreader:
# only allow words with baretext representation
bare_word = baretext(row[0])
if bare_word != "":
glossary_entries[baretext(row[0])] = (row[0], row[1])
glossary_entries[baretext(row[0])] = row[0], row[1]
return glossary_entries
20 changes: 10 additions & 10 deletions keiko/keiko/settings.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"""Keiko settings module."""
from pathlib import Path

from pydantic import BaseSettings, Field
from pydantic import BaseSettings, Field, DirectoryPath, FilePath


class Settings(BaseSettings):
"""Application settings loaded from environment variables."""

debug: bool = Field(False, env="KEIKO_DEBUG")
log_cfg: str = Field(
"logging.json",
env="KEIKO_LOG_CFG",
)
debug: bool = Field(False, description="Enable debug mode")
log_cfg: FilePath = Field("logging.json", description="Path to the logging configuration file")
templates_folder: DirectoryPath = Field("templates", description="Folder containing the templates")
glossaries_folder: DirectoryPath = Field("glossaries", description="Folder containing the glossaries")
assets_folder: DirectoryPath = Field("assets", description="Folder containing the assets")
reports_folder: Path = Field("/reports", description="Output folder containing the reports")

templates_folder: str = Field("templates", env="KEIKO_TEMPLATES_FOLDER")
glossaries_folder: str = Field("glossaries", env="KEIKO_GLOSSARIES_FOLDER")
reports_folder: str = Field("/reports", env="KEIKO_REPORTS_FOLDER")
assets_folder: str = Field("assets", env="KEIKO_ASSETS_FOLDER")
class Config:
env_prefix = "KEIKO_"