11"""The main CLI entrypoint and commands."""
22import sys
3- import time
4- import webbrowser
5- from pathlib import Path
63from typing import Any , Optional
74
85from pluggy import PluginManager
96
107from pyscript import __version__ , app , console , plugins , typer
11- from pyscript ._generator import file_to_html , string_to_html
128from pyscript .plugins import hookspecs
139
14- DEFAULT_PLUGINS = ["create" , "delete " ]
10+ DEFAULT_PLUGINS = ["create" , "wrap " ]
1511
1612
17- def _print_version ():
18- console .print (f"PyScript CLI version: { __version__ } " , style = "bold green" )
13+ class Abort (typer .Abort ):
14+ """
15+ Abort with a consistent error message.
16+ """
17+
18+ def __init__ (self , msg : str , * args : Any , ** kwargs : Any ):
19+ console .print (msg , style = "red" )
20+ super ().__init__ (* args , ** kwargs )
1921
2022
2123@app .callback (invoke_without_command = True , no_args_is_help = True )
@@ -24,106 +26,28 @@ def main(
2426 None , "--version" , help = "Show project version and exit."
2527 )
2628):
27- """Command Line Interface for PyScript."""
29+ """
30+ Command Line Interface for PyScript.
31+ """
2832 if version :
29- _print_version ( )
33+ console . print ( f"PyScript CLI version: { __version__ } " , style = "bold green" )
3034 raise typer .Exit ()
3135
3236
33- @app .command ()
34- def version () -> None :
35- """Show project version and exit."""
36- _print_version ()
37-
38-
39- _input_file_argument = typer .Argument (
40- None ,
41- help = "An optional path to the input .py script. If not provided, must use '-c' flag." ,
42- )
43- _output_file_option = typer .Option (
44- None ,
45- "-o" ,
46- "--output" ,
47- help = "Path to the resulting HTML output file. Defaults to input_file with suffix replaced." ,
48- )
49- _command_option = typer .Option (
50- None , "-c" , "--command" , help = "If provided, embed a single command string."
51- )
52- _show_option = typer .Option (None , help = "Open output file in web browser." )
53- _title_option = typer .Option (None , help = "Add title to HTML file." )
54-
55-
56- class Abort (typer .Abort ):
57- def __init__ (self , msg : str , * args : Any , ** kwargs : Any ):
58- console .print (msg , style = "red" )
59- super ().__init__ (* args , ** kwargs )
60-
61-
62- @app .command ()
63- def wrap (
64- input_file : Optional [Path ] = _input_file_argument ,
65- output : Optional [Path ] = _output_file_option ,
66- command : Optional [str ] = _command_option ,
67- show : Optional [bool ] = _show_option ,
68- title : Optional [str ] = _title_option ,
69- ) -> None :
70- """Wrap a Python script inside an HTML file."""
71- title = title or "PyScript App"
72-
73- if not input_file and not command :
74- raise Abort (
75- "Must provide either an input '.py' file or a command with the '-c' option."
76- )
77- if input_file and command :
78- raise Abort ("Cannot provide both an input '.py' file and '-c' option." )
79-
80- # Derive the output path if it is not provided
81- remove_output = False
82- if output is None :
83- if command and show :
84- output = Path ("pyscript_tmp.html" )
85- remove_output = True
86- elif not command :
87- assert input_file is not None
88- output = input_file .with_suffix (".html" )
89- else :
90- raise Abort ("Must provide an output file or use `--show` option" )
91-
92- if input_file is not None :
93- file_to_html (input_file , title , output )
94-
95- if command :
96- string_to_html (command , title , output )
97-
98- assert output is not None
99-
100- if show :
101- console .print ("Opening in web browser!" )
102- webbrowser .open (f"file://{ output .resolve ()} " )
103-
104- if remove_output :
105- time .sleep (1 )
106- output .unlink ()
107-
108-
10937pm = PluginManager ("pyscript" )
11038
11139pm .add_hookspecs (hookspecs )
112-
11340for modname in DEFAULT_PLUGINS :
11441 importspec = f"pyscript.plugins.{ modname } "
115-
11642 try :
11743 __import__ (importspec )
11844 except ImportError as e :
11945 raise ImportError (
12046 f'Error importing plugin "{ modname } ": { e .args [0 ]} '
12147 ).with_traceback (e .__traceback__ ) from e
122-
12348 else :
12449 mod = sys .modules [importspec ]
12550 pm .register (mod , modname )
126-
12751 loaded = pm .load_setuptools_entrypoints ("pyscript" )
12852
12953for cmd in pm .hook .pyscript_subcommand ():
0 commit comments