-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CLI interface, with config file
- Loading branch information
1 parent
96679f5
commit 4f7717f
Showing
11 changed files
with
350 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
""" | ||
Create video and image files via TerminHTML rather than HTML/CSS/JS | ||
""" | ||
from terminhtml_recorder.recorder import TerminHTMLRecorder | ||
from terminhtml_recorder.formats import OutputFormat | ||
from terminhtml_recorder.recorder import ( | ||
PageLocators, | ||
TerminHTMLRecorder, | ||
default_page_interactor, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from terminhtml_recorder.cli import cli | ||
|
||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import os | ||
import sys | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import typer | ||
from cliconf import CLIAppConfig, CLIConf, configure | ||
from pyappconf import ConfigFormats | ||
|
||
from terminhtml_recorder.exc import NoHTMLFoundException | ||
from terminhtml_recorder.formats import OutputFormat | ||
from terminhtml_recorder.recorder import ( | ||
PageInteractor, | ||
TerminHTMLRecorder, | ||
default_page_interactor, | ||
) | ||
|
||
cli = CLIConf( | ||
name="terminhtml-recorder", | ||
help="Record TerminHTML animated output to a video file.", | ||
) | ||
|
||
|
||
@cli.command() | ||
@configure( | ||
CLIAppConfig( | ||
app_name="terminhtml-recorder", | ||
config_name="terminrec", | ||
default_format=ConfigFormats.PY, | ||
py_config_imports=[ | ||
"from pathlib import Path", | ||
"from terminhtml_recorder import PageLocators, default_page_interactor, OutputFormat", | ||
"import terminhtml_recorder.recorder", | ||
"from terminhtml_recorder.cli import record", | ||
], | ||
) | ||
) | ||
def record( | ||
in_path: Optional[Path] = typer.Option( | ||
None, | ||
"--input-path", | ||
"-i", | ||
help="Path of file containing TerminHTML output. If not passed, must be piped to stdin.", | ||
), | ||
out_path: Optional[Path] = typer.Option( | ||
None, | ||
"--output-path", | ||
"-o", | ||
help="Path of file to write recording to. Defaults to terminhtml.<output-format>", | ||
show_default=False, | ||
), | ||
output_format: Optional[OutputFormat] = typer.Option( | ||
OutputFormat.GIF, "--format", "-f", help="Format of recording" | ||
), | ||
delay: float = typer.Option( | ||
1.1, | ||
"--delay", | ||
"-d", | ||
help="Delay from browser start until recording start. Increase if you see an uninitialized terminal at the start. Decrease if you don't see the beginning of typing.", | ||
), | ||
interactor: PageInteractor = default_page_interactor, | ||
): | ||
""" | ||
Record TerminHTML animated output to a video file. | ||
""" | ||
if in_path is not None: | ||
html = in_path.read_text() | ||
# Else if input was piped to stdin | ||
elif not sys.stdin.isatty() and not os.getenv("PYCHARM_HOSTED"): | ||
# Read HTML from stdin | ||
html = sys.stdin.read() | ||
else: | ||
raise NoHTMLFoundException( | ||
f"No HTML passed. Must either specify an input file or pipe HTML to stdin." | ||
) | ||
|
||
if not html: | ||
raise NoHTMLFoundException( | ||
f"Empty HTML found. Must either specify a valid input file or pipe HTML to stdin." | ||
) | ||
|
||
out_path = out_path or Path("terminhtml").with_suffix(f".{output_format.value}") | ||
|
||
recorder = TerminHTMLRecorder(html, interactor) | ||
recorder.record(out_path, format=output_format, delay=delay) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class TerminHTMLRecorderException(Exception): | ||
pass | ||
|
||
|
||
class NoHTMLFoundException(TerminHTMLRecorderException): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.