toolbox package

Submodules

toolbox.cli module

toolbox.cli.main()[source]

toolbox.config module

class toolbox.config.ConfigManager[source]

Bases: object

The config manager has the responsibility of persisting plugin configs. On initialisation it creates a default file structure in the user’s home directory

FILE_EXT = '.json'
load_plugin(name)[source]

Load the plugin config file by name and return an py:class:toolbox.config.PluginConfig

Parameters:name (str) –
Returns:an instance of PluginConfig for given plugin name
Return type:toolbox.config.PluginConfig
save(plugins)[source]

Convenience method to save a list of plugins. Only configs that have been modified since loading will be saved. :param iterable plugins: list of instances of base class py:class:toolbox.plugin.ToolboxPlugin :return:

save_plugin(name, config)[source]

save a plugin config by name before saving the global config key is deleted

Parameters:
  • name (str) – Name of the plugin
  • config – instance of an py:class:toolbox.config.PluginConfig
Returns:

class toolbox.config.PluginConfig[source]

Bases: object

Config container for plugin configs. Acts like a dictionary with some extra convenience methods. The config has a special key for global configs which can be accessed with the ‘get_global_config’ method

GLOBAL_KEY = '__GLOBAL__'
classmethod create_from_dict(dict)[source]

Factory method to create a PluginConfig from a python dictionary :param dict: :return: a PluginConfig :rtype: py:class:toolbox.config.PluginConfig

get_global_config()[source]
keys()[source]
set_global_config(config)[source]
to_json()[source]

Converts the config values to a JSON string :return: JSON string :rtype: str

toolbox.core module

class toolbox.core.Toolbox(external=True, local=True)[source]

Bases: object

Initialize the toolbox, sets up the main argument parser and Tool registry Core tools in de contrib package are always loaded

The local and external flags can be used to limit the loaded modules

Parameters:
  • external (bool) –
  • local (bool) –
Returns:

execute(args)[source]
This is the Toolbox’s main function which parses the arguments, which should yield:
  • a Tool
  • an Executable
  • and some optional arguments
The Tool gets fetched from the registry where it will be fully loaded
and the exectuble is executed with the remaining args
Parameters:args (list) – List of arguments
Returns:
prepare()[source]

Prepares the main Argument parser by loading all registered plugins and setting their executable

Returns:
shutdown()[source]

Shuts down the application, save and close config files etc. :return:

exception toolbox.core.UnknownPlugin[source]

Bases: exceptions.Exception

toolbox.defaults module

toolbox.mixins module

class toolbox.mixins.ConfigMixin[source]

Bases: object

The ConfigMixin provides the plugin with an persisted py:class:toolbox.config.PluginConfig which is basically an expanded python dictionary which contains the plugin config as well as an global config.

get_config()[source]
get_global_config()[source]
set_config(config)[source]
class toolbox.mixins.LogMixin[source]

Bases: object

The LogMixin provides the plugin with a zero configuration python logger.

get_logger()[source]
set_logger(logger)[source]
class toolbox.mixins.RegistryMixin[source]

Bases: object

The RegistryMixin provides the plugin with an registry interface. Plugins that subclass this mixin get full access to the toolbox registry so you could use plugins within plugins

get_registry()[source]
set_registry(registry)[source]

toolbox.plugin module

class toolbox.plugin.BasePlugin(name, description=None, prepare_parser_func=None, execute_func=None)[source]

Bases: toolbox.plugin.ToolboxPlugin

An easy to use Plugin wrapper that sets all the necessary parts of a ToolboxPlugin

Parameters:
  • name – The name of the plugin
  • description – short description of the plugin (used to index and search plugins)
  • prepare_parser_func – The function that prepares the Toolbox main parser
  • execute_func – the main function of the plugin, accecpts argparse.Namespace
Returns:

execute(args)[source]
prepare_parser(parser)[source]
set_description(description)[source]
set_execute(execute_func)[source]

Set the executable for the toolbox. the executable gets passed a ArgumentParser Namespace

Parameters:parser (function) –
Raise:py:class:toolbox.plugin.NotCallableException
Returns:
set_prepare_parser(prepare_parser_func)[source]

Set the callable that prepares the Toolbox parser. the callable gets a argparse.Argumentparser as argument

Parameters:parser (function) –
Raise:py:class:toolbox.plugin.NotCallableException
Returns:
class toolbox.plugin.ExecutablePlugin[source]

Bases: toolbox.plugin.ToolboxPlugin

The ExecutablePlugin is a basic plugin to register shell scripts and the like. Just subclass it and provide the following attributes:

  • name
  • description (optional)
  • executable (the main executable)
  • args (a list of default arguments)

optionally the prepare_parser and execute methods can be overridden to add exta functionality

args = []
description = None
executable = None
execute(args)[source]
name = None
prepare_parser(parser)[source]
setup(name, executable, **kwargs)[source]

Method to setup an Executable plugin, no need to subclass it this way!

Parameters:
  • name
  • executable
  • kwargs
Returns:

exception toolbox.plugin.NotCallableException[source]

Bases: exceptions.Exception

class toolbox.plugin.ToolboxPlugin[source]

Bases: object

Abstract base class for an Toolbox plugin

description = None
execute(args)[source]
name = None
prepare_parser(parser)[source]

toolbox.registry module

exception toolbox.registry.NoPluginException[source]

Bases: exceptions.Exception

class toolbox.registry.Registry[source]

Bases: object

Registry of all available plugins Setup the config manager and creates two empty dicts to track the tools

Variables:
  • config_manager (toolbox.config.ConfigManager) – Instance of the config manager
  • _registered_plugins (dict) – dictionary containing all registered plugins
  • _loaded_plugins (dict) – dictionary containing all fully loaded plugins
add_plugin(plugin)[source]
Add a toolbox.plugin.ToolboxPlugin to the registry, but don’t load it yet
Checks whether the provided plugin is an instance of toolbox.plugin.ToolboxPlugin
Parameters:plugin (toolbox.plugin.ToolboxPlugin) – A plugin to add to the registry
Raises:AtttibuteError, toolbox.registry.NoPluginException
Returns:
get_plugin(name)[source]

fetch a plugin from the registry and load it if it is not loaded already.

Parameters:name (str) – Name of the registered plugin
Returns:An loaded plugin
Return type:toolbox.plugin.ToolboxPlugin
Raise:ValueError
get_plugin_names()[source]
Returns:the list of registered plugins (names)
Return type:list
get_plugins()[source]
Returns:List of registered plugins
Return type:list
populate(modules)[source]

Given a list of ‘importable’ modules populate the registry First import the module, check whether it has a ‘Plugin’ attribute in the namespace and add the plugin to the registry

Also accepts an module where there is a class definition suffixed by ‘Plugin’ like ‘TestPlugin’

Parameters:modules (list) –
Returns:
shutdown()[source]

Shutdown the registry and notify the config manager to shutdown :return:

toolbox.scanner module

toolbox.scanner.find_contrib_modules()[source]

Find all core modules in the contrib package and return a list of importable packages :return: A list of importable packages :rtype: list

toolbox.scanner.find_local_modules(plugin_dir)[source]

Find locally installed tools

Parameters:plugin_dir (str) –
Returns:A list of importable packages
Return type:list
toolbox.scanner.find_modules(prefix=None)[source]

Find all python modules with that contain the given prefix as package name

Returns:A list of importable packages
Return type:list

toolbox.utils module

toolbox.utils.generate_name()[source]

Generate a random plugin name :return:

Module contents