madbot_cli.discover
Find what an instance offers, before writing a deposit file.
The plugins of the instance, the parameters each one expects, and the connections the account already has. Everything is read from the API: the plugins publish the JSON Schema of their shared parameters, of their credentials and of their settings, and this module reads those schemas generically, with no case for a given plugin. Nothing is written in madbot.
Attributes
| Name | Description |
|---|---|
DATA_PLUGIN | The type of a plugin that is a source of data. |
DESCRIPTION_WIDTH | The longest description shown on one line. |
FORMAT_NAMES | How a format of a value is called on screen. |
INTERNAL_TYPES | The types of plugin internal to madbot, not shown. |
KIND_NAMES | How the type of a value is called on screen. |
LONG_LIST | The most values of a field written on screen; --search narrows the others. |
MANY_CHOICES | Beyond this number of values, each value is shown with its title only. |
MAX_REF_DEPTH | The most references followed one inside another, to stop on a loop. |
MULTILINE | The format of a value that spans several lines, such as an SSH key. |
SUBMISSION_PLUGIN | The type of a plugin that is a repository. |
TYPE_NAMES | How a type of plugin is called on screen, in this order; another type keeps |
Write | Writes a line. |
Functions
| Name | Description |
|---|---|
choice_matches | Tell whether a value, or a form, contains a text, whatever the case. |
choices_of | List the values a schema allows, when it allows only a few. |
connection_block | Give the part of a source or a repository that names its connection. |
credential_examples | Give an example of the credentials for each form they may take. |
describe | Describe what a schema asks for. |
describe_field | Describe what a metadata field accepts, with an example to paste. |
describe_plugin | Describe what a plugin asks for in a deposit file. |
env_name | Name the environment variable of the credentials of a plugin. |
example_blocks | Give the blocks of a deposit file that use a plugin. |
example_object | Give an example object, with every required part and every fixed value. |
example_of | Give an example value of a parameter, with placeholders to replace. |
field_in_submission | Read the level and the values of a field in a submission of a deposit file. |
find_field | Find a metadata field of the instance by its slug. |
flatten | Put the parameters a schema wraps in an object at the top. |
has_values | Tell whether a parameter has a list of values or forms, at any depth. |
head_of | Write the head line of a parameter: its key, its flags and its description. |
inline_refs | Put in place the definitions a schema points to inside itself. |
is_object | Tell whether a schema describes an object with parts. |
jq_command | Write a jq command that builds credentials holding a multi-line value. |
jq_variable | Name the jq variable that holds a value read from a file. |
kind_of | Say what a value is, in a few words. |
list_connections | List the connections of the account, by workspace. |
list_plugins | List the plugins of the instance. |
multiline_keys | Find the keys of the values that span several lines. |
object_names | Name each metadata object once, by its alias, or else its label. |
one_line | Put a description on one line. |
params_of | Describe the properties of an object schema. |
part_example | Give an example value of a part of an object, as value_example does. |
placeholder | Write the value to replace for a parameter. |
read_schema | Read a schema the API publishes. |
render_choices | Write the values a parameter allows, one per line. |
render_connections | Write the connections of the account, by workspace. |
render_credentials | Write how to give the credentials of a plugin. |
render_example | Write an example block of a deposit file. |
render_field | Write what a metadata field accepts, an example, and its place in a submission. |
render_items | Write what each item of a list holds, the list said once. |
render_param | Write a parameter: its key, whether it is required, its values or forms. |
render_plugin | Write what a plugin asks for, and the blocks to paste in a deposit file. |
render_plugins | Write the plugins of the instance, one per line. |
render_within | Write the level and the values of a field in a submission. |
search_param | Keep only the values of a parameter that contain a text, at any depth. |
search_params | Narrow the values of each parameter to those that contain a text. |
setting_of | Describe a setting of a repository from its schema. |
source_key | Give the key of a source in the example, from the slug of its plugin. |
type_names | Name the types of a plugin. |
value_example | Give an example value of a metadata field, with the values to choose left to fill. |
value_head | Introduce a value without a key by what it is. |
variants_of | List the forms an object may take, such as a password or a key. |
yaml_text | Write a value as YAML lines. |
DATA_PLUGINattributemodule attribute#
DATA_PLUGIN = 'data_plugin'The type of a plugin that is a source of data.
DESCRIPTION_WIDTHattributemodule attribute#
DESCRIPTION_WIDTH = 100The longest description shown on one line.
FORMAT_NAMESattributemodule attribute#
FORMAT_NAMES = {'date': 'a date, YYYY-MM-DD', 'date-time': 'a date and time, YYYY-MM-DDThh:mm:ss'}How a format of a value is called on screen.
INTERNAL_TYPESattributemodule attribute#
INTERNAL_TYPES = frozenset(
{
'connection_plugin',
'madbot_plugin',
'metadata_field_definition_plugin_with_standard_mapping'
}
)The types of plugin internal to madbot, not shown.
KIND_NAMESattributemodule attribute#
KIND_NAMES = {
'string': 'a text',
'number': 'a number',
'integer': 'a whole number',
'boolean': 'true or false',
'array': 'a list',
'object': 'an object'
}How the type of a value is called on screen.
LONG_LISTattributemodule attribute#
LONG_LIST = 30The most values of a field written on screen; —search narrows the others.
MANY_CHOICESattributemodule attribute#
MANY_CHOICES = 10Beyond this number of values, each value is shown with its title only.
MAX_REF_DEPTHattributemodule attribute#
MAX_REF_DEPTH = 20The most references followed one inside another, to stop on a loop.
MULTILINEattributemodule attribute#
MULTILINE = 'multi'The format of a value that spans several lines, such as an SSH key.
SUBMISSION_PLUGINattributemodule attribute#
SUBMISSION_PLUGIN = 'submission_plugin'The type of a plugin that is a repository.
TYPE_NAMESattributemodule attribute#
TYPE_NAMES = {
DATA_PLUGIN: 'data source',
SUBMISSION_PLUGIN: 'repository',
'metadata_plugin': 'metadata',
'metadata_field_definition_plugin': 'metadata fields'
}How a type of plugin is called on screen, in this order; another type keeps its own name.
Writeattributemodule attribute#
Write = Callable[[str], None]Writes a line.
choice_matchesfunction#
def choice_matches(choice: Mapping, text: str) -> boolTell whether a value, or a form, contains a text, whatever the case.
Parameters
| Name | Type | Description |
|---|---|---|
choice | mapping | A value of choices_of, or a form of variants_of. |
text | str | The text searched. |
Returns
bool- True when its value, title or description contains the text.
choices_offunction#
def choices_of(schema: Mapping) -> list[dict]List the values a schema allows, when it allows only a few.
Parameters
| Name | Type | Description |
|---|---|---|
schema | mapping | A JSON Schema. |
Returns
list of dict- Each value with its
titleanddescriptionwhen the schema gives them: the constants ofoneOforanyOf, or theenum. Empty when the schema allows any value.
connection_blockfunction#
def connection_block(described: Mapping) -> dictGive the part of a source or a repository that names its connection.
Parameters
| Name | Type | Description |
|---|---|---|
described | mapping | A plugin, as describe_plugin gives it. |
Returns
dict- Its
plugin,shared_paramsandcredentials_env.
credential_examplesfunction#
def credential_examples(params: list[Mapping]) -> list[dict]Give an example of the credentials for each form they may take.
Parameters
| Name | Type | Description |
|---|---|---|
params | list of mapping | The private parameters of a plugin. |
Returns
list of dict- Each example with
forms, the titles of the forms it follows,value, the JSON object to put in the environment variable, andjq, a command writing that object when a value spans several lines (an SSH key, whose line breaks JSON writes escaped), else None.
describefunction#
def describe(schema: Mapping, key: str | None = None) -> dictDescribe what a schema asks for.
Parameters
Returns
dict- Its
key,title,description,type,format, thepatterna text must match and thepattern_messageexplaining it, its fixed value (const), the values it allows (choices), the forms of an object (variants), the parts of an object (params) and what each item of a list is (items, else None).
describe_fieldfunction#
def describe_field(client: Client, slug: str, *, source: str | None = None, search: str | None = None) -> dictDescribe what a metadata field accepts, with an example to paste.
Parameters
Returns
dict- The field (
slug,name,description,source,kind: a field of madbot or of a plugin,level: its level outside any submission), what itsvalueis (asdescribegives it), thesearchand the number of values and forms thatmatchit (None without a search, or when the field has no list), and anexamplevalue (None when no value matches the search).
describe_pluginfunction#
def describe_plugin(client: Client, reference: str) -> dictDescribe what a plugin asks for in a deposit file.
Parameters
Returns
dict- The plugin (
slug,name,types,description), itssharedparameters, itsprivateones (the credentials), itssettingswhen it is a repository,publicwhen it reads public data without credentials, thecredentials_envthe example names, thecredentialsexamples and theexampleblocks to paste in the file.
env_namefunction#
def env_name(slug: str) -> strName the environment variable of the credentials of a plugin.
Parameters
| Name | Type | Description |
|---|---|---|
slug | str | The slug of the plugin. |
Returns
str- Such as
SSHFS_CREDENTIALS.
example_blocksfunction#
def example_blocks(described: Mapping) -> dictGive the blocks of a deposit file that use a plugin.
Parameters
| Name | Type | Description |
|---|---|---|
described | mapping | A plugin, as describe_plugin gives it. |
Returns
dictsources, the top-level block of a source, when the plugin is a source of data;repository, the block of a submission, when it is a repository.
example_objectfunction#
def example_object(params: Iterable[Mapping]) -> dictGive an example object, with every required part and every fixed value.
Parameters
| Name | Type | Description |
|---|---|---|
params | iterable of mapping | The parts of the object, as params_of gives them. |
Returns
dict- Each part by its key.
example_offunction#
def example_of(param: Mapping, variant: int = 0) -> AnyGive an example value of a parameter, with placeholders to replace.
A value to choose stays a placeholder: the example never picks one, such as a production server, for the reader.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
param | mapping | required | A parameter, as describe gives it. |
variant | int | 0 | The form to follow, when the parameter has several. |
Returns
Any- The fixed value, an object of examples, or a placeholder.
field_in_submissionfunction#
def field_in_submission(client: Client, deposit: Manifest, title: str, slug: str, environ: Mapping[str, str] | None = None) -> dictRead the level and the values of a field in a submission of a deposit file.
The level comes from the submission, with its settings: a checklist may make a field mandatory. The submission must have been prepared by apply.
Parameters
Returns
dict- The
submissionand, for each type of metadata object that has the field (types): itstype,level, thevalueall objects share (or None), the value of each object (by_object, by alias) and the objects stillmissinga mandatory value.
find_fieldfunction#
def find_field(client: Client, slug: str, source: str | None = None) -> dictFind a metadata field of the instance by its slug.
Parameters
Returns
dict- The field as the API returns it.
Raises
MadbotError- When no field has this slug, giving those whose slug or name is
close; or when several have it and
sourcedoes not choose.
flattenfunction#
def flatten(params: Iterable[Mapping]) -> list[dict]Put the parameters a schema wraps in an object at the top.
A plugin publishes each connection parameter as an object holding that
parameter, the way the API checks {key: value}.
Parameters
| Name | Type | Description |
|---|---|---|
params | iterable of mapping | The parameters, one per schema of the plugin. |
Returns
list of dict- The parameters inside those objects.
has_valuesfunction#
def has_values(param: Mapping) -> boolTell whether a parameter has a list of values or forms, at any depth.
Parameters
| Name | Type | Description |
|---|---|---|
param | mapping | A parameter, as describe gives it. |
Returns
bool- True when
--searchhas something to narrow.
head_offunction#
def head_of(param: Mapping) -> strWrite the head line of a parameter: its key, its flags and its description.
Parameters
| Name | Type | Description |
|---|---|---|
param | mapping | The parameter, as describe gives it. |
Returns
str- Such as
release_date (required, a date, YYYY-MM-DD).
inline_refsfunction#
def inline_refs(schema: Any, root: Mapping | None = None, depth: int = 0) -> AnyPut in place the definitions a schema points to inside itself.
The API resolves the references between schemas (?ref=false) but
keeps those to the $defs of the schema itself.
Parameters
Returns
Any- The schema with each
{"$ref": "#/$defs/…"}replaced by its definition.
is_objectfunction#
def is_object(schema: Mapping) -> boolTell whether a schema describes an object with parts.
Parameters
| Name | Type | Description |
|---|---|---|
schema | mapping | A JSON Schema. |
Returns
bool- True when it is of type object, or lists properties or required parts.
jq_commandfunction#
def jq_command(value: Any, multiline: set[str]) -> str | NoneWrite a jq command that builds credentials holding a multi-line value.
Parameters
| Name | Type | Description |
|---|---|---|
value | Any | The example of the credentials, as credential_examples builds it. |
multiline | set of str | The keys of the values that span several lines. |
jq_variablefunction#
def jq_variable(key: str) -> strName the jq variable that holds a value read from a file.
Parameters
| Name | Type | Description |
|---|---|---|
key | str | The key of the value. |
Returns
str- A name jq accepts, such as
private_key.
kind_offunction#
def kind_of(param: Mapping) -> strSay what a value is, in a few words.
Parameters
| Name | Type | Description |
|---|---|---|
param | mapping | A value, as describe gives it. |
Returns
str- Such as
a text,a date, YYYY-MM-DD,one of these formsora list, each item an object with these parts.
list_connectionsfunction#
def list_connections(client: Client, workspace: str | None = None) -> list[dict]List the connections of the account, by workspace.
Only the shared parameters are read: the API never returns credentials.
Parameters
Returns
list of dict- Each connection with its
workspace(name),name,plugin(slug),shared_params,connected, whether the account has working credentials for it, andpublic, whether it reads the public data of the source without them.
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- Each plugin with its
slug,name,typesanddescription, sorted by slug.
multiline_keysfunction#
def multiline_keys(params: Iterable[Mapping]) -> set[str]Find the keys of the values that span several lines.
Parameters
| Name | Type | Description |
|---|---|---|
params | iterable of mapping | Parameters, as describe gives them. |
Returns
set of str- The keys whose format is
MULTILINE, at any depth and in any form.
object_namesfunction#
def object_names(objects: Iterable[Mapping]) -> list[str]Name each metadata object once, by its alias, or else its label.
Parameters
| Name | Type | Description |
|---|---|---|
objects | iterable of mapping | The objects, with their alias and label. |
Returns
list of str- One name per object, in order; a name two objects share is followed by its rank, so that no value hides another.
one_linefunction#
def one_line(text: str | None, width: int = DESCRIPTION_WIDTH) -> strPut a description on one line.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
text | str or None | required | The description. |
width | int | DESCRIPTION_WIDTH | The longest text kept. |
Returns
str- The text with its spaces collapsed, cut at
width.
params_offunction#
def params_of(schema: Mapping) -> list[dict]Describe the properties of an object schema.
Parameters
| Name | Type | Description |
|---|---|---|
schema | mapping | A JSON Schema of an object. |
Returns
list of dict- Each property as
describegives it, withrequired.
part_examplefunction#
def part_example(part: Mapping) -> AnyGive an example value of a part of an object, as value_example does.
Parameters
| Name | Type | Description |
|---|---|---|
part | mapping | The part, as describe gives it. |
Returns
Any- The example.
placeholderfunction#
def placeholder(param: Mapping) -> strWrite the value to replace for a parameter.
Parameters
| Name | Type | Description |
|---|---|---|
param | mapping | A parameter, as describe gives it. |
Returns
str- Such as
<host>, or<YYYY-MM-DD>for a date.
read_schemafunction#
def read_schema(client: Client, url: str) -> dictRead a schema the API publishes.
Returns
dict- The schema.
render_choicesfunction#
def render_choices(write: Write, choices: list[Mapping], pad: str, limit: int | None = None) -> NoneWrite the values a parameter allows, one per line.
render_connectionsfunction#
def render_connections(write: Write, connections: list[Mapping]) -> NoneWrite the connections of the account, by workspace.
Parameters
| Name | Type | Description |
|---|---|---|
write | Write | Writes a line. |
connections | list of mapping | The connections, from list_connections. |
render_credentialsfunction#
def render_credentials(write: Write, described: Mapping) -> NoneWrite how to give the credentials of a plugin.
Parameters
| Name | Type | Description |
|---|---|---|
write | Write | Writes a line. |
described | mapping | The plugin, from describe_plugin. |
render_examplefunction#
def render_example(write: Write, where: str, block: Any) -> NoneWrite an example block of a deposit file.
render_fieldfunction#
def render_field(write: Write, described: Mapping, within: Mapping | None = None) -> NoneWrite what a metadata field accepts, an example, and its place in a submission.
render_itemsfunction#
def render_items(write: Write, param: Mapping, depth: int, limit: int | None) -> NoneWrite what each item of a list holds, the list said once.
render_paramfunction#
def render_param(write: Write, param: Mapping, depth: int = 1, limit: int | None = None, *, body: bool = False) -> NoneWrite a parameter: its key, whether it is required, its values or forms.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
write | Write | required | Writes a line. |
param | mapping | required | The parameter, as describe gives it. |
depth | int | 1 | How far it is indented. |
limit | int | None | The most values written for a list; every value by default. |
body | bool | False | Write what the parameter holds without its head line, for the item of a list, which the line of the list introduces. |
render_pluginfunction#
def render_plugin(write: Write, described: Mapping) -> NoneWrite what a plugin asks for, and the blocks to paste in a deposit file.
Parameters
| Name | Type | Description |
|---|---|---|
write | Write | Writes a line. |
described | mapping | The plugin, from describe_plugin. |
render_pluginsfunction#
def render_plugins(write: Write, plugins: list[Mapping]) -> NoneWrite the plugins of the instance, one per line.
Parameters
| Name | Type | Description |
|---|---|---|
write | Write | Writes a line. |
plugins | list of mapping | The plugins, from list_plugins. |
render_withinfunction#
def render_within(write: Write, within: Mapping) -> NoneWrite the level and the values of a field in a submission.
Parameters
| Name | Type | Description |
|---|---|---|
write | Write | Writes a line. |
within | mapping | The field in a submission, from field_in_submission. |
search_paramfunction#
def search_param(param: Mapping, text: str) -> tuple[dict, int]Keep only the values of a parameter that contain a text, at any depth.
A form whose title or description contains the text keeps all its values; another form is kept only when some of its values contain it.
Parameters
| Name | Type | Description |
|---|---|---|
param | mapping | A parameter, as describe gives it. |
text | str | The text searched, whatever the case. |
Returns
tuple of (dict, int)- The parameter with its lists narrowed, and how many values and forms contain the text.
search_paramsfunction#
def search_params(params: Iterable[Mapping], text: str) -> tuple[list[dict], int]Narrow the values of each parameter to those that contain a text.
A parameter none of whose values contains the text keeps them all: the parts of one object go together, and the example takes one of them.
Parameters
| Name | Type | Description |
|---|---|---|
params | iterable of mapping | Parameters, as describe gives them. |
text | str | The text searched. |
Returns
tuple of (list of dict, int)- The parameters, and how many values and forms contain the text.
setting_offunction#
def setting_of(schema: Mapping, url: str) -> dictDescribe a setting of a repository from its schema.
A setting is mandatory when its schema says so with level, as the web
application reads it, or when it gives the message of a missing value.
A mapping of messages under required is about the parts of an object
setting, not about the setting itself: it does not make it mandatory.
Parameters
| Name | Type | Description |
|---|---|---|
schema | mapping | The schema of the setting. |
url | str | The address of the schema, whose end is the key of the setting. |
Returns
dict- The setting as
describegives it, withrequired.
source_keyfunction#
def source_key(slug: str) -> strGive the key of a source in the example, from the slug of its plugin.
Parameters
| Name | Type | Description |
|---|---|---|
slug | str | The slug of the plugin. |
Returns
str- A key the format accepts.
type_namesfunction#
def type_names(plugin: Mapping) -> list[str]Name the types of a plugin.
Parameters
| Name | Type | Description |
|---|---|---|
plugin | mapping | A plugin as the API returns it. |
Returns
list of str- Such as
data sourceorrepository, in the order ofTYPE_NAMES, then a type madbot-cli does not know under its own name; theINTERNAL_TYPES, such asconnection_plugin, are left out.
value_examplefunction#
def value_example(param: Mapping, *, required: bool = True, title: str | None = None) -> AnyGive an example value of a metadata field, with the values to choose left to fill.
Unlike example_of, a closed list in a required place gives its first
value, so that the example is valid as written. A value chosen among
others in a list, or in a part that may be left out, stays a <…>
value: the example does not pick a type of data or a language for the
reader. A list narrowed to one value by --search gives that value;
a free text stays a placeholder to replace. Every part of an object is
written, required or not.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
param | mapping | required | The field, or a part of it, as describe gives it. |
required | bool | True | Whether the value must be given where it is: false for a part that may be left out, or an item of a list. |
title | str | None | The title of the place, for the placeholder of a value without one, such as the item of a list. |
Returns
Any- The example.
value_headfunction#
def value_head(value: Mapping) -> dictIntroduce a value without a key by what it is.
Parameters
| Name | Type | Description |
|---|---|---|
value | mapping | A value, as describe gives it. |
Returns
dict- The value, titled by
kind_of, which already says it is a date.
variants_offunction#
def variants_of(schema: Mapping) -> list[dict]List the forms an object may take, such as a password or a key.
Parameters
| Name | Type | Description |
|---|---|---|
schema | mapping | A JSON Schema. |
Returns
list of dict- Each form of
oneOforanyOfthat is not a constant, with itstitle,description,paramsand, for a form that is not an object (such as a date or a year), thevalueit is, asdescribegives it (None for an object).
yaml_textfunction#
def yaml_text(value: Any) -> list[str]Write a value as YAML lines.
Parameters
| Name | Type | Description |
|---|---|---|
value | Any | The value. |
Returns
list of str- Its lines.

