|
1 | 1 | """The main CLI entrypoint and commands."""
|
| 2 | +import time |
2 | 3 | import webbrowser
|
3 | 4 | from pathlib import Path
|
4 | 5 | from typing import Any, Optional
|
@@ -69,18 +70,38 @@ def wrap(
|
69 | 70 | show: Optional[bool] = _show_option,
|
70 | 71 | ) -> None:
|
71 | 72 | """Wrap a Python script inside an HTML file."""
|
| 73 | + |
| 74 | + if not input_file and not command: |
| 75 | + raise Abort( |
| 76 | + "Must provide either an input '.py' file or a command with the '-c' option." |
| 77 | + ) |
| 78 | + if input_file and command: |
| 79 | + raise Abort("Cannot provide both an input '.py' file and '-c' option.") |
| 80 | + |
| 81 | + # Derive the output path if it is not provided |
| 82 | + remove_output = False |
| 83 | + if output is None: |
| 84 | + if command and show: |
| 85 | + output = Path("pyscript_tmp.html") |
| 86 | + remove_output = True |
| 87 | + elif not command: |
| 88 | + assert input_file is not None |
| 89 | + output = input_file.with_suffix(".html") |
| 90 | + else: |
| 91 | + raise Abort("Must provide an output file or use `--show` option") |
| 92 | + |
72 | 93 | if input_file is not None:
|
73 |
| - if command is not None: |
74 |
| - raise Abort("Cannot provide both an input file and `-c` option.") |
75 | 94 | file_to_html(input_file, output)
|
76 | 95 |
|
77 | 96 | if command:
|
78 |
| - if output is None: |
79 |
| - raise Abort("Must provide an output file or use `--show` option") |
80 | 97 | string_to_html(command, output)
|
81 | 98 |
|
| 99 | + assert output is not None |
| 100 | + |
82 | 101 | if show:
|
83 |
| - if output is None: |
84 |
| - raise Abort("Must provide an output file to use `--show` option") |
85 | 102 | console.print("Opening in web browser!")
|
86 | 103 | webbrowser.open(f"file://{output.resolve()}")
|
| 104 | + |
| 105 | + if remove_output: |
| 106 | + time.sleep(1) |
| 107 | + output.unlink() |
0 commit comments