Skip to content

Commit 8d71b61

Browse files
authored
fix(pdf): fix the failure on PDF saving (#186)
1 parent 8a415b0 commit 8d71b61

File tree

4 files changed

+70
-54
lines changed

4 files changed

+70
-54
lines changed

_kosmorro/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
__title__ = "kosmorro"
2020
__description__ = "A program that computes your ephemerides"
2121
__url__ = "https://kosmorro.space"
22-
__version__ = '0.10.0'
22+
__version__ = "0.10.0"
2323
__author__ = "Jérôme Deuchnord"
2424
__author_email__ = "jerome@deuchnord.fr"
2525
__license__ = "AGPL"

_kosmorro/debug.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/usr/bin/env python3
22

3-
from traceback import print_exc
3+
from traceback import print_exception
44

55
show_debug_messages = False
66

77

8-
def debug_print(what):
9-
if not show_debug_messages:
8+
def debug_print(what, force: bool = False):
9+
if not force and not show_debug_messages:
1010
return
1111

1212
if isinstance(what, Exception):
13-
print_exc(what)
13+
print_exception(type(what), value=what, tb=None)
1414
else:
1515
print("[DEBUG] %s" % what)
16+

_kosmorro/dumper.py

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
from .i18n.utils import _, FULL_DATE_FORMAT, SHORT_DATETIME_FORMAT, TIME_FORMAT
3636
from .i18n import strings
3737
from .__version__ import __version__ as version
38-
from .exceptions import CompileError
38+
from .exceptions import (
39+
CompileError,
40+
UnavailableFeatureError as KosmorroUnavailableFeatureError,
41+
)
3942
from .debug import debug_print
4043

4144

@@ -474,12 +477,13 @@ def to_string(self):
474477
with_colors=self.with_colors,
475478
show_graph=self.show_graph,
476479
)
480+
477481
return self._compile(latex_dumper.to_string())
478482
except RuntimeError as error:
479-
raise UnavailableFeatureError(
483+
raise KosmorroUnavailableFeatureError(
480484
_(
481485
"Building PDF was not possible, because some dependencies are not"
482-
" installed.\nPlease look at the documentation at http://kosmorro.space "
486+
" installed.\nPlease look at the documentation at https://kosmorro.space "
483487
"for more information."
484488
)
485489
) from error
@@ -491,37 +495,48 @@ def is_file_output_needed() -> bool:
491495
@staticmethod
492496
def _compile(latex_input) -> bytes:
493497
package = str(Path(__file__).parent.absolute()) + "/assets/pdf/kosmorro.sty"
498+
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
499+
current_dir = (
500+
os.getcwd()
501+
) # Keep the current directory to return to it after the PDFLaTeX execution
502+
503+
try:
504+
temp_dir = tempfile.mkdtemp()
505+
506+
shutil.copy(package, temp_dir)
494507

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

499-
with open("%s/%s.tex" % (tempdir, timestamp), "w") as texfile:
500-
texfile.write(latex_input)
510+
with open(temp_tex, "w") as tex_file:
511+
tex_file.write(latex_input)
501512

502-
os.chdir(tempdir)
513+
os.chdir(temp_dir)
503514
debug_print("LaTeX content:\n%s" % latex_input)
504515

505-
try:
506-
subprocess.run(
507-
["pdflatex", "-interaction", "nonstopmode", "%s.tex" % timestamp],
508-
capture_output=True,
509-
check=True,
510-
)
511-
except FileNotFoundError as error:
512-
raise RuntimeError("pdflatex is not installed.") from error
513-
except subprocess.CalledProcessError as error:
514-
with open("/tmp/kosmorro-%s.log" % timestamp, "wb") as file:
515-
file.write(error.stdout)
516+
subprocess.run(
517+
["pdflatex", "-interaction", "nonstopmode", "%s.tex" % timestamp],
518+
capture_output=True,
519+
check=True,
520+
)
516521

517-
raise CompileError(
518-
_(
519-
"An error occurred during the compilation of the PDF.\n"
520-
"Please open an issue at https://github.com/Kosmorro/kosmorro/issues and share "
521-
"the content of the log file at /tmp/kosmorro-%s.log"
522-
% timestamp
523-
)
524-
) from error
522+
os.chdir(current_dir)
525523

526-
with open("%s.pdf" % timestamp, "rb") as pdffile:
524+
with open("%s/%s.pdf" % (temp_dir, timestamp), "rb") as pdffile:
527525
return bytes(pdffile.read())
526+
527+
except FileNotFoundError as error:
528+
raise KosmorroUnavailableFeatureError(
529+
"TeXLive is not installed."
530+
) from error
531+
532+
except subprocess.CalledProcessError as error:
533+
with open("/tmp/kosmorro-%s.log" % timestamp, "wb") as file:
534+
file.write(error.stdout)
535+
536+
raise CompileError(
537+
_(
538+
"An error occurred during the compilation of the PDF.\n"
539+
"Please open an issue at https://github.com/Kosmorro/kosmorro/issues and share "
540+
"the content of the log file at /tmp/kosmorro-%s.log" % timestamp
541+
)
542+
) from error

_kosmorro/locales/messages.pot

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: kosmorro 0.10.0\n"
1010
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
11-
"POT-Creation-Date: 2021-05-30 17:22+0200\n"
11+
"POT-Creation-Date: 2021-06-05 17:59+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -27,84 +27,84 @@ msgid ""
2727
"offset format."
2828
msgstr ""
2929

30-
#: _kosmorro/dumper.py:108
30+
#: _kosmorro/dumper.py:111
3131
msgid "Expected events:"
3232
msgstr ""
3333

34-
#: _kosmorro/dumper.py:115
34+
#: _kosmorro/dumper.py:118
3535
msgid "Note: All the hours are given in UTC."
3636
msgstr ""
3737

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

42-
#: _kosmorro/dumper.py:186 _kosmorro/dumper.py:322
42+
#: _kosmorro/dumper.py:189 _kosmorro/dumper.py:325
4343
msgid "Object"
4444
msgstr ""
4545

46-
#: _kosmorro/dumper.py:187 _kosmorro/dumper.py:323
46+
#: _kosmorro/dumper.py:190 _kosmorro/dumper.py:326
4747
msgid "Rise time"
4848
msgstr ""
4949

50-
#: _kosmorro/dumper.py:188 _kosmorro/dumper.py:325
50+
#: _kosmorro/dumper.py:191 _kosmorro/dumper.py:328
5151
msgid "Culmination time"
5252
msgstr ""
5353

54-
#: _kosmorro/dumper.py:189 _kosmorro/dumper.py:327
54+
#: _kosmorro/dumper.py:192 _kosmorro/dumper.py:330
5555
msgid "Set time"
5656
msgstr ""
5757

58-
#: _kosmorro/dumper.py:223
58+
#: _kosmorro/dumper.py:226
5959
msgid "Moon phase is unavailable for this date."
6060
msgstr ""
6161

62-
#: _kosmorro/dumper.py:227 _kosmorro/dumper.py:331
62+
#: _kosmorro/dumper.py:230 _kosmorro/dumper.py:334
6363
msgid "Moon phase:"
6464
msgstr ""
6565

66-
#: _kosmorro/dumper.py:231
66+
#: _kosmorro/dumper.py:234
6767
msgid "{next_moon_phase} on {next_moon_phase_date} at {next_moon_phase_time}"
6868
msgstr ""
6969

70-
#: _kosmorro/dumper.py:295
70+
#: _kosmorro/dumper.py:298
7171
msgid "Overview of your sky"
7272
msgstr ""
7373

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

81-
#: _kosmorro/dumper.py:313
81+
#: _kosmorro/dumper.py:316
8282
msgid ""
8383
"Don't forget to check the weather forecast before you go out with your "
8484
"equipment."
8585
msgstr ""
8686

87-
#: _kosmorro/dumper.py:320
87+
#: _kosmorro/dumper.py:323
8888
msgid "Ephemerides of the day"
8989
msgstr ""
9090

91-
#: _kosmorro/dumper.py:329
91+
#: _kosmorro/dumper.py:332
9292
msgid "hours"
9393
msgstr ""
9494

95-
#: _kosmorro/dumper.py:336
95+
#: _kosmorro/dumper.py:339
9696
msgid "Expected events"
9797
msgstr ""
9898

99-
#: _kosmorro/dumper.py:480
99+
#: _kosmorro/dumper.py:484
100100
msgid ""
101101
"Building PDF was not possible, because some dependencies are not "
102102
"installed.\n"
103-
"Please look at the documentation at http://kosmorro.space for more "
103+
"Please look at the documentation at https://kosmorro.space for more "
104104
"information."
105105
msgstr ""
106106

107-
#: _kosmorro/dumper.py:518
107+
#: _kosmorro/dumper.py:537
108108
#, python-format
109109
msgid ""
110110
"An error occurred during the compilation of the PDF.\n"

0 commit comments

Comments
 (0)