Skip to content

Commit

Permalink
Code cleanup and additional error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TJohnsonAZ committed Jan 25, 2024
1 parent 02fcd27 commit 5bbefd9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
35 changes: 25 additions & 10 deletions epymorph/cli/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"""
import os
from argparse import _SubParsersAction
from pathlib import Path

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

Expand Down Expand Up @@ -38,8 +40,7 @@ def define_argparser(command_parser: _SubParsersAction):
action='store_true',
help='(optional) include this flag to force an override of previously cached data')
fetch_command.set_defaults(handler=lambda args: fetch(
geo_name=args.geo,
geo_path=args.path,
geo_name_or_path=args.geo,
force=args.force
))

Expand All @@ -51,7 +52,7 @@ def define_argparser(command_parser: _SubParsersAction):
type=str,
help='the name of a geo from the library')
remove_command.set_defaults(handler=lambda args: remove(
geo_name=args.geo
geo_name_or_path=args.geo
))

list_command = sp.add_parser(
Expand All @@ -71,28 +72,42 @@ def define_argparser(command_parser: _SubParsersAction):
# - 2 empty cache


def fetch(geo_name: str, force: bool, geo_path=None) -> int:
def fetch(geo_name_or_path: str, force: bool) -> int:
"""CLI command handler: cache dynamic geo data."""
# cache specified geo

# split geo name and path
if geo_name_or_path in geo_library_dynamic:
geo_name = geo_name_or_path
geo_path = None
elif os.path.exists(Path(geo_name_or_path)):
geo_path = Path(geo_name_or_path)
geo_name = geo_path.name.split('.')[0]
else:
return 1 # exit code: geo not found

# cache geo according to information passed
filepath = cache.CACHE_PATH / F.to_archive_filename(geo_name)
if geo_path is not None and geo_name in geo_library:
msg = f"A geo named {geo_name} is already present in the library. Please use the existing geo or change the file name."
raise cache.GeoCacheException(msg)
choice = 'y'
if os.path.exists(filepath) and not force:
choice = input(f'{geo_name} is already cached, overwrite? [y/n] ')
if force or choice == 'y':
try:
cache.fetch(geo_name, geo_path)
print('geo sucessfully cached.')
cache.fetch(geo_name_or_path)
print("geo sucessfully cached.")
except cache.GeoCacheException as e:
print(e)
return 1 # exit code: geo not found
return 0 # exit code: success


def remove(geo_name: str) -> int:
def remove(geo_name_or_path: str) -> int:
"""CLI command handler: remove geo from cache"""
try:
cache.remove(geo_name)
print(f'{geo_name} removed from cache.')
cache.remove(geo_name_or_path)
print(f'{geo_name_or_path} removed from cache.')
return 0 # exit code: success
except cache.GeoCacheException as e:
print(e)
Expand Down
5 changes: 5 additions & 0 deletions epymorph/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ def load_model_geo(name_or_path: str, ignore_cache: bool) -> Geo:
if not path.exists():
raise UnknownModel('GEO', name_or_path)

# check for non-library geo cached
cached = load_from_cache(path.name.split('.')[0])
if cached is not None:
return cached

if path.suffix == ".geo":
return DynamicGeoFileOps.load_from_spec(path, adrio_maker_library)
else:
Expand Down
37 changes: 21 additions & 16 deletions epymorph/geo/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,32 @@ class GeoCacheException(Exception):
"""An exception raised when a geo cache operation fails."""


def fetch(geo_name: str, geo_path=None) -> None:
def fetch(geo_name_or_path: str) -> 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.
"""
filepath = CACHE_PATH / F.to_archive_filename(geo_name)
geo_load = geo_library_dynamic.get(geo_name)
if geo_path is not None:
geo_path = Path(geo_path)
# checks for geo in library
if geo_load is not None:
geo = geo_load()
static_geo = convert_to_static_geo(geo)
static_geo.save(filepath)
# checks for geo spec at given path
elif geo_path is not None and os.path.exists(geo_path):
geo = DF.load_from_spec(geo_path, adrio_maker_library)
static_geo = convert_to_static_geo(geo)
static_geo.save(filepath)

# checks for geo in the library (name passed)
if geo_name_or_path in geo_library_dynamic:
filepath = CACHE_PATH / F.to_archive_filename(geo_name_or_path)
geo_load = geo_library_dynamic.get(geo_name_or_path)
if geo_load is not None:
geo = geo_load()
static_geo = convert_to_static_geo(geo)
static_geo.save(filepath)

# checks for geo spec at given path (path passed)
else:
raise GeoCacheException(f'spec file for {geo_name} not found.')
geo_path = Path(geo_name_or_path)
if os.path.exists(geo_path):
geo_name = geo_path.name.split('.')[0]
filepath = CACHE_PATH / F.to_archive_filename(geo_name)
geo = DF.load_from_spec(geo_path, adrio_maker_library)
static_geo = convert_to_static_geo(geo)
static_geo.save(filepath)
else:
raise GeoCacheException(f'spec file at {geo_name_or_path} not found.')


def remove(geo_name: str) -> None:
Expand Down

0 comments on commit 5bbefd9

Please sign in to comment.