Skip to content

Commit

Permalink
Return full path 🚟
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimesque committed Jul 15, 2022
1 parent 09bc3b1 commit e555468
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
17 changes: 14 additions & 3 deletions paperglobe/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from paperglobe import get_stripes, write_pdf, STATUS_TYPES, format_output_filename


Expand Down Expand Up @@ -53,7 +55,7 @@ def update_status(self, status_type, message):
if callable(self.on_update):
self.on_update(status_type, message)

def generate_paperglobe(self, file, projection, print_size):
def generate_paperglobe(self, file, projection, print_size, out_path):
"""Runs the paper globe template generation and calls status updates
Parameters
Expand All @@ -69,24 +71,33 @@ def generate_paperglobe(self, file, projection, print_size):
printing size of the template. one of:
- "a4"
- "us-letter"
Returns
-------
tuple : (str, bool)
full path of the output file, boolean value of the file existence
"""

self.update_status(
STATUS_TYPES["INFO"],
f"{self.bold(file)} has been found, starting conversion. 🧑‍🚀🪄 🗺",
)

filename = format_output_filename(file, print_size)
out_path = os.path.join(os.path.dirname(out_path), filename)

try:
stripes = get_stripes(file, projection)
write_pdf(file, stripes, print_size)
write_pdf(stripes, print_size, out_path)
except Exception as ex:
self.update_status(
STATUS_TYPES["ERROR"],
f"The file couldn’t be written ({self.bold(type(ex).__name__)})",
)
else:
filename = format_output_filename(file, print_size)
self.update_status(
STATUS_TYPES["INFO"],
f"The file {self.bold(filename)} has been saved 🧑‍🚀 ✨🌏🌍🌎✨",
)

return (os.path.abspath(out_path), os.path.exists(out_path))
7 changes: 4 additions & 3 deletions paperglobe/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@


@click.command()
@click.argument("file", type=click.Path(exists=True), metavar="<file>")
@click.argument("file", type=click.Path(exists=True, file_okay=True), metavar="<file>")
@click.argument("out_path", type=click.Path(), default="", metavar="<output_path>")
@click.option(
"-p",
"--projection",
Expand All @@ -25,7 +26,7 @@
type=click.Choice([PRINT_SIZES[i] for i in PRINT_SIZES]),
)
@click.version_option()
def cli(file, projection, print_size):
def cli(file, projection, print_size, out_path):
"""Generate a Paper Globe template from a cylindrical projection map. 🗺
\b
Expand All @@ -44,4 +45,4 @@ def bold(text):
return click.style(text, bold=True)

pg = PaperGlobe(on_update=echo_status, bold=bold)
pg.generate_paperglobe(file, projection, print_size)
pg.generate_paperglobe(file, projection, print_size, out_path)
11 changes: 5 additions & 6 deletions paperglobe/write_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@

from fitz import fitz

from paperglobe import format_output_filename, PRINT_SIZES
from paperglobe import PRINT_SIZES


def write_pdf(file, stripes, print_size):
def write_pdf(stripes, print_size, out_path):
"""Place the stripe Image objects onto the PDF
If will take a list of stripes and inserts each one at its right position
on the right page of the right PDF template file
Parameters
----------
file : str
path of the image file to be used to generate the template
stripes : list
a list of Image objects
print_size : str
printing size of the template. one of:
- "a4"
- "us-letter"
out_path : str
path of the output file
"""

output_filename = format_output_filename(file, print_size)
pdf_path = os.path.join(
os.path.dirname(__file__), f"assets/template-{print_size}.pdf"
)
Expand Down Expand Up @@ -62,4 +61,4 @@ def write_pdf(file, stripes, print_size):

page.insert_image(rect, stream=stripe_blob)

pdf.save(output_filename)
pdf.save(out_path)

0 comments on commit e555468

Please sign in to comment.