Browse Source

feat: remove PDF output

BREAKING CHANGE: PDF output has been removed. If you want to create a PDF file, use the LaTeX format instead.
features
Jérôme Deuchnord 1 year ago
committed by Deuchnord
parent
commit
0ca503e939
12 changed files with 249 additions and 189 deletions
  1. +0
    -4
      .gitignore
  2. +0
    -5
      README.md
  3. +10
    -22
      kosmorro/__main__.py
  4. +0
    -8
      kosmorro/assets/latex/template.tex
  5. +0
    -0
      kosmorro/assets/pdf/kosmorro.sty
  6. +0
    -0
      kosmorro/assets/pdf/template.tex
  7. +0
    -76
      kosmorro/dumper.py
  8. +33
    -58
      kosmorro/locales/messages.pot
  9. +2
    -8
      manpage/kosmorro.1.md
  10. +4
    -5
      tests/general.py
  11. +19
    -3
      tests/output.py
  12. +181
    -0
      tests/outputs/2020-01-27-with-position.tex

+ 0
- 4
.gitignore View File

@@ -5,10 +5,6 @@ kosmorro.egg-info
.coverage
coverage.xml

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

/manpage/*
!/manpage/*.md



+ 0
- 5
README.md View File

@@ -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.


+ 10
- 22
kosmorro/__main__.py View File

@@ -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

@@ -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"


@@ -320,8 +309,7 @@ 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(
@@ -329,7 +317,7 @@ def get_args(output_formats: [str]):
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(


+ 0
- 8
kosmorro/assets/latex/template.tex View File

@@ -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}


+ 0
- 0
kosmorro/assets/pdf/kosmorro.sty View File


+ 0
- 0
kosmorro/assets/pdf/template.tex View File


+ 0
- 76
kosmorro/dumper.py View File

@@ -304,9 +304,6 @@ class LatexDumper(Dumper):
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"))
@@ -472,76 +469,3 @@ class LatexDumper(Dumper):
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

+ 33
- 58
kosmorro/locales/messages.pot View File

@@ -8,103 +8,94 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2023-05-24 13:41+0200\n"
"POT-Creation-Date: 2023-11-12 11:33+0100\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"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.12.1\n"
"Generated-By: Babel 2.13.1\n"

#: kosmorro/__main__.py:82
#: kosmorro/__main__.py:83
msgid ""
"Save the planet and paper!\n"
"Consider printing your PDF document only if really necessary, and use the"
" other side of the sheet."
msgstr ""

#: kosmorro/__main__.py:90
msgid ""
"PDF output will not contain the ephemerides, because you didn't provide "
"Output file will not contain the ephemerides, because you didn't provide "
"the observation coordinates."
msgstr ""

#: kosmorro/__main__.py:142
#: kosmorro/__main__.py:135
msgid "The file could not be saved in \"{path}\": {error}"
msgstr ""

#: kosmorro/__main__.py:156
#: kosmorro/__main__.py:149
msgid "Please provide a file path to export in this format (--output)."
msgstr ""

#: kosmorro/__main__.py:187
#: kosmorro/__main__.py:180
msgid "Moon phase can only be computed between {min_date} and {max_date}"
msgstr ""

#: kosmorro/__main__.py:238
#: kosmorro/__main__.py:227
msgid "Running on Python {python_version} with Kosmorrolib v{kosmorrolib_version}"
msgstr ""

#: kosmorro/__main__.py:251
#: kosmorro/__main__.py:240
msgid ""
"Compute the ephemerides and the events for a given date and a given "
"position on Earth."
msgstr ""

#: kosmorro/__main__.py:254
#: kosmorro/__main__.py:243
msgid ""
"By default, only the events will be computed for today.\n"
"To compute also the ephemerides, latitude and longitude arguments are "
"needed."
msgstr ""

#: kosmorro/__main__.py:267
#: kosmorro/__main__.py:256
msgid "Show the program version"
msgstr ""

#: kosmorro/__main__.py:275
#: kosmorro/__main__.py:264
msgid ""
"The format to output the information to. If not provided, the output "
"format will be inferred from the file extension of the output file."
msgstr ""

#: kosmorro/__main__.py:285
#: kosmorro/__main__.py:274
msgid ""
"The observer's position on Earth, in the \"{latitude},{longitude}\" "
"format. Can also be set in the KOSMORRO_POSITION environment variable."
msgstr ""

#: kosmorro/__main__.py:295
#: kosmorro/__main__.py:284
msgid ""
"The date for which the ephemerides must be calculated. Can be in the "
"YYYY-MM-DD format or an interval in the \"[+-]YyMmDd\" format (with Y, M,"
" and D numbers). Defaults to current date."
msgstr ""

#: kosmorro/__main__.py:306
#: kosmorro/__main__.py:295
msgid ""
"The timezone to display the hours in (e.g. 2 for UTC+2 or -3 for UTC-3). "
"Can also be set in the KOSMORRO_TIMEZONE environment variable."
msgstr ""

#: kosmorro/__main__.py:315
#: kosmorro/__main__.py:304
msgid "Disable the colors in the console."
msgstr ""

#: kosmorro/__main__.py:322
msgid ""
"A file to export the output to. If not given, the standard output is "
"used. This argument is needed for PDF format."
#: kosmorro/__main__.py:311
msgid "A file to export the output to. If not given, the standard output is used."
msgstr ""

#: kosmorro/__main__.py:331
#: kosmorro/__main__.py:319
msgid ""
"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."
msgstr ""

#: kosmorro/__main__.py:339
#: kosmorro/__main__.py:327
msgid "Show debugging messages"
msgstr ""

@@ -130,19 +121,19 @@ msgstr ""
msgid "Note: All the hours are given in the UTC{offset} timezone."
msgstr ""

#: kosmorro/dumper.py:205 kosmorro/dumper.py:339
#: kosmorro/dumper.py:205 kosmorro/dumper.py:336
msgid "Object"
msgstr ""

#: kosmorro/dumper.py:206 kosmorro/dumper.py:340
#: kosmorro/dumper.py:206 kosmorro/dumper.py:337
msgid "Rise time"
msgstr ""

#: kosmorro/dumper.py:207 kosmorro/dumper.py:342
#: kosmorro/dumper.py:207 kosmorro/dumper.py:339
msgid "Culmination time"
msgstr ""

#: kosmorro/dumper.py:208 kosmorro/dumper.py:344
#: kosmorro/dumper.py:208 kosmorro/dumper.py:341
msgid "Set time"
msgstr ""

@@ -150,7 +141,7 @@ msgstr ""
msgid "Moon phase is unavailable for this date."
msgstr ""

#: kosmorro/dumper.py:238 kosmorro/dumper.py:348
#: kosmorro/dumper.py:238 kosmorro/dumper.py:345
msgid "Moon phase:"
msgstr ""

@@ -158,51 +149,35 @@ msgstr ""
msgid "{next_moon_phase} on {next_moon_phase_date} at {next_moon_phase_time}"
msgstr ""

#: kosmorro/dumper.py:312
#: kosmorro/dumper.py:309
msgid "Overview of your sky"
msgstr ""

#: kosmorro/dumper.py:320
#: kosmorro/dumper.py:317
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:330
#: kosmorro/dumper.py:327
msgid ""
"Don't forget to check the weather forecast before you go out with your "
"equipment."
msgstr ""

#: kosmorro/dumper.py:337
#: kosmorro/dumper.py:334
msgid "Ephemerides of the day"
msgstr ""

#: kosmorro/dumper.py:346
#: kosmorro/dumper.py:343
msgid "hours"
msgstr ""

#: kosmorro/dumper.py:353
#: kosmorro/dumper.py:350
msgid "Expected events"
msgstr ""

#: kosmorro/dumper.py:493
msgid ""
"Building PDF was not possible, because some dependencies are not "
"installed.\n"
"Please look at the documentation at https://kosmorro.space/cli/generate-"
"pdf/ for more information."
msgstr ""

#: kosmorro/dumper.py:542
#, python-format
msgid ""
"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"
msgstr ""

#: kosmorro/exceptions.py:35
msgid "The date must be between {minimum_date} and {maximum_date}"
msgstr ""


+ 2
- 8
manpage/kosmorro.1.md View File

@@ -33,11 +33,11 @@

`--format=`_FORMAT_, `-f` _FORMAT_ (optional)
the format under which the information have to be output; one of the following:
text (plain text, like normal console output), json, tex (LaTeX), pdf.
text (plain text, like normal console output), json, tex (LaTeX).
If no format is provided, the output format will be inferred from the extension of the output file

`--no-graph`
present the ephemerides in a table instead of a graph; PDF output format only
present the ephemerides in a table instead of a graph; LaTeX output format only

## ENVIRONMENT VARIABLES

@@ -70,12 +70,6 @@ Compute the ephemerides for Lille, France, on April 1st, 2022:
kosmorro --latitude=50.5876 --longitude=3.0624 --date=2022-04-01
```

Compute the ephemerides for Lille, France, on April 1st, 2022, and export them in a PDF document:

```
kosmorro --latitude=50.5876 --longitude=3.0624 -date=2022-04-01 --output=file.pdf
```

## AUTHOR

Written by Jérôme Deuchnord.


+ 4
- 5
tests/general.py View File

@@ -42,7 +42,7 @@ def test_help_message():

assert (
result.stdout
== """usage: kosmorro [-h] [--version] [--format {txt,json,pdf,tex}]
== """usage: kosmorro [-h] [--version] [--format {txt,json,tex}]
[--position POSITION] [--date DATE] [--timezone TIMEZONE]
[--no-colors] [--output OUTPUT] [--no-graph] [--debug]

@@ -52,7 +52,7 @@ on Earth.
%s:
-h, --help show this help message and exit
--version, -v Show the program version
--format {txt,json,pdf,tex}, -f {txt,json,pdf,tex}
--format {txt,json,tex}, -f {txt,json,tex}
The format to output the information to. If not
provided, the output format will be inferred from the
file extension of the output file.
@@ -71,10 +71,9 @@ on Earth.
--no-colors Disable the colors in the console.
--output OUTPUT, -o OUTPUT
A file to export the output to. If not given, the
standard output is used. This argument is needed for
PDF format.
standard output is used.
--no-graph Do not generate a graph to represent the rise and set
times in the PDF format.
times in the LaTeX file.
--debug Show debugging messages

By default, only the events will be computed for today. To compute also the


+ 19
- 3
tests/output.py View File

@@ -4,9 +4,8 @@ from .utils import (
execute,
KOSMORRO,
)
import tempfile
from os import path, environ
from sys import platform

from os import path


def test_json_output():
@@ -150,3 +149,20 @@ def test_json_output():
}
"""
)


def test_latex_output():
result = execute(
KOSMORRO + ["--position=50.5876,3.0624", "-d2020-01-27", "--format=tex"]
)
assert result.successful

with open(
path.join(path.dirname(__file__), "outputs", "2020-01-27-with-position.tex")
) as expected_output:
expected_output = expected_output.read().replace(
"__PROJECT_PATH__",
path.join(path.dirname(path.dirname(__file__)), "kosmorro"),
)

assert result.stdout == expected_output

+ 181
- 0
tests/outputs/2020-01-27-with-position.tex View File

@@ -0,0 +1,181 @@
\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=25mm]{geometry}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{fp}

% Command showing the Moon phase
\newcommand{\moonphase}[2]{
\begin{center}
\begin{minipage}{2cm}
\includegraphics[width=\linewidth]{#1}
\end{minipage}
\hspace{5mm}
\begin{minipage}{7cm}
\textbf{\currentmoonphasetitle}\\#2
\end{minipage}
\end{center}
}

% Environment for the ephemerides, when --no-graph is given on the command line
\newenvironment{ephemerides}{
\begin{table}[h]
\centering
\begin{tabular}{lccc}
\textbf{\ephemeridesobjecttitle} &
\textbf{\ephemeridesrisetimetitle} &
\textbf{\ephemeridesculminationtimetitle} &
\textbf{\ephemeridessettimetitle}\\
\hline
}{
\end{tabular}
\end{table}
}

% Command adding an object to the ephemerides environment
\newcommand{\object}[4]{
\hline
\textbf{#1} & {#2} & {#3} & {#4}\\
}

% Environment to insert the ephemerides graph
\newenvironment{graphephemerides}{\setlength{\unitlength}{0.02\linewidth}
\begin{picture}(20,20)
% Axes
\put(0,-2){\vector(1,0){50}}
\multiput(0,-2)(2,0){24}{
\line(0,-1){0.25}
}
\newcounter{hour}
\multiput(-0.25,-3.5)(4,0){12}{
\sffamily\footnotesize
\arabic{hour}\stepcounter{hour}\stepcounter{hour}
}
\put(49,-3.5){\sffamily\footnotesize \hourslabel}

% Graduation

\put(50,-0.5){\sffamily\footnotesize \Pluto}
\put(50,1.5){\sffamily\footnotesize \Neptune}
\put(50,3.5){\sffamily\footnotesize \Uranus}
\put(50,5.5){\sffamily\footnotesize \Saturn}
\put(50,7.5){\sffamily\footnotesize \Jupiter}
\put(50,9.5){\sffamily\footnotesize \Mars}
\put(50,11.5){\sffamily\footnotesize \Venus}
\put(50,13.5){\sffamily\footnotesize \Mercury}
\put(50,15.5){\sffamily\footnotesize \Moon}
\put(50,17.5){\sffamily\footnotesize \Sun}

\multiput(0,0)(0,2){10}{
\color{gray}\line(1,0){48}
}

\linethickness{1.5mm}
}{
\end{picture}
\vspace{1cm}
}

% Command to add an object to the graph
\newcommand{\graphobject}[8]{%
% #1: Y coordinate component
% #2: Color
% #3: Hour rise time
% #4: Minute rise time
% #5: Hour set time
% #6: Minute set time
% #7: Human-readable rise time
% #8: Human-readable set time

\FPeval{\start}{#3*2+(#4/60)*2}%
\FPeval{\length}{#5*2+(#6/60)*2 - \start}%
\FPeval{\starttext}{\start+0.7}%
\FPeval{\endtext}{\start+\length-3.25}%

{\color{#2}%
\put(\start,#1){%
\line(1, 0){\length}%
}}%

\put(\starttext,#1.5){\sffamily\footnotesize #7}%
\put(\endtext,#1.5){\sffamily\footnotesize #8}%
}

\newcommand{\event}[2]{
\textbf{#1} & {#2}\\
}

\newenvironment{events}{
\begin{table}[h]
\begin{tabular}{ll}
}{
\end{tabular}
\end{table}
}

% Commands to handle the translated strings
\newcommand{\currentmoonphasetitle}{Moon phase:}
\newcommand{\ephemeridesobjecttitle}{Object}
\newcommand{\ephemeridesrisetimetitle}{Rise time}
\newcommand{\ephemeridesculminationtimetitle}{Culmination time}
\newcommand{\ephemeridessettimetitle}{Set time}
\newcommand{\hourslabel}{hours}

\newcommand{\Pluto}{Pluto}
\newcommand{\Neptune}{Neptune}
\newcommand{\Uranus}{Uranus}
\newcommand{\Saturn}{Saturn}
\newcommand{\Jupiter}{Jupiter}
\newcommand{\Mars}{Mars}
\newcommand{\Venus}{Venus}
\newcommand{\Mercury}{Mercury}
\newcommand{\Moon}{Moon}
\newcommand{\Sun}{Sun}

% Fix Unicode issues
\DeclareUnicodeCharacter{202F}{~}
\DeclareUnicodeCharacter{00B0}{$^\circ$}

\hypersetup{pdfinfo={%
Title={Overview of your sky},
Creator={Kosmorro v0.10.12}
}}

\pagenumbering{gobble}
\setcounter{secnumdepth}{0}

\title{\sffamily\href{http://kosmorro.space}{\includegraphics[width=5cm]{__PROJECT_PATH__/assets/png/kosmorro-logo.png}}\\Overview of your sky}
\date{\vspace{-11mm}\sffamily Monday, January 27, 2020}

\begin{document}

\maketitle

This document summarizes the ephemerides and the events of Monday, January 27, 2020. It aims to help you to prepare your observation session. All the hours are given in UTC.

Don't forget to check the weather forecast before you go out with your equipment.

\moonphase{__PROJECT_PATH__/assets/moonphases/png/new-moon.png}{New Moon}

%%% BEGIN-EPHEMERIDES-SECTION
\section{\sffamily Ephemerides of the day}

\begin{graphephemerides}%
\graphobject{18}{gray}{7}{31}{16}{30}{7:31 AM}{4:30 PM}\graphobject{16}{gray}{9}{6}{19}{13}{9:06 AM}{7:13 PM}\graphobject{14}{gray}{8}{10}{17}{28}{8:10 AM}{5:28 PM}\graphobject{12}{gray}{9}{1}{20}{10}{9:01 AM}{8:10 PM}\graphobject{10}{gray}{4}{19}{12}{28}{4:19 AM}{12:28 PM}\graphobject{8}{gray}{6}{15}{14}{21}{6:15 AM}{2:21 PM}\graphobject{6}{gray}{6}{56}{15}{22}{6:56 AM}{3:22 PM}\graphobject{4}{gray}{0}{0}{0}{33}{}{12:33 AM}\graphobject{4}{gray}{10}{21}{24}{0}{10:21 AM}{}\graphobject{2}{gray}{9}{1}{20}{10}{9:01 AM}{8:10 PM}\graphobject{0}{gray}{6}{57}{15}{11}{6:57 AM}{3:11 PM}
\end{graphephemerides}
%%% END-EPHEMERIDES-SECTION

%%% BEGIN-EVENTS-SECTION
\section{\sffamily Expected events}

\begin{events}
\event{8:00 PM}{Venus and Neptune are in conjunction}
\end{events}
%%% END-EVENTS-SECTION

\end{document}


Loading…
Cancel
Save