madbot_cli.operations
The steps of a deposit in madbot, each with explicit inputs.
Each function takes a Client and the identifiers it works on, and returns
what the API answered. None of them reads a deposit file: a platform that
writes Python can chain them in its own tool. madbot_cli.plan and
madbot_cli.apply chain them from a file.
Functions named find_* and list_* only read; the others write.
Attributes
| Name | Description |
|---|---|
BATCH_SIZE | Most data the API takes in one batch (DATALINK_BATCH_MAX_SIZE of the instance). |
DATA_ACTIVE | State of a data madbot has read from its source. |
DATA_FAILED | State of a data madbot failed to read: see its enrichment_error. |
DATA_IN_PROGRESS | State of a data madbot is reading from its source. |
FOUND_SHOWN | The most names a message gives of what a folder holds instead of the name sought. |
Log | Called with a message for each change and failure. |
NODE_ACTIVE | State of a node in use. |
NODE_PENDING | State of a node madbot was given no state for: the web application shows it |
RATE_LIMITED | The code of a source that refuses requests for a while. |
SEARCH_MIN_LENGTH | The shortest text the API searches for (EXTERNAL_SEARCH_MIN_LENGTH of the instance). |
SUBMISSION_BUSY | States of a submission while madbot works on it in the background. |
Functions
| Name | Description |
|---|---|
activate_node | Make a node created as a draft (NODE_PENDING) a node in use. |
add_submission_data | Add data of a node to a submission. |
alias_of | Make the alias of a sample name, as the web application makes it. |
apply_samples | Create the missing samples of a plan and bind their data to them. |
bind_sample | Bind a data of a node to a sample of that node. |
browse | List what a source holds at one level. |
browse_path | Follow a path of names from the root of a source. |
close_names | Find the names of a folder close to a name, whatever their case. |
connect | Connect the workspace to a source or a repository, or give the account its credentials. |
create_node | Create a node in the workspace of the client, at the top or under a node. |
create_sample | Create a sample in a node. |
create_submission | Create a submission to a repository. |
create_workspace | Create a workspace, owned by the account. |
current_value | Return the value of a metadata item of a submission. |
data_name | Return the name of the data of a datalink. |
error_codes | Give the codes of a madbot error answer and of its details. |
error_message | Return the message of an error the API reports, such as why a data could not be read. |
find_connection | Find the connection of a plugin with given shared parameters. |
find_in_source | Resolve a path of names and a pattern into the external identifiers of a source. |
find_node | Find a node by its title, at the top of the workspace or under a node. |
find_submission | Find a submission of the workspace by its title. |
find_workspace | Find a workspace of the account by its name. |
found_by_search | Find an object a folder does not list, by searching the source. |
found_instead | Say what a folder holds instead of a name not found, in a few names. |
generate_metadata | Ask madbot to make the metadata of a submission again, and check whether it is ready. |
get_submission | Read a submission. |
is_connected | Tell whether the account has working credentials for a connection. |
is_empty | Tell whether a metadata value is empty. |
link_data | Link data of a source to a node, by batches. |
list_child_nodes | List the nodes right under a node. |
list_datalinks | List the data linked to a node. |
list_metadata_objects | List the metadata objects of a submission with their values. |
list_plugins | List the plugins of the instance. |
list_root_nodes | List the nodes at the top of the workspace of the client. |
list_sample_datalinks | Return the datalinks bound to a sample. |
list_samples | List the samples of a node. |
list_submission_data | List the data of a submission. |
list_submissions | List the submissions of the workspace of the client. |
missing_metadata | List the metadata objects of a submission that still lack mandatory values. |
object_id | Return the identifier of an object the API gives either embedded or by identifier. |
plan_samples | Group the data of a node into samples, by the sample group of a pattern. |
plugin_id_of | Return the plugin identifier of a connection. |
reads_without_credentials | Tell whether a plugin reads the public data of its source without credentials. |
resolve_plugin | Find a plugin by its identifier or its slug. |
retry_data | Read again from its source a data madbot failed to read. |
search | Search the data of a source, one page of results. |
set_metadata | Give values to fields of a metadata object of a submission, replacing those already there. |
shared_params_of | Return the shared parameters of a connection as a mapping. |
submit | Send a ready submission to its repository. This cannot be undone. |
wait_for_data | Wait until madbot has read the data of some batches from their sources. |
wait_submission | Wait until madbot no longer works on a submission in the background. |
BATCH_SIZEattributemodule attribute#
BATCH_SIZE = 1000Most data the API takes in one batch (DATALINK_BATCH_MAX_SIZE of the instance).
DATA_ACTIVEattributemodule attribute#
DATA_ACTIVE = 'active'State of a data madbot has read from its source.
DATA_FAILEDattributemodule attribute#
DATA_FAILED = 'failed'State of a data madbot failed to read: see its enrichment_error.
DATA_IN_PROGRESSattributemodule attribute#
DATA_IN_PROGRESS = 'in progress'State of a data madbot is reading from its source.
FOUND_SHOWNattributemodule attribute#
FOUND_SHOWN = 10The most names a message gives of what a folder holds instead of the name sought.
Logattributemodule attribute#
Log = Callable[[str], None]Called with a message for each change and failure.
NODE_ACTIVEattributemodule attribute#
NODE_ACTIVE = 'active'State of a node in use.
NODE_PENDINGattributemodule attribute#
NODE_PENDING = 'pending'State of a node madbot was given no state for: the web application shows it as a draft and asks how to create it.
RATE_LIMITEDattributemodule attribute#
RATE_LIMITED = 'rate_limited'The code of a source that refuses requests for a while.
SEARCH_MIN_LENGTHattributemodule attribute#
SEARCH_MIN_LENGTH = 3The shortest text the API searches for (EXTERNAL_SEARCH_MIN_LENGTH of the instance).
SUBMISSION_BUSYattributemodule attribute#
SUBMISSION_BUSY = {'generating metadata', 'submission in progress'}States of a submission while madbot works on it in the background.
activate_nodefunction#
def activate_node(client: Client, node: Mapping) -> dictMake a node created as a draft (NODE_PENDING) a node in use.
Its title, description and icon are sent back as they are: the API replaces the node with what it is given.
Parameters
| Name | Type | Description |
|---|---|---|
client | Client | The client, with its workspace set. |
node | mapping | The node, as the API gives it. |
Returns
dict- The node.
add_submission_datafunction#
def add_submission_data(client: Client, submission_id: str, datalinks: Iterable[Mapping]) -> list[dict]Add data of a node to a submission.
madbot then makes the metadata objects of the submission again, in the
background: wait for it with wait_submission.
Parameters
Returns
list of dict- The data added.
alias_offunction#
def alias_of(name: str) -> strMake the alias of a sample name, as the web application makes it.
Parameters
| Name | Type | Description |
|---|---|---|
name | str | The sample name. |
Returns
str- The name without its accents, in lower case, with every character
other than a letter, a digit or a space removed, then each run of
spaces replaced by ”-”:
Élevage_1 (A)giveselevage1-a. A sample created in the web application is thus found again.
apply_samplesfunction#
def apply_samples(client: Client, node_id: str, plan: Mapping, log: Log = _silent) -> dictCreate the missing samples of a plan and bind their data to them.
A failure is reported and the next sample or data is handled.
Parameters
Returns
dictcreated,linkedandalready_linkedcounts, thefailures, and for each sample itsalias,idand boundfiles.
bind_samplefunction#
def bind_sample(client: Client, data_id: str, datalink_id: str, sample_id: str) -> boolBind a data of a node to a sample of that node.
Parameters
Returns
bool- True when the binding is new, False when it was already there.
browsefunction#
def browse(client: Client, connection_id: str, parent: str | None = None) -> list[dict]List what a source holds at one level.
Parameters
Returns
list of dict- The objects, each with its
id,name,typeandhas_children.
browse_pathfunction#
def browse_path(client: Client, connection_id: str, path: Iterable[str]) -> str | NoneFollow a path of names from the root of a source.
Parameters
Raises
MadbotError- When a name is not found, with the names found at that level.
close_namesfunction#
def close_names(name: str, names: Iterable[str]) -> list[str]Find the names of a folder close to a name, whatever their case.
Parameters
| Name | Type | Description |
|---|---|---|
name | str | The name sought. |
names | iterable of str | The names the folder holds. |
Returns
list of str- At most
FOUND_SHOWNnames: those equal tonamebut for the case first, such asREADME.mdforreadme.md, then the closest.
connectfunction#
def connect(client: Client, plugin_id: str, shared_params: Mapping[str, Any], private_params: Mapping[str, Any] | None = None, name: str | None = None) -> dictConnect the workspace to a source or a repository, or give the account its credentials.
The API returns the existing connection when one has the same plugin and shared parameters, and stores the credentials of the account for it.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client, with its workspace set. |
plugin_id | str | required | The identifier of the plugin. |
shared_params | mapping | required | The parameters shared by the members, such as the address. |
private_params | mapping | None | The credentials of the account. A repository may go without. |
name | str | None | The name of a new connection; computed by the plugin otherwise. |
Returns
dict- The connection.
create_nodefunction#
def create_node(client: Client, title: str, description: str | None = None, parent: str | None = None) -> dictCreate a node in the workspace of the client, at the top or under a node.
Parameters
Returns
dict- The node.
create_samplefunction#
def create_sample(client: Client, node_id: str, title: str, alias: str) -> dictCreate a sample in a node.
Parameters
Returns
dict- The sample.
create_submissionfunction#
def create_submission(client: Client, title: str, connection_id: str, settings: Mapping[str, Any] | None = None) -> dictCreate a submission to a repository.
Parameters
Returns
dict- The submission.
create_workspacefunction#
def create_workspace(client: Client, name: str) -> dictCreate a workspace, owned by the account.
Returns
dict- The workspace.
current_valuefunction#
def current_value(item: Mapping) -> AnyReturn the value of a metadata item of a submission.
Parameters
| Name | Type | Description |
|---|---|---|
item | mapping | A metadata item, as include_metadata=true lists it. |
Returns
Any- Its value, None when it has none.
data_namefunction#
def data_name(datalink: Mapping) -> strReturn the name of the data of a datalink.
Parameters
| Name | Type | Description |
|---|---|---|
datalink | dict | A datalink as the API lists it. |
Returns
str- The name of its data, "" when it has none.
error_codesfunction#
def error_codes(error: ApiError) -> set[str]Give the codes of a madbot error answer and of its details.
Parameters
| Name | Type | Description |
|---|---|---|
error | ApiError | The failed request. |
Returns
set of str- Its
codeand thecodeof each of itsdetails.
error_messagefunction#
def error_message(error: Any) -> strReturn the message of an error the API reports, such as why a data could not be read.
Parameters
| Name | Type | Description |
|---|---|---|
error | Any | The error: a dict with a message, a text, or None. |
Returns
str- Its message.
find_connectionfunction#
def find_connection(connections: Iterable[Mapping], plugin_id: str, shared_params: Mapping[str, Any]) -> dict | NoneFind the connection of a plugin with given shared parameters.
madbot keeps one connection per plugin and shared parameters in a workspace: this is how a connection is recognised.
Parameters
| Name | Type | Description |
|---|---|---|
connections | iterable of dict | The connections of the workspace. |
plugin_id | str | The identifier of the plugin. |
shared_params | mapping | The shared parameters, such as the address of the source. |
find_in_sourcefunction#
def find_in_source(client: Client, connection_id: str, path: Iterable[str], match: str = '*') -> list[dict]Resolve a path of names and a pattern into the external identifiers of a source.
The external identifier of a data has a format of its own in each plugin; a path of names is easier to write. The source is browsed through the API: nothing is written.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client, with its workspace set. |
connection_id | str | required | The connection to the source. |
path | iterable of str | required | The names of the containers to open, from the root. |
match | str | '*' | A shell-style pattern (*.csv) the names of the objects found
there must match. |
Returns
list of dictexternal_idandnameof each object found, sorted by name.
find_nodefunction#
def find_node(client: Client, title: str, parent: str | None = None) -> dict | NoneFind a node by its title, at the top of the workspace or under a node.
A node has no other name than its title: look for it before creating it, so that a second run creates nothing twice.
Parameters
Raises
MadbotError- When several nodes have that title there: madbot cannot tell which one is meant.
find_submissionfunction#
def find_submission(client: Client, title: str, connection_id: str | None = None) -> dict | NoneFind a submission of the workspace by its title.
A submission has no other name than its title: look for it before creating it, so that a second run creates nothing twice.
Parameters
Raises
MadbotError- When several submissions have that title, or the one of that title
goes to another repository than
connection_id. Two submissions of the same title are refused even when they go to two repositories:connection_idchecks the one found, it does not choose between them, asmadbot-cli plandoes.
find_workspacefunction#
def find_workspace(client: Client, name: str) -> dict | NoneFind a workspace of the account by its name.
found_by_searchfunction#
def found_by_search(client: Client, connection_id: str, name: str, parent: str | None) -> dict | NoneFind an object a folder does not list, by searching the source.
The object found is the one whose identifier or name is name, else
the only result of the search, such as the record a DOI names. Only a
folder that lists nothing is searched, such as the root of Zenodo.
Parameters
found_insteadfunction#
def found_instead(name: str, children: list[dict]) -> strSay what a folder holds instead of a name not found, in a few names.
A folder may hold thousands of objects: the message gives those close to the name sought, else the first ones, and how many there are.
Parameters
| Name | Type | Description |
|---|---|---|
name | str | The name sought. |
children | list of dict | What the folder holds. |
Returns
str- Such as
Close to it: raw, raw-old (1,204 in all; madbot-cli browse lists them).
generate_metadatafunction#
def generate_metadata(client: Client, submission_id: str) -> dictAsk madbot to make the metadata of a submission again, and check whether it is ready.
Parameters
Returns
dict- The submission, generating its metadata.
get_submissionfunction#
def get_submission(client: Client, submission_id: str) -> dictRead a submission.
Parameters
Returns
dict- The submission, with its
status,errors,error_reasonandexternal_identifiers.
is_connectedfunction#
def is_connected(connection: Mapping) -> boolTell whether the account has working credentials for a connection.
Parameters
| Name | Type | Description |
|---|---|---|
connection | dict | A connection as the API returns it. |
Returns
bool- True when its credential is
connected.
is_emptyfunction#
def is_empty(value: Any) -> boolTell whether a metadata value is empty.
Parameters
| Name | Type | Description |
|---|---|---|
value | Any | The value. |
Returns
bool- True for None, an empty text, an EditorJS text without blocks (as
the web client saves a cleared description, with its
timeandversion), or a mapping or a list whose parts are all empty (none at all included).
link_datafunction#
def link_data(client: Client, node_id: str, connection_id: str, items: Iterable[Mapping[str, str]], batch_size: int = BATCH_SIZE) -> list[dict]Link data of a source to a node, by batches.
Data already linked to the node are left as they are. madbot then reads
each new data from its source in a background task: wait for it with
wait_for_data.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client, with its workspace set. |
node_id | str | required | The node. |
connection_id | str | required | The connection to the source. |
items | iterable of mapping | required | Each data, as {"external_id": ..., "name": ...}; the name is
optional and shown until the data is read. |
batch_size | int | BATCH_SIZE | Most data sent in one request. |
Returns
list of dict- The answer to each batch:
task_id,created,existingandresults.
list_child_nodesfunction#
def list_child_nodes(client: Client, parent_id: str) -> list[dict]List the nodes right under a node.
Parameters
Returns
list of dict- Its child nodes, not their own children.
list_datalinksfunction#
def list_datalinks(client: Client, node_id: str, status: str | None = None) -> list[dict]List the data linked to a node.
Parameters
Returns
list of dict- The datalinks, each with its
data(id,name,connection,external_id,status,enrichment_error).
list_metadata_objectsfunction#
def list_metadata_objects(client: Client, submission_id: str) -> list[dict]List the metadata objects of a submission with their values.
Parameters
Returns
list of dict- Each object with its
type,summary_fieldsandmetadata_items: the field (slug,level) and its value.
list_pluginsfunction#
def list_plugins(client: Client) -> list[dict]List the plugins of the instance.
Parameters
| Name | Type | Description |
|---|---|---|
client | Client | The client. |
Returns
list of dict- The plugins, each with its
id(the path a connection names) and itsslug.
list_root_nodesfunction#
def list_root_nodes(client: Client) -> list[dict]List the nodes at the top of the workspace of the client.
Parameters
| Name | Type | Description |
|---|---|---|
client | Client | The client, with its workspace set. |
Returns
list of dict- The nodes without a parent.
list_sample_datalinksfunction#
def list_sample_datalinks(client: Client, node_id: str, sample_id: str) -> set[str]Return the datalinks bound to a sample.
Parameters
Returns
set of str- The identifiers of its datalinks.
list_samplesfunction#
def list_samples(client: Client, node_id: str) -> list[dict]List the samples of a node.
Returns
list of dict- The samples, each with its
id,aliasandtitle.
list_submission_datafunction#
def list_submission_data(client: Client, submission_id: str) -> list[dict]List the data of a submission.
Parameters
Returns
list of dict- Each data of the submission, with its
dataanddatalink.
list_submissionsfunction#
def list_submissions(client: Client) -> list[dict]List the submissions of the workspace of the client.
Parameters
| Name | Type | Description |
|---|---|---|
client | Client | The client, with its workspace set. |
Returns
list of dict- Each submission, with its
title,statusandconnection.
missing_metadatafunction#
def missing_metadata(client: Client, submission_id: str) -> list[dict]List the metadata objects of a submission that still lack mandatory values.
Give the values with set_metadata, object by object. Once none is
left, generate_metadata has madbot check the submission as a whole.
Parameters
Returns
list of dict- Each object with mandatory fields still empty: its
id,typeandsummary_fields(such as its alias), andmissing, each field (slug,name,description) to fill.
object_idfunction#
def object_id(value: Any) -> str | NoneReturn the identifier of an object the API gives either embedded or by identifier.
Parameters
| Name | Type | Description |
|---|---|---|
value | Any | A dict with an id, an identifier, or None. |
plan_samplesfunction#
def plan_samples(datalinks: Iterable[Mapping], samples: Iterable[Mapping], pattern: re.Pattern | str, bound: Mapping[str, set[str]] | None = None) -> dictGroup the data of a node into samples, by the sample group of a pattern.
Nothing is read nor written: the inputs come from the API.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
datalinks | iterable of dict | required | The datalinks the samples are made from. |
samples | iterable of dict | required | The samples already on the node, found again by their alias, else by their title. |
pattern | re.Pattern or str | required | A regular expression with a sample group, searched in each data name. |
bound | mapping | None | The datalinks already bound to each sample, by sample identifier. |
Returns
dictsamples: one entry per sample,{alias, title, exists, sample_id, files: [{name, datalink, data, linked}]};ignored: the names that do not match;warnings: names that give the same alias.
plugin_id_offunction#
def plugin_id_of(connection: Mapping) -> str | NoneReturn the plugin identifier of a connection.
Parameters
| Name | Type | Description |
|---|---|---|
connection | dict | A connection as the API returns it. |
reads_without_credentialsfunction#
def reads_without_credentials(plugin: Mapping) -> boolTell whether a plugin reads the public data of its source without credentials.
Parameters
| Name | Type | Description |
|---|---|---|
plugin | dict | A plugin as GET /api/plugins returns it. |
Returns
bool- True for a data plugin with
supports_anonymous_access; false with an API older than this field.
resolve_pluginfunction#
def resolve_plugin(plugins: Iterable[Mapping], reference: str) -> dictFind a plugin by its identifier or its slug.
Parameters
| Name | Type | Description |
|---|---|---|
plugins | iterable of dict | The plugins of the instance, from list_plugins. |
reference | str | The identifier of the plugin (its Python path) or its slug. |
Returns
dict- The plugin.
Raises
MadbotError- When the instance has no such plugin.
retry_datafunction#
def retry_data(client: Client, node_id: str, datalink_id: str) -> dictRead again from its source a data madbot failed to read.
Parameters
Returns
dicttask_idof the new reading, and thedatalink.
searchfunction#
def search(client: Client, connection_id: str, query: str, parent: str | None = None) -> dictSearch the data of a source, one page of results.
A source that serves public data to anyone, such as Zenodo, lists
nothing at its root: its records are found by searching, by identifier,
DOI or title. Follows GET /api/connections/{id}/external_data/search.
Parameters
Returns
dictcount, the number of results in all,results, the first page (each object with itsid,name,typeandhas_children), andmessage, why it is empty when the source says so.
set_metadatafunction#
def set_metadata(client: Client, submission_id: str, object_id: str, values: Mapping[str, Any]) -> list[dict]Give values to fields of a metadata object of a submission, replacing those already there.
Parameters
Returns
list of dict- The metadata of the object.
shared_params_offunction#
def shared_params_of(connection: Mapping) -> dict[str, str]Return the shared parameters of a connection as a mapping.
Parameters
| Name | Type | Description |
|---|---|---|
connection | dict | A connection as the API returns it. |
Returns
dict- Each shared parameter by its key.
submitfunction#
def submit(client: Client, submission_id: str) -> dictSend a ready submission to its repository. This cannot be undone.
Parameters
Returns
dict- The submission, being submitted: wait for it with
wait_submission.
wait_for_datafunction#
def wait_for_data(client: Client, task_ids: Iterable[str | None], **wait: Any) -> list[dict]Wait until madbot has read the data of some batches from their sources.
Parameters
Returns
list of dict- Each task, with its
result:enrichedandfailedcounts, and the outcome of each data initems.
wait_submissionfunction#
def wait_submission(client: Client, submission_id: str, *, timeout: float = 1800, interval: float = 2, on_wait: Callable[[float], None] | None = None) -> dictWait until madbot no longer works on a submission in the background.
Parameters
Returns
dict- The submission, neither generating its metadata nor being submitted.
Raises
WaitTimeout- When it is still busy after
timeoutseconds.

