Skip to content

Commit

Permalink
Untangle some circular imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Coles committed May 16, 2024
1 parent cc9e15d commit d09a0ed
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
7 changes: 4 additions & 3 deletions epymorph/cli/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path

from epymorph.cache import CACHE_PATH
from epymorph.data import geo_library, geo_library_dynamic
from epymorph.data import adrio_maker_library, geo_library, geo_library_dynamic
from epymorph.geo import cache
from epymorph.geo.static import StaticGeoFileOps as F

Expand Down Expand Up @@ -121,7 +121,7 @@ def fetch(geo_name_or_path: str, force: bool) -> int:
choice = input(f'{geo_name} is already cached, overwrite? [y/n] ')
if force or choice == 'y':
try:
cache.fetch(geo_name_or_path)
cache.fetch(geo_name_or_path, geo_library_dynamic, adrio_maker_library)
print("geo sucessfully cached.")
except cache.GeoCacheException as e:
print(e)
Expand All @@ -141,7 +141,8 @@ def export(geo_name_or_path: str, out: str | None, rename: str | None, ignore_ca
else:
raise cache.GeoCacheException("Specified geo not found.")

cache.export(geo_name, geo_path, out, rename, ignore_cache)
cache.export(geo_name, geo_path, out, rename, ignore_cache,
geo_library_dynamic, adrio_maker_library)

print("Geo successfully exported.")

Expand Down
12 changes: 7 additions & 5 deletions epymorph/geo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""epymorph's geo package and exports"""
from epymorph.geo.cache import load_from_cache, save_to_cache
from epymorph.geo.dynamic import DynamicGeo
from epymorph.geo.spec import (AttribDef, CentroidDType, DateAndDuration,
DateRange, DynamicGeoSpec, Geography, GeoSpec,
NonspecificDuration, SpecificTimePeriod,
StaticGeoSpec, TimePeriod, Year)
from epymorph.geo.static import StaticGeo

__all__ = [
'AttribDef',
Expand All @@ -17,9 +20,8 @@
'StaticGeoSpec',
'TimePeriod',
'Year',
# TODO: these imports cause circular deps
# 'DynamicGeo',
# 'StaticGeo',
# 'save_to_cache',
# 'load_from_cache',
'DynamicGeo',
'StaticGeo',
'save_to_cache',
'load_from_cache',
]
17 changes: 14 additions & 3 deletions epymorph/geo/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Callable, overload

from epymorph.cache import CACHE_PATH
from epymorph.data import adrio_maker_library, geo_library_dynamic
from epymorph.geo.adrio.adrio import ADRIOMaker
from epymorph.geo.dynamic import DynamicGeo
from epymorph.geo.dynamic import DynamicGeoFileOps as DF
from epymorph.geo.geo import Geo
Expand All @@ -13,12 +13,17 @@
from epymorph.geo.util import convert_to_static_geo
from epymorph.log.messaging import dynamic_geo_messaging

AdrioMakerLibrary = dict[str, type[ADRIOMaker]]
DynamicGeoLibrary = dict[str, Callable[[], DynamicGeo]]


class GeoCacheException(Exception):
"""An exception raised when a geo cache operation fails."""


def fetch(geo_name_or_path: str) -> None:
def fetch(geo_name_or_path: str,
geo_library_dynamic: DynamicGeoLibrary,
adrio_maker_library: AdrioMakerLibrary) -> None:
"""
Caches all attribute data for a dynamic geo from the library or spec file at a given path.
Raises GeoCacheException if spec not found.
Expand Down Expand Up @@ -48,7 +53,13 @@ def fetch(geo_name_or_path: str) -> None:
raise GeoCacheException(f'spec file at {geo_name_or_path} not found.')


def export(geo_name: str, geo_path: Path, out: str | None, rename: str | None, ignore_cache: bool) -> None:
def export(geo_name: str,
geo_path: Path,
out: str | None,
rename: str | None,
ignore_cache: bool,
geo_library_dynamic: DynamicGeoLibrary,
adrio_maker_library: AdrioMakerLibrary) -> None:
"""
Exports a geo as a .geo.tar file to a location outside the cache.
If uncached, geo to export is also cached.
Expand Down

0 comments on commit d09a0ed

Please sign in to comment.