Skip to content

Commit

Permalink
pio cal list is now defined
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Jan 5, 2025
1 parent 7003e2a commit 3f58496
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
8 changes: 8 additions & 0 deletions pioreactor/calibrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ def list_of_calibrations_by_device(device: str) -> list[str]:
return []

return [file.stem for file in calibration_dir.glob("*.yaml")]


def list_devices() -> list[str]:
calibration_dir = CALIBRATION_PATH
if not calibration_dir.exists():
return []

return [f.name for f in calibration_dir.iterdir() if f.is_dir()]
21 changes: 16 additions & 5 deletions pioreactor/cli/calibrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pioreactor import structs
from pioreactor.calibrations import CALIBRATION_PATH
from pioreactor.calibrations import calibration_protocols
from pioreactor.calibrations import list_devices
from pioreactor.calibrations import list_of_calibrations_by_device
from pioreactor.calibrations import load_calibration
from pioreactor.calibrations.utils import curve_to_callable
Expand All @@ -24,17 +25,27 @@ def calibration() -> None:


@calibration.command(name="list")
@click.option("--device", required=True)
def list_calibrations(device: str) -> None:
@click.option("--device", required=False)
def list_calibrations(device: str | None) -> None:
"""
List existing calibrations for the given device.
List existing calibrations for the given device if provided, else all.
"""
if device is None:
for device in list_devices():
_display_calibrations_by_device(device)
click.echo()
click.echo()
else:
_display_calibrations_by_device(device)


def _display_calibrations_by_device(device: str) -> None:
calibration_dir = CALIBRATION_PATH / device
if not calibration_dir.exists():
click.echo(f"No calibrations found for device '{device}'. Directory does not exist.")
raise click.Abort()

header = f"{'Name':<50}{'Created At':<25}{'Active?':<10}{'Location':<75}"
header = f"{'Device':<25}{'Name':<50}{'Created At':<25}{'Active?':<10}{'Location':<75}"
click.echo(header)
click.echo("-" * len(header))

Expand All @@ -46,7 +57,7 @@ def list_calibrations(device: str) -> None:
location.read_bytes(), type=structs.subclass_union(structs.CalibrationBase)
)
active = c.get(device) == data.calibration_name
row = f"{data.calibration_name:<50}{data.created_at.strftime('%Y-%m-%d %H:%M:%S'):<25}{'✅' if active else '':<10}{location}"
row = f"{device:<25}{data.calibration_name:<50}{data.created_at.strftime('%Y-%m-%d %H:%M:%S'):<25}{'✅' if active else '':<10}{location}"
click.echo(row)
except Exception as e:
error_message = f"Error reading {name}: {e}"
Expand Down

0 comments on commit 3f58496

Please sign in to comment.