overall changes
This commit is contained in:
+144
@@ -0,0 +1,144 @@
|
||||
---
|
||||
title: The Anatomy of a Scott application
|
||||
---
|
||||
|
||||
## Anatomy of a Scott application
|
||||
|
||||
A Scott application is a plain Python script (the *entry point*) plus a `commands/` directory of
|
||||
command packages, laid out like this:
|
||||
|
||||
```
|
||||
scott-app/
|
||||
├── main.py # entry point script
|
||||
└── commands/
|
||||
├── cmd1/
|
||||
│ └── handler.py # implements the `cmd1` command
|
||||
├── cmd2/
|
||||
│ └── handler.py # implements the `cmd2` command
|
||||
└── cmd3/
|
||||
└── handler.py # implements the `cmd3` command
|
||||
```
|
||||
|
||||
Each command name maps to `commands/<name>/handler.py`. The entry point registers the
|
||||
commands it wants to expose, and Scott imports and dispatches them dynamically.
|
||||
|
||||
### The entry point
|
||||
|
||||
For the above example this would be the `main.py` script. Typically, this would look like:
|
||||
|
||||
```python
|
||||
from scott import Scott
|
||||
|
||||
def main():
|
||||
# app_dir: path of this script — Scott resolves it to locate the tools/ directory
|
||||
# app_desc: description shown in the tool's help output
|
||||
scott = Scott(__file__, "A toolset to do something")
|
||||
|
||||
commands = [
|
||||
"cmd1",
|
||||
"cmd2",
|
||||
"cmd3",
|
||||
]
|
||||
for cmd in commands:
|
||||
scott.register(cmd)
|
||||
scott.run()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
```
|
||||
|
||||
Running the tool:
|
||||
|
||||
```sh
|
||||
$ python main.py --help
|
||||
usage: main.py [-h] {cmd1,cmd2,cmd3} ...
|
||||
|
||||
A toolset to do something
|
||||
|
||||
positional arguments:
|
||||
{cmd1,cmd2,cmd3}
|
||||
cmd1 description for cmd1
|
||||
cmd2 description for cmd2
|
||||
cmd3 description for cmd3
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
```
|
||||
|
||||
## The command contract
|
||||
|
||||
Every command module (`commands/<name>/handler.py`) is a plain Python module. Scott recognizes the
|
||||
following functions within `handler.py`:
|
||||
|
||||
| Function | Required | Purpose |
|
||||
| --- | --- | --- |
|
||||
| `s_run(args)` | **Yes** | The command's entry point. Called with the parsed `argparse` namespace. |
|
||||
| `s_description()` | Optional | Returns a short help string shown in the command list. |
|
||||
| `s_arguments(parser)` | Optional | Registers command-specific options on the command's subparser. |
|
||||
|
||||
__`s_run(args)` — required__
|
||||
|
||||
The command's main function. It **must accept exactly one argument** (the parsed `argparse`
|
||||
namespace); registration is skipped with a warning otherwise.
|
||||
|
||||
```python
|
||||
def s_run(args):
|
||||
# args contains any options registered via scott_arguments(),
|
||||
# plus args.command (the subcommand name)
|
||||
print("command: cmd1")
|
||||
```
|
||||
|
||||
__`s_description()` — optional__
|
||||
|
||||
Returns a one-line description used as the command's help text in the parent help listing.
|
||||
|
||||
```python
|
||||
def s_description():
|
||||
return "description for cmd1"
|
||||
```
|
||||
|
||||
__`s_arguments(parser)` — optional__
|
||||
|
||||
Receives the command's `argparse` subparser so the command can define its own positional arguments
|
||||
and options. They arrive in `args` at dispatch time.
|
||||
|
||||
```python
|
||||
def s_arguments(parser):
|
||||
parser.add_argument(
|
||||
"posarg",
|
||||
help="The positional argument."
|
||||
)
|
||||
```
|
||||
|
||||
A complete command module:
|
||||
|
||||
```python
|
||||
# commands/cmd1/handler.py
|
||||
|
||||
def s_description():
|
||||
return "description for cmd1"
|
||||
|
||||
def s_arguments(parser):
|
||||
parser.add_argument(
|
||||
"posarg",
|
||||
help="The positional argument."
|
||||
)
|
||||
|
||||
def s_run(args):
|
||||
print(f"command: cmd1, posarg: {args.posarg}")
|
||||
```
|
||||
|
||||
Which produces:
|
||||
|
||||
```sh
|
||||
$ python main.py cmd1 --help
|
||||
usage: main.py cmd1 [-h] posarg
|
||||
|
||||
positional arguments:
|
||||
posarg The positional argument.
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: Scott API Reference
|
||||
---
|
||||
|
||||
# Scott API Reference
|
||||
|
||||
__`Scott(app_dir: str, app_desc: str = "")`__
|
||||
|
||||
Creates the framework and its argument parser.
|
||||
|
||||
- `app_dir` — typically `__file__` from the entry script. Scott resolves it to the containing
|
||||
directory, which is used to (a) locate the `commands/` packages and (b) is prepended to `sys.path`
|
||||
so the tool works no matter which directory you invoke it from.
|
||||
|
||||
- `app_desc` — description shown at the top of the tool's help output.
|
||||
|
||||
__`Scott.register(cmd_name: str)`__
|
||||
|
||||
Registers a command by dynamically importing `commands.<cmd_name>.handler`. During registration Scott:
|
||||
|
||||
1. Imports the command module; if it cannot be imported, prints a warning to *stderr* and skips
|
||||
the command.
|
||||
|
||||
2. Verifies `s_run` method exists, is callable, and takes **exactly one** parameter; otherwise warns
|
||||
to *stderr* and skips the command.
|
||||
|
||||
3. Creates an `argparse` subparser named `cmd_name`, using `s_description()` (if present) as its
|
||||
help text.
|
||||
|
||||
1. Calls `handler.s_arguments(subparser)` (if present) so the command can add options.
|
||||
|
||||
Skipped commands never appear in help output and cannot be invoked.
|
||||
|
||||
__`Scott.run()`__
|
||||
|
||||
Parses the process arguments and dispatches to the selected command's `s_run(args)`.
|
||||
@@ -0,0 +1,51 @@
|
||||
---
|
||||
title: Scott - Build From Source
|
||||
---
|
||||
|
||||
# Scott - Build From Source
|
||||
|
||||
Open a terminal and change the active directory to the project root folder for `Scott`.
|
||||
|
||||
Follow the given steps in order to create a clean Python sandbox, activate it, install the build
|
||||
tool, and then use that tool to compile `Scott` into distributable artifacts.
|
||||
|
||||
__Step 1: Create an isolated virtual environment.__
|
||||
|
||||
```sh
|
||||
python -m venv .venv
|
||||
```
|
||||
|
||||
The virtual environment is created in a folder called `.venv` under the project root folder. This
|
||||
gives the project its own Python interpreter and package space, so dependencies don't conflict with
|
||||
the system Python or other projects.
|
||||
|
||||
__Step 2: Activate the virtual environment.__
|
||||
|
||||
```sh
|
||||
source .venv/bin/activate
|
||||
```
|
||||
|
||||
Activation happens for the current shell session only. It prepends `.venv/bin/` to your `PATH`, so
|
||||
`python` and `pip` now resolve to the environment's copies. Your prompt typically changes to show
|
||||
`(.venv)`.
|
||||
|
||||
__Step 3: Install (or upgrade) the build package.__
|
||||
|
||||
```sh
|
||||
pip install --upgrade build
|
||||
```
|
||||
|
||||
The virtual environment now has the latest `build` package. `build` is the PyPA-recommended PEP 517
|
||||
build frontend — it reads the project's `pyproject.toml`, sets up an isolated build environment, and
|
||||
invokes the configured build backend (`setuptools`) to produce distribution artifacts.
|
||||
|
||||
__Step 4: Build Scott as a package__
|
||||
|
||||
```sh
|
||||
python -m build
|
||||
```
|
||||
|
||||
By default the build process produces both a **source distribution** (`.tar.gz` sdist) and a
|
||||
**wheel** (`.whl`) in the `dist/` directory under the project root folder.
|
||||
|
||||
The resulting files in `dist/` are what you'd upload to PyPI or install with `pip install`.
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
---
|
||||
title: Scott - Install
|
||||
---
|
||||
|
||||
# Installing Scott
|
||||
|
||||
You can install Scott for your application using one of the following mechanisms:
|
||||
|
||||
## Option 1: Install from a built wheel
|
||||
|
||||
Follow the instructions to [build Scott from source](build-from-source.md). Then from the
|
||||
`{project-root}/dist` folder, install the wheel with pip:
|
||||
|
||||
```sh
|
||||
cd /your/application/project/root
|
||||
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
|
||||
pip install {project-root}/dist/*.whl
|
||||
```
|
||||
|
||||
This assumes that your application is using a virtual environment.
|
||||
|
||||
## Option 2: Install from a Git repository
|
||||
|
||||
This is the recommended approach. `Scott` is hosted as a Python package on a Git repository at
|
||||
|
||||
https://rprime.indroneel.co/termhole/scott.git
|
||||
|
||||
You can install directly from this repository instead of a built wheel. Use pip with the repository URL.
|
||||
|
||||
```sh
|
||||
cd /your/application/project/root
|
||||
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
|
||||
pip install "git+https://rprime.indroneel.co/termhole/scott.git"
|
||||
```
|
||||
|
||||
__Using the Python build tool__
|
||||
|
||||
If your application is managed with Python build tooling, you can declare `Scott` as a dependency in your app's `pyproject.toml` instead of installing it manually with `pip`.
|
||||
|
||||
Add the following entry under the project's dependencies:
|
||||
|
||||
```toml
|
||||
dependencies = [
|
||||
"scott @ git+https://rprime.indroneel.co/termhole/scott.git"
|
||||
]
|
||||
```
|
||||
|
||||
The next time you build or refresh your application's environment, `Scott` will be installed automatically into the active virtual environment.
|
||||
|
||||
```sh
|
||||
cd /your/application/project/root
|
||||
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
|
||||
python -m build
|
||||
```
|
||||
OR
|
||||
```sh
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
## Option 3: Install a local editable copy
|
||||
|
||||
Use this option when you want to modify `Scott` while developing against an application that depends on it.
|
||||
|
||||
First, clone the `Scott` repository to a local working directory:
|
||||
|
||||
```sh
|
||||
cd /your/working/directory
|
||||
|
||||
git clone https://rprime.indroneel.co/termhole/scott.git
|
||||
```
|
||||
|
||||
Then install that checkout in editable mode from your application project:
|
||||
|
||||
```sh
|
||||
cd /your/application/project/root
|
||||
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
|
||||
pip install -e /your/working/directory/scott
|
||||
```
|
||||
|
||||
This makes the installed package point to your local source tree, so changes in `Scott` are picked up immediately. After that, you can either build your application package or install your app in editable mode from its own project directory:
|
||||
|
||||
```sh
|
||||
python -m build
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```sh
|
||||
pip install -e .
|
||||
```
|
||||
Reference in New Issue
Block a user