Skip to content

Commit

Permalink
Refactored CLI layout to be more intuitive in terms of naming, and al…
Browse files Browse the repository at this point in the history
…so the module logic is no longer imported into weird places before executing (#30)
  • Loading branch information
zachsa committed Apr 12, 2023
1 parent 44a97d2 commit 029892f
Show file tree
Hide file tree
Showing 54 changed files with 108 additions and 103 deletions.
77 changes: 46 additions & 31 deletions toolkit/__main__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import argparse
import os
from cli.commands import (
lacce as lacce_cmd,
mhw as mhw_cmd,
ops as ops_cmd,
kerchunk as kerchunk_cmd,
update as update_cmd,
from cli.define import (
lacce as define_lacce,
mhw as define_mhw,
ops as define_ops,
kerchunk as define_kerchunk,
update as define_update,
)
from cli.exe import (
lacce as lacce_exe,
mhw as mhw_exe,
ops as ops_exe,
kerchunk as kerchunk_exe,
update as update_exe,
from cli.parse import (
lacce as parse_lacce,
mhw as parse_mhw,
ops as parse_ops,
kerchunk as parse_kerchunk,
update as parse_update,
)
from cli.applications import (
lacce,
mhw,
ops,
kerchunk,
update,
)

prog = "somisana"
Expand All @@ -30,29 +37,37 @@ def main():
metavar="Applications",
)

# Build commands
mhw = mhw_cmd.build(module_parser)
ops = ops_cmd.build(module_parser)
lacce = lacce_cmd.build(module_parser)
kerchunk = kerchunk_cmd.build(module_parser)
update = update_cmd.build(module_parser)
# (1) Build commands
mhw_app = define_mhw.build(module_parser)
ops_app = define_ops.build(module_parser)
lacce_app = define_lacce.build(module_parser)
kerchunk_app = define_kerchunk.build(module_parser)
update_app = define_update.build(module_parser)

# Parse args
# (2) Parse command string
args = parser.parse_args()

# Run the CLI
if args.command == "ops":
ops_exe.run(ops, args)
elif args.command == "mhw":
mhw_exe.run(mhw, args)
elif args.command == "lacce":
lacce_exe.run(lacce, args)
elif args.command == "kerchunk":
kerchunk_exe.run(kerchunk, args)
elif args.command == "update":
update_exe.run(update, args)
else:
# (3) Select the application by parsing flag options
exe = (
parse_ops.parse(ops_app, args, ops)
if args.command == "ops"
else parse_mhw.parse(mhw_app, args, mhw)
if args.command == "mhw"
else parse_lacce.parse(lacce_app, args, lacce)
if args.command == "lacce"
else parse_kerchunk.parse(kerchunk_app, args, kerchunk)
if args.command == "kerchunk"
else parse_update.run(update_app, args, update)
if args.command == "update"
else None
)

# (4) Execute the application
if not exe:
parser.print_help()
return
else:
exe(args)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions toolkit/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from cli import commands, modules, exe
from cli import define, applications, parse

__all__ = ["commands", "modules", "exe"]
__all__ = ["define", "applications", "parse"]
3 changes: 3 additions & 0 deletions toolkit/cli/applications/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from cli.applications import lacce, mhw, ops, kerchunk, update

__all__ = ["lacce", "mhw", "ops", "kerchunk", "update"]
3 changes: 3 additions & 0 deletions toolkit/cli/applications/kerchunk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from cli.applications.kerchunk.run import run

__all__ = ["run"]
File renamed without changes.
4 changes: 4 additions & 0 deletions toolkit/cli/applications/mhw/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from cli.applications.mhw.start import start
from cli.applications.mhw.thresholds import create_thresholds

__all__ = ["start", "create_thresholds"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cli.modules.mhw.thresholds import create_thresholds
from cli.applications.mhw.thresholds import create_thresholds


def start(args):
create_thresholds(args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from os.path import join
from config import CACHDIR
from cli.modules.mhw.thresholds.catalogue import update_cache
from cli.modules.mhw.thresholds.climatology import calculate_daily_clim
from cli.modules.mhw.thresholds.climatology import calculate_mhw
from cli.applications.mhw.thresholds.catalogue import update_cache
from cli.applications.mhw.thresholds.climatology import calculate_daily_clim
from cli.applications.mhw.thresholds.climatology import calculate_mhw
from oisst import Catalogue

MHW_BULK_CACHE = join(CACHDIR, "ohw", "thresholds", "bulk")
Expand Down
3 changes: 3 additions & 0 deletions toolkit/cli/applications/mhw/thresholds/catalogue/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from cli.applications.mhw.thresholds.catalogue.update_cache import update_cache

__all__ = ["update_cache"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from cli.applications.mhw.thresholds.climatology.daily import calculate_daily_clim
from cli.applications.mhw.thresholds.climatology.define_mhw import calculate_mhw

__all__ = ["calculate_daily_clim", "calculate_mhw"]
5 changes: 5 additions & 0 deletions toolkit/cli/applications/ops/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from cli.applications.ops.download import download
from cli.applications.ops.transform import transform
from cli.applications.ops.load import load

__all__ = ["download", "transform", "load"]
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from datetime import datetime, timedelta
from pathlib import Path
from cli.modules.ops.download.mercator import download as mercator_download
from cli.modules.ops.download.gfs import download as gfs_download
from cli.applications.ops.download.mercator import download as mercator_download
from cli.applications.ops.download.gfs import download as gfs_download


def download(args):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
from datetime import datetime, timedelta, time
from cli.modules.ops.download.gfs.functions import (
from cli.applications.ops.download.gfs.functions import (
download_file,
get_latest_available_dt,
set_params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from datetime import datetime
from postgis import setup as installDb, drop as dropSchema, pool
from config import PY_ENV
from cli.modules.ops.load.raster2pgsql import register as refresh_rasters
from cli.modules.ops.load.coordinates import upsert as refresh_coordinates
from cli.modules.ops.load.values import upsert as refresh_values
from cli.modules.ops.load.finalize import run as finalize
from cli.applications.ops.load.raster2pgsql import register as refresh_rasters
from cli.applications.ops.load.coordinates import upsert as refresh_coordinates
from cli.applications.ops.load.values import upsert as refresh_values
from cli.applications.ops.load.finalize import run as finalize


def load(args):
start_time = datetime.now()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cli.modules.ops.load.values.load_band import load
from cli.applications.ops.load.values.load_band import load


def upsert(runid, start_time, depths, datetimes, total_depth_levels):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import numpy as np
import os
from datetime import timedelta, datetime
from cli.modules.ops.transform.depth_functions import z_levels
from cli.modules.ops.transform.functions import hour_rounder, u2rho_4d, v2rho_4d
from cli.applications.ops.transform.depth_functions import z_levels
from cli.applications.ops.transform.functions import hour_rounder, u2rho_4d, v2rho_4d
import subprocess

# All dates in the CROCO output are represented
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions toolkit/cli/commands/__init__.py

This file was deleted.

3 changes: 3 additions & 0 deletions toolkit/cli/define/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from cli.define import lacce, mhw, ops, kerchunk, update

__all__ = ["lacce", "mhw", "ops", "kerchunk", "update"]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions toolkit/cli/exe/kerchunk/__init__.py

This file was deleted.

7 changes: 0 additions & 7 deletions toolkit/cli/exe/mhw/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions toolkit/cli/modules/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions toolkit/cli/modules/kerchunk/__init__.py

This file was deleted.

4 changes: 0 additions & 4 deletions toolkit/cli/modules/mhw/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions toolkit/cli/modules/mhw/thresholds/catalogue/__init__.py

This file was deleted.

4 changes: 0 additions & 4 deletions toolkit/cli/modules/mhw/thresholds/climatology/__init__.py

This file was deleted.

5 changes: 0 additions & 5 deletions toolkit/cli/modules/ops/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from cli.exe import lacce, mhw, ops, kerchunk, update
from cli.parse import lacce, mhw, ops, kerchunk, update

__all__ = ["lacce", "mhw", "ops", "kerchunk", "update"]
5 changes: 5 additions & 0 deletions toolkit/cli/parse/kerchunk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def parse(cmd, args, module):
if args.kerchunk_command == "run":
return module.run
else:
print(cmd.format_help())
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def run(cmd, args):
def parse(cmd, args, module):
input = args.input
output = args.output
if input and output:
print("LACCE", input, output)
return
return lambda: print("Not implemented")
print(cmd.format_help())
5 changes: 5 additions & 0 deletions toolkit/cli/parse/mhw/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def parse(cmd, args, module):
if args.mhw_command == "start":
return module.start
else:
print(cmd.format_help())
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from cli.modules.ops import download, load, transform


def run(cmd, args):
def parse(cmd, args, module):
if args.ops_command == "download":
download(args)
return module.download
elif args.ops_command == "transform":
transform(args)
return module.transform
elif args.ops_command == "load":
load(args)
return module.load
else:
print(cmd.format_help())
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from cli.modules.update.update_bashrc import update_bashrc


def run(cmd, args):
def run(cmd, args, module):
version = args.version
reset = args.reset
if reset and version:
raise Exception("Please specify either reset or version flags")

if version or reset:
update_bashrc(args)
return
return module.update_bashrc
print(cmd.format_help())

0 comments on commit 029892f

Please sign in to comment.