Commands¶
High-level command objects that back the CLI verbs. They can also be used directly as library entry points when you want terminal-formatted output without calling the CLI.
Fetcher¶
Bases: HTTPClient
Fetch a URL and print the parsed response to the terminal.
Inherits from :class:~go2web.http.client.HTTPClient so all constructor
arguments (cache, use_cache) are available.
The response body is parsed by the appropriate
:class:~go2web.http.parsers.abstract_parser.Parser for its content type,
then displayed inside a styled Rich panel. Errors are caught and printed
as red error panels rather than propagating as exceptions.
Example
from go2web.commands.fetch import Fetcher fetcher = Fetcher(use_cache=False) text = fetcher.fetch("https://example.com")
Source code in src/go2web/commands/fetch.py
fetch(url)
¶
Fetch url, parse the body, and print it to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to fetch. A missing |
required |
Returns:
| Type | Description |
|---|---|
str
|
The parsed body string on success, or the error message string |
str
|
when an :class: |
str
|
class: |
Source code in src/go2web/commands/fetch.py
Searcher¶
Search the web and interactively open a result.
Combines a :class:~go2web.search.engines.base.BaseSearchEngine with a
:class:~go2web.commands.fetch.Fetcher to implement the full
go2web search workflow:
- Query the search engine.
- Present results in a
questionaryinteractive picker. - Fetch and display the selected result.
The engine and fetcher are injected so they can be swapped in tests or extended for custom workflows.
Example
Using the default Bing engine:
from go2web.commands.search import Searcher Searcher().search("python packaging")
Using a custom engine and disabling the fetcher cache:
from go2web.commands.fetch import Fetcher from go2web.search.engines.bing import BingEngine Searcher(engine=BingEngine(), fetcher=Fetcher(use_cache=False)).search("python")
Source code in src/go2web/commands/search.py
__init__(engine=None, fetcher=None, limit=10)
¶
Initialise the searcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
BaseSearchEngine | None
|
The search engine backend to use. Defaults to
:class: |
None
|
fetcher
|
Fetcher | None
|
The fetcher used to open the selected result. Defaults to
a :class: |
None
|
limit
|
int
|
Number of results to request from the engine (default |
10
|
Source code in src/go2web/commands/search.py
search(query, limit=None)
¶
Run a search for query and open the chosen result.
Prints an interactive picker to the terminal. If the user selects a
result, it is fetched and displayed via :meth:~go2web.commands.fetch.Fetcher.fetch.
Choosing Cancel exits without fetching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The search query string. |
required |
limit
|
int | None
|
Override the instance-level result limit for this call. |
None
|