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
| Name | Description |
|---|---|
DATA_COUNT_INTERVAL | Seconds between two counts of the data madbot has read, while waiting. |
DEFAULT_TIMEOUT | How long to wait for what madbot does in the background, in seconds. |
Log | Called with a line for each change, warning and failure. |
RESUME | What the report says when apply stops on a lost connection. |
Classes
| Name | Description |
|---|---|
Applier | Apply a deposit file, step after step. |
ConnectionLost | madbot cannot be reached any more: apply stops, the report says so. |
DataCount | Says how many data of a node madbot has read, while apply waits for them. |
Report | What an apply did: the changes, the failures, and the state of each submission. |
Functions
| Name | Description |
|---|---|
apply | Make madbot hold what a deposit file describes, without deleting anything. |
credentials_hint | Say how to give the credentials of a connection. |
failure_message | Return why the last task madbot ran on a submission failed. |
node_outcome | Count the data of a node by their state, for the summary of apply. |
DATA_COUNT_INTERVALattributemodule attribute#
DATA_COUNT_INTERVAL = 10Seconds between two counts of the data madbot has read, while waiting.
DEFAULT_TIMEOUTattributemodule attribute#
DEFAULT_TIMEOUT = 1800How 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
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client. |
manifest | Manifest | required | The deposit file. |
submit | bool | False | Send the ready submissions to their repositories. |
retry_failed | bool | False | Read again the data madbot failed to read. |
environ | mapping | None | The environment holding the credentials; os.environ by default. |
log | callable | _silent | Called with a line for each change, warning and failure. |
timeout | float | DEFAULT_TIMEOUT | Seconds to wait for what madbot does in the background. |
progress | Progress | None | Says the step under way; none by default. |
verbose | bool | False | Log 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 = clientconnectionsattributeinstance 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 environmanifestattributeinstance attribute#
manifest = manifestprogressattributeinstance attribute#
progress = progressreportattributeinstance attribute#
report = Report(log, verbose=verbose)retry_failedattributeinstance attribute#
retry_failed = retry_failedstateattributeinstance attribute#
state: State | None = Nonesubmitattributeinstance attribute#
submit = submittimeoutattributeinstance attribute#
timeout = timeoutunlistedattributeinstance 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
| Name | Type | Description |
|---|---|---|
submission | Submission | The submission, from the file. |
submission_id | str | Its identifier. |
subtree | list of Linked | The data of its node, then of each node under it. |
label | str | The 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]) -> NoneCreate 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.
apply_nodemethod#
def apply_node(node: Node, parent_id: str | None = None) -> LinkedApply a node and what it holds, its submissions aside.
Parameters
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]) -> NoneCreate the samples of a node and bind their data to them.
apply_sourcesmethod#
def apply_sources(node: Node, node_id: str) -> NoneLink the data of a node, and wait until madbot has read them.
apply_submissionmethod#
def apply_submission(submission: Submission, subtree: list[Linked]) -> NonePrepare a submission, and send it to its repository when asked.
Parameters
| Name | Type | Description |
|---|---|---|
submission | Submission | The submission, from the file. |
subtree | list of Linked | The 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
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) -> boolTell whether the account has credentials for the repository of a submission.
Parameters
| Name | Type | Description |
|---|---|---|
submission | Submission | The submission, from the file. |
Returns
bool- True when the connection is connected; madbot refuses a submission otherwise.
connect_sourcesmethod#
def connect_sources() -> NoneConnect the sources of the file, once for the whole file.
describemethodstaticmethod#
def describe(submission: Mapping) -> dictKeep what a person needs to know of a submission.
Parameters
| Name | Type | Description |
|---|---|---|
submission | mapping | The submission, as the API returns it. |
Returns
dict- Its
id,status,error_reasonandexternal_identifiers.
ensure_connectionmethod#
def ensure_connection(spec: Mapping[str, Any], label: str, *, source: bool = False) -> str | NoneConnect to a source or a repository when the account is not yet.
Parameters
not_readymethod#
def not_ready(submission: Submission, current: Mapping, label: str, outcome: dict) -> NoneReport why a submission is not ready: a failure when it was to be sent.
Parameters
| Name | Type | Description |
|---|---|---|
submission | Submission | The submission, from the file. |
current | mapping | The submission, as the API returns it. |
label | str | The submission, in the messages. |
outcome | dict | The report of the submission, given the missing values. |
open_submissionmethod#
def open_submission(submission: Submission, label: str) -> str | NoneFind a submission of the file, or create it with its repository.
Parameters
| Name | Type | Description |
|---|---|---|
submission | Submission | The submission, from the file. |
label | str | The submission, in the messages. |
prepare_metadatamethod#
def prepare_metadata(submission: Submission, submission_id: str, label: str, changed: bool) -> dictWrite the metadata of the file, then have madbot check the submission.
Parameters
| Name | Type | Description |
|---|---|---|
submission | Submission | The submission, from the file. |
submission_id | str | Its identifier. |
label | str | The submission, in the messages. |
changed | bool | Whether data were added. |
Returns
dict- The submission, checked:
readyordraft.
runmethod#
def run() -> dictApply the file.
Returns
dict- The report:
changes,warnings,failures, and for each submission itstitle,status,submittedandexternal_identifiers;stoppedwhen madbot could not be reached any more.
sendmethod#
def send(submission_id: str, label: str, outcome: dict) -> NoneSend a ready submission to its repository, and wait for its answer.
settle_submissionmethod#
def settle_submission(submission: Submission, subtree: list[Linked], label: str, outcome: dict[str, Any]) -> NoneOpen a submission, give it its data and metadata, and send it when asked.
Parameters
| Name | Type | Description |
|---|---|---|
submission | Submission | The submission, from the file. |
subtree | list of Linked | The data of its node, then of each node under it. |
label | str | The submission, in the messages. |
outcome | dict | The report of the submission, completed as it goes. |
stepmethod#
def step(text: str) -> NoneSay the step under way, when the progress is shown.
Parameters
| Name | Type | Description |
|---|---|---|
text | str | What apply does. |
stepsmethod#
def steps() -> NoneApply 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
| Name | Type | Description |
|---|---|---|
node | Node | The node of the submission. |
submission | Submission | The submission, from the file. |
Returns
list of str- The nodes, under
nodeornodeitself, 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]) -> NoneWait until madbot has read the data of a node.
waitingmethod#
def waiting(text: str) -> Callable[[float], None] | NoneReturn what says a wait, from its first pause: a quick answer says nothing.
Parameters
| Name | Type | Description |
|---|---|---|
text | str | What madbot does meanwhile. |
waiting_for_datamethod#
def waiting_for_data(node: Node, node_id: str) -> DataCount | NoneReturn what says how many data madbot has read, while waiting for them.
write_metadatamethod#
def write_metadata(submission: Submission, submission_id: str, label: str) -> boolWrite the values of the file that differ from those of a submission.
Parameters
| Name | Type | Description |
|---|---|---|
submission | Submission | The submission, from the file. |
submission_id | str | Its identifier. |
label | str | The 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
atattributeinstance attribute#
at = 0.0When they were last counted.
clientattributeinstance attribute#
client = clientelapsedattributeinstance attribute#
elapsed = 0.0labelattributeinstance attribute#
label = node {node.label}: madbot readsnode_idattributeinstance attribute#
node_id = node_idpendingattributeinstance attribute#
pending: set | None = NoneThe data being read at the first pause; None before it.
progressattributeinstance attribute#
progress = progressreadattributeinstance attribute#
read = 0Reportclass#
class Report(log: Log, *, verbose: bool = False)What an apply did: the changes, the failures, and the state of each submission.
Parameters
change_countattributeinstance attribute#
change_count = 0The 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 = lognodesattributeinstance attribute#
nodes: list[dict] = []stoppedattributeinstance attribute#
stopped: str | None = Nonesubmissionsattributeinstance attribute#
submissions: list[dict] = []verboseattributeinstance attribute#
verbose = verbosewarningsattributeinstance attribute#
warnings: list[str] = []as_dictmethod#
def as_dict() -> dictReturn the report.
Returns
dictchanges,change_count(the objects and values written, a change of several data counting each),warnings,failures;failure_details, each failure with itsobject, itsmessageand the request and answer of theapi(None when the API did not answer it);nodes,submissions; andstopped, why apply stopped before the end (None when it did not).
changemethod#
def change(message: str, count: int = 1) -> NoneRecord a change.
failmethod#
def fail(what: str, error: Exception | str, api: Mapping | None = None) -> NoneRecord a failure; apply goes on with the next object.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
what | str | required | The object that failed, as the file names it. |
error | Exception or str | required | Why. An ApiError is said in a sentence; its request and answer
go to the details, and to the log with verbose. |
api | mapping | None | The request and the answer, when error is already a sentence. |
Raises
ConnectionLost- When
errorsays madbot cannot be reached: apply stops.
recordmethod#
def record(what: str | None, error: Exception | str, api: Mapping | None = None) -> NoneRecord a failure, in the summary, the details and the log.
stopmethod#
def stop() -> NoneRecord that apply stops, madbot being out of reach.
warnmethod#
def warn(message: str) -> NoneRecord something to look at, which is not a failure.
Parameters
| Name | Type | Description |
|---|---|---|
message | str | What 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) -> dictMake madbot hold what a deposit file describes, without deleting anything.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
client | Client | required | The client. |
manifest | Manifest | required | The deposit file. |
submit | bool | False | Send the ready submissions to their repositories. This cannot be undone. |
retry_failed | bool | False | Read again the data madbot failed to read. |
environ | mapping | None | The environment holding the credentials; os.environ by default. |
log | callable | _silent | Called with a line for each change, warning and failure. |
timeout | float | DEFAULT_TIMEOUT | Seconds to wait for what madbot does in the background. |
progress | Progress | None | Says the step under way; none by default. |
verbose | bool | False | Log the request and the answer of the API under a failure. |
Returns
dictchanges,warnings,failureswith theirfailure_details, andstoppedwhen madbot could not be reached any more; for each node itslabeland its dataread,readingandfailed_data,failedTrue when the node itself failed; and for each submission itstitle,status,submitted,external_identifiersand the mandatory metadatamissing(leftTrue when a node under it failed,failedTrue when it failed itself).
credentials_hintfunction#
def credentials_hint(spec: Mapping[str, Any]) -> strSay how to give the credentials of a connection.
Parameters
| Name | Type | Description |
|---|---|---|
spec | mapping | The 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 | NoneReturn why the last task madbot ran on a submission failed.
Parameters
| Name | Type | Description |
|---|---|---|
submission | mapping | The submission, as the API returns it. |
node_outcomefunction#
def node_outcome(node: Node, linked: Linked) -> dictCount the data of a node by their state, for the summary of apply.
Parameters
Returns
dict- Its
label, and its dataread, stillreadingandfailed_data.

