Skip to content

madbot_cli.manifest

The deposit file: read it, check it, and give access to its parts.

The file is YAML or JSON. It is checked against the JSON Schema of its version, then against the rules a schema cannot say (titles unique in the file, sources declared at the top and named where they are used, one key per connection, a valid pattern), before any request to the API. Every error comes with its path in the file.

Attributes

NameDescription
FORMSThe forms of a data entry, by definition of the schema: the key that tells
JSON_TYPESThe Python types of the JSON types a choice of the format names.
NEXT_TOHow many keys of a place a message names.
PLACESThe places of the format a person knows, by definition of the schema, with
SUPPORTED_VERSIONSThe versions of the format this release reads.

Classes

NameDescription
LoaderReads YAML as yaml.safe_load does; its numbers keep how they are written.
ManifestA deposit file, checked.
ManifestErrorThe deposit file is invalid.
NodeA node and what it holds, as the file describes it.
SourceA source of data, declared once at the top of the file.
SubmissionA submission of data of a node to a repository, as the file describes it.
WrittenFloatA decimal number of the file that keeps how it is written, such as 1.10.
WrittenIntAn integer of the file that keeps how it is written, such as 01.

Functions

NameDescription
as_textRead as names the numbers of the paths and names of the data entries.
check_patternCheck the pattern of the rule of samples of a node.
check_rulesCheck the rules of a deposit file that its JSON Schema cannot say.
connection_ofTell which connection a source of the file stands for, as the file writes it.
credentialsRead credentials from an environment variable holding a JSON object.
descendantsList the paths of a node of the document and of the nodes under it.
details_ofExplain the errors inside a choice, each with its path from the choice.
eachConvert each item of a list, and leave anything else as it is.
entry_as_textRead as names the numbers of the path and names of a data entry.
label_of_pathWrite the path of a node for a person.
loadRead and check a deposit file.
load_schemaReturn the JSON Schema of a version of the deposit file.
message_ofExplain an error of the format, saying where a misplaced key goes.
message_of_formsExplain a data entry that takes none of its forms, or several.
message_of_typeExplain a value that takes the one choice of its type with an error in it.
name_ofRead a number given as a name as the text it is written with.
node_as_textRead as names the numbers of the data entries of a node and its children.
node_ofRead a node of a checked document, and the nodes under it.
parseCheck a deposit document and return it as a Manifest.
path_ofWrite a path in a document the way a person reads it.
places_ofList, for each key of the format, the places it goes and the keys beside it.
read_yamlRead a YAML or JSON document with Loader, a safe loader.
rule_as_textRead as names the numbers of the data of the associations of a rule.
via_pathRead the via of a data entry as a path of titles.
without_emptyDrop the empty parts of a metadata value, at any depth.
written_numberRead a number of the file, with the text it is written with.

FORMSattributemodule attribute#

FORMS = {
  'externalIdEntry': ('external_id', '{source, external_id}'),
  'browseEntry': ('browse', '{source, browse: {path, match or names}}'),
  'matchEntry': ('match', '{source, match}'),
  'namesEntry': ('names', '{source, names}')
}

The forms of a data entry, by definition of the schema: the key that tells the form, and the keys the form takes.

JSON_TYPESattributemodule attribute#

JSON_TYPES = {'object': Mapping, 'array': list, 'string': str, 'boolean': bool}

The Python types of the JSON types a choice of the format names.

NEXT_TOattributemodule attribute#

NEXT_TO = 3

How many keys of a place a message names.

PLACESattributemodule attribute#

PLACES = {
  'source': ('a source', 'sources'),
  'node': ('a node', 'nodes'),
  'submission': ('a submission', 'submissions'),
  'samples': ('the samples of a node', 'samples'),
  'associationRule': ('an association rule', 'associations'),
  'listedAssociation': ('an association of a list', 'list'),
  'externalIdEntry': ('a data entry', 'data'),
  'browseEntry': ('a data entry', 'data'),
  'matchEntry': ('a data entry', 'data'),
  'namesEntry': ('a data entry', 'data')
}

The places of the format a person knows, by definition of the schema, with the key of the file under which they are.

SUPPORTED_VERSIONSattributemodule attribute#

SUPPORTED_VERSIONS = (1)

The versions of the format this release reads.

Loaderclass#

class Loader(yaml.SafeLoader)

Bases: yaml.SafeLoader

Reads YAML as yaml.safe_load does; its numbers keep how they are written.

Manifestclassdataclass#

class Manifest(version: int, workspace: str, nodes: list[Node], sources: dict[str, Source] = dict(), source: str = '<file>')

A deposit file, checked.

Attributes

NameTypeDescription
versionintThe version of the format.
workspacestrThe name of the workspace.
nodeslist of NodeThe nodes at the top of the workspace, in the order of the file.
sourcesdict of SourceThe sources of the file, by key.
sourcestrWhere the file was read from.

nodesattributeinstance attribute#

nodes: list[Node]

sourceattributeclass attributeinstance attribute#

source: str = '<file>'

sourcesattributeclass attributeinstance attribute#

sources: dict[str, Source] = field(default_factory=dict)

versionattributeinstance attribute#

version: int

workspaceattributeinstance attribute#

workspace: str

find_sourcemethod#

def find_source(key: str) -> Source

Find a source of the file by its key.

Parameters

NameTypeDescription
keystrThe key of the source.

Returns

Source
The source.

Raises

MadbotError
When the file has no source of that key.

find_submissionmethod#

def find_submission(title: str) -> tuple[Node, Submission]

Find a submission of the file by its title.

Parameters

NameTypeDescription
titlestrThe title of the submission.

Returns

tuple
The node and the submission.

Raises

MadbotError
When the file has no submission of that title.

submissionsmethod#

def submissions() -> Iterator[tuple[Node, Submission]]

List the submissions of the file with their node.

Yields

tuple
The node and the submission.

walkmethod#

def walk() -> Iterator[Node]

List every node of the file, each before its children.

Yields

Node
The nodes, at every depth.

ManifestErrorclass#

class ManifestError(source: str, errors: list[str])

Bases: MadbotError

The deposit file is invalid.

Attributes

NameTypeDescription
errorslist of strOne message per error, each starting with its path in the file.

Parameters

NameTypeDescription
sourcestrThe file, or what the content came from.
errorslist of strOne message per error.

errorsattributeinstance attribute#

errors = errors

Nodeclassdataclass#

class Node(title: str, description: str | None = None, data: list[dict] = list(), samples: dict[str, Any] | None = None, associations: list[dict] = list(), submissions: list[Submission] = list(), nodes: list[Node] = list(), path: tuple[str, ...] = ())

A node and what it holds, as the file describes it.

associationsattributeclass attributeinstance attribute#

associations: list[dict] = field(default_factory=list)

The rules that make the associations of the data of the node.

dataattributeclass attributeinstance attribute#

data: list[dict] = field(default_factory=list)

The data entries to link to the node, each naming its source.

descriptionattributeclass attributeinstance attribute#

description: str | None = None

nodesattributeclass attributeinstance attribute#

nodes: list[Node] = field(default_factory=list)

The child nodes, in the order of the file.

pathattributeclass attributeinstance attribute#

path: tuple[str, ...] = ()

The titles from the top of the workspace to the node, its own last.

samplesattributeclass attributeinstance attribute#

samples: dict[str, Any] | None = None

submissionsattributeclass attributeinstance attribute#

submissions: list[Submission] = field(default_factory=list)

titleattributeinstance attribute#

title: str

labelproperty#

label: str

The path of the node for a person, such as Campaign / Night.

source_keysmethod#

def source_keys() -> list[str]

List the sources the data of the node come from.

Returns

list of str
The keys of the sources, in the order the entries name them.

walkmethod#

def walk() -> Iterator[Node]

List the node and the nodes under it, each before its children.

Yields

Node
The node, then its descendants.

Sourceclassdataclass#

class Source(key: str, plugin: str, shared_params: dict[str, Any], credentials_env: str | None = None)

A source of data, declared once at the top of the file.

credentials_envattributeclass attributeinstance attribute#

credentials_env: str | None = None

keyattributeinstance attribute#

key: str

pluginattributeinstance attribute#

plugin: str

shared_paramsattributeinstance attribute#

shared_params: dict[str, Any]

Submissionclassdataclass#

class Submission(title: str, repository: dict[str, Any], data: list[dict] = list(), take_all: bool = False, via: tuple[str, ...] | None = None, metadata: dict[str, dict[str, Any]] = dict())

A submission of data of a node to a repository, as the file describes it.

dataattributeclass attributeinstance attribute#

data: list[dict] = field(default_factory=list)

The data entries the submission takes; empty when it takes them all.

metadataattributeclass attributeinstance attribute#

metadata: dict[str, dict[str, Any]] = field(default_factory=dict)

repositoryattributeinstance attribute#

repository: dict[str, Any]

take_allattributeclass attributeinstance attribute#

take_all: bool = False

Whether the submission takes every data of its node and the nodes under it.

titleattributeinstance attribute#

title: str

viaattributeclass attributeinstance attribute#

via: tuple[str, ...] | None = None

With take_all, the node a data linked to several nodes goes through.

WrittenFloatclass#

class WrittenFloat(float)

Bases: float

A decimal number of the file that keeps how it is written, such as 1.10.

writtenattributeinstance attribute#

written: str

WrittenIntclass#

class WrittenInt(int)

Bases: int

An integer of the file that keeps how it is written, such as 01.

writtenattributeinstance attribute#

written: str

as_textfunction#

def as_text(document: Any) -> Any

Read as names the numbers of the paths and names of the data entries.

YAML reads path: [24343971] or names: [2026] as numbers, while a folder or a file is named by text. A number read by Loader gives the text it is written with, so that 01 stays "01" and 1.10 stays "1.10". Only the places of the format that hold names are read again; a value of a metadata field keeps its number.

Parameters

NameTypeDescription
documentAnyThe content of the file, as Loader reads it.

Returns

Any
A copy of the document, with those numbers written as text.

check_patternfunction#

def check_pattern(samples: Mapping, where: list) -> list[str]

Check the pattern of the rule of samples of a node.

Parameters

NameTypeDescription
samplesmappingThe samples of a node.
wherelistThe path of the node in the file.

Returns

list of str
The errors, each starting with its path.

check_rulesfunction#

def check_rules(document: Mapping) -> list[str]

Check the rules of a deposit file that its JSON Schema cannot say.

Parameters

NameTypeDescription
documentmappingA document valid against the JSON Schema.

Returns

list of str
The errors, each starting with its path.

connection_offunction#

def connection_of(spec: Mapping) -> tuple[str, tuple]

Tell which connection a source of the file stands for, as the file writes it.

Parameters

NameTypeDescription
specmappingA source: its plugin and shared_params.

Returns

tuple
The plugin and the shared parameters, as text and sorted.

credentialsfunction#

def credentials(env_name: str | None, environ: Mapping[str, str] | None = None) -> dict | None

Read credentials from an environment variable holding a JSON object.

Parameters

NameTypeDefaultDescription
env_namestr or NonerequiredThe name of the variable.
environmappingNoneThe environment; os.environ by default.

Returns

dict or None
The credentials, None when no variable is named or it is not set.

Raises

MadbotError
When the variable does not hold a JSON object.

descendantsfunction#

def descendants(node: Mapping) -> set[tuple[str, ...]]

List the paths of a node of the document and of the nodes under it.

Parameters

NameTypeDescription
nodemappingA node of the document.

Returns

set of tuple
The paths under the node, () for the node itself.

details_offunction#

def details_of(errors: list[jsonschema.ValidationError], schema: Mapping) -> str

Explain the errors inside a choice, each with its path from the choice.

Parameters

NameTypeDescription
errorslist of ValidationErrorThe errors of the choice.
schemamappingThe JSON Schema of the format.

Returns

str
The messages, joined by ; .

eachfunction#

def each(items: Any, convert: Any) -> Any

Convert each item of a list, and leave anything else as it is.

Parameters

NameTypeDescription
itemsAnyA list, or a value the format will refuse.
convertcallableWhat to do to each item.

Returns

Any
The converted list, or items itself.

entry_as_textfunction#

def entry_as_text(entry: Any) -> Any

Read as names the numbers of the path and names of a data entry.

Parameters

NameTypeDescription
entryAnyA data entry of a node or a submission, or a data of an association.

Returns

Any
A copy of the entry, with those numbers written as text.

label_of_pathfunction#

def label_of_path(path: Iterable[str]) -> str

Write the path of a node for a person.

Parameters

NameTypeDescription
pathiterable of strThe titles from the top of the workspace.

Returns

str
The titles joined by /.

loadfunction#

def load(path: str | os.PathLike) -> Manifest

Read and check a deposit file.

Parameters

NameTypeDescription
pathstr or path - likeThe file, YAML or JSON.

Returns

Manifest
The checked deposit.

Raises

ManifestError
When the file cannot be read or is invalid.

load_schemafunction#

def load_schema(version: int = 1) -> dict

Return the JSON Schema of a version of the deposit file.

Parameters

NameTypeDefaultDescription
versionint1The version of the format.

Returns

dict
The JSON Schema.

message_offunction#

def message_of(error: jsonschema.ValidationError, schema: Mapping) -> str

Explain an error of the format, saying where a misplaced key goes.

Parameters

NameTypeDescription
errorValidationErrorAn error of the file against the format.
schemamappingThe JSON Schema of the format.

Returns

str
The message; for a key not expected where it is, the place the format expects it (those on the path of the key first), or the keys expected there.

message_of_formsfunction#

def message_of_forms(error: jsonschema.ValidationError, schema: Mapping) -> str | None

Explain a data entry that takes none of its forms, or several.

Parameters

NameTypeDescription
errorValidationErrorA oneOf error of the file against the format.
schemamappingThe JSON Schema of the format.

Returns

str or None
The form the entry was meant to take, from the key that tells it, with what is wrong in it; the forms expected when no key tells one; None when the error is not on a data entry.

message_of_typefunction#

def message_of_type(error: jsonschema.ValidationError, schema: Mapping) -> str | None

Explain a value that takes the one choice of its type with an error in it.

Parameters

NameTypeDescription
errorValidationErrorA oneOf error of the file against the format.
schemamappingThe JSON Schema of the format.

Returns

str or None
The errors inside the one choice whose type is that of the value; None when no choice, or several, have that type.

name_offunction#

def name_of(item: Any) -> Any

Read a number given as a name as the text it is written with.

Parameters

NameTypeDescription
itemAnyAn item of a path or of a list of names.

Returns

Any
"01" for 01, "1.10" for 1.10; any other item as it is.

node_as_textfunction#

def node_as_text(node: Any) -> Any

Read as names the numbers of the data entries of a node and its children.

Parameters

NameTypeDescription
nodeAnyA node of the file.

Returns

Any
A copy of the node, with the names of its data entries, those of its submissions and of its associations given one by one, as text.

node_offunction#

def node_of(node: Mapping, parent: tuple[str, ...] = ()) -> Node

Read a node of a checked document, and the nodes under it.

Parameters

NameTypeDefaultDescription
nodemappingrequiredA node of the document.
parenttuple of str()The path of its parent.

Returns

Node
The node.

parsefunction#

def parse(document: Any, source: str = '<file>') -> Manifest

Check a deposit document and return it as a Manifest.

Parameters

NameTypeDefaultDescription
documentAnyrequiredThe content of the file, as YAML or JSON decode it.
sourcestr'<file>'Where it comes from, for the messages.

Returns

Manifest
The checked deposit.

Raises

ManifestError
With the path of each error.

path_offunction#

def path_of(parts: Any) -> str

Write a path in a document the way a person reads it.

Parameters

NameTypeDescription
partsiterableThe keys and indexes from the root.

Returns

str
Such as sources.instrument.plugin; (file) for the root.

places_offunction#

def places_of(schema: Mapping) -> dict[str, dict[tuple[str, str], list[str]]]

List, for each key of the format, the places it goes and the keys beside it.

Parameters

NameTypeDescription
schemamappingThe JSON Schema of the format.

Returns

dict
For each key, each place it goes, as (name, key of the file it is under) such as ("a submission", "submissions"), with the keys of that place, the required ones first.

read_yamlfunction#

def read_yaml(text: str) -> Any

Read a YAML or JSON document with Loader, a safe loader.

Parameters

NameTypeDescription
textstrThe document.

Returns

Any
Its content, its numbers keeping how they are written.

Raises

yaml.YAMLError
When the text is not valid YAML.

rule_as_textfunction#

def rule_as_text(rule: Any) -> Any

Read as names the numbers of the data of the associations of a rule.

Parameters

NameTypeDescription
ruleAnyA rule of associations of a node.

Returns

Any
A copy of the rule, data: [{names: [2026]}] read as ["2026"].

via_pathfunction#

def via_path(via: str | list[str] | None) -> tuple[str, ...] | None

Read the via of a data entry as a path of titles.

Parameters

NameTypeDescription
viastr or list of str or NoneThe title of a child node, a path of titles, or [] for the node of the submission itself.

Returns

tuple of str or None
The path under the node of the submission; None when not given.

without_emptyfunction#

def without_empty(value: Any) -> Any

Drop the empty parts of a metadata value, at any depth.

A skeleton of madbot-cli fields writes an object field with its keys and empty values: the parts left empty are no value, so that a part filled is sent alone and a part forgotten is named as missing.

Parameters

NameTypeDescription
valueAnyThe value, as the file gives it.

Returns

Any
The value, each mapping and list without its empty parts.

written_numberfunction#

def written_number(loader: Loader, node: yaml.ScalarNode) -> WrittenInt | WrittenFloat

Read a number of the file, with the text it is written with.

Parameters

NameTypeDescription
loaderLoaderThe loader reading the file.
nodeScalarNodeThe number, as written.

Returns

WrittenInt or WrittenFloat
The number, its written text such as 01, 1.10 or 0x1F.