diff --git a/.scripts/tests-e2e.sh b/.scripts/tests-e2e.sh index 6b823ff..e13765a 100755 --- a/.scripts/tests-e2e.sh +++ b/.scripts/tests-e2e.sh @@ -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 diff --git a/_kosmorro/geolocation.py b/_kosmorro/geolocation.py new file mode 100644 index 0000000..3bee5aa --- /dev/null +++ b/_kosmorro/geolocation.py @@ -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) diff --git a/_kosmorro/main.py b/_kosmorro/main.py index bd852c7..234acc8 100644 --- a/_kosmorro/main.py +++ b/_kosmorro/main.py @@ -13,6 +13,7 @@ from termcolor import colored 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 @@ -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( @@ -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(