Skip to content

Commit

Permalink
Rename "main" typer CLI app to "app"
Browse files Browse the repository at this point in the history
This will make it possible to customise the logging behaviour when
the code is being run as a CLI / script / app (vs a library).
  • Loading branch information
benthorner authored and avaldebe committed Jan 11, 2023
1 parent 7c6c41b commit 90c4d6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
19 changes: 11 additions & 8 deletions src/pms/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@
import importlib_metadata as metadata

from loguru import logger
from typer import Argument, Context, Exit, Option, Typer, echo
from typer import Argument, Context, Exit, Option, Typer, echo, run

from pms.core import MessageReader, SensorReader, Supported, exit_on_fail

main = Typer(help="Data acquisition and logging for Air Quality Sensors with UART interface")

app = Typer(help="Data acquisition and logging for Air Quality Sensors with UART interface")

"""
Extra cli commands from plugins
additional Typer commands are loaded from plugins (entry points) advertized as `"pypms.extras"`
"""
for ep in metadata.entry_points(group="pypms.extras"):
main.command(name=ep.name)(ep.load())
app.command(name=ep.name)(ep.load())


def main():
app()


def version_callback(value: bool): # pragma: no cover
Expand All @@ -35,7 +38,7 @@ def version_callback(value: bool): # pragma: no cover
raise Exit()


@main.callback()
@app.callback()
def callback(
ctx: Context,
model: Supported = Option(Supported.default, "--sensor-model", "-m", help="sensor model"),
Expand All @@ -60,7 +63,7 @@ def callback(
ctx.obj = {"reader": SensorReader(model, port, seconds, samples)}


@main.command()
@app.command()
def info(ctx: Context): # pragma: no cover
"""Information about the sensor observations"""
sensor = ctx.obj["reader"].sensor
Expand All @@ -84,7 +87,7 @@ def __str__(self) -> str:
return self.value


@main.command()
@app.command()
def serial(
ctx: Context,
format: Optional[Format] = Option(None, "--format", "-f", help="formatted output"),
Expand All @@ -111,7 +114,7 @@ def serial(
echo(str(obs))


@main.command()
@app.command()
def csv(
ctx: Context,
capture: bool = Option(False, "--capture", help="write raw messages instead of observations"),
Expand Down
12 changes: 6 additions & 6 deletions tests/extra/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def client_sub(

def test_mqtt(capture, mock_mqtt):

from pms.cli import main
from pms.cli import app

result = runner.invoke(main, capture.options("mqtt"))
result = runner.invoke(app, capture.options("mqtt"))
assert result.exit_code == 0


Expand All @@ -62,9 +62,9 @@ def pub(*, time: int, tags: Dict[str, str], data: Dict[str, float]) -> None:

def test_influxdb(capture, mock_influxdb):

from pms.cli import main
from pms.cli import app

result = runner.invoke(main, capture.options("influxdb"))
result = runner.invoke(app, capture.options("influxdb"))
assert result.exit_code == 0


Expand Down Expand Up @@ -110,8 +110,8 @@ def client_sub(

def test_bridge(mock_bridge):

from pms.cli import main
from pms.cli import app

capture = mock_bridge
result = runner.invoke(main, capture.options("bridge"))
result = runner.invoke(app, capture.options("bridge"))
assert result.exit_code == 0
14 changes: 7 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
@pytest.mark.parametrize("format", {"csv", "hexdump"})
def test_serial(capture, format):

from pms.cli import main
from pms.cli import app

result = runner.invoke(main, capture.options(f"serial_{format}"))
result = runner.invoke(app, capture.options(f"serial_{format}"))
assert result.exit_code == 0
assert result.stdout == capture.output(format)


def test_csv(capture):

from pms.cli import main
from pms.cli import app

result = runner.invoke(main, capture.options("csv"))
result = runner.invoke(app, capture.options("csv"))
assert result.exit_code == 0

csv = Path(capture.options("csv")[-1])
Expand All @@ -31,15 +31,15 @@ def test_csv(capture):

def test_capture_decode(capture):

from pms.cli import main
from pms.cli import app

result = runner.invoke(main, capture.options("capture"))
result = runner.invoke(app, capture.options("capture"))
assert result.exit_code == 0

csv = Path(capture.options("capture")[-1])
assert csv.exists()

result = runner.invoke(main, capture.options("decode"))
result = runner.invoke(app, capture.options("decode"))
assert result.exit_code == 0
csv.unlink()
assert result.stdout == capture.output("csv")

0 comments on commit 90c4d6c

Please sign in to comment.