cointoss.cli¶
cli
¶
The cointoss command: set up an instance, sweep by hand, check on it, and back it up.
cointoss init [--data-dir D] [--systemd [--unit NAME]] [--port N] [--schedule CRON]
create the instance, idempotently
cointoss sweep [--data-dir D] [--force]
cointoss backfill [--data-dir D] [--days N] fill dates with no stored bar, stocks too
cointoss sweep-stocks [--data-dir D] [--force] evaluate yahoo-industries for the last session
cointoss status [--data-dir D]
cointoss token [--data-dir D] print the JSON-RPC bearer token
cointoss backup [--data-dir D] DEST copy the database and configs into DEST
cointoss restore [--data-dir D] SRC copy a backup into a stopped instance
cointoss portfolio create NAME [--kind K] [--custodian C] [--url U] [--token=T]
cointoss trade add PORTFOLIO INSTRUMENT QUANTITY [--cost USD] [--at TIME] [--kind K]
[--note N] [--source S] [--coingecko-id ID] [--url U] [--token=T]
cointoss trade void TRADE [--url U] [--token=T]
The data directory is --data-dir, else $COINTOSS_HOME, else ~/.local/share/cointoss
(cointoss.config.resolve_data_dir). It holds cointoss.db, cointoss.yaml, woodglue.yaml
and woodglue's own stores (auth.db, mounts/, and wgl.log when not under systemd).
init writes each config only when it is missing, so an edited one is never overwritten, then
validates both, so a re-run doubles as a config check. The woodglue.yaml it writes does not
name the data directory: the fragment takes woodglue's own (wgl --data D), so wgl --data D
start from a shell serves the same store the unit does, and a copied instance serves its own.
The unit also sets COINTOSS_HOME, for any cointoss command run under it.
When Streamlit is installed (the ui extra), a new woodglue.yaml also serves the Dataset
explorer as uis.explore, naming the absolute path of cointoss/explorer_app.py in this
install, since woodglue resolves a relative app against the data directory. woodglue runs it
with its own Python, which is this environment's. A kept woodglue.yaml without it is reported
with the entry to add, as a differing port is.
JupyterLab (cointoss.notebook, the notebook extra) is never added: when it is installed
and woodglue.yaml has no uis.notebook, init prints the entry to add by hand; when it is
configured, init warns of a Role other than admin that may view it, or auth turned off.
A second instance on the same host, such as a dev checkout beside an installed one, takes its
own --data-dir, --unit and --port. Each unit names its own virtualenv's bin, which is why
it is a plain unit per instance rather than a cointoss@.service template.
A sweep missed while the server was down is caught up by lythonic when the server starts: a schedule trigger that missed its last firing fires once on start, so the unit needs no catch-up step of its own.
sweep calls the fragment's own sweep node, so a manual run follows exactly the scheduled
run's idempotency rule and takes the same lock, but without waiting for it. It records
CoinGecko's current listing for today (UTC), so it has no date to choose; a missed past day is
history backfill: backfill calls the fragment's backfill node, waiting for the sweep lock
before each write as the scheduled one does, and fills stock bars as the stock sweep does.
sweep-stocks calls the fragment's stock_sweep node the same way: it evaluates
yahoo-industries for the latest US session closed and fetches the stocks' bars, and waits for
the sweep lock before its writes. init reports a kept woodglue.yaml that lacks the
backfill or stock_sweep trigger, with the entry to add.
backup and restore are described in cointoss.backup. backup runs beside a serving
instance and waits for a sweep in progress; restore refuses while the server for its data
directory runs, and checks the backup's configs as init checks an instance's before writing
anything. It leaves auth.db alone: cointoss init afterwards creates a token if there is none.
portfolio and trade are JSON-RPC clients of a running server rather than writers of the
local store, so they work the same from another machine on the tailnet, calling int, and run
as whoever the token names: that User owns what they create. They call the fragment's
create_portfolio, add_trade and void_trade (see cointoss.app for the Role they need).
The server is --url, else $COINTOSS_URL, else the local instance's own woodglue.yaml; the
token is --token, else $COINTOSS_TOKEN, else the local instance's admin token when calling
it, else the one wgl login URL stored. init writes the portfolio Role into a new
woodglue.yaml, and reports a kept one that lacks it.
Expected failures -- a config that does not load, a sweep already running, a missing store --
are turned into CliError where they arise and reported as one line. Anything else is a bug
and keeps its traceback.
The CLI uses argparse rather than lythonic's ActionTree, which derives option names from
Python parameter names (--data_dir) and takes root options only before the subcommand.
CliError
¶
Endpoint
¶
Bases: NamedTuple
A server's base URL, and the bearer token to send it; None for what wgl login stored.
Source code in src/cointoss/cli.py
user_unit_dir()
¶
Where systemd --user looks for units the user installed.
render_unit(*, bin_dir, data_dir, unit=DEFAULT_UNIT)
¶
The shipped cointoss.service template with this install's paths and unit name filled in.
systemd splits Exec*= lines on whitespace, so a path containing any is refused rather than
quoted into a unit that might still be misread. A % is doubled, since systemd would read
it as the start of a specifier.
Source code in src/cointoss/cli.py
check_unit_name(name)
¶
Refuse a unit name that is not a plain one, installed as NAME.service.
>>> check_unit_name("cointoss-dev")
>>> check_unit_name("x.service")
Traceback (most recent call last):
...
cointoss.cli.CliError: --unit 'x.service': give the name without .service
Source code in src/cointoss/cli.py
check_schedule(schedule)
¶
Refuse anything but a 5-field cron expression, read as UTC by lythonic's trigger, that fires.
>>> check_schedule("0 0 30 2 *")
Traceback (most recent call last):
...
cointoss.cli.CliError: --schedule '0 0 30 2 *': never fires
Source code in src/cointoss/cli.py
first_daily_firing(schedule)
¶
How long after 00:00 UTC a cron schedule first fires on a day it fires at all.
The hour and minute fields do not depend on the day, so any day's first firing will do.
>>> first_daily_firing("5 0 * * *")
datetime.timedelta(seconds=300)
>>> first_daily_firing("30 1,12 * * 1")
datetime.timedelta(seconds=5400)
Source code in src/cointoss/cli.py
streamlit_installed()
¶
Whether this environment, the one woodglue runs a Streamlit UI with, has Streamlit.
explorer_ui()
¶
The uis.explore entry serving this install's Dataset explorer.
Its only Grant is call on the cointoss namespace, so whoever views it, the app can do no
more than call cointoss's read nodes: woodglue exposes only api nodes over JSON-RPC, so
the sweep is not callable, and firing its trigger needs a trigger Grant. The portfolio
writes are api nodes but require cointoss.app.WRITE_ACTION, which no call Grant
reaches.
Source code in src/cointoss/cli.py
auth_roles()
¶
The Roles a new woodglue.yaml defines: portfolio, for Users who record their trades.
The built-in admin Role, whose token cointoss token prints, needs no definition. Only a
write cointoss Grant reaches the portfolio writes, so a reader's Role may use any call
pattern.
Source code in src/cointoss/cli.py
woodglue_config(*, port=DEFAULT_PORT, schedule=DEFAULT_SWEEP_SCHEDULE, explorer=False)
¶
The woodglue.yaml a new instance starts from: the cointoss namespace, local, with auth.
With explorer, it also serves the Dataset explorer at /ui/explore/.
Source code in src/cointoss/cli.py
cointoss_config()
¶
The cointoss.yaml a new instance starts from: the default universes and listing width.
Source code in src/cointoss/cli.py
cmd_init(data_dir, systemd, *, unit=None, port=None, schedule=None)
¶
Create or check the instance; port and schedule apply to a new woodglue.yaml only.
Every option is checked before anything is written.
Source code in src/cointoss/cli.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
resolve_endpoint(data_dir, url, token)
¶
Where the portfolio commands and cointoss.client send their calls, and as whom.
The URL is --url, else $COINTOSS_URL, else the local instance's woodglue.yaml. The
token is --token, else $COINTOSS_TOKEN, else the local instance's admin token when
calling that instance, else whatever wgl login stored for the URL.