pythonlib: Add "test" CLI to open Playwright inspector

This commit is contained in:
daijro 2024-09-19 16:33:41 -05:00
parent 4eb559f043
commit f45852c579
2 changed files with 45 additions and 7 deletions

View file

@ -1,9 +1,12 @@
<div align="center">
# Camoufox Python Interface
#### Lightweight wrapper around the Playwright API to help launch Camoufox.
> [!WARNING]
> This is still experimental and in active development!
</div>
---
## Installation
@ -29,6 +32,24 @@ python3 -m camoufox fetch
To uninstall, run `camoufox remove`.
<details>
<summary>CLI options</summary>
```
Usage: python -m camoufox [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
fetch Fetch the latest version of Camoufox
remove Remove all downloaded files
test Open the Playwright inspector
version Display the current version
```
</details>
<hr width=50>
## Usage

View file

@ -1,11 +1,12 @@
"""
Binary CLI manager for Camoufox.
CLI package manager for Camoufox.
Adapted from https://github.com/daijro/hrequests/blob/main/hrequests/__main__.py
"""
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as pkg_version
from typing import Optional
import click
@ -22,12 +23,13 @@ class CamoufoxUpdate(CamoufoxFetcher):
Initializes the CamoufoxUpdate class
"""
super().__init__()
self.current_verstr: Optional[str]
try:
self.current_verstr = installed_verstr()
except FileNotFoundError:
self.current_verstr = None
def is_updated_needed(self) -> None:
def is_updated_needed(self) -> bool:
if self.current_verstr is None:
return True
# If the installed version is not the latest version
@ -37,7 +39,7 @@ class CamoufoxUpdate(CamoufoxFetcher):
def update(self) -> None:
"""
Updates the library if needed
Updates Camoufox if needed
"""
# Check if the version is the same as the latest available version
if not self.is_updated_needed():
@ -74,12 +76,27 @@ def fetch() -> None:
@cli.command(name='remove')
def remove() -> None:
"""
Remove all library files
Remove all downloaded files
"""
if not CamoufoxUpdate().cleanup():
rprint("Camoufox binaries not found!", fg="red")
@cli.command(name='test')
@click.argument('url', default=None, required=False)
def test(url: Optional[str] = None) -> None:
"""
Open the Playwright inspector
"""
from .sync_api import Camoufox
with Camoufox(headless=False) as browser:
page = browser.new_page()
if url:
page.goto(url)
page.pause() # Open the Playwright inspector
@cli.command(name='version')
def version() -> None:
"""
@ -101,7 +118,7 @@ def version() -> None:
# Print the base version
rprint(f"Camoufox:\tv{bin_ver} ", fg="green", nl=False)
# Check for library updates
# Check for Camoufox updates
if updater.is_updated_needed():
rprint(f"(Latest: v{updater.verstr})", fg="red")
else: