Skip to main content

A typed, Pythonic client and CLI for the TrueNAS middleware API

Project description

pytruenas

PyPI version Python versions Documentation License: MIT

A typed, Pythonic client and CLI for the TrueNAS middleware API.

pytruenas speaks the middleware's JSON-RPC 2.0 websocket protocol directly — over wss:///ws:// to a remote host, or over the local unix socket (ws+unix://) when running on the NAS itself. It exposes the whole API surface through an attribute-style namespace, adds convenience helpers for the common create/update/upsert patterns, a remote-filesystem abstraction, an optional typings generator, and a small CLI for scripting host configuration.

Install

pip install pytruenas          # once published to PyPI
# or, from a checkout:
pip install .

Optional extras:

Extra Enables
pytruenas[ssh] Remote shell + SFTP filesystem backend (asyncssh)
pytruenas[config] YAML config/targets file for the CLI (pyyaml)
pytruenas[codegen] generate-typings command (jinja2)
pytruenas[host] Local network-adapter / packaging helpers (ifaddr)

Quickstart

from pytruenas import TrueNASClient

# Remote host (api key, or "user:password", or a token)
client = TrueNASClient("nas.example.com", "1-<64-char-api-key>", sslverify=False)

# Attribute-style access to any API namespace/method:
for user in client.api.user.query():
    print(user["username"])

# Convenience helpers for common DB patterns:
client.api.user._upsert("username", username="svc", full_name="Service", group_create=True)

# Running on the NAS itself talks to the local unix socket, no auth:
local = TrueNASClient()            # ws+unix:///var/run/middleware/middlewared.sock
print(local.api.system.info())

Credentials

TrueNASClient(target, creds) accepts, for creds:

  • an API key string "<id>-<64 chars>",
  • "user:password" (optionally "user:password\n<otp>"),
  • a token string,
  • a (user, password) tuple,
  • None / omitted → local socket auth.

Credentials.from_env() reads TN_CREDS.

CLI

pytruenas --help
pytruenas query user -f username=root nas.example.com
pytruenas call system.info nas.example.com          # any method by dotted name
pytruenas dump-api nas.example.com > api.json
pytruenas generate-typings --path typings --api-version v26.0.0 nas.example.com

The target host(s) are the trailing positional arguments — a command's own positionals (like query's namespace) come first, then the hosts. Each target may be comma-separated and supports [A-Z]/[0-9] range expansion (e.g. 'nas[1-3].example.com'); with no target the command runs against localhost. --parallel N runs several targets concurrently. Filter query with -f/--filter KEY=VALUE (repeatable).

Typings generator

generate-typings turns a host's API definition into a package of .pyi stubs so editors and type checkers understand client.api.<namespace>.<method>(...). It is validated against the full real API (every version in a live dump).

pytruenas generate-typings --path truenasapi_typings/current nas.example.com

Development

py -3.14 -m venv .venv
.venv/Scripts/python -m pip install -e .[dev]
.venv/Scripts/python -m pytest

Supports Python 3.9+.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytruenas-0.1.1.tar.gz (107.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytruenas-0.1.1-py3-none-any.whl (83.2 kB view details)

Uploaded Python 3

File details

Details for the file pytruenas-0.1.1.tar.gz.

File metadata

  • Download URL: pytruenas-0.1.1.tar.gz
  • Upload date:
  • Size: 107.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for pytruenas-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a0ce3bc6762b08fe62741c3a1317423486367f4aeac3da1ee6accc1252a7fd43
MD5 b28c8f22008e0193f8d614d9eb1b5967
BLAKE2b-256 fa8bfa7f02f469ab4907f71bccb2d75ec00650b911201045b2700c4ca00a98a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytruenas-0.1.1.tar.gz:

Publisher: release.yml on jose-pr/pytruenas

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytruenas-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytruenas-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 83.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for pytruenas-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1d193b4c1de0f0ef49a508f3853f4d77e57e22666bf26fd379bf4d16bb85dfa0
MD5 bec64effe6a9a3500412925306ce431c
BLAKE2b-256 20df823517e20abe7fbe124609f8a14dd78a23fb917603179d26fb48db139ac6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytruenas-0.1.1-py3-none-any.whl:

Publisher: release.yml on jose-pr/pytruenas

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page