Skip to content

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

NameDescription
DATA_PLUGINThe type of a plugin that is a source of data.
DESCRIPTION_WIDTHThe longest description shown on one line.
FORMAT_NAMESHow a format of a value is called on screen.
INTERNAL_TYPESThe types of plugin internal to madbot, not shown.
KIND_NAMESHow the type of a value is called on screen.
LONG_LISTThe most values of a field written on screen; --search narrows the others.
MANY_CHOICESBeyond this number of values, each value is shown with its title only.
MAX_REF_DEPTHThe most references followed one inside another, to stop on a loop.
MULTILINEThe format of a value that spans several lines, such as an SSH key.
SUBMISSION_PLUGINThe type of a plugin that is a repository.
TYPE_NAMESHow a type of plugin is called on screen, in this order; another type keeps
WriteWrites a line.

Functions

NameDescription
choice_matchesTell whether a value, or a form, contains a text, whatever the case.
choices_ofList the values a schema allows, when it allows only a few.
connection_blockGive the part of a source or a repository that names its connection.
credential_examplesGive an example of the credentials for each form they may take.
describeDescribe what a schema asks for.
describe_fieldDescribe what a metadata field accepts, with an example to paste.
describe_pluginDescribe what a plugin asks for in a deposit file.
env_nameName the environment variable of the credentials of a plugin.
example_blocksGive the blocks of a deposit file that use a plugin.
example_objectGive an example object, with every required part and every fixed value.
example_ofGive an example value of a parameter, with placeholders to replace.
field_in_submissionRead the level and the values of a field in a submission of a deposit file.
find_fieldFind a metadata field of the instance by its slug.
flattenPut the parameters a schema wraps in an object at the top.
has_valuesTell whether a parameter has a list of values or forms, at any depth.
head_ofWrite the head line of a parameter: its key, its flags and its description.
inline_refsPut in place the definitions a schema points to inside itself.
is_objectTell whether a schema describes an object with parts.
jq_commandWrite a jq command that builds credentials holding a multi-line value.
jq_variableName the jq variable that holds a value read from a file.
kind_ofSay what a value is, in a few words.
list_connectionsList the connections of the account, by workspace.
list_pluginsList the plugins of the instance.
multiline_keysFind the keys of the values that span several lines.
object_namesName each metadata object once, by its alias, or else its label.
one_linePut a description on one line.
params_ofDescribe the properties of an object schema.
part_exampleGive an example value of a part of an object, as value_example does.
placeholderWrite the value to replace for a parameter.
read_schemaRead a schema the API publishes.
render_choicesWrite the values a parameter allows, one per line.
render_connectionsWrite the connections of the account, by workspace.
render_credentialsWrite how to give the credentials of a plugin.
render_exampleWrite an example block of a deposit file.
render_fieldWrite what a metadata field accepts, an example, and its place in a submission.
render_itemsWrite what each item of a list holds, the list said once.
render_paramWrite a parameter: its key, whether it is required, its values or forms.
render_pluginWrite what a plugin asks for, and the blocks to paste in a deposit file.
render_pluginsWrite the plugins of the instance, one per line.
render_withinWrite the level and the values of a field in a submission.
search_paramKeep only the values of a parameter that contain a text, at any depth.
search_paramsNarrow the values of each parameter to those that contain a text.
setting_ofDescribe a setting of a repository from its schema.
source_keyGive the key of a source in the example, from the slug of its plugin.
type_namesName the types of a plugin.
value_exampleGive an example value of a metadata field, with the values to choose left to fill.
value_headIntroduce a value without a key by what it is.
variants_ofList the forms an object may take, such as a password or a key.
yaml_textWrite 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 = 100

The 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 = 30

The most values of a field written on screen; —search narrows the others.

MANY_CHOICESattributemodule attribute#

MANY_CHOICES = 10

Beyond this number of values, each value is shown with its title only.

MAX_REF_DEPTHattributemodule attribute#

MAX_REF_DEPTH = 20

The 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) -> bool

Tell whether a value, or a form, contains a text, whatever the case.

Parameters

NameTypeDescription
choicemappingA value of choices_of, or a form of variants_of.
textstrThe 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

NameTypeDescription
schemamappingA JSON Schema.

Returns

list of dict
Each value with its title and description when the schema gives them: the constants of oneOf or anyOf, or the enum. Empty when the schema allows any value.

connection_blockfunction#

def connection_block(described: Mapping) -> dict

Give the part of a source or a repository that names its connection.

Parameters

NameTypeDescription
describedmappingA plugin, as describe_plugin gives it.

Returns

dict
Its plugin, shared_params and credentials_env.

credential_examplesfunction#

def credential_examples(params: list[Mapping]) -> list[dict]

Give an example of the credentials for each form they may take.

Parameters

NameTypeDescription
paramslist of mappingThe 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, and jq, 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) -> dict

Describe what a schema asks for.

Parameters

NameTypeDefaultDescription
schemamappingrequiredA JSON Schema.
keystrNoneThe key the value goes under.

Returns

dict
Its key, title, description, type, format, the pattern a text must match and the pattern_message explaining 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) -> dict

Describe what a metadata field accepts, with an example to paste.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client.
slugstrrequiredThe slug of the field.
sourcestrNoneThe source of the field, when several fields share the slug.
searchstrNoneKeep only the values that contain this text, whatever the case.

Returns

dict
The field (slug, name, description, source, kind: a field of madbot or of a plugin, level: its level outside any submission), what its value is (as describe gives it), the search and the number of values and forms that match it (None without a search, or when the field has no list), and an example value (None when no value matches the search).

describe_pluginfunction#

def describe_plugin(client: Client, reference: str) -> dict

Describe what a plugin asks for in a deposit file.

Parameters

NameTypeDescription
clientClientThe client.
referencestrThe slug or the identifier of the plugin.

Returns

dict
The plugin (slug, name, types, description), its shared parameters, its private ones (the credentials), its settings when it is a repository, public when it reads public data without credentials, the credentials_env the example names, the credentials examples and the example blocks to paste in the file.

env_namefunction#

def env_name(slug: str) -> str

Name the environment variable of the credentials of a plugin.

Parameters

NameTypeDescription
slugstrThe slug of the plugin.

Returns

str
Such as SSHFS_CREDENTIALS.

example_blocksfunction#

def example_blocks(described: Mapping) -> dict

Give the blocks of a deposit file that use a plugin.

Parameters

NameTypeDescription
describedmappingA plugin, as describe_plugin gives it.

Returns

dict
sources, 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]) -> dict

Give an example object, with every required part and every fixed value.

Parameters

NameTypeDescription
paramsiterable of mappingThe 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) -> Any

Give 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

NameTypeDefaultDescription
parammappingrequiredA parameter, as describe gives it.
variantint0The 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) -> dict

Read 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

NameTypeDefaultDescription
clientClientrequiredThe client.
depositManifestrequiredThe deposit file.
titlestrrequiredThe title of the submission, in the file.
slugstrrequiredThe slug of the field.
environmappingNoneThe environment holding the credentials; os.environ by default.

Returns

dict
The submission and, for each type of metadata object that has the field (types): its type, level, the value all objects share (or None), the value of each object (by_object, by alias) and the objects still missing a mandatory value.

find_fieldfunction#

def find_field(client: Client, slug: str, source: str | None = None) -> dict

Find a metadata field of the instance by its slug.

Parameters

NameTypeDefaultDescription
clientClientrequiredThe client.
slugstrrequiredThe slug of the field, as a deposit file writes it.
sourcestrNoneThe source of the field (a plugin slug, or referential), when several fields share the slug.

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 source does 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

NameTypeDescription
paramsiterable of mappingThe parameters, one per schema of the plugin.

Returns

list of dict
The parameters inside those objects.

has_valuesfunction#

def has_values(param: Mapping) -> bool

Tell whether a parameter has a list of values or forms, at any depth.

Parameters

NameTypeDescription
parammappingA parameter, as describe gives it.

Returns

bool
True when --search has something to narrow.

head_offunction#

def head_of(param: Mapping) -> str

Write the head line of a parameter: its key, its flags and its description.

Parameters

NameTypeDescription
parammappingThe 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) -> Any

Put 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

NameTypeDefaultDescription
schemaAnyrequiredA JSON Schema, or a part of it.
rootmappingNoneThe whole schema, holding the $defs; schema by default.
depthint0How many references were followed, to stop on a loop.

Returns

Any
The schema with each {"$ref": "#/$defs/…"} replaced by its definition.

is_objectfunction#

def is_object(schema: Mapping) -> bool

Tell whether a schema describes an object with parts.

Parameters

NameTypeDescription
schemamappingA 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 | None

Write a jq command that builds credentials holding a multi-line value.

Parameters

NameTypeDescription
valueAnyThe example of the credentials, as credential_examples builds it.
multilineset of strThe keys of the values that span several lines.

Returns

str or None
Such as jq -n --rawfile private_key '<file of private_key>' '{…}', or None when the example has no such value.

jq_variablefunction#

def jq_variable(key: str) -> str

Name the jq variable that holds a value read from a file.

Parameters

NameTypeDescription
keystrThe key of the value.

Returns

str
A name jq accepts, such as private_key.

kind_offunction#

def kind_of(param: Mapping) -> str

Say what a value is, in a few words.

Parameters

NameTypeDescription
parammappingA value, as describe gives it.

Returns

str
Such as a text, a date, YYYY-MM-DD, one of these forms or a 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

NameTypeDefaultDescription
clientClientrequiredThe client.
workspacestrNoneThe name, slug or identifier of one workspace; every workspace of the account by default.

Returns

list of dict
Each connection with its workspace (name), name, plugin (slug), shared_params, connected, whether the account has working credentials for it, and public, 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

NameTypeDescription
clientClientThe client.

Returns

list of dict
Each plugin with its slug, name, types and description, sorted by slug.

multiline_keysfunction#

def multiline_keys(params: Iterable[Mapping]) -> set[str]

Find the keys of the values that span several lines.

Parameters

NameTypeDescription
paramsiterable of mappingParameters, 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

NameTypeDescription
objectsiterable of mappingThe 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) -> str

Put a description on one line.

Parameters

NameTypeDefaultDescription
textstr or NonerequiredThe description.
widthintDESCRIPTION_WIDTHThe 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

NameTypeDescription
schemamappingA JSON Schema of an object.

Returns

list of dict
Each property as describe gives it, with required.

part_examplefunction#

def part_example(part: Mapping) -> Any

Give an example value of a part of an object, as value_example does.

Parameters

NameTypeDescription
partmappingThe part, as describe gives it.

Returns

Any
The example.

placeholderfunction#

def placeholder(param: Mapping) -> str

Write the value to replace for a parameter.

Parameters

NameTypeDescription
parammappingA 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) -> dict

Read a schema the API publishes.

Parameters

NameTypeDescription
clientClientThe client.
urlstrIts address, as a plugin gives it.

Returns

dict
The schema.

render_choicesfunction#

def render_choices(write: Write, choices: list[Mapping], pad: str, limit: int | None = None) -> None

Write the values a parameter allows, one per line.

Parameters

NameTypeDefaultDescription
writeWriterequiredWrites a line.
choiceslist of mappingrequiredThe values, as choices_of gives them.
padstrrequiredThe indentation of the parameter.
limitintNoneThe most values written; every value by default.

render_connectionsfunction#

def render_connections(write: Write, connections: list[Mapping]) -> None

Write the connections of the account, by workspace.

Parameters

NameTypeDescription
writeWriteWrites a line.
connectionslist of mappingThe connections, from list_connections.

render_credentialsfunction#

def render_credentials(write: Write, described: Mapping) -> None

Write how to give the credentials of a plugin.

Parameters

NameTypeDescription
writeWriteWrites a line.
describedmappingThe plugin, from describe_plugin.

render_examplefunction#

def render_example(write: Write, where: str, block: Any) -> None

Write an example block of a deposit file.

Parameters

NameTypeDescription
writeWriteWrites a line.
wherestrWhere it goes in the file.
blockAnyThe block, written as YAML.

render_fieldfunction#

def render_field(write: Write, described: Mapping, within: Mapping | None = None) -> None

Write what a metadata field accepts, an example, and its place in a submission.

Parameters

NameTypeDefaultDescription
writeWriterequiredWrites a line.
describedmappingrequiredThe field, from describe_field.
withinmappingNoneThe field in a submission, from field_in_submission.

render_itemsfunction#

def render_items(write: Write, param: Mapping, depth: int, limit: int | None) -> None

Write what each item of a list holds, the list said once.

Parameters

NameTypeDescription
writeWriteWrites a line.
parammappingThe list, as describe gives it.
depthintHow far the list is indented.
limitint or NoneThe most values written for a list of values.

render_paramfunction#

def render_param(write: Write, param: Mapping, depth: int = 1, limit: int | None = None, *, body: bool = False) -> None

Write a parameter: its key, whether it is required, its values or forms.

Parameters

NameTypeDefaultDescription
writeWriterequiredWrites a line.
parammappingrequiredThe parameter, as describe gives it.
depthint1How far it is indented.
limitintNoneThe most values written for a list; every value by default.
bodyboolFalseWrite 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) -> None

Write what a plugin asks for, and the blocks to paste in a deposit file.

Parameters

NameTypeDescription
writeWriteWrites a line.
describedmappingThe plugin, from describe_plugin.

render_pluginsfunction#

def render_plugins(write: Write, plugins: list[Mapping]) -> None

Write the plugins of the instance, one per line.

Parameters

NameTypeDescription
writeWriteWrites a line.
pluginslist of mappingThe plugins, from list_plugins.

render_withinfunction#

def render_within(write: Write, within: Mapping) -> None

Write the level and the values of a field in a submission.

Parameters

NameTypeDescription
writeWriteWrites a line.
withinmappingThe 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

NameTypeDescription
parammappingA parameter, as describe gives it.
textstrThe 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

NameTypeDescription
paramsiterable of mappingParameters, as describe gives them.
textstrThe 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) -> dict

Describe 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

NameTypeDescription
schemamappingThe schema of the setting.
urlstrThe address of the schema, whose end is the key of the setting.

Returns

dict
The setting as describe gives it, with required.

source_keyfunction#

def source_key(slug: str) -> str

Give the key of a source in the example, from the slug of its plugin.

Parameters

NameTypeDescription
slugstrThe 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

NameTypeDescription
pluginmappingA plugin as the API returns it.

Returns

list of str
Such as data source or repository, in the order of TYPE_NAMES, then a type madbot-cli does not know under its own name; the INTERNAL_TYPES, such as connection_plugin, are left out.

value_examplefunction#

def value_example(param: Mapping, *, required: bool = True, title: str | None = None) -> Any

Give 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

NameTypeDefaultDescription
parammappingrequiredThe field, or a part of it, as describe gives it.
requiredboolTrueWhether the value must be given where it is: false for a part that may be left out, or an item of a list.
titlestrNoneThe 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) -> dict

Introduce a value without a key by what it is.

Parameters

NameTypeDescription
valuemappingA 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

NameTypeDescription
schemamappingA JSON Schema.

Returns

list of dict
Each form of oneOf or anyOf that is not a constant, with its title, description, params and, for a form that is not an object (such as a date or a year), the value it is, as describe gives it (None for an object).

yaml_textfunction#

def yaml_text(value: Any) -> list[str]

Write a value as YAML lines.

Parameters

NameTypeDescription
valueAnyThe value.

Returns

list of str
Its lines.