@@ -1,13 +1,8 @@ | |||
#!/usr/bin/env python3 | |||
import os | |||
import re | |||
from shutil import rmtree | |||
from pathlib import Path | |||
from datetime import date | |||
from dateutil.relativedelta import relativedelta | |||
from skyfield.api import Loader | |||
from skyfield.timelib import Time | |||
from skyfield.nutationlib import iau2000b | |||
@@ -15,36 +10,6 @@ from skyfield.nutationlib import iau2000b | |||
CACHE_FOLDER = str(Path.home()) + "/.kosmorro-cache" | |||
class Environment: | |||
def __init__(self): | |||
self._vars = {} | |||
def __set__(self, key, value): | |||
self._vars[key] = value | |||
def __getattr__(self, key): | |||
return self._vars[key] if key in self._vars else None | |||
def __str__(self): | |||
return self._vars.__str__() | |||
def __len__(self): | |||
return len(self._vars) | |||
def get_env() -> Environment: | |||
environment = Environment() | |||
for var in os.environ: | |||
if not re.search("^KOSMORRO_", var): | |||
continue | |||
[_, env] = var.split("_", 1) | |||
environment.__set__(env.lower(), os.getenv(var)) | |||
return environment | |||
def get_loader(): | |||
return Loader(CACHE_FOLDER) | |||
@@ -76,35 +41,3 @@ def flatten_list(the_list: list): | |||
new_list.append(item) | |||
return new_list | |||
def get_date(date_arg: str) -> date: | |||
if re.match(r"^\d{4}-\d{2}-\d{2}$", date_arg): | |||
try: | |||
return date.fromisoformat(date_arg) | |||
except ValueError as error: | |||
raise ValueError( | |||
"The date {date} is not valid: {error}".format( | |||
date=date_arg, error=error.args[0] | |||
) | |||
) from error | |||
elif re.match(r"^([+-])(([0-9]+)y)?[ ]?(([0-9]+)m)?[ ]?(([0-9]+)d)?$", date_arg): | |||
def get_offset(date_arg: str, signifier: str): | |||
if re.search(r"([0-9]+)" + signifier, date_arg): | |||
return abs( | |||
int(re.search(r"[+-]?([0-9]+)" + signifier, date_arg).group(0)[:-1]) | |||
) | |||
return 0 | |||
days = get_offset(date_arg, "d") | |||
months = get_offset(date_arg, "m") | |||
years = get_offset(date_arg, "y") | |||
if date_arg[0] == "+": | |||
return date.today() + relativedelta(days=days, months=months, years=years) | |||
return date.today() - relativedelta(days=days, months=months, years=years) | |||
else: | |||
error_msg = "The date {date} does not match the required YYYY-MM-DD format or the offset format." | |||
raise ValueError(error_msg.format(date=date_arg)) |
@@ -227,18 +227,14 @@ ASTERS = [ | |||
class Position: | |||
def __init__(self, latitude: float, longitude: float, aster: Object = EARTH): | |||
def __init__(self, latitude: float, longitude: float): | |||
self.latitude = latitude | |||
self.longitude = longitude | |||
self.aster = aster | |||
self._topos = None | |||
def get_planet_topos(self) -> Topos: | |||
if self.aster is None: | |||
raise TypeError("Observation planet must be set.") | |||
if self._topos is None: | |||
self._topos = self.aster.get_skyfield_object() + Topos( | |||
self._topos = EARTH.get_skyfield_object() + Topos( | |||
latitude_degrees=self.latitude, longitude_degrees=self.longitude | |||
) | |||
@@ -1,11 +1,7 @@ | |||
import unittest | |||
import os | |||
import kosmorrolib.core as core | |||
from datetime import date | |||
from dateutil.relativedelta import relativedelta | |||
class CoreTestCase(unittest.TestCase): | |||
def test_flatten_list(self): | |||
@@ -14,37 +10,6 @@ class CoreTestCase(unittest.TestCase): | |||
core.flatten_list([0, 1, 2, [3, 4, [5, 6], 7], 8, [9]]), | |||
) | |||
def test_get_env(self): | |||
self.assertEqual(0, len(core.get_env())) | |||
os.environ["SOME_RANDOM_VAR"] = "an awesome value" | |||
self.assertEqual(0, len(core.get_env())) | |||
os.environ["KOSMORRO_GREAT_VARIABLE"] = "value" | |||
env = core.get_env() | |||
self.assertEqual(1, len(env)) | |||
self.assertEqual("value", env.great_variable) | |||
os.environ["KOSMORRO_ANOTHER_VARIABLE"] = "another value" | |||
env = core.get_env() | |||
self.assertEqual(2, len(env)) | |||
self.assertEqual("value", env.great_variable) | |||
self.assertEqual("another value", env.another_variable) | |||
self.assertEqual( | |||
"{'great_variable': 'value', 'another_variable': 'another value'}", str(env) | |||
) | |||
def test_date_arg_parsing(self): | |||
self.assertEqual( | |||
core.get_date("+1y 2m3d"), | |||
date.today() + relativedelta(years=1, months=2, days=3), | |||
) | |||
self.assertEqual( | |||
core.get_date("-1y2d"), date.today() - relativedelta(years=1, days=2) | |||
) | |||
self.assertEqual(core.get_date("1111-11-13"), date(1111, 11, 13)) | |||
if __name__ == "__main__": | |||
unittest.main() |
@@ -4,14 +4,14 @@ from kosmorrolib.enum import MoonPhaseType | |||
from .testutils import expect_assertions | |||
from kosmorrolib import ephemerides | |||
from kosmorrolib.data import EARTH, Position, MoonPhase | |||
from kosmorrolib.data import Position, MoonPhase | |||
from datetime import date | |||
from kosmorrolib.exceptions import OutOfRangeDateError | |||
class EphemeridesTestCase(unittest.TestCase): | |||
def test_get_ephemerides_for_aster_returns_correct_hours(self): | |||
position = Position(0, 0, EARTH) | |||
position = Position(0, 0) | |||
eph = ephemerides.get_ephemerides(date=date(2019, 11, 18), position=position) | |||
@expect_assertions(self.assertRegex, num=3) | |||
@@ -114,7 +114,7 @@ class EphemeridesTestCase(unittest.TestCase): | |||
def test_get_ephemerides_raises_exception_on_out_of_date_range(self): | |||
with self.assertRaises(OutOfRangeDateError): | |||
ephemerides.get_ephemerides(Position(0, 0, EARTH), date(1789, 5, 5)) | |||
ephemerides.get_ephemerides(Position(0, 0), date(1789, 5, 5)) | |||
def test_get_moon_phase_raises_exception_on_out_of_date_range(self): | |||
with self.assertRaises(OutOfRangeDateError): | |||