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
| Name | Description |
|---|---|
BY_ALIAS | The key of a metadata type holding the values of single objects, by alias. |
CONNECT | The action on a connection that exists, for which the account has no working credentials yet. |
CREATE | The action on an object apply creates. |
DEPOSITED | The state of a submission already sent to its repository: madbot cannot change it. |
DEPOSITED_STATUSES | States of a submission sent, or being sent, to its repository. |
LABEL_TEXT | The longest text of a summary field kept in the name of a metadata object. |
UNCHANGED | The action on an object madbot already holds as the file describes it. |
UPDATE | The action on an object apply changes. |
Classes
Functions
| Name | Description |
|---|---|
actions | List the action of every object of a plan. |
association_candidates | List the data of a node an association can take, linked or about to be. |
association_fields | Read the madbot fields the associations of some rules have, once per run. |
browse_folder | List the data of a folder of a source, once per plan. |
by_title | Index objects of madbot by their title, noting the titles given twice. |
entry_label | Describe a data entry of the file for a person. |
existing_node | Find the node madbot holds for a node of the file. |
existing_submission | Find the submission madbot holds for a submission of the file. |
identity | Tell which data a datalink or a data still to link stands for. |
label_of | Name a metadata object for a person. |
missing_names | Name the names a folder does not hold, each with the names close to it. |
moved_data | Find the data a submission holds through another node than the one the file chooses. |
moved_warning | Say that a data goes through another node than the one the file chooses. |
name_of | Name a datalink or a data still to link for a person. |
node_action | Tell what apply does to a node of the file. |
plan | Compare a deposit file with madbot, without writing anything. |
plan_connection | Plan the connection to a source or a repository. |
plan_data | Plan the data of a source on a node. |
plan_metadata | Compare the metadata a file gives with those of a submission. |
plan_node | Plan a node of the file, what it holds, and the nodes under it. |
plan_node_associations | Plan the associations of a node from the rules of the file. |
plan_node_samples | Plan the samples of a node from the rule of the file. |
plan_sources | Plan the connection of each source of the file, once for the whole file. |
plan_submission | Plan a submission of the file. |
read_state | Read the workspace of a deposit file and what it holds. |
resolve_data | Turn the data entries of a node into external identifiers, by source. |
resolve_entry | Turn a data entry of the file into the external identifiers it designates in its source. |
select | Pick the data a submission takes in its node and the nodes under it, entry by entry. |
short_text | Cut a text to a width, on a word when it can. |
source_datalinks | Keep the datalinks whose data come from some connections. |
submission_actions | List the actions of a submission as apply counts its changes. |
summarize | Count what a plan would do. |
values_for | Return the values a file gives for one metadata object. |
walk_plans | List 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 = 40The 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.
datalinksattributeinstance attribute#
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: strThe 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 = Noneread_childrenmethod#
def read_children(client: Client, nodes: Iterable[Node], parent: tuple[str, ...] = ()) -> NoneRead 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
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client, with its workspace set. |
nodes | iterable of Node | required | The nodes of the file under parent. |
parent | tuple 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
| Name | Type | Description |
|---|---|---|
result | mapping | A 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
| Name | Type | Description |
|---|---|---|
datalinks | iterable of mapping | The datalinks of the node. |
sources | mapping | The plan of the data of each source of the node, by key; empty once the data are linked. |
connections | mapping | The plan of the connection of each source of the file, by key. |
keys | iterable of str | The sources of the node. |
Returns
list of dict- Each data as
{name, data, connection};datais None for a data still to link, which also gives the key of itssource(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
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
Returns
list of dictexternal_idandnameof 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
Returns
dict- Each object by its title.
entry_labelfunction#
def entry_label(entry: Mapping) -> strDescribe a data entry of the file for a person.
Parameters
| Name | Type | Description |
|---|---|---|
entry | mapping | A data entry: external_id, browse, match or names. |
Returns
str- Such as
campaign/raw/*.tif,campaign/raw/{a.tif, b.tif}ormatch *.tif.
existing_nodefunction#
def existing_node(state: State, path: tuple[str, ...]) -> dict | NoneFind the node madbot holds for a node of the file.
Parameters
| Name | Type | Description |
|---|---|---|
state | State | What madbot holds. |
path | tuple of str | The path of titles of the node. |
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 | NoneFind 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
| Name | Type | Description |
|---|---|---|
state | State | What madbot holds. |
submission | Submission | The submission, from the file. |
repository_id | str or None | The connection to its repository, None when it is to be created. |
Raises
MadbotError- When madbot has several submissions of that title, or one to another repository.
identityfunction#
def identity(item: Mapping) -> tupleTell which data a datalink or a data still to link stands for.
Parameters
| Name | Type | Description |
|---|---|---|
item | mapping | A 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) -> strName a metadata object for a person.
Parameters
| Name | Type | Description |
|---|---|---|
metadata_object | mapping | A 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]) -> strName the names a folder does not hold, each with the names close to it.
Parameters
| Name | Type | Description |
|---|---|---|
missing | list of str | The names not found. |
found | list of mapping | What 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 asnotes.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
| Name | Type | Description |
|---|---|---|
in_submission | iterable of mapping | The data of the submission, as the API returns them. |
selected | iterable of mapping | The 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) -> strSay that a data goes through another node than the one the file chooses.
Parameters
| Name | Type | Description |
|---|---|---|
label | str | The submission, in the message. |
moved | mapping | A data found by moved_data. |
Returns
str- The warning.
name_offunction#
def name_of(item: Mapping) -> strName a datalink or a data still to link for a person.
Parameters
| Name | Type | Description |
|---|---|---|
item | mapping | A datalink, or a data still to link. |
Returns
str- Its name, else its external identifier.
node_actionfunction#
def node_action(existing: Mapping | None) -> strTell what apply does to a node of the file.
Parameters
| Name | Type | Description |
|---|---|---|
existing | mapping or None | The node madbot holds, None when it has none. |
Returns
strCREATEwithout a node;UPDATEfor a node madbot holds as a draft (madbot_cli.operations.NODE_PENDING), which apply makes a node in use;UNCHANGEDotherwise.
planfunction#
def plan(client: Client, manifest: Manifest, environ: Mapping[str, str] | None = None) -> dictCompare a deposit file with madbot, without writing anything.
Parameters
Returns
dict- The
workspace, the plan of the connection of each source insources, and the plan of each node innodes; what the workspace holds that the file does not mention inuntracked;errorsandwarnings; and the counts ofsummary.
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) -> dictPlan the connection to a source or a repository.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
state | State | required | What madbot holds. |
spec | mapping | required | plugin, shared_params and credentials_env from the file. |
environ | mapping | None | The environment holding the credentials; os.environ by default. |
source | bool | False | True 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
dictplugin(its identifier),shared_params,action(create,connectorunchanged),idandnameof an existing connection,credentials(given,missing,not neededorpublic: 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
Returns
list of dict- Each data with its
action, and for a linked one itsdatalink,data,statusanderror.
plan_metadatafunction#
def plan_metadata(wanted: Mapping[str, Mapping], metadata_objects: Iterable[Mapping]) -> dictCompare the metadata a file gives with those of a submission.
Parameters
| Name | Type | Description |
|---|---|---|
wanted | mapping | The metadata of the submission in the file. |
metadata_objects | iterable of mapping | The metadata objects of the submission, with their
metadata_items. |
Returns
dictwrites:{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
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client, with the workspace set when it exists. |
state | State | required | What madbot holds. |
node | Node | required | The node, from the file. |
connections | mapping | required | The plan of the connection of each source of the file, by key. |
environ | mapping | None | The environment holding the credentials. |
cache | dict | None | The 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 insources, itssamples,associations,submissionsand childnodes, 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 | NonePlan the associations of a node from the rules of the file.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client, with the workspace set when it exists. |
node | Node | required | The node, from the file. |
node_id | str or None | required | The node in madbot; None before it is created. |
datalinks | list of dict | required | The datalinks of the node. |
sources | mapping | required | The plan of the data of each source of the node, by key. |
connections | mapping | required | The plan of the connection of each source of the file, by key. |
cache | dict | None | What the run already read. |
complete | bool | True | Whether every data of the node is known: False while a source waits for its connection. |
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 | NonePlan the samples of a node from the rule of the file.
Data still to be linked are counted when their name is known.
Parameters
| Name | Type | Description |
|---|---|---|
node | Node | The node, from the file. |
datalinks | list of dict | The datalinks of the node. |
samples | list of dict | The samples of the node. |
bound | mapping | The datalinks bound to each sample, by sample identifier. |
sources | mapping | The plan of the data of each source of the node, by key. |
connections | mapping | The plan of the connection of each source of the file, by key. |
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
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) -> dictPlan a submission of the file.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client, with its workspace set. |
state | State | required | What madbot holds. |
submission | Submission | required | The submission, from the file. |
subtree | list of Linked | required | The data of its node, first, then of each node under it. |
connections | mapping | required | The plan of the connection of each source of the file, by key. |
environ | mapping | None | The environment holding the credentials. |
cache | dict | None | The folders of the sources already listed. |
Returns
dicttitle,action,id,status, therepositoryconnection, thedatatoaddorpresent, thosewaitingto be read, the entriesunresolveduntil their source is connected, thoseoutsidethe subtree, the node each data toaddorwaitinggoesthrough(in the same order, None when the subtree has one node), the datamoved(held through another node than the one chosen),not_in_depositandnot_selected, themetadataplan, anderrorsandwarningsof its entries.
read_statefunction#
def read_state(client: Client, manifest: Manifest) -> StateRead the workspace of a deposit file and what it holds.
The client is set to the workspace when it exists.
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) -> dictTurn the data entries of a node into external identifiers, by source.
Parameters
Returns
dict- By source key:
items(each data found, once),unresolved(thebrowseentries left for after the connection) anderrors.
resolve_entryfunction#
def resolve_entry(client: Client, entry: Mapping, connection: Mapping, cache: dict | None = None) -> dictTurn 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
Returns
dictitems:{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) -> dictPick 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
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client, with its workspace set. |
submission | Submission | required | The submission, from the file. |
subtree | list of Linked | required | The data of its node, first, then of each node under it. |
connections | mapping | required | The plan of the connection of each source of the file, by key. |
cache | dict | None | The folders already listed. |
Returns
dictdatalinks: those taken, each with the path of its node inthrough;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;errorsandwarnings.
short_textfunction#
def short_text(text: str, width: int = LABEL_TEXT) -> strCut a text to a width, on a word when it can.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
text | str | required | The text. |
width | int | LABEL_TEXT | The longest text kept. |
Returns
str- The text, or its beginning followed by
….
source_datalinksfunction#
def source_datalinks(datalinks: Iterable[Mapping], connection_ids: Iterable[str | None]) -> list[dict]Keep the datalinks whose data come from some connections.
Parameters
| Name | Type | Description |
|---|---|---|
datalinks | iterable of mapping | The datalinks of a node. |
connection_ids | iterable of str | The 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
| Name | Type | Description |
|---|---|---|
submission | mapping | The 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) -> dictCount what a plan would do.
Parameters
| Name | Type | Description |
|---|---|---|
result | mapping | A plan. |
Returns
dictcreate,update,unchangedanddepositedcounts,to_submit(submissions not sent yet),missing_metadata,missing_association_values(associations whose fields are not all valid), andchanges: whetherapplywould write anything.
values_forfunction#
def values_for(wanted: Mapping[str, Mapping], metadata_object: Mapping) -> dictReturn the values a file gives for one metadata object.
Parameters
| Name | Type | Description |
|---|---|---|
wanted | mapping | The metadata of the submission in the file. |
metadata_object | mapping | A 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 fieldspasted as it is writes nothing, and a value left empty underby_aliasleaves 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
| Name | Type | Description |
|---|---|---|
nodes | iterable of mapping | The plans of the nodes at the top. |
Yields
mapping- The plan of each node.

