Skip to content

Commit

Permalink
feat: remove PDF output
Browse files Browse the repository at this point in the history
BREAKING CHANGE: PDF output has been removed. If you want to create a PDF file, use the LaTeX format instead.
  • Loading branch information
Deuchnord committed Nov 12, 2023
1 parent ee8adab commit 0ca503e
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 189 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ kosmorro.egg-info
.coverage
coverage.xml

/kosmorrolib/assets/pdf/*
!/kosmorrolib/assets/pdf/*.tex
!/kosmorrolib/assets/pdf/*.sty

/manpage/*
!/manpage/*.md

Expand Down
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ Kosmorro has a lot of available options. To get a list of them, run `kosmorro --

Note: the first time it runs, Kosmorro will download some important files needed to make the computations. They are stored in a cache folder named `.kosmorro-cache` located in your home directory (`/home/<username>` on Linux, `/Users/<username>` on macOS).

### Exporting to PDF

Kosmorro can export the computation results to PDF files, but this feature requires first that you install some additional dependencies.
You can find documentation about this on [Kosmorro's website](https://kosmorro.space/cli/generate-pdf/).

## Help translating Kosmorro!

Kosmorro is translated on [Weblate](https://hosted.weblate.org/engage/kosmorro/), a popular free platform for crowd-sourced internationalization.
Expand Down
32 changes: 10 additions & 22 deletions kosmorro/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,16 @@ def run():
if output_format is None:
output_format = "txt"

if output_format == "pdf":
print(
_(
"Save the planet and paper!\n"
"Consider printing your PDF document only if really necessary, and use the other side of the sheet."
if output_format == "tex" and position is None:
print_stderr(
colored(
_(
"Output file will not contain the ephemerides, because you didn't provide the observation "
"coordinates."
),
"yellow",
)
)
if position is None:
print_stderr(
colored(
_(
"PDF output will not contain the ephemerides, because you didn't provide the observation "
"coordinates."
),
"yellow",
)
)

timezone = args.timezone

Expand Down Expand Up @@ -215,15 +208,11 @@ def get_dumpers() -> {str: dumper.Dumper}:
return {
"txt": dumper.TextDumper,
"json": dumper.JsonDumper,
"pdf": dumper.PdfDumper,
"tex": dumper.LatexDumper,
}


def get_opening_mode(format: str) -> str:
if format == "pdf":
return "wb"

return "w"


Expand Down Expand Up @@ -320,16 +309,15 @@ def get_args(output_formats: [str]):
type=str,
default=None,
help=_(
"A file to export the output to. If not given, the standard output is used. "
"This argument is needed for PDF format."
"A file to export the output to. If not given, the standard output is used."
),
)
parser.add_argument(
"--no-graph",
dest="show_graph",
action="store_false",
help=_(
"Do not generate a graph to represent the rise and set times in the PDF format."
"Do not generate a graph to represent the rise and set times in the LaTeX file."
),
)
parser.add_argument(
Expand Down
8 changes: 0 additions & 8 deletions kosmorro/assets/latex/template.tex
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
\documentclass[a4paper,12pt]{article}

% This file has been generated with Kosmorro version +++KOSMORRO-VERSION+++ (https://kosmorro.space) on +++CURRENT-DATE+++.
% Feel free to modify it at your needs.
%
% To compile this file, you will need to install LaTeX distribution like:
%
% - TeXLive (https://tug.org/texlive/) on Windows and Linux
% - MacTeX (https://www.tug.org/mactex/) on macOS

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=25mm]{geometry}
Expand Down
Empty file removed kosmorro/assets/pdf/kosmorro.sty
Empty file.
Empty file removed kosmorro/assets/pdf/template.tex
Empty file.
76 changes: 0 additions & 76 deletions kosmorro/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,6 @@ def _make_document(self, template: str) -> str:
def add_strings(
self, document: str, kosmorro_logo_path: str, moon_phase_graphics: str
) -> str:
document = document.replace(
"+++CURRENT-DATE+++", datetime.datetime.now().isoformat()
)
document = document.replace("+++KOSMORRO-VERSION+++", KOSMORRO_VERSION)
document = document.replace("+++KOSMORRO-LOGO+++", kosmorro_logo_path)
document = document.replace("+++DOCUMENT-TITLE+++", _("Overview of your sky"))
Expand Down Expand Up @@ -472,76 +469,3 @@ def _remove_section(document: str, section: str):
new_document.append(line)

return "\n".join(new_document)


class PdfDumper(Dumper):
def to_string(self):
try:
latex_dumper = LatexDumper(
self.ephemerides,
self.moon_phase,
self.events,
date=self.date,
timezone=self.timezone,
with_colors=self.with_colors,
show_graph=self.show_graph,
)

return self._compile(latex_dumper.to_string())
except RuntimeError as error:
raise KosmorroUnavailableFeatureError(
_(
"Building PDF was not possible, because some dependencies are not"
" installed.\nPlease look at the documentation at https://kosmorro.space/cli/generate-pdf/ "
"for more information."
)
) from error

@staticmethod
def is_file_output_needed() -> bool:
return True

@staticmethod
def _compile(latex_input) -> bytes:
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
current_dir = (
os.getcwd()
) # Keep the current directory to return to it after the PDFLaTeX execution

try:
temp_dir = tempfile.mkdtemp()
temp_tex = "%s/%s.tex" % (temp_dir, timestamp)

with open(temp_tex, "w") as tex_file:
tex_file.write(latex_input)

os.chdir(temp_dir)
debug_print("LaTeX content:\n%s" % latex_input)

subprocess.run(
["pdflatex", "-interaction", "nonstopmode", "%s.tex" % timestamp],
capture_output=True,
check=True,
)

os.chdir(current_dir)

with open("%s/%s.pdf" % (temp_dir, timestamp), "rb") as pdffile:
return bytes(pdffile.read())

except FileNotFoundError as error:
raise KosmorroUnavailableFeatureError(
"TeXLive is not installed."
) from error

except subprocess.CalledProcessError as error:
with open("/tmp/kosmorro-%s.log" % timestamp, "wb") as file:
file.write(error.stdout)

raise CompileError(
_(
"An error occurred during the compilation of the PDF.\n"
"Please open an issue at https://github.com/Kosmorro/kosmorro/issues and share "
"the content of the log file at /tmp/kosmorro-%s.log" % timestamp
)
) from error
Loading

0 comments on commit 0ca503e

Please sign in to comment.