Skip to content

Commit

Permalink
feat: replace latitude and longitude with one argument alone: --position
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Deuchnord committed Jun 20, 2021
1 parent 6ffe0ad commit 9d2d0cc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
24 changes: 15 additions & 9 deletions .scripts/tests-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,34 @@ assertSuccess "$KOSMORRO_COMMAND --date='+3y 5m3d'"
assertSuccess "$KOSMORRO_COMMAND --date='-1y3d'"
assertFailure "$KOSMORRO_COMMAND --date='+3d4m"
assertFailure "$KOSMORRO_COMMAND -date='3y'"
assertSuccess "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624"
assertSuccess "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624 -d 2020-01-27"
assertSuccess "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624 -d 2020-01-27 --timezone=1"
assertSuccess "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624 -d 2020-01-27 --timezone=-1"
assertSuccess "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624 -d 2020-01-27 --format=json"
assertFailure "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624 -d 2020-01-27 --format=pdf"
assertFailure "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624"
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\""
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876,3.0624\""
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876;-3.0624\""
assertSuccess "$KOSMORRO_COMMAND --position=\"-50.5876;-3.\"0624"
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876,-3.0624\""
assertSuccess "$KOSMORRO_COMMAND --position=\"-50.5876,-3.\"0624"
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\" -d 2020-01-27"
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\" -d 2020-01-27 --timezone=1"
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\" -d 2020-01-27 --timezone=-1"
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\" -d 2020-01-27 --format=json"
assertFailure "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\" -d 2020-01-27 --format=pdf"

# Environment variables
assertSuccess "LATITUDE=50.5876 LONGITUDE=3.0624 TIMEZONE=1 kosmorro -d 2020-01-27"
assertSuccess "LATITUDE=50.5876 LONGITUDE=3.0624 TIMEZONE=-1 kosmorro -d 2020-01-27"

# Missing dependencies, should fail
assertFailure "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624 -d 2020-01-27 --format=pdf -o $HOME/kosmorro/export/document.pdf"
assertFailure "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\" -d 2020-01-27 --format=pdf -o $HOME/kosmorro/export/document.pdf"
assertFailure "ls $HOME/kosmorro/export/document.pdf"

assertSuccess "sudo apt-get install -y texlive texlive-latex-extra" "CI"

# Dependencies installed, should not fail
assertSuccess "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624 -d 2020-01-27 --format=pdf -o $HOME/kosmorro/export/document.pdf"
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\" -d 2020-01-27 --format=pdf -o $HOME/kosmorro/export/document.pdf"
assertSuccess "ls $HOME/kosmorro/export/document.pdf"

assertSuccess "$KOSMORRO_COMMAND --latitude=50.5876 --longitude=3.0624 -d 2020-01-27 --format=pdf -o $HOME/kosmorro/export/document-no-graph.pdf --no-graph"
assertSuccess "$KOSMORRO_COMMAND --position=\"50.5876;3.0624\" -d 2020-01-27 --format=pdf -o $HOME/kosmorro/export/document-no-graph.pdf --no-graph"
assertSuccess "ls $HOME/kosmorro/export/document-no-graph.pdf"

# man page
Expand Down
22 changes: 22 additions & 0 deletions _kosmorro/geolocation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

import re

from kosmorrolib import Position

from .i18n.utils import _


def _parse_latitude_longitude(from_str: str) -> Position:
if not re.search(r"^([\d.-]+)[,;]([\d.-]+)$", from_str):
raise ValueError(_("The given position (%s) is not valid." % from_str))

latitude_longitude = from_str.split(';')
if len(latitude_longitude) == 1:
latitude_longitude = from_str.split(',')

return Position(float(latitude_longitude[0]), float(latitude_longitude[1]))


def get_position(from_str: str) -> Position:
return _parse_latitude_longitude(from_str)
28 changes: 7 additions & 21 deletions _kosmorro/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from . import dumper, environment, debug
from .date import parse_date
from .geolocation import get_position
from .__version__ import __version__ as kosmorro_version
from .exceptions import UnavailableFeatureError, OutOfRangeDateError as DateRangeError
from _kosmorro.i18n.utils import _, SHORT_DATE_FORMAT
Expand All @@ -34,12 +35,7 @@ def main():
print(colored(error.args[0], color="red", attrs=["bold"]))
return -1

position = None

if args.latitude is not None or args.longitude is not None:
position = Position(args.latitude, args.longitude)
elif env_vars.latitude is not None and env_vars.longitude is not None:
position = Position(float(env_vars.latitude), float(env_vars.longitude))
position = get_position(args.position) if args.position is not None else None

if output_format == "pdf":
print(
Expand Down Expand Up @@ -247,23 +243,13 @@ def get_args(output_formats: [str]):
help=_("The format to output the information to"),
)
parser.add_argument(
"--latitude",
"-lat",
type=float,
default=None,
help=_(
"The observer's latitude on Earth. Can also be set in the KOSMORRO_LATITUDE environment "
"variable."
),
)
parser.add_argument(
"--longitude",
"-lon",
type=float,
"--position",
"-p",
type=str,
default=None,
help=_(
"The observer's longitude on Earth. Can also be set in the KOSMORRO_LONGITUDE "
"environment variable."
"The observer's position on Earth, in the \"{latitude},{longitude}\" format."
"Can also be set in the KOSMORRO_POSITION environment variable."
),
)
parser.add_argument(
Expand Down

0 comments on commit 9d2d0cc

Please sign in to comment.