-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcli.py
More file actions
75 lines (56 loc) · 1.99 KB
/
cli.py
File metadata and controls
75 lines (56 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""The main CLI entrypoint and commands."""
import sys
from typing import Any, Optional
from pluggy import PluginManager
from pyscript import __version__, app, console, plugins, typer
from pyscript.plugins import hookspecs
DEFAULT_PLUGINS = ["create", "run"]
def ok(msg: str = ""):
"""
Simply prints "OK" and an optional message, to the console, before cleanly
exiting.
Provides a standard way to end/confirm a successful command.
"""
console.print(f"OK. {msg}".rstrip(), style="green")
raise typer.Exit()
class Abort(typer.Abort):
"""
Abort with a consistent error message.
"""
def __init__(self, msg: str, *args: Any, **kwargs: Any):
console.print(msg, style="red")
super().__init__(*args, **kwargs)
@app.callback(invoke_without_command=True, no_args_is_help=True)
def main(
version: Optional[bool] = typer.Option(
None, "--version", help="Show project version and exit."
)
):
"""
Command Line Interface for PyScript.
"""
if version:
console.print(f"PyScript CLI version: {__version__}", style="bold green")
raise typer.Exit()
# Create the default PluginManager
pm = PluginManager("pyscript")
# Register the hooks specifications available for PyScript Plugins
pm.add_hookspecs(hookspecs)
# Register the default plugins available with the bare pyscript cli installation
for modname in DEFAULT_PLUGINS:
importspec = f"pyscript.plugins.{modname}"
try:
__import__(importspec)
except ImportError as e:
raise ImportError(
f'Error importing plugin "{modname}": {e.args[0]}'
).with_traceback(e.__traceback__) from e
else:
mod = sys.modules[importspec]
pm.register(mod, modname)
# Load plugins registered via setuptools entrypoints
loaded = pm.load_setuptools_entrypoints("pyscript")
# Register the commands from plugins that have been loaded and used the
# `pyscript_subcommand` hook.
for cmd in pm.hook.pyscript_subcommand():
plugins._add_cmd(cmd)