Skip to content

Commit

Permalink
fix: use Babel to translate the dates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Deuchnord authored and Deuchnord committed Mar 7, 2022
1 parent 3b13175 commit 9268113
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 157 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ COPY _kosmorro/ _kosmorro/
COPY kosmorro .

# Compile the translations
RUN pip install Babel
COPY setup.py setup.py
COPY setup.cfg setup.cfg
COPY README.md README.md
Expand All @@ -27,7 +26,7 @@ RUN python setup.py compile_catalog
# Clean the image
RUN rm setup.py setup.cfg README.md && \
rm _kosmorro/locales/messages.pot _kosmorro/locales/*/LC_MESSAGES/messages.po && \
pip uninstall --yes Babel pipenv
pip uninstall --yes pipenv

USER kosmorro

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tests:
echo; \
fi

pipenv run python3 -m pytest tests/*.py
LANG=C pipenv run python3 -m pytest tests/*.py

.PHONY: build
build: manpage
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
babel = "*"
black = "*"
pytest = "*"
aurornis = "*"
Expand All @@ -14,6 +13,7 @@ tabulate = "*"
termcolor = "*"
kosmorrolib = ">=1.0.0,<2.0.0"
python-dateutil = "*"
babel = ">=2.9.0,<3.0.0"

[requires]
python_version = "3"
Expand Down
32 changes: 16 additions & 16 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 36 additions & 69 deletions _kosmorro/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import shutil
from pathlib import Path

import kosmorrolib
from babel.dates import format_date, format_time
from tabulate import tabulate
from termcolor import colored

Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(
self.show_graph = show_graph

def get_date_as_string(self, capitalized: bool = False) -> str:
date = self.date.strftime(FULL_DATE_FORMAT)
date = format_date(self.date, "full")

if capitalized:
return "".join([date[0].upper(), date[1:]])
Expand Down Expand Up @@ -181,35 +181,21 @@ def stringify_ephemerides(self) -> str:

name = self.style(object_name, "th")

if ephemeris.rise_time is not None:
time_fmt = (
TIME_FORMAT
if ephemeris.rise_time.day == self.date.day
else SHORT_DATETIME_FORMAT
)
planet_rise = ephemeris.rise_time.strftime(time_fmt)
else:
planet_rise = "-"

if ephemeris.culmination_time is not None:
time_fmt = (
TIME_FORMAT
if ephemeris.culmination_time.day == self.date.day
else SHORT_DATETIME_FORMAT
)
planet_culmination = ephemeris.culmination_time.strftime(time_fmt)
else:
planet_culmination = "-"

if ephemeris.set_time is not None:
time_fmt = (
TIME_FORMAT
if ephemeris.set_time.day == self.date.day
else SHORT_DATETIME_FORMAT
)
planet_set = ephemeris.set_time.strftime(time_fmt)
else:
planet_set = "-"
planet_rise = (
"-"
if ephemeris.rise_time is None
else format_time(ephemeris.rise_time, "short")
)
planet_culmination = (
"-"
if ephemeris.culmination_time is None
else format_time(ephemeris.culmination_time, "short")
)
planet_set = (
"-"
if ephemeris.set_time is None
else format_time(ephemeris.set_time, "short")
)

data.append([name, planet_rise, planet_culmination, planet_set])

Expand All @@ -234,14 +220,9 @@ def get_events(self, events: [Event]) -> str:
if description is None:
continue

time_fmt = (
TIME_FORMAT
if event.start_time.day == self.date.day
else SHORT_DATETIME_FORMAT
)
data.append(
[
self.style(event.start_time.strftime(time_fmt), "th"),
self.style(format_time(event.start_time, "short"), "th"),
description,
]
)
Expand All @@ -262,8 +243,8 @@ def get_moon(self, moon_phase: MoonPhase) -> str:
"{next_moon_phase} on {next_moon_phase_date} at {next_moon_phase_time}"
).format(
next_moon_phase=_(strings.from_moon_phase(moon_phase.get_next_phase())),
next_moon_phase_date=moon_phase.next_phase_date.strftime(FULL_DATE_FORMAT),
next_moon_phase_time=moon_phase.next_phase_date.strftime(TIME_FORMAT),
next_moon_phase_date=format_date(moon_phase.next_phase_date, "full"),
next_moon_phase_time=format_time(moon_phase.next_phase_date, "short"),
)

return "\n".join([current_moon_phase, new_moon_phase])
Expand Down Expand Up @@ -384,35 +365,21 @@ def _make_ephemerides(self) -> str:

if self.ephemerides is not None:
for ephemeris in self.ephemerides:
if ephemeris.rise_time is not None:
time_fmt = (
TIME_FORMAT
if ephemeris.rise_time.day == self.date.day
else SHORT_DATETIME_FORMAT
)
aster_rise = ephemeris.rise_time.strftime(time_fmt)
else:
aster_rise = "-"

if ephemeris.culmination_time is not None:
time_fmt = (
TIME_FORMAT
if ephemeris.culmination_time.day == self.date.day
else SHORT_DATETIME_FORMAT
)
aster_culmination = ephemeris.culmination_time.strftime(time_fmt)
else:
aster_culmination = "-"

if ephemeris.set_time is not None:
time_fmt = (
TIME_FORMAT
if ephemeris.set_time.day == self.date.day
else SHORT_DATETIME_FORMAT
)
aster_set = ephemeris.set_time.strftime(time_fmt)
else:
aster_set = "-"
aster_rise = (
"-"
if ephemeris.rise_time is None
else format_time(ephemeris.rise_time, "short")
)
aster_culmination = (
"-"
if ephemeris.culmination_time is None
else format_time(ephemeris.culmination_time, "short")
)
aster_set = (
"-"
if ephemeris.set_time is None
else format_time(ephemeris.set_time, "short")
)

if not self.show_graph:
object_name = strings.from_object(ephemeris.object.identifier)
Expand Down Expand Up @@ -476,7 +443,7 @@ def _make_events(self) -> str:
continue

latex.append(
r"\event{%s}{%s}" % (event.start_time.strftime(TIME_FORMAT), event_name)
r"\event{%s}{%s}" % (format_time(event.start_time, "short"), event_name)
)

return "".join(latex)
Expand Down
5 changes: 3 additions & 2 deletions _kosmorro/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from datetime import date
from babel.dates import format_date
from _kosmorro.i18n.utils import _, SHORT_DATE_FORMAT


Expand All @@ -34,8 +35,8 @@ def __init__(self, min_date: date, max_date: date):
self.msg = _(
"The date must be between {minimum_date} and {maximum_date}"
).format(
minimum_date=min_date.strftime(SHORT_DATE_FORMAT),
maximum_date=max_date.strftime(SHORT_DATE_FORMAT),
minimum_date=format_date(min_date, "long"),
maximum_date=format_date(max_date, "long"),
)


Expand Down

0 comments on commit 9268113

Please sign in to comment.