Skip to content

Commit

Permalink
Initial commit for CLI scaffolding. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewoldag authored Aug 8, 2024
1 parent cf6aade commit 2d5c9a5
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ requires-python = ">=3.9"
dependencies = [
]

[project.scripts]
fibad = "fibad_cli.main:main"
fibad-train = "fibad_cli.train:main"
fibad-predict = "fibad_cli.predict:main"

[project.urls]
"Source Code" = "https://github.com/lincc-frameworks/fibad"

Expand Down
14 changes: 14 additions & 0 deletions src/fibad/predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Scaffolding placeholder for prediction code."""


def run(args):
"""Placeholder for prediction code.
Parameters
----------
args : argparse.Namespace
The parsed command line arguments.
"""

print("Prending to run prediction...")
print(f"Runtime config: {args.runtime_config}")
14 changes: 14 additions & 0 deletions src/fibad/train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Scaffolding placeholder for training code."""


def run(args):
"""Placeholder for training code.
Parameters
----------
args : argparse.Namespace
The parsed command line arguments.
"""

print("Prending to run training...")
print(f"Runtime config: {args.runtime_config}")
53 changes: 53 additions & 0 deletions src/fibad_cli/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import argparse
import shutil
import subprocess
import sys
from importlib.metadata import version


def main():
"""Primary entry point for the Fibad CLI. This handles dispatching to the various
Fibad actions. The actions are defined in the pyproject.toml project.scripts
section.
"""

description = "Fibad CLI"
epilog = "FIBAD is the Framework for Image-Based Anomaly Detection"

#! We could potentially make this dynamic
fibad_verbs = ["train", "predict"]

parser = argparse.ArgumentParser(description=description, epilog=epilog)
parser.add_argument("--version", dest="version", action="store_true", help="Show version")
parser.add_argument("verb", nargs="?", choices=fibad_verbs, help="Verb to execute")
parser.add_argument("args", nargs=argparse.REMAINDER, help="Arguments for the verb")

args = parser.parse_args()

if args.version:
print(version("fibad"))
return

if not args.verb:
parser.print_help()
sys.exit(1)

fibad_action = f"fibad-{args.verb}"

# Ensure the action is available
if not shutil.which(fibad_action):
print(f"Error: '{fibad_action}' is not available.")
print("Is the action defined in the pyproject.toml project.scripts section?")
sys.exit(1)

# Execute the action with the remaining arguments
try:
result = subprocess.run([fibad_action] + args.args, check=True)
sys.exit(result.returncode)
except subprocess.CalledProcessError as e:
print(f"Error: Command '{fibad_action}' failed with exit code {e.returncode}.")
sys.exit(e.returncode)


if __name__ == "__main__":
main()
27 changes: 27 additions & 0 deletions src/fibad_cli/predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import argparse


def main():
"""Argument parser to process command line arguments when predicting with Fibad."""

parser = argparse.ArgumentParser(description="Predicting with Fibad.")

parser.add_argument("-c", "--runtime-config", type=str, help="Full path to runtime config file")

args = parser.parse_args()

run(args)


def run(args):
"""Note: Don't import anything from Fibad outside of this run function.
Keeping all the imports inside the run function ensures that the --help option
returns quickly without loading all the dependencies.
"""
from fibad.predict import run

run(args)


if __name__ == "__main__":
main()
27 changes: 27 additions & 0 deletions src/fibad_cli/train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import argparse


def main():
"""Argument parser to process command line arguments when training with Fibad."""

parser = argparse.ArgumentParser(description="Training with Fibad.")

parser.add_argument("-c", "--runtime-config", type=str, help="Full path to runtime config file")

args = parser.parse_args()

run(args)


def run(args):
"""Note: Don't import anything from Fibad outside of this run function.
Keeping all the imports inside the run function ensures that the --help option
returns quickly without loading all the dependencies.
"""
from fibad.train import run

run(args)


if __name__ == "__main__":
main()

0 comments on commit 2d5c9a5

Please sign in to comment.