Skip to content
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
14 changes: 6 additions & 8 deletions src/ymprint/cli/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from pathlib import Path
from typing import Optional

CONFIG_FILENAMES = ['doctemplate.yml', 'textstyles.yml', 'tablestyles.yml']

def locate_config_dir(cwd: Path) -> Optional[Path]:
config_dir = None
def locate_config_file(cwd: Path) -> Optional[Path]:
config_file = None
for parent in cwd.parents:
filenames = [path.name for path in parent.glob("*.yml")]
intersection = set(CONFIG_FILENAMES) & set(filenames)
if intersection!= set():
config_dir = parent
return config_dir
filenames = [path.name for path in parent.glob("*.ymprint.yml")]
if filenames:
config_file = filenames[0]
return config_file


10 changes: 5 additions & 5 deletions src/ymprint/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typer import Typer

from ..report_reader import load_report
from .config import locate_config_dir, CONFIG_FILENAMES
from .config import locate_config_file
from .okular import ensure_okular
from ..config.config_loaders import load_config_directory

Expand Down Expand Up @@ -63,7 +63,7 @@ def convert(
# Identify config files and content files to watch here
# ensure_demo_file()
if config_dir is None:
config_dir = locate_config_dir(Path.cwd())
config_dir = locate_config_file(Path.cwd())

load_report(source, destination, config_dir)
console = Console()
Expand All @@ -81,7 +81,7 @@ def convert(
def live(
src: Annotated[str, "YAML file path to render to PDF"],
dest: Annotated[Optional[str], "File path of output PDF file. If not provided file name and path of source file will be used (wtih .pdf extension)."] = None,
config_dir: Annotated[Optional[str], "Directory of optional config files (doctemplate.yml, textstyles.yml, tablestyles.yml)"] = None,
config_file: Annotated[Optional[str], "Location of optional document config *.ymprint.yml file"] = None,
):
source = Path(src)
if dest is None:
Expand All @@ -90,8 +90,8 @@ def live(
destination = Path(dest)
# Identify config files and content files to watch here
# ensure_demo_file()
if config_dir is None:
config_dir = locate_config_dir(Path.cwd())
if config_file is None:
config_dir = locate_config_file(Path.cwd())

file_watchers = [FileWatcher(Path(source))]
if config_dir is not None:
Expand Down
5 changes: 4 additions & 1 deletion src/ymprint/config/config_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ def build_current_config(default_config: dict, config_data: DeepChainMap):
for key in default_config:
value = config_data[key]
if isinstance(value, DeepChainMap):
value = build_current_config(default_config[key], value)
default_value = default_config[key]
# This condition only exists for the background key which is None in the defaults
if default_value is not None:
value = build_current_config(default_config[key], value)
style_map.update({key: value})
return style_map

Expand Down
11 changes: 10 additions & 1 deletion src/ymprint/config/doctemplate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import StrEnum
import pathlib
from typing import Optional
from pydantic import BaseModel, Field, ConfigDict
Expand All @@ -12,10 +13,18 @@ class Margins(BaseModel):
right: float
bottom: float

class RelativeTo(StrEnum):
CONFIG = 'config'
SOURCE = 'source'

class PDFBackground(BaseModel):
filepath: str
relative_to: Optional[RelativeTo] = Field(alias='relative-to', default=None)

class PageConfig(BaseModel):
# model_config = ConfigDict(populate_by_name=True)
margins: Margins
background: Optional[str] = None
background: Optional[PDFBackground] = None

class PageSizeMixin:
page_size: str = Field(alias='page-size')
Expand Down
38 changes: 31 additions & 7 deletions src/ymprint/config/pdf_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,30 @@ def fill_forms_and_bake(vars: dict, pdf_backgrounds: dict[str, io.BytesIO | None
def load_pdf_backgrounds(context: dict) -> dict[str, io.BytesIO | None]:
source_path = pathlib.Path(context['source_path'])
source_parent = source_path.parent
first_page = context['doctemplate']['yaml']['_doc'].get('first-page')
if isinstance(first_page, dict):
first_page_background = first_page.get("background")
if first_page_background is not None:
first_page_background = source_parent / first_page_background
print(f"{source_parent=}")

if context['config_path'] is not None:
config_path = pathlib.Path(context['config_path'])
config_parent = config_path.parent
else:
config_path = source_path
config_parent = source_parent

first_page_bg = context['doctemplate']['yaml']['_doc'].get('first-page', {}).get('background')
if isinstance(first_page_bg, dict):
print(f"{first_page.get('background', {})=}")
first_page_background_filepath = first_page_bg.get('filepath')
relative_to = first_page_bg.get('relative_to')
if relative_to == 'source':
first_page_background = source_parent / first_page_background_filepath
elif relative_to == 'config':
first_page_background = config_parent / first_page_background_filepath
else:
first_page_background = first_page_background_filepath
else:
first_page_background = None

remaining = context['doctemplate']['yaml']['_doc'].get('background')
remaining = context['doctemplate']['yaml']['_doc'].get('background', {})
first_page_pdf = remaining_pdf = None
first_page_data = remaining_page_data = None
if first_page_background is not None:
Expand All @@ -126,7 +141,16 @@ def load_pdf_backgrounds(context: dict) -> dict[str, io.BytesIO | None]:
first_page_pdf.save(first_page_data)
first_page_data.seek(0)
if remaining is not None:
remaining_page_background = source_parent / remaining
relative_to = remaining.get('relative-to')
print(f"{relative_to=}")
if relative_to == 'source':
remaining_page_background = source_parent / remaining.get('filepath')
elif relative_to == 'config':
remaining_page_background = config_parent / remaining.get('filepath')
else:
remaining_page_background = remaining.get('filepath')

print(f"{remaining_page_background=}")
remaining_pdf = mu.open(remaining_page_background)
remaining_page_data = io.BytesIO()
remaining_pdf.save(remaining_page_data)
Expand Down
4 changes: 3 additions & 1 deletion src/ymprint/context_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def build_context(
tablestyles_yaml: dict,
document_vars: dict,
source_path: str | pathlib.Path,
destination_path: str | pathlib.Path
destination_path: str | pathlib.Path,
config_path: str | pathlib.Path
) -> dict:
# inline_styles = {} if "_style" not in content_yaml else content_yaml.pop("_style")
report_styles = ReportStyles.model_validate(text_styles_yaml['_style'])
Expand Down Expand Up @@ -65,6 +66,7 @@ def build_context(
}
},
"source_path": source_path,
"config_path": config_path,
"destination_path": destination_path,
}
return context
10 changes: 3 additions & 7 deletions src/ymprint/report_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ def load_report(source_yaml: str | pathlib.Path, destination_pdf: str | pathlib.
textstyles, tablestyles, doc_data = load_report_config(source_data, report_config_path)
doctemplate = DocConfig.model_validate(doc_data['_doc'])
document_vars = extract_vars(source_data)


context = build_context(
source_data,
textstyles,
doc_data,
tablestyles,
document_vars,
source_yaml,
destination_pdf
destination_pdf,
report_config_path,
)
print(f"{doc_data=}")
story = build_story(source_data, context)
if context['doctemplate']['yaml']['_doc'].get('first-page') is not None:
story = [NextPageTemplate(1)] + story
Expand All @@ -74,10 +74,6 @@ def load_report(source_yaml: str | pathlib.Path, destination_pdf: str | pathlib.
)






def extract_vars(source_data: dict) -> dict:
if "_vars" in source_data:
return source_data.pop("_vars")
Expand Down
Binary file added test.pdf
Binary file not shown.
19 changes: 19 additions & 0 deletions test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
_doc:
margins:
top: 1.0
left: 1.0
right: 1.0
bottom: 1.0
first-page:
margins:
top: 1.0
left: 1.0
right: 1.0
bottom: 1.0
yep: >
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
_doc:
page-size: a4
landscape: false
margins:
top: 84
left: 84
right: 84
bottom: 84

_style:
headings:
font: Helvetica
Expand Down
8 changes: 0 additions & 8 deletions tests/test-data/example_1_config/doctemplate.yml

This file was deleted.

69 changes: 69 additions & 0 deletions tests/test-data/example_2_config/config.ymprint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
_doc:
page-size: letter
landscape: true
margins:
top: 84
left: 84
right: 84
bottom: 84
background:
filepath: background_other_pages.pdf
relative-to: source
first-page:
margins:
top: 240
left: 84
right: 84
bottom: 84
background:
filepath: background_first_page.pdf
relative-to: source

_tablestyle:
cell-padding:
top: 5
left: 5
right: 5
bottom: 5

headers:
text:
font: Helvetica
size: 11
color: "#000000"
row:
color: "#ffffff"
lines:
- above
- below
- between
body:
text:
font: "Helvetica"
size: 11
color: "black"
rows:
color:
even: "#ffffff"
odd: "#eeeeee"
lines:
- above
- below
- between


_style:
headings:
font: Helvetica
color: "#111111"
ratio: minor second
body:
# font: Times
color: black
size: 10 # pts
bullets:
font: Helvetica
size: 12
color: black
symbol: "-"
spacing: 1.1
16 changes: 0 additions & 16 deletions tests/test-data/example_2_config/doctemplate.yml

This file was deleted.

34 changes: 0 additions & 34 deletions tests/test-data/example_2_config/tablestyles.yml

This file was deleted.

15 changes: 0 additions & 15 deletions tests/test-data/example_2_config/textstyles.yml

This file was deleted.

Binary file modified tests/test-data/example_output1.pdf
Binary file not shown.
Binary file modified tests/test-data/example_output2.pdf
Binary file not shown.
Binary file modified tests/test-data/example_output3.pdf
Binary file not shown.
Binary file modified tests/test-data/filled_forms.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion tests/test-data/report_example_2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _vars:
time: 13:00
region: Level 1 columns
_doc: # TODO: this background is not loading
background: background_other_pages.pdf
background:
filepath: background_other_pages.pdf
relative-to: source
title:
first topic:
- >
Expand Down
Loading
Loading