Skip to content

madbot_cli.plan

Compare a deposit file with what madbot holds, without writing anything.

plan reads madbot through the API, with lists rather than one request per object, and returns for each object of the file what apply would do: create, update or nothing (unchanged). It also reports what madbot holds that the file does not mention, which apply leaves alone, and the metadata a repository requires that the file does not give.

The functions that plan one kind of object are shared with madbot_cli.apply, which runs them again after each step.

Attributes

NameDescription
BY_ALIASThe key of a metadata type holding the values of single objects, by alias.
CONNECTThe action on a connection that exists, for which the account has no working credentials yet.
CREATEThe action on an object apply creates.
DEPOSITEDThe state of a submission already sent to its repository: madbot cannot change it.
DEPOSITED_STATUSESStates of a submission sent, or being sent, to its repository.
LABEL_TEXTThe longest text of a summary field kept in the name of a metadata object.
UNCHANGEDThe action on an object madbot already holds as the file describes it.
UPDATEThe action on an object apply changes.

Classes

NameDescription
LinkedThe data of one node a submission chooses from.
StateWhat madbot holds, read once for a plan.

Functions

NameDescription
actionsList the action of every object of a plan.
association_candidatesList the data of a node an association can take, linked or about to be.
association_fieldsRead the madbot fields the associations of some rules have, once per run.
browse_folderList the data of a folder of a source, once per plan.
by_titleIndex objects of madbot by their title, noting the titles given twice.
entry_labelDescribe a data entry of the file for a person.
existing_nodeFind the node madbot holds for a node of the file.
existing_submissionFind the submission madbot holds for a submission of the file.
identityTell which data a datalink or a data still to link stands for.
label_ofName a metadata object for a person.
missing_namesName the names a folder does not hold, each with the names close to it.
moved_dataFind the data a submission holds through another node than the one the file chooses.
moved_warningSay that a data goes through another node than the one the file chooses.
name_ofName a datalink or a data still to link for a person.
node_actionTell what apply does to a node of the file.
planCompare a deposit file with madbot, without writing anything.
plan_connectionPlan the connection to a source or a repository.
plan_dataPlan the data of a source on a node.
plan_metadataCompare the metadata a file gives with those of a submission.
plan_nodePlan a node of the file, what it holds, and the nodes under it.
plan_node_associationsPlan the associations of a node from the rules of the file.
plan_node_samplesPlan the samples of a node from the rule of the file.
plan_sourcesPlan the connection of each source of the file, once for the whole file.
plan_submissionPlan a submission of the file.
read_stateRead the workspace of a deposit file and what it holds.
resolve_dataTurn the data entries of a node into external identifiers, by source.
resolve_entryTurn a data entry of the file into the external identifiers it designates in its source.
selectPick the data a submission takes in its node and the nodes under it, entry by entry.
short_textCut a text to a width, on a word when it can.
source_datalinksKeep the datalinks whose data come from some connections.
submission_actionsList the actions of a submission as apply counts its changes.
summarizeCount what a plan would do.
values_forReturn the values a file gives for one metadata object.
walk_plansList the plans of the nodes of a plan, at every depth, each before its children.

BY_ALIASattributemodule attribute#

BY_ALIAS = 'by_alias'

The key of a metadata type holding the values of single objects, by alias.

CONNECTattributemodule attribute#

CONNECT = 'connect'

The action on a connection that exists, for which the account has no working credentials yet.

CREATEattributemodule attribute#

CREATE = 'create'

The action on an object apply creates.

DEPOSITEDattributemodule attribute#

DEPOSITED = 'deposited'

The state of a submission already sent to its repository: madbot cannot change it.

DEPOSITED_STATUSESattributemodule attribute#

DEPOSITED_STATUSES = {'submission in progress', 'submitted', 'update available'}

States of a submission sent, or being sent, to its repository.

LABEL_TEXTattributemodule attribute#

LABEL_TEXT = 40

The longest text of a summary field kept in the name of a metadata object.

UNCHANGEDattributemodule attribute#

UNCHANGED = 'unchanged'

The action on an object madbot already holds as the file describes it.

UPDATEattributemodule attribute#

UPDATE = 'update'

The action on an object apply changes.

Linkedclassdataclass#

class Linked(title: str, datalinks: list[dict], pending: list[dict] = list(), unresolved: set[str] = set(), path: tuple[str, ...] = ())

The data of one node a submission chooses from.

datalinks: list[dict]

The datalinks of the node.

pathattributeclass attributeinstance attribute#

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

The titles from the top of the workspace to the node.

pendingattributeclass attributeinstance attribute#

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

The data still to be linked to the node, from the plan of its data.

titleattributeinstance attribute#

title: str

The path of the node, for a person.

unresolvedattributeclass attributeinstance attribute#

unresolved: set[str] = field(default_factory=set)

The sources whose browse entries wait for the connection.

Stateclassdataclass#

class State(plugins: list[dict], workspace: dict | None = None, nodes: dict[tuple[str, ...], dict] = dict(), children: dict[tuple[str, ...], list[dict]] = dict(), connections: list[dict] = list(), submissions: dict[str, dict] = dict(), ambiguous: set[tuple[str, Any]] = set())

What madbot holds, read once for a plan.

ambiguousattributeclass attributeinstance attribute#

ambiguous: set[tuple[str, Any]] = field(default_factory=set)

What madbot gives to several objects, as (kind, key): node and its path, or submission and its title. madbot does not keep titles unique.

childrenattributeclass attributeinstance attribute#

children: dict[tuple[str, ...], list[dict]] = field(default_factory=dict)

The nodes madbot holds under each node of the file, by path; () for the top.

connectionsattributeclass attributeinstance attribute#

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

nodesattributeclass attributeinstance attribute#

nodes: dict[tuple[str, ...], dict] = field(default_factory=dict)

The nodes of the file madbot holds, by path of titles.

pluginsattributeinstance attribute#

plugins: list[dict]

submissionsattributeclass attributeinstance attribute#

submissions: dict[str, dict] = field(default_factory=dict)

workspaceattributeclass attributeinstance attribute#

workspace: dict | None = None

read_childrenmethod#

def read_children(client: Client, nodes: Iterable[Node], parent: tuple[str, ...] = ()) -> None

Read the nodes of the file madbot holds, level by level.

Only the nodes of the file are opened: the children of a node absent from the file are not read.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client, with its workspace set.
nodesiterable of NoderequiredThe nodes of the file under parent.
parenttuple of str()The path of their parent, whose children are already read.

actionsfunction#

def actions(result: Mapping) -> Iterable[str]

List the action of every object of a plan.

Parameters

NameTypeDescription
resultmappingA plan.

Yields

str
The action of each object, a metadata value counting as one.

association_candidatesfunction#

def association_candidates(datalinks: Iterable[Mapping], sources: Mapping[str, Mapping], connections: Mapping[str, Mapping], keys: Iterable[str]) -> list[dict]

List the data of a node an association can take, linked or about to be.

Parameters

NameTypeDescription
datalinksiterable of mappingThe datalinks of the node.
sourcesmappingThe plan of the data of each source of the node, by key; empty once the data are linked.
connectionsmappingThe plan of the connection of each source of the file, by key.
keysiterable of strThe sources of the node.

Returns

list of dict
Each data as {name, data, connection}; data is None for a data still to link, which also gives the key of its source (its connection may not exist yet). The data madbot failed to read are left out.

association_fieldsfunction#

def association_fields(client: Client, rules: Iterable[Mapping], cache: dict | None) -> dict[str, dict]

Read the madbot fields the associations of some rules have, once per run.

Parameters

NameTypeDescription
clientClientThe client.
rulesiterable of mappingThe associations of a node.
cachedictThe fields already read, under association_fields.

Returns

dict
The fields, by slug.

browse_folderfunction#

def browse_folder(client: Client, connection_id: str, path: list[str], cache: dict | None = None) -> list[dict]

List the data of a folder of a source, once per plan.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client, with its workspace set.
connection_idstrrequiredThe connection to the source.
pathlist of strrequiredThe names of the containers to open, from the root.
cachedictNoneThe folders already listed, kept between calls.

Returns

list of dict
external_id and name of each object of the folder, sorted by name.

by_titlefunction#

def by_title(objects: Iterable[Mapping], kind: str, ambiguous: set, under: tuple[str, ...] | None = None) -> dict[str, dict]

Index objects of madbot by their title, noting the titles given twice.

Parameters

NameTypeDefaultDescription
objectsiterable of mappingrequiredThe nodes or the submissions.
kindstrrequirednode or submission.
ambiguoussetrequiredReceives (kind, title) for each title given to several objects, (kind, path) for nodes.
undertuple of strNoneThe path of the parent of nodes.

Returns

dict
Each object by its title.

entry_labelfunction#

def entry_label(entry: Mapping) -> str

Describe a data entry of the file for a person.

Parameters

NameTypeDescription
entrymappingA data entry: external_id, browse, match or names.

Returns

str
Such as campaign/raw/*.tif, campaign/raw/{a.tif, b.tif} or match *.tif.

existing_nodefunction#

def existing_node(state: State, path: tuple[str, ...]) -> dict | None

Find the node madbot holds for a node of the file.

Parameters

NameTypeDescription
stateStateWhat madbot holds.
pathtuple of strThe path of titles of the node.

Returns

dict or None
The node, None when madbot has none there.

Raises

MadbotError
When madbot has several nodes of that title at that place: the file cannot say which.

existing_submissionfunction#

def existing_submission(state: State, submission: Submission, repository_id: str | None) -> dict | None

Find the submission madbot holds for a submission of the file.

A submission is found by its title, and must go to the repository the file names: one of the same title to another repository is another submission, made in the web application.

Parameters

NameTypeDescription
stateStateWhat madbot holds.
submissionSubmissionThe submission, from the file.
repository_idstr or NoneThe connection to its repository, None when it is to be created.

Returns

dict or None
The submission, None when madbot has none of that title.

Raises

MadbotError
When madbot has several submissions of that title, or one to another repository.

identityfunction#

def identity(item: Mapping) -> tuple

Tell which data a datalink or a data still to link stands for.

Parameters

NameTypeDescription
itemmappingA datalink, or a data still to link from the plan of a node.

Returns

tuple
Its connection and its external identifier.

label_offunction#

def label_of(metadata_object: Mapping) -> str

Name a metadata object for a person.

Parameters

NameTypeDescription
metadata_objectmappingA metadata object of a submission.

Returns

str
Its type and the values of its summary fields, such as sample plate3.

missing_namesfunction#

def missing_names(missing: list[str], found: list[Mapping]) -> str

Name the names a folder does not hold, each with the names close to it.

Parameters

NameTypeDescription
missinglist of strThe names not found.
foundlist of mappingWhat the folder holds.

Returns

str
Such as readme.md (close to it: README.md); when no name is close to any, what the folder holds, in a few names, such as notes.txt (found: a.csv, b.csv).

moved_datafunction#

def moved_data(in_submission: Iterable[Mapping], selected: Iterable[Mapping]) -> list[dict]

Find the data a submission holds through another node than the one the file chooses.

apply never moves them: it would take the data out of the submission first, and apply never deletes. A data held without a datalink is left out: it goes through no node to compare.

Parameters

NameTypeDescription
in_submissioniterable of mappingThe data of the submission, as the API returns them.
selectediterable of mappingThe datalinks the file selects, each with the path of its node in through.

Returns

list of dict
For each, its name, the title of the node it goes through in madbot (current) and the path of the one of the file (through).

moved_warningfunction#

def moved_warning(label: str, moved: Mapping) -> str

Say that a data goes through another node than the one the file chooses.

Parameters

NameTypeDescription
labelstrThe submission, in the message.
movedmappingA data found by moved_data.

Returns

str
The warning.

name_offunction#

def name_of(item: Mapping) -> str

Name a datalink or a data still to link for a person.

Parameters

NameTypeDescription
itemmappingA datalink, or a data still to link.

Returns

str
Its name, else its external identifier.

node_actionfunction#

def node_action(existing: Mapping | None) -> str

Tell what apply does to a node of the file.

Parameters

NameTypeDescription
existingmapping or NoneThe node madbot holds, None when it has none.

Returns

str
CREATE without a node; UPDATE for a node madbot holds as a draft (madbot_cli.operations.NODE_PENDING), which apply makes a node in use; UNCHANGED otherwise.

planfunction#

def plan(client: Client, manifest: Manifest, environ: Mapping[str, str] | None = None) -> dict

Compare a deposit file with madbot, without writing anything.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client.
manifestManifestrequiredThe deposit file.
environmappingNoneThe environment holding the credentials; os.environ by default.

Returns

dict
The workspace, the plan of the connection of each source in sources, and the plan of each node in nodes; what the workspace holds that the file does not mention in untracked; errors and warnings; and the counts of summary.

Raises

madbot_cli.checks.CheckError
When the file gives values madbot would refuse, once madbot is read and before anything is planned.

plan_connectionfunction#

def plan_connection(state: State, spec: Mapping[str, Any], environ: Mapping[str, str] | None = None, *, source: bool = False) -> dict

Plan the connection to a source or a repository.

Parameters

NameTypeDefaultDescription
stateStaterequiredWhat madbot holds.
specmappingrequiredplugin, shared_params and credentials_env from the file.
environmappingNoneThe environment holding the credentials; os.environ by default.
sourceboolFalseTrue for a source of data: its public data may be read without credentials when its plugin allows it. A repository always needs them to submit.

Returns

dict
plugin (its identifier), shared_params, action (create, connect or unchanged), id and name of an existing connection, credentials (given, missing, not needed or public: a source read without credentials, its public data only).

plan_datafunction#

def plan_data(items: Iterable[Mapping], connection_id: str | None, datalinks: Iterable[Mapping]) -> list[dict]

Plan the data of a source on a node.

Parameters

NameTypeDescription
itemsiterable of mappingThe data of the source, from resolve_data.
connection_idstr or NoneThe connection to the source, None when it is to be created.
datalinksiterable of mappingThe datalinks of the node.

Returns

list of dict
Each data with its action, and for a linked one its datalink, data, status and error.

plan_metadatafunction#

def plan_metadata(wanted: Mapping[str, Mapping], metadata_objects: Iterable[Mapping]) -> dict

Compare the metadata a file gives with those of a submission.

Parameters

NameTypeDescription
wantedmappingThe metadata of the submission in the file.
metadata_objectsiterable of mappingThe metadata objects of the submission, with their metadata_items.

Returns

dict
writes: {object, label, field, current, value, action} for each value to set (create) or to replace (update); unchanged: the count of values already right; missing: {object, label, field, name} for each mandatory field without a value; errors: values for a field of the repository that this object does not have, such as a field of a sample given under the study, left unwritten; warnings: values the file gives for no object.

plan_nodefunction#

def plan_node(client: Client, state: State, node: Node, connections: Mapping[str, Mapping], environ: Mapping[str, str] | None = None, cache: dict | None = None) -> tuple[dict, list[Linked]]

Plan a node of the file, what it holds, and the nodes under it.

The submissions of a node are planned once the nodes under it are, as they take their data in the whole subtree.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client, with the workspace set when it exists.
stateStaterequiredWhat madbot holds.
nodeNoderequiredThe node, from the file.
connectionsmappingrequiredThe plan of the connection of each source of the file, by key.
environmappingNoneThe environment holding the credentials.
cachedictNoneThe folders of the sources already listed.

Returns

tuple
The plan of the node, with its action, the plan of the data of each source it names in sources, its samples, associations, submissions and child nodes, and what madbot holds on the node that the file does not mention (untracked); and the data of the node and of each node under it, for the submissions above.

plan_node_associationsfunction#

def plan_node_associations(client: Client, node: Node, node_id: str | None, datalinks: list[dict], sources: Mapping[str, Mapping], connections: Mapping[str, Mapping], cache: dict | None = None, complete: bool = True) -> dict | None

Plan the associations of a node from the rules of the file.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client, with the workspace set when it exists.
nodeNoderequiredThe node, from the file.
node_idstr or NonerequiredThe node in madbot; None before it is created.
datalinkslist of dictrequiredThe datalinks of the node.
sourcesmappingrequiredThe plan of the data of each source of the node, by key.
connectionsmappingrequiredThe plan of the connection of each source of the file, by key.
cachedictNoneWhat the run already read.
completeboolTrueWhether every data of the node is known: False while a source waits for its connection.

Returns

dict or None
The plan of associations.plan_associations, its later also true when the suggestions of madbot wait for data still to link; None when the file gives no rule.

plan_node_samplesfunction#

def plan_node_samples(node: Node, datalinks: list[dict], samples: list[dict], bound: Mapping[str, set[str]], sources: Mapping[str, Mapping], connections: Mapping[str, Mapping]) -> dict | None

Plan the samples of a node from the rule of the file.

Data still to be linked are counted when their name is known.

Parameters

NameTypeDescription
nodeNodeThe node, from the file.
datalinkslist of dictThe datalinks of the node.
sampleslist of dictThe samples of the node.
boundmappingThe datalinks bound to each sample, by sample identifier.
sourcesmappingThe plan of the data of each source of the node, by key.
connectionsmappingThe plan of the connection of each source of the file, by key.

Returns

dict or None
The plan of operations.plan_samples, each sample with its action; None when the file gives no rule.

plan_sourcesfunction#

def plan_sources(state: State, manifest: Manifest, environ: Mapping[str, str] | None = None) -> tuple[dict[str, dict], list[str]]

Plan the connection of each source of the file, once for the whole file.

Parameters

NameTypeDefaultDescription
stateStaterequiredWhat madbot holds.
manifestManifestrequiredThe deposit file.
environmappingNoneThe environment holding the credentials.

Returns

tuple
The plan of each connection by key, and the errors: two keys that stand for the same connection once their plugins are known.

plan_submissionfunction#

def plan_submission(client: Client, state: State, submission: Submission, subtree: list[Linked], connections: Mapping[str, Mapping], environ: Mapping[str, str] | None = None, cache: dict | None = None) -> dict

Plan a submission of the file.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client, with its workspace set.
stateStaterequiredWhat madbot holds.
submissionSubmissionrequiredThe submission, from the file.
subtreelist of LinkedrequiredThe data of its node, first, then of each node under it.
connectionsmappingrequiredThe plan of the connection of each source of the file, by key.
environmappingNoneThe environment holding the credentials.
cachedictNoneThe folders of the sources already listed.

Returns

dict
title, action, id, status, the repository connection, the data to add or present, those waiting to be read, the entries unresolved until their source is connected, those outside the subtree, the node each data to add or waiting goes through (in the same order, None when the subtree has one node), the data moved (held through another node than the one chosen), not_in_deposit and not_selected, the metadata plan, and errors and warnings of its entries.

read_statefunction#

def read_state(client: Client, manifest: Manifest) -> State

Read the workspace of a deposit file and what it holds.

The client is set to the workspace when it exists.

Parameters

NameTypeDescription
clientClientThe client.
manifestManifestThe deposit file.

Returns

State
The plugins of the instance, and the workspace with the nodes of the file and their children, the connections and the submissions, when it exists.

resolve_datafunction#

def resolve_data(client: Client, entries: Iterable[Mapping], connections: Mapping[str, Mapping], cache: dict | None = None) -> dict

Turn the data entries of a node into external identifiers, by source.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client, with its workspace set.
entriesiterable of mappingrequiredThe data entries of the node.
connectionsmappingrequiredThe plan of the connection of each source of the file, by key.
cachedictNoneThe folders already listed.

Returns

dict
By source key: items (each data found, once), unresolved (the browse entries left for after the connection) and errors.

resolve_entryfunction#

def resolve_entry(client: Client, entry: Mapping, connection: Mapping, cache: dict | None = None) -> dict

Turn a data entry of the file into the external identifiers it designates in its source.

A browse entry is looked for in the source, through the API; it can only be when the account is connected to the source. A name of names the folder does not hold is an error, and the entry then designates nothing: naming a file says it must exist.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client, with its workspace set.
entrymappingrequiredAn external_id or browse entry.
connectionmappingrequiredThe connection of its source, from plan_connection.
cachedictNoneThe folders already listed.

Returns

dict
items: {source, external_id, name, browse} for each data; unresolved: the label of the entry when it waits for the connection, else None; error: what could not be found, else None.

selectfunction#

def select(client: Client, submission: Submission, subtree: list[Linked], connections: Mapping[str, Mapping], cache: dict | None = None) -> dict

Pick the data a submission takes in its node and the nodes under it, entry by entry.

Each entry is resolved like one of a node, then only the data linked to the node of the submission or to a node under it are kept; match and names alone filter those data by their name in madbot. A data enters once, through one node: the only one it is linked to, or the one via names when it is linked to several. A data an entry designates that no node of the subtree holds is reported, never added.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client, with its workspace set.
submissionSubmissionrequiredThe submission, from the file.
subtreelist of LinkedrequiredThe data of its node, first, then of each node under it.
connectionsmappingrequiredThe plan of the connection of each source of the file, by key.
cachedictNoneThe folders already listed.

Returns

dict
datalinks: those taken, each with the path of its node in through; pending: the data taken once linked; unresolved: the entries found once their source is connected, each as {source, entry}; outside: the names of data no node of the subtree holds; errors and warnings.

short_textfunction#

def short_text(text: str, width: int = LABEL_TEXT) -> str

Cut a text to a width, on a word when it can.

Parameters

NameTypeDefaultDescription
textstrrequiredThe text.
widthintLABEL_TEXTThe longest text kept.

Returns

str
The text, or its beginning followed by ….
def source_datalinks(datalinks: Iterable[Mapping], connection_ids: Iterable[str | None]) -> list[dict]

Keep the datalinks whose data come from some connections.

Parameters

NameTypeDescription
datalinksiterable of mappingThe datalinks of a node.
connection_idsiterable of strThe connections.

Returns

list of dict
The datalinks of those connections.

submission_actionsfunction#

def submission_actions(submission: Mapping) -> Iterable[str]

List the actions of a submission as apply counts its changes.

An existing submission is updated only through the data it receives and the metadata values written: those are counted, one each, not the submission on top of them.

Parameters

NameTypeDescription
submissionmappingThe plan of a submission.

Yields

str
Its own action when it is created, deposited or left as is; then a creation per data added, and the action of each metadata value.

summarizefunction#

def summarize(result: Mapping) -> dict

Count what a plan would do.

Parameters

NameTypeDescription
resultmappingA plan.

Returns

dict
create, update, unchanged and deposited counts, to_submit (submissions not sent yet), missing_metadata, missing_association_values (associations whose fields are not all valid), and changes: whether apply would write anything.

values_forfunction#

def values_for(wanted: Mapping[str, Mapping], metadata_object: Mapping) -> dict

Return the values a file gives for one metadata object.

Parameters

NameTypeDescription
wantedmappingThe metadata of the submission in the file.
metadata_objectmappingA metadata object of the submission, with its type and summary_fields.

Returns

dict
Each value by field slug: those of the type, then those of the object found by its alias. An empty value is no value: apply never clears one, a skeleton of madbot-cli fields pasted as it is writes nothing, and a value left empty under by_alias leaves the value of the type in place.

walk_plansfunction#

def walk_plans(nodes: Iterable[Mapping]) -> Iterator[Mapping]

List the plans of the nodes of a plan, at every depth, each before its children.

Parameters

NameTypeDescription
nodesiterable of mappingThe plans of the nodes at the top.

Yields

mapping
The plan of each node.