Skip to content

Commit

Permalink
fix(pdf): fix the failure on PDF saving (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deuchnord committed Jun 5, 2021
1 parent 8a415b0 commit 8d71b61
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 54 deletions.
2 changes: 1 addition & 1 deletion _kosmorro/__version__.py
Expand Up @@ -19,7 +19,7 @@
__title__ = "kosmorro"
__description__ = "A program that computes your ephemerides"
__url__ = "https://kosmorro.space"
__version__ = '0.10.0'
__version__ = "0.10.0"
__author__ = "Jérôme Deuchnord"
__author_email__ = "jerome@deuchnord.fr"
__license__ = "AGPL"
Expand Down
9 changes: 5 additions & 4 deletions _kosmorro/debug.py
@@ -1,15 +1,16 @@
#!/usr/bin/env python3

from traceback import print_exc
from traceback import print_exception

show_debug_messages = False


def debug_print(what):
if not show_debug_messages:
def debug_print(what, force: bool = False):
if not force and not show_debug_messages:
return

if isinstance(what, Exception):
print_exc(what)
print_exception(type(what), value=what, tb=None)
else:
print("[DEBUG] %s" % what)

73 changes: 44 additions & 29 deletions _kosmorro/dumper.py
Expand Up @@ -35,7 +35,10 @@
from .i18n.utils import _, FULL_DATE_FORMAT, SHORT_DATETIME_FORMAT, TIME_FORMAT
from .i18n import strings
from .__version__ import __version__ as version
from .exceptions import CompileError
from .exceptions import (
CompileError,
UnavailableFeatureError as KosmorroUnavailableFeatureError,
)
from .debug import debug_print


Expand Down Expand Up @@ -474,12 +477,13 @@ def to_string(self):
with_colors=self.with_colors,
show_graph=self.show_graph,
)

return self._compile(latex_dumper.to_string())
except RuntimeError as error:
raise UnavailableFeatureError(
raise KosmorroUnavailableFeatureError(
_(
"Building PDF was not possible, because some dependencies are not"
" installed.\nPlease look at the documentation at http://kosmorro.space "
" installed.\nPlease look at the documentation at https://kosmorro.space "
"for more information."
)
) from error
Expand All @@ -491,37 +495,48 @@ def is_file_output_needed() -> bool:
@staticmethod
def _compile(latex_input) -> bytes:
package = str(Path(__file__).parent.absolute()) + "/assets/pdf/kosmorro.sty"
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()

shutil.copy(package, temp_dir)

with tempfile.TemporaryDirectory() as tempdir:
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
shutil.copy(package, tempdir)
temp_tex = "%s/%s.tex" % (temp_dir, timestamp)

with open("%s/%s.tex" % (tempdir, timestamp), "w") as texfile:
texfile.write(latex_input)
with open(temp_tex, "w") as tex_file:
tex_file.write(latex_input)

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

try:
subprocess.run(
["pdflatex", "-interaction", "nonstopmode", "%s.tex" % timestamp],
capture_output=True,
check=True,
)
except FileNotFoundError as error:
raise RuntimeError("pdflatex is not installed.") from error
except subprocess.CalledProcessError as error:
with open("/tmp/kosmorro-%s.log" % timestamp, "wb") as file:
file.write(error.stdout)
subprocess.run(
["pdflatex", "-interaction", "nonstopmode", "%s.tex" % timestamp],
capture_output=True,
check=True,
)

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
os.chdir(current_dir)

with open("%s.pdf" % timestamp, "rb") as pdffile:
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
40 changes: 20 additions & 20 deletions _kosmorro/locales/messages.pot
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: kosmorro 0.10.0\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2021-05-30 17:22+0200\n"
"POT-Creation-Date: 2021-06-05 17:59+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand All @@ -27,84 +27,84 @@ msgid ""
"offset format."
msgstr ""

#: _kosmorro/dumper.py:108
#: _kosmorro/dumper.py:111
msgid "Expected events:"
msgstr ""

#: _kosmorro/dumper.py:115
#: _kosmorro/dumper.py:118
msgid "Note: All the hours are given in UTC."
msgstr ""

#: _kosmorro/dumper.py:122
#: _kosmorro/dumper.py:125
msgid "Note: All the hours are given in the UTC{offset} timezone."
msgstr ""

#: _kosmorro/dumper.py:186 _kosmorro/dumper.py:322
#: _kosmorro/dumper.py:189 _kosmorro/dumper.py:325
msgid "Object"
msgstr ""

#: _kosmorro/dumper.py:187 _kosmorro/dumper.py:323
#: _kosmorro/dumper.py:190 _kosmorro/dumper.py:326
msgid "Rise time"
msgstr ""

#: _kosmorro/dumper.py:188 _kosmorro/dumper.py:325
#: _kosmorro/dumper.py:191 _kosmorro/dumper.py:328
msgid "Culmination time"
msgstr ""

#: _kosmorro/dumper.py:189 _kosmorro/dumper.py:327
#: _kosmorro/dumper.py:192 _kosmorro/dumper.py:330
msgid "Set time"
msgstr ""

#: _kosmorro/dumper.py:223
#: _kosmorro/dumper.py:226
msgid "Moon phase is unavailable for this date."
msgstr ""

#: _kosmorro/dumper.py:227 _kosmorro/dumper.py:331
#: _kosmorro/dumper.py:230 _kosmorro/dumper.py:334
msgid "Moon phase:"
msgstr ""

#: _kosmorro/dumper.py:231
#: _kosmorro/dumper.py:234
msgid "{next_moon_phase} on {next_moon_phase_date} at {next_moon_phase_time}"
msgstr ""

#: _kosmorro/dumper.py:295
#: _kosmorro/dumper.py:298
msgid "Overview of your sky"
msgstr ""

#: _kosmorro/dumper.py:303
#: _kosmorro/dumper.py:306
msgid ""
"This document summarizes the ephemerides and the events of {date}. It "
"aims to help you to prepare your observation session. All the hours are "
"given in {timezone}."
msgstr ""

#: _kosmorro/dumper.py:313
#: _kosmorro/dumper.py:316
msgid ""
"Don't forget to check the weather forecast before you go out with your "
"equipment."
msgstr ""

#: _kosmorro/dumper.py:320
#: _kosmorro/dumper.py:323
msgid "Ephemerides of the day"
msgstr ""

#: _kosmorro/dumper.py:329
#: _kosmorro/dumper.py:332
msgid "hours"
msgstr ""

#: _kosmorro/dumper.py:336
#: _kosmorro/dumper.py:339
msgid "Expected events"
msgstr ""

#: _kosmorro/dumper.py:480
#: _kosmorro/dumper.py:484
msgid ""
"Building PDF was not possible, because some dependencies are not "
"installed.\n"
"Please look at the documentation at http://kosmorro.space for more "
"Please look at the documentation at https://kosmorro.space for more "
"information."
msgstr ""

#: _kosmorro/dumper.py:518
#: _kosmorro/dumper.py:537
#, python-format
msgid ""
"An error occurred during the compilation of the PDF.\n"
Expand Down

0 comments on commit 8d71b61

Please sign in to comment.