Przeglądaj źródła

fix(pdf): fix the failure on PDF saving (#186)

tags/v0.10.1
Jérôme Deuchnord 2 lat temu
committed by GitHub
rodzic
commit
8d71b611e0
Nie znaleziono w bazie danych klucza dla tego podpisu ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 70 dodań i 54 usunięć
  1. +1
    -1
      _kosmorro/__version__.py
  2. +5
    -4
      _kosmorro/debug.py
  3. +44
    -29
      _kosmorro/dumper.py
  4. +20
    -20
      _kosmorro/locales/messages.pot

+ 1
- 1
_kosmorro/__version__.py Wyświetl plik

@@ -19,7 +19,7 @@
__title__ = "kosmorro" __title__ = "kosmorro"
__description__ = "A program that computes your ephemerides" __description__ = "A program that computes your ephemerides"
__url__ = "https://kosmorro.space" __url__ = "https://kosmorro.space"
__version__ = '0.10.0'
__version__ = "0.10.0"
__author__ = "Jérôme Deuchnord" __author__ = "Jérôme Deuchnord"
__author_email__ = "jerome@deuchnord.fr" __author_email__ = "jerome@deuchnord.fr"
__license__ = "AGPL" __license__ = "AGPL"


+ 5
- 4
_kosmorro/debug.py Wyświetl plik

@@ -1,15 +1,16 @@
#!/usr/bin/env python3 #!/usr/bin/env python3


from traceback import print_exc
from traceback import print_exception


show_debug_messages = False 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 return


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


+ 44
- 29
_kosmorro/dumper.py Wyświetl plik

@@ -35,7 +35,10 @@ from kosmorrolib.model import ASTERS, MoonPhase
from .i18n.utils import _, FULL_DATE_FORMAT, SHORT_DATETIME_FORMAT, TIME_FORMAT from .i18n.utils import _, FULL_DATE_FORMAT, SHORT_DATETIME_FORMAT, TIME_FORMAT
from .i18n import strings from .i18n import strings
from .__version__ import __version__ as version from .__version__ import __version__ as version
from .exceptions import CompileError
from .exceptions import (
CompileError,
UnavailableFeatureError as KosmorroUnavailableFeatureError,
)
from .debug import debug_print from .debug import debug_print




@@ -474,12 +477,13 @@ class PdfDumper(Dumper):
with_colors=self.with_colors, with_colors=self.with_colors,
show_graph=self.show_graph, show_graph=self.show_graph,
) )

return self._compile(latex_dumper.to_string()) return self._compile(latex_dumper.to_string())
except RuntimeError as error: except RuntimeError as error:
raise UnavailableFeatureError(
raise KosmorroUnavailableFeatureError(
_( _(
"Building PDF was not possible, because some dependencies are not" "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." "for more information."
) )
) from error ) from error
@@ -491,37 +495,48 @@ class PdfDumper(Dumper):
@staticmethod @staticmethod
def _compile(latex_input) -> bytes: def _compile(latex_input) -> bytes:
package = str(Path(__file__).parent.absolute()) + "/assets/pdf/kosmorro.sty" 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) 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()) 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

+ 20
- 20
_kosmorro/locales/messages.pot Wyświetl plik

@@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: kosmorro 0.10.0\n" "Project-Id-Version: kosmorro 0.10.0\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -27,84 +27,84 @@ msgid ""
"offset format." "offset format."
msgstr "" msgstr ""


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


#: _kosmorro/dumper.py:480
#: _kosmorro/dumper.py:484
msgid "" msgid ""
"Building PDF was not possible, because some dependencies are not " "Building PDF was not possible, because some dependencies are not "
"installed.\n" "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." "information."
msgstr "" msgstr ""


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


Ładowanie…
Anuluj
Zapisz