class documentation

class Collection: (source)

View In Hierarchy

A collection of executable tasks. See :doc:`/concepts/namespaces`. .. versionadded:: 1.0

Class Method from_module Return a new `.Collection` created from ``module``.
Method __bool__ Undocumented
Method __contains__ Undocumented
Method __eq__ Undocumented
Method __getitem__ Returns task named ``name``. Honors aliases and subcollections.
Method __init__ Create a new task collection/namespace.
Method __ne__ Undocumented
Method __nonzero__ Undocumented
Method __repr__ Undocumented
Method add_collection Add `.Collection` ``coll`` as a sub-collection of this one.
Method add_task Add `.Task` ``task`` to this collection.
Method configuration Obtain merged configuration values from collection & children.
Method configure (Recursively) merge ``options`` into the current `.configuration`.
Method serialized Return an appropriate-for-serialization version of this object.
Method subcollection_from_path Given a ``path`` to a subcollection, return that subcollection.
Method subtask_name Undocumented
Method task_with_config Return task named ``name`` plus its configuration dict.
Method to_contexts Returns all contained tasks and subtasks as a list of parser contexts.
Method transform Transform ``name`` with the configured auto-dashes behavior.
Instance Variable auto_dash_names Undocumented
Instance Variable collections Undocumented
Instance Variable default Undocumented
Instance Variable loaded_from Undocumented
Instance Variable name Undocumented
Instance Variable tasks Undocumented
Property task_names Return all task identifiers for this collection as a one-level dict.
Method _add_object Undocumented
Method _check_default_collision Undocumented
Method _split_path Obtain first collection + remainder, of a task path.
Method _task_with_merged_config Undocumented
Method _transform_lexicon Take a Lexicon and apply `.transform` to its keys and aliases.
Instance Variable _configuration Undocumented
@classmethod
def from_module(cls, module, name=None, config=None, loaded_from=None, auto_dash_names=None): (source)

Return a new `.Collection` created from ``module``. Inspects ``module`` for any `.Task` instances and adds them to a new `.Collection`, returning it. If any explicit namespace collections exist (named ``ns`` or ``namespace``) a copy of that collection object is preferentially loaded instead. When the implicit/default collection is generated, it will be named after the module's ``__name__`` attribute, or its last dotted section if it's a submodule. (I.e. it should usually map to the actual ``.py`` filename.) Explicitly given collections will only be given that module-derived name if they don't already have a valid ``.name`` attribute. If the module has a docstring (``__doc__``) it is copied onto the resulting `.Collection` (and used for display in help, list etc output.) :param str name: A string, which if given will override any automatically derived collection name (or name set on the module's root namespace, if it has one.) :param dict config: Used to set config options on the newly created `.Collection` before returning it (saving you a call to `.configure`.) If the imported module had a root namespace object, ``config`` is merged on top of it (i.e. overriding any conflicts.) :param str loaded_from: Identical to the same-named kwarg from the regular class constructor - should be the path where the module was found. :param bool auto_dash_names: Identical to the same-named kwarg from the regular class constructor - determines whether emitted names are auto-dashed. .. versionadded:: 1.0

def __bool__(self): (source)

Undocumented

def __contains__(self, name): (source)

Undocumented

def __eq__(self, other): (source)

Undocumented

def __getitem__(self, name=None): (source)

Returns task named ``name``. Honors aliases and subcollections. If this collection has a default task, it is returned when ``name`` is empty or ``None``. If empty input is given and no task has been selected as the default, ValueError will be raised. Tasks within subcollections should be given in dotted form, e.g. 'foo.bar'. Subcollection default tasks will be returned on the subcollection's name. .. versionadded:: 1.0

def __init__(self, *args, **kwargs): (source)

Create a new task collection/namespace. `.Collection` offers a set of methods for building a collection of tasks from scratch, plus a convenient constructor wrapping said API. In either case: * The first positional argument may be a string, which (if given) is used as the collection's default name when performing namespace lookups; * A ``loaded_from`` keyword argument may be given, which sets metadata indicating the filesystem path the collection was loaded from. This is used as a guide when loading per-project :ref:`configuration files <config-hierarchy>`. * An ``auto_dash_names`` kwarg may be given, controlling whether task and collection names have underscores turned to dashes in most cases; it defaults to ``True`` but may be set to ``False`` to disable. The CLI machinery will pass in the value of the ``tasks.auto_dash_names`` config value to this kwarg. **The method approach** May initialize with no arguments and use methods (e.g. `.add_task`/`.add_collection`) to insert objects:: c = Collection() c.add_task(some_task) If an initial string argument is given, it is used as the default name for this collection, should it be inserted into another collection as a sub-namespace:: docs = Collection('docs') docs.add_task(doc_task) ns = Collection() ns.add_task(top_level_task) ns.add_collection(docs) # Valid identifiers are now 'top_level_task' and 'docs.doc_task' # (assuming the task objects were actually named the same as the # variables we're using :)) For details, see the API docs for the rest of the class. **The constructor approach** All ``*args`` given to `.Collection` (besides the abovementioned optional positional 'name' argument and ``loaded_from`` kwarg) are expected to be `.Task` or `.Collection` instances which will be passed to `.add_task`/`.add_collection` as appropriate. Module objects are also valid (as they are for `.add_collection`). For example, the below snippet results in the same two task identifiers as the one above:: ns = Collection(top_level_task, Collection('docs', doc_task)) If any ``**kwargs`` are given, the keywords are used as the initial name arguments for the respective values:: ns = Collection( top_level_task=some_other_task, docs=Collection(doc_task) ) That's exactly equivalent to:: docs = Collection(doc_task) ns = Collection() ns.add_task(some_other_task, 'top_level_task') ns.add_collection(docs, 'docs') See individual methods' API docs for details.

def __ne__(self, other): (source)

Undocumented

def __nonzero__(self): (source)

Undocumented

def __repr__(self): (source)

Undocumented

def add_collection(self, coll, name=None, default=None): (source)

Add `.Collection` ``coll`` as a sub-collection of this one. :param coll: The `.Collection` to add. :param str name: The name to attach the collection as. Defaults to the collection's own internal name. :param default: Whether this sub-collection('s default task-or-collection) should be the default invocation of the parent collection. .. versionadded:: 1.0 .. versionchanged:: 1.5 Added the ``default`` parameter.

def add_task(self, task, name=None, aliases=None, default=None): (source)

Add `.Task` ``task`` to this collection. :param task: The `.Task` object to add to this collection. :param name: Optional string name to bind to (overrides the task's own self-defined ``name`` attribute and/or any Python identifier (i.e. ``.func_name``.) :param aliases: Optional iterable of additional names to bind the task as, on top of the primary name. These will be used in addition to any aliases the task itself declares internally. :param default: Whether this task should be the collection default. .. versionadded:: 1.0

def configuration(self, taskpath=None): (source)

Obtain merged configuration values from collection & children. :param taskpath: (Optional) Task name/path, identical to that used for `~.Collection.__getitem__` (e.g. may be dotted for nested tasks, etc.) Used to decide which path to follow in the collection tree when merging config values. :returns: A `dict` containing configuration values. .. versionadded:: 1.0

def configure(self, options): (source)

(Recursively) merge ``options`` into the current `.configuration`. Options configured this way will be available to all tasks. It is recommended to use unique keys to avoid potential clashes with other config options For example, if you were configuring a Sphinx docs build target directory, it's better to use a key like ``'sphinx.target'`` than simply ``'target'``. :param options: An object implementing the dictionary protocol. :returns: ``None``. .. versionadded:: 1.0

def serialized(self): (source)

Return an appropriate-for-serialization version of this object. See the documentation for `.Program` and its ``json`` task listing format; this method is the driver for that functionality. .. versionadded:: 1.0

def subcollection_from_path(self, path): (source)

Given a ``path`` to a subcollection, return that subcollection. .. versionadded:: 1.0

def subtask_name(self, collection_name, task_name): (source)

Undocumented

def task_with_config(self, name): (source)

Return task named ``name`` plus its configuration dict. E.g. in a deeply nested tree, this method returns the `.Task`, and a configuration dict created by merging that of this `.Collection` and any nested `Collections <.Collection>`, up through the one actually holding the `.Task`. See `~.Collection.__getitem__` for semantics of the ``name`` argument. :returns: Two-tuple of (`.Task`, `dict`). .. versionadded:: 1.0

def to_contexts(self, ignore_unknown_help=None): (source)

Returns all contained tasks and subtasks as a list of parser contexts. :param bool ignore_unknown_help: Passed on to each task's ``get_arguments()`` method. See the config option by the same name for details. .. versionadded:: 1.0 .. versionchanged:: 1.7 Added the ``ignore_unknown_help`` kwarg.

def transform(self, name): (source)

Transform ``name`` with the configured auto-dashes behavior. If the collection's ``auto_dash_names`` attribute is ``True`` (default), all non leading/trailing underscores are turned into dashes. (Leading/trailing underscores tend to get stripped elsewhere in the stack.) If it is ``False``, the inverse is applied - all dashes are turned into underscores. .. versionadded:: 1.0

auto_dash_names: bool = (source)

Undocumented

collections = (source)

Undocumented

Undocumented

loaded_from = (source)

Undocumented

Undocumented

Undocumented

Return all task identifiers for this collection as a one-level dict. Specifically, a dict with the primary/"real" task names as the key, and any aliases as a list value. It basically collapses the namespace tree into a single easily-scannable collection of invocation strings, and is thus suitable for things like flat-style task listings or transformation into parser contexts. .. versionadded:: 1.0

def _add_object(self, obj, name=None): (source)

Undocumented

def _check_default_collision(self, name): (source)

Undocumented

def _split_path(self, path): (source)

Obtain first collection + remainder, of a task path. E.g. for ``"subcollection.taskname"``, return ``("subcollection", "taskname")``; for ``"subcollection.nested.taskname"`` return ``("subcollection", "nested.taskname")``, etc. An empty path becomes simply ``('', '')``.

def _task_with_merged_config(self, coll, rest, ours): (source)

Undocumented

def _transform_lexicon(self, old): (source)

Take a Lexicon and apply `.transform` to its keys and aliases. :returns: A new Lexicon.

_configuration: dict = (source)

Undocumented