ocracy.kit.ledger#
The ledger loader: a filterable catalog over a facade’s backends.json.
Each fleet facade ships a ledger: a JSON catalog that describes every engine or
service in its domain, whether or not the facade can run it yet. The research
changes far more often than the code, so it is data, not code. This module reads
any such file and exposes it as a dict-like Catalog of
BackendInfo records. A facade binds its own data:
from ocracy.kit.ledger import Catalog
catalog = Catalog(
default_path=Path(__file__).parent / "data" / "backends.json",
env_var="ACME_LEDGER", # override the path from the env
implemented=lambda: registry.list_backends(),
text_fields=DEFAULT_TEXT_FIELDS | {"pros", "cons"},
)
catalog["some-engine"] # one record
catalog.filter(is_local=True, open_source=True) # a new Catalog; filters compose
catalog.filter(implemented=True) # what the facade runs today
catalog.supports_language("French")
catalog.can("diarization")
implemented is the one computed field. It comes from the facade’s registry
through the implemented= callable, so it cannot drift from the code. A record
that stores its own implemented keeps it.
The seams, all keyword-only: default_path and env_var (where the JSON
lives), implemented (ids with a working adapter), text_fields (fields
filtered by case-insensitive membership rather than equality),
capability_suffixes (extra field names Catalog.can() looks at, such as
ocracy’s math -> math_formula) and compare_fields (the default columns
of Catalog.compare()).
Stdlib only; imports nothing else from ocracy. pandas is imported by
Catalog.to_dataframe() only.
Module Attributes
filtered by a case-insensitive substring test rather than by equality. |
|
The columns |
|
Field values that mean "this backend has the capability", for |
Classes
|
One backend's ledger entry: a read-only record, by key or by attribute. |
|
A filterable, dict-like collection of |
- class ocracy.kit.ledger.BackendInfo(record, *, implemented=False)[source]#
Bases:
MappingOne backend’s ledger entry: a read-only record, by key or by attribute.
New fields added to the JSON are available at once (
info.price_note,info["price_note"]) with no code change; a few common fields get typed properties.
- ocracy.kit.ledger.CAPABILITY_VALUES = ('yes', True)#
Field values that mean “this backend has the capability”, for
Catalog.can(). Compared by identity forTrue, so a count of1is not a capability – and neither is a numpyTrueor the string"True": JSON gives real bools.
- class ocracy.kit.ledger.Catalog(path=None, *, records=None, default_path=None, env_var=None, implemented=<function _nothing_implemented>, text_fields=frozenset({'beyond_text', 'languages_note', 'output_formats'}), capability_suffixes=(), compare_fields=('name', 'is_local', 'is_remote', 'open_source', 'pricing_model', 'price_note', 'accuracy_tier', 'languages_count', 'best_for'))[source]#
Bases:
MappingA filterable, dict-like collection of
BackendInfo, keyed by id.Loaded lazily from
path, else from theenv_varenvironment variable, else fromdefault_path; a missing file is an empty catalog.recordsgives the records directly instead.filter()returns a newCatalogwith the same settings over the matching subset, so filters compose:catalog.filter(is_remote=True).filter(pricing_model="free_tier_then_paid")
- Parameters:
path (
Union[str,Path,None]) – The ledger JSON (a{"backends": [...]}object or a bare list).records (
Optional[List[dict]]) – The records themselves, instead of a file.default_path (
Union[str,Path,None]) – Where the ledger lives when neitherpathnor the environment says otherwise.env_var (
Optional[str]) – An environment variable that overridesdefault_path.implemented (
Callable[[],Iterable[str]]) – Returns the ids that have a working adapter; called once, on first load (a filtered subset reuses the flags already computed). An exception it raises propagates: catch it in the binding if a broken registry should read as “nothing implemented”.text_fields (
Iterable[str]) – Fields filtered by case-insensitive membership.capability_suffixes (
Iterable[str]) – Extra field-name suffixescan()checks besides the capability’s own field:("_formula",)makescan("math")also readmath_formula.compare_fields (
Iterable[str]) – Default columns ofcompare().
text_fields,capability_suffixesandcompare_fieldstake collections of names; a bare string is refused rather than split into letters. A subclass binding a facade’s data should keep no derived state of its own:filter()builds a subset from these settings alone.- can(capability)[source]#
Backends with
capability: listed inbeyond_text, or a field set.A field counts when it is named
capability, or that plus one of the catalog’scapability_suffixes, and its value is one ofCAPABILITY_VALUES.- Return type:
- compare(ids=None, *, fields=None)[source]#
A trimmed, side-by-side view of selected backends and fields.
- filter(*, predicate=None, implemented=None, **criteria)[source]#
Return a new
Catalogof backends matching every criterion.- Parameters:
predicate (
Optional[Callable[[BackendInfo],bool]]) – An arbitraryBackendInfo -> boolcallable.implemented (
Optional[bool]) – If set, keep only (un)implemented backends.**criteria (
Any) –field=valueconstraints.valuemay be a set/list/tuple meaning “one of”. The catalog’s text fields (by defaultlanguages_note,beyond_text,output_formats) use a case-insensitive substring match.
- Return type:
Example:
catalog.filter(is_local=True, open_source=True)
- supports_language(language)[source]#
Backends whose
languages_notementionslanguage(name or code).- Return type:
- ocracy.kit.ledger.DEFAULT_COMPARE_FIELDS = ('name', 'is_local', 'is_remote', 'open_source', 'pricing_model', 'price_note', 'accuracy_tier', 'languages_count', 'best_for')#
The columns
Catalog.compare()shows when a facade does not name its own.
- ocracy.kit.ledger.DEFAULT_TEXT_FIELDS = frozenset({'beyond_text', 'languages_note', 'output_formats'})#
filtered by a case-insensitive substring test rather than by equality.
- Type:
Fields whose meaning is “membership in a free-text description”