Commands and views
Commands
commands is a sequence of Command(name, help, run). Each becomes ludex <provider> <name> [args...]. Names are lowercase letters, digits and -. A provider named like a built-in command cannot have commands (the built-in wins).
import argparse
from ludex.library import list_games
from ludex.plugins import Command, CommandResult
def count(ctx, args):
parser = argparse.ArgumentParser(prog="ludex demo count")
parser.add_argument("--store")
ns = parser.parse_args(args)
games = list_games(ctx.conn, store=ns.store)
return CommandResult({"count": len(games)}, f"{len(games)} games")
PLUGIN = Provider("demo", "Demo", (), commands=(Command("count", "Count games", count),))
run(ctx, args)gets aCommandContext(conn,providers, andsettings: your plugin-wide settings) and the raw argument strings after the command name. Parse them yourself;argparseworks, including-hand usage errors.- Return
CommandResult(data, text):datais printed as JSON under--json,textotherwise. - Act on the library through
ludex.library(list_games,get_game,resolve_game,run_import, ...), never raw SQL. The core holds the writer lock for the whole run, so commands are serialized with imports. - Raise
LudexError(code, message)with a fixed, secret-free message for expected failures; any other exception is reported ascommand_failedwithout its text. - A command that needs a long-running process can use
ludex.services.start_service(ctx.conn, provider, name, argv, cwd=..., settle=...), thenstop_serviceandservice_status. It runs as a detachedservicejob with its output in a private log, visible on the GUI Jobs page. One service of a given name runs at a time.
Views
views is a tuple of View(id, title, fetch). A frontend lists each as a navigation entry: a sidebar page in ludex gui, and ludex plugin view PLUGIN [VIEW] on the command line. Ids are lowercase letters, digits and -, unique within the provider; title is a single line.
fetch(settings) receives the plugin-wide settings (never account settings) and returns:
ViewTable(columns, rows): eachViewRow(id, cells, actions)has a stringidunique in the table and one string cell per column;- or
ViewDetail(fields): a tuple of(label, value)pairs.
from ludex.plugins import View, ViewAction, ViewRow, ViewTable
def players(settings):
return ViewTable(
("Name", "State"),
(ViewRow("alice", ("Alice", "online"), (ViewAction("Kick", "kick", ("alice",)),)),),
)
views = (View("players", "Players", players),)
- A
ViewAction(label, command, args)names one of this provider'scommands. The frontend shows the label, asks the user to confirm, and only then runs the command withargs(no shell). Fetching never runs an action. Actions must not prompt or print. - Everything is plain text: no markup, no links the frontend opens.
fetchmay block; frontends call it off their UI thread. The same rules assearchapply: bounded responses, verified TLS, fixed secret-freeLudexErrormessages (any other exception is reported asview_failedwithout its text).
Needs ludex-core>=0.61.0; API_VERSION stays 3.