Blame doc/api_plugins.rst

Packit 6f3914
..
Packit 6f3914
  Copyright (C) 2014-2018 Red Hat, Inc.
Packit 6f3914
Packit 6f3914
  This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
  modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
  the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
  This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
  ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
  Public License for more details.  You should have received a copy of the
Packit 6f3914
  GNU General Public License along with this program; if not, write to the
Packit 6f3914
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
  02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
  source code or documentation are not subject to the GNU General Public
Packit 6f3914
  License and may only be used or replicated with the express permission of
Packit 6f3914
  Red Hat, Inc.
Packit 6f3914
Packit 6f3914
==================
Packit 6f3914
 Plugin Interface
Packit 6f3914
==================
Packit 6f3914
Packit 6f3914
DNF plugin can be any Python class fulfilling the following criteria:
Packit 6f3914
Packit 6f3914
1. it derives from :class:`dnf.Plugin`,
Packit 6f3914
2. it is made available in a Python module stored in one of the :attr:`.Conf.pluginpath`,
Packit 6f3914
3. provides its own :attr:`~.Plugin.name` and :meth:`~.Plugin.__init__`.
Packit 6f3914
Packit 6f3914
When DNF CLI runs it loads the plugins found in the paths during the CLI's initialization.
Packit 6f3914
Packit 6f3914
.. class:: dnf.Plugin
Packit 6f3914
Packit 6f3914
  The base class all DNF plugins must derive from.
Packit 6f3914
Packit 6f3914
  .. attribute:: name
Packit 6f3914
Packit 6f3914
    The plugin must set this class variable to a string identifying the plugin. The string can only contain alphanumeric characters and underscores.
Packit 6f3914
Packit 6f3914
  .. staticmethod:: read_config(conf)
Packit 6f3914
Packit 6f3914
    Read plugin's configuration into a `ConfigParser <http://docs.python.org/3/library/configparser.html>`_ compatible instance. `conf` is a :class:`.Conf` instance used to look up the plugin configuration directory.
Packit 6f3914
Packit 6f3914
  .. method:: __init__(base, cli)
Packit 6f3914
Packit 6f3914
    The plugin must override this. Called immediately after all the plugins are loaded. `base` is an instance of :class:`dnf.Base`. `cli` is an instance of :class:`dnf.cli.Cli` but can also be ``None`` in case DNF is running without a CLI (e.g. from an extension).
Packit 6f3914
Packit 6f3914
  .. method:: pre_config()
Packit 6f3914
Packit 6f3914
    This hook is called before configuring the repos.
Packit 6f3914
Packit 6f3914
  .. method:: config()
Packit 6f3914
Packit 6f3914
    This hook is called immediately after the CLI/extension is finished configuring DNF.  The plugin can use this to tweak the global configuration or the repository configuration.
Packit 6f3914
Packit 6f3914
  .. method:: resolved()
Packit 6f3914
Packit 6f3914
    This hook is called immediately after the CLI has finished resolving a transaction. The plugin can use this to inspect the resolved but not yet executed :attr:`Base.transaction`.
Packit 6f3914
Packit 6f3914
  .. method:: sack()
Packit 6f3914
Packit 6f3914
    This hook is called immediately after :attr:`.Base.sack` is initialized with data from all the enabled repos.
Packit 6f3914
Packit 6f3914
  .. method:: pre_transaction()
Packit 6f3914
Packit 6f3914
    This hook is called just before transaction execution. This means after a successful transaction test. RPMDB is locked during that time.
Packit 6f3914
Packit 6f3914
  .. method:: transaction()
Packit 6f3914
Packit 6f3914
    This hook is called immediately after a successful transaction.
Packit 6f3914
    Plugins that were removed or obsoleted by the transaction will not run the transaction hook.
Packit 6f3914
Packit 6f3914
.. method:: register_command(command_class)
Packit 6f3914
Packit 6f3914
    A class decorator for automatic command registration.
Packit 6f3914
Packit 6f3914
    Example of a plugin that provides a hello-world dnf command (the file must be placed in one of the :ref:`pluginpath <pluginpath-label>` directories::
Packit 6f3914
Packit 6f3914
        import dnf
Packit 6f3914
Packit 6f3914
        @dnf.plugin.register_command
Packit 6f3914
        class HelloWorldCommand(dnf.cli.Command):
Packit 6f3914
            aliases = ('hello-world',)
Packit 6f3914
            summary = 'The example command'
Packit 6f3914
Packit 6f3914
            def run(self):
Packit 6f3914
                print('Hello world!')
Packit 6f3914
Packit 6f3914
    To run the command::
Packit 6f3914
Packit 6f3914
        $ dnf hello-world
Packit 6f3914
        Hello world!
Packit 6f3914
Packit 6f3914
Packit 6f3914
You may want to see the comparison with `yum plugin hook API`_.
Packit 6f3914
Packit 6f3914
.. _yum plugin hook API: http://dnf.readthedocs.org/en/latest/api_vs_yum.html