瀏覽代碼

feat: replace latitude and longitude with one argument alone: --position

pull/191/head
Jérôme Deuchnord 2 年之前
父節點
當前提交
62f7304a68
共有 3 個檔案被更改,包括 44 行新增30 行删除
  1. +15
    -9
      .scripts/tests-e2e.sh
  2. +22
    -0
      _kosmorro/geolocation.py
  3. +7
    -21
      _kosmorro/main.py

+ 15
- 9
.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


+ 22
- 0
_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)

+ 7
- 21
_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(


Loading…
取消
儲存