Skip to content

madbot_cli.apply

Make madbot hold what a deposit file describes.

apply follows the order of a deposit: the workspace, the connections, then each node under its parent with its data (and the wait until madbot has read them), its samples, its child nodes, and last its submissions, which take their data in the whole subtree. Each step plans again from what madbot holds, so an apply stopped half way is run again and goes on where it stopped, without creating anything twice.

An object that fails does not stop the others. When madbot cannot be reached any more, once the client has tried again, apply stops: the report says so, and running it again goes on where it stopped.

Nothing is ever deleted. A submission goes to its repository only when asked (submit=True): a deposit cannot be taken back. A submission already deposited is never changed.

Attributes

NameDescription
DATA_COUNT_INTERVALSeconds between two counts of the data madbot has read, while waiting.
DEFAULT_TIMEOUTHow long to wait for what madbot does in the background, in seconds.
LogCalled with a line for each change, warning and failure.
RESUMEWhat the report says when apply stops on a lost connection.

Classes

NameDescription
ApplierApply a deposit file, step after step.
ConnectionLostmadbot cannot be reached any more: apply stops, the report says so.
DataCountSays how many data of a node madbot has read, while apply waits for them.
ReportWhat an apply did: the changes, the failures, and the state of each submission.

Functions

NameDescription
applyMake madbot hold what a deposit file describes, without deleting anything.
credentials_hintSay how to give the credentials of a connection.
failure_messageReturn why the last task madbot ran on a submission failed.
node_outcomeCount the data of a node by their state, for the summary of apply.

DATA_COUNT_INTERVALattributemodule attribute#

DATA_COUNT_INTERVAL = 10

Seconds between two counts of the data madbot has read, while waiting.

DEFAULT_TIMEOUTattributemodule attribute#

DEFAULT_TIMEOUT = 1800

How long to wait for what madbot does in the background, in seconds.

Logattributemodule attribute#

Log = Callable[[str], None]

Called with a line for each change, warning and failure.

RESUMEattributemodule attribute#

RESUME = 'apply stopped: madbot cannot be reached. Run apply again once it can: it goes on from what madbot holds, and creates nothing twice.'

What the report says when apply stops on a lost connection.

Applierclass#

class Applier(client: Client, manifest: Manifest, *, submit: bool = False, retry_failed: bool = False, environ: Mapping[str, str] | None = None, log: Log = _silent, timeout: float = DEFAULT_TIMEOUT, progress: Progress | None = None, verbose: bool = False)

Apply a deposit file, step after step.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client.
manifestManifestrequiredThe deposit file.
submitboolFalseSend the ready submissions to their repositories.
retry_failedboolFalseRead again the data madbot failed to read.
environmappingNoneThe environment holding the credentials; os.environ by default.
logcallable_silentCalled with a line for each change, warning and failure.
timeoutfloatDEFAULT_TIMEOUTSeconds to wait for what madbot does in the background.
progressProgressNoneSays the step under way; none by default.
verboseboolFalseLog the request and the answer of the API under a failure.

cacheattributeinstance attribute#

cache: dict = {}

The folders of the sources already listed.

clientattributeinstance attribute#

client = client

connectionsattributeinstance attribute#

connections: dict[str, dict] = {}

The connection of each source of the file, by key: its id, None when it failed.

environattributeinstance attribute#

environ = os.environ if environ is None else environ

manifestattributeinstance attribute#

manifest = manifest

progressattributeinstance attribute#

progress = progress

reportattributeinstance attribute#

report = Report(log, verbose=verbose)

retry_failedattributeinstance attribute#

retry_failed = retry_failed

stateattributeinstance attribute#

state: State | None = None

submitattributeinstance attribute#

submit = submit

timeoutattributeinstance attribute#

timeout = timeout

unlistedattributeinstance attribute#

unlisted: dict[tuple[str, ...], set[str]] = {}

For each node some data of which could not be found, the keys of their sources.

add_datamethod#

def add_data(submission: Submission, submission_id: str, subtree: list[Linked], label: str) -> tuple[bool, list[dict]]

Add to a submission the data it selects that it does not hold yet.

A data an entry designates that the node does not hold is a failure, and is not added. A data the submission holds through another node than the one the file chooses is a warning: apply does not move it.

Parameters

NameTypeDescription
submissionSubmissionThe submission, from the file.
submission_idstrIts identifier.
subtreelist of LinkedThe data of its node, then of each node under it.
labelstrThe submission, in the messages.

Returns

tuple
True when data were added; and the data held through another node, from moved_data.

apply_associationsmethod#

def apply_associations(node: Node, node_id: str, datalinks: list[dict]) -> None

Create the associations of a node and write the values of their fields.

They are made before the submissions above, which give each data its associations.

Parameters

NameTypeDescription
nodeNodeThe node, from the file.
node_idstrIts identifier.
datalinkslist of dictThe datalinks of the node.

apply_nodemethod#

def apply_node(node: Node, parent_id: str | None = None) -> Linked

Apply a node and what it holds, its submissions aside.

Parameters

NameTypeDefaultDescription
nodeNoderequiredThe node, from the file.
parent_idstrNoneThe node it goes under; the top of the workspace when None.

Returns

Linked
The data of the node, once linked and bound to their samples.

apply_samplesmethod#

def apply_samples(node: Node, node_id: str, datalinks: list[dict]) -> None

Create the samples of a node and bind their data to them.

Parameters

NameTypeDescription
nodeNodeThe node, from the file.
node_idstrIts identifier.
datalinkslist of dictThe datalinks of the node.

apply_sourcesmethod#

def apply_sources(node: Node, node_id: str) -> None

Link the data of a node, and wait until madbot has read them.

Parameters

NameTypeDescription
nodeNodeThe node, from the file.
node_idstrIts identifier.

apply_submissionmethod#

def apply_submission(submission: Submission, subtree: list[Linked]) -> None

Prepare a submission, and send it to its repository when asked.

Parameters

NameTypeDescription
submissionSubmissionThe submission, from the file.
subtreelist of LinkedThe data of its node, then of each node under it.

apply_treemethod#

def apply_tree(node: Node, parent_id: str | None = None) -> tuple[list[Linked], list[str]]

Apply a node, then the nodes under it, then the submissions of the node.

A node that fails does not stop its siblings; the nodes under it are left for the next apply. The submissions of the nodes above it are left too: their data would miss those of the failed node, and a deposit cannot be completed once sent.

Parameters

NameTypeDefaultDescription
nodeNoderequiredThe node, from the file.
parent_idstrNoneThe node it goes under; the top of the workspace when None.

Returns

tuple
The data of the node and of each node under it, empty when the node failed; and the nodes of the subtree that failed, for a person.

can_submitmethod#

def can_submit(submission: Submission) -> bool

Tell whether the account has credentials for the repository of a submission.

Parameters

NameTypeDescription
submissionSubmissionThe submission, from the file.

Returns

bool
True when the connection is connected; madbot refuses a submission otherwise.

connect_sourcesmethod#

def connect_sources() -> None

Connect the sources of the file, once for the whole file.

describemethodstaticmethod#

def describe(submission: Mapping) -> dict

Keep what a person needs to know of a submission.

Parameters

NameTypeDescription
submissionmappingThe submission, as the API returns it.

Returns

dict
Its id, status, error_reason and external_identifiers.

ensure_connectionmethod#

def ensure_connection(spec: Mapping[str, Any], label: str, *, source: bool = False) -> str | None

Connect to a source or a repository when the account is not yet.

Parameters

NameTypeDefaultDescription
specmappingrequiredplugin, shared_params and credentials_env from the file.
labelstrrequiredWhat the connection is for, in the messages.
sourceboolFalseTrue for a source of data, which may be read without credentials.

Returns

str or None
The identifier of the connection; None when it could not be made.

not_readymethod#

def not_ready(submission: Submission, current: Mapping, label: str, outcome: dict) -> None

Report why a submission is not ready: a failure when it was to be sent.

Parameters

NameTypeDescription
submissionSubmissionThe submission, from the file.
currentmappingThe submission, as the API returns it.
labelstrThe submission, in the messages.
outcomedictThe report of the submission, given the missing values.

open_submissionmethod#

def open_submission(submission: Submission, label: str) -> str | None

Find a submission of the file, or create it with its repository.

Parameters

NameTypeDescription
submissionSubmissionThe submission, from the file.
labelstrThe submission, in the messages.

Returns

str or None
Its identifier; None when its repository could not be connected.

prepare_metadatamethod#

def prepare_metadata(submission: Submission, submission_id: str, label: str, changed: bool) -> dict

Write the metadata of the file, then have madbot check the submission.

Parameters

NameTypeDescription
submissionSubmissionThe submission, from the file.
submission_idstrIts identifier.
labelstrThe submission, in the messages.
changedboolWhether data were added.

Returns

dict
The submission, checked: ready or draft.

runmethod#

def run() -> dict

Apply the file.

Returns

dict
The report: changes, warnings, failures, and for each submission its title, status, submitted and external_identifiers; stopped when madbot could not be reached any more.

sendmethod#

def send(submission_id: str, label: str, outcome: dict) -> None

Send a ready submission to its repository, and wait for its answer.

Parameters

NameTypeDescription
submission_idstrThe submission.
labelstrThe submission, in the messages.
outcomedictThe report of the submission.

settle_submissionmethod#

def settle_submission(submission: Submission, subtree: list[Linked], label: str, outcome: dict[str, Any]) -> None

Open a submission, give it its data and metadata, and send it when asked.

Parameters

NameTypeDescription
submissionSubmissionThe submission, from the file.
subtreelist of LinkedThe data of its node, then of each node under it.
labelstrThe submission, in the messages.
outcomedictThe report of the submission, completed as it goes.

stepmethod#

def step(text: str) -> None

Say the step under way, when the progress is shown.

Parameters

NameTypeDescription
textstrWhat apply does.

stepsmethod#

def steps() -> None

Apply the workspace, the sources, then each node, in order.

unlisted_formethod#

def unlisted_for(node: Node, submission: Submission) -> list[str]

Name the nodes of a subtree that miss data of the file a submission takes.

Parameters

NameTypeDescription
nodeNodeThe node of the submission.
submissionSubmissionThe submission, from the file.

Returns

list of str
The nodes, under node or node itself, some data of which were not found in a source the submission takes data from.

wait_for_datamethod#

def wait_for_data(node: Node, node_id: str, tasks: list[str]) -> None

Wait until madbot has read the data of a node.

Parameters

NameTypeDescription
nodeNodeThe node, from the file.
node_idstrIts identifier.
taskslist of strThe tasks reading them; an empty one is left.

waitingmethod#

def waiting(text: str) -> Callable[[float], None] | None

Return what says a wait, from its first pause: a quick answer says nothing.

Parameters

NameTypeDescription
textstrWhat madbot does meanwhile.

Returns

callable or None
Called with the seconds waited, at each pause; None when the progress is not shown.

waiting_for_datamethod#

def waiting_for_data(node: Node, node_id: str) -> DataCount | None

Return what says how many data madbot has read, while waiting for them.

Parameters

NameTypeDescription
nodeNodeThe node, from the file.
node_idstrIts identifier.

Returns

DataCount or None
Called with the seconds waited, at each pause; None when the progress is not shown.

write_metadatamethod#

def write_metadata(submission: Submission, submission_id: str, label: str) -> bool

Write the values of the file that differ from those of a submission.

Parameters

NameTypeDescription
submissionSubmissionThe submission, from the file.
submission_idstrIts identifier.
labelstrThe submission, in the messages.

Returns

bool
True when a value was written.

ConnectionLostclass#

class ConnectionLost(Exception)

Bases: Exception

madbot cannot be reached any more: apply stops, the report says so.

It derives from neither ApiError nor MadbotError, so that the steps that go on after a failure let it through.

DataCountclass#

class DataCount(client: Client, progress: Progress, node: Node, node_id: str)

Says how many data of a node madbot has read, while apply waits for them.

At the first pause, the data being read are listed; they are counted again every DATA_COUNT_INTERVAL seconds at most. A quick answer reads and says nothing.

Parameters

NameTypeDescription
clientClientThe client.
progressProgressWhere to say the count.
nodeNodeThe node, from the file.
node_idstrIts identifier.

atattributeinstance attribute#

at = 0.0

When they were last counted.

clientattributeinstance attribute#

client = client

elapsedattributeinstance attribute#

elapsed = 0.0

labelattributeinstance attribute#

label = node {node.label}: madbot reads

node_idattributeinstance attribute#

node_id = node_id

pendingattributeinstance attribute#

pending: set | None = None

The data being read at the first pause; None before it.

progressattributeinstance attribute#

progress = progress

readattributeinstance attribute#

read = 0

finishmethod#

def finish() -> None

Say the data are all read, once the wait is over, when it said anything.

readingmethod#

def reading() -> set

Return the identifiers of the data madbot is still reading.

saymethod#

def say() -> None

Write where the data stand.

Reportclass#

class Report(log: Log, *, verbose: bool = False)

What an apply did: the changes, the failures, and the state of each submission.

Parameters

NameTypeDefaultDescription
logcallablerequiredCalled with a line for each change, warning and failure.
verboseboolFalseLog the request and the answer of the API under a failure.

change_countattributeinstance attribute#

change_count = 0

The objects and values written: a line of changes may stand for several.

changesattributeinstance attribute#

changes: list[str] = []

failure_detailsattributeinstance attribute#

failure_details: list[dict] = []

failuresattributeinstance attribute#

failures: list[str] = []

logattributeinstance attribute#

log = log

nodesattributeinstance attribute#

nodes: list[dict] = []

stoppedattributeinstance attribute#

stopped: str | None = None

submissionsattributeinstance attribute#

submissions: list[dict] = []

verboseattributeinstance attribute#

verbose = verbose

warningsattributeinstance attribute#

warnings: list[str] = []

as_dictmethod#

def as_dict() -> dict

Return the report.

Returns

dict
changes, change_count (the objects and values written, a change of several data counting each), warnings, failures; failure_details, each failure with its object, its message and the request and answer of the api (None when the API did not answer it); nodes, submissions; and stopped, why apply stopped before the end (None when it did not).

changemethod#

def change(message: str, count: int = 1) -> None

Record a change.

Parameters

NameTypeDefaultDescription
messagestrrequiredWhat was done.
countint1The objects or values it wrote, such as the data of one batch.

failmethod#

def fail(what: str, error: Exception | str, api: Mapping | None = None) -> None

Record a failure; apply goes on with the next object.

Parameters

NameTypeDefaultDescription
whatstrrequiredThe object that failed, as the file names it.
errorException or strrequiredWhy. An ApiError is said in a sentence; its request and answer go to the details, and to the log with verbose.
apimappingNoneThe request and the answer, when error is already a sentence.

Raises

ConnectionLost
When error says madbot cannot be reached: apply stops.

recordmethod#

def record(what: str | None, error: Exception | str, api: Mapping | None = None) -> None

Record a failure, in the summary, the details and the log.

Parameters

NameTypeDefaultDescription
whatstr or NonerequiredThe object that failed, as the file names it; None out of an object.
errorException or strrequiredWhy; see fail.
apimappingNoneThe request and the answer, when error is already a sentence.

stopmethod#

def stop() -> None

Record that apply stops, madbot being out of reach.

warnmethod#

def warn(message: str) -> None

Record something to look at, which is not a failure.

Parameters

NameTypeDescription
messagestrWhat to look at.

applyfunction#

def apply(client: Client, manifest: Manifest, *, submit: bool = False, retry_failed: bool = False, environ: Mapping[str, str] | None = None, log: Log = _silent, timeout: float = DEFAULT_TIMEOUT, progress: Progress | None = None, verbose: bool = False) -> dict

Make madbot hold what a deposit file describes, without deleting anything.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client.
manifestManifestrequiredThe deposit file.
submitboolFalseSend the ready submissions to their repositories. This cannot be undone.
retry_failedboolFalseRead again the data madbot failed to read.
environmappingNoneThe environment holding the credentials; os.environ by default.
logcallable_silentCalled with a line for each change, warning and failure.
timeoutfloatDEFAULT_TIMEOUTSeconds to wait for what madbot does in the background.
progressProgressNoneSays the step under way; none by default.
verboseboolFalseLog the request and the answer of the API under a failure.

Returns

dict
changes, warnings, failures with their failure_details, and stopped when madbot could not be reached any more; for each node its label and its data read, reading and failed_data, failed True when the node itself failed; and for each submission its title, status, submitted, external_identifiers and the mandatory metadata missing (left True when a node under it failed, failed True when it failed itself).

credentials_hintfunction#

def credentials_hint(spec: Mapping[str, Any]) -> str

Say how to give the credentials of a connection.

Parameters

NameTypeDescription
specmappingThe source or the repository, from the file.

Returns

str
The variable to set, or that the file must name one.

failure_messagefunction#

def failure_message(submission: Mapping) -> str | None

Return why the last task madbot ran on a submission failed.

Parameters

NameTypeDescription
submissionmappingThe submission, as the API returns it.

Returns

str or None
The message of the failure, such as what the repository answered; None when the last task did not fail.

node_outcomefunction#

def node_outcome(node: Node, linked: Linked) -> dict

Count the data of a node by their state, for the summary of apply.

Parameters

NameTypeDescription
nodeNodeThe node, from the file.
linkedLinkedIts data, as apply leaves them.

Returns

dict
Its label, and its data read, still reading and failed_data.