1..
2  Copyright (C) 2014-2018 Red Hat, Inc.
3
4  This copyrighted material is made available to anyone wishing to use,
5  modify, copy, or redistribute it subject to the terms and conditions of
6  the GNU General Public License v.2, or (at your option) any later version.
7  This program is distributed in the hope that it will be useful, but WITHOUT
8  ANY WARRANTY expressed or implied, including the implied warranties of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
10  Public License for more details.  You should have received a copy of the
11  GNU General Public License along with this program; if not, write to the
12  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
13  02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
14  source code or documentation are not subject to the GNU General Public
15  License and may only be used or replicated with the express permission of
16  Red Hat, Inc.
17
18==================
19 Plugin Interface
20==================
21
22DNF plugin can be any Python class fulfilling the following criteria:
23
241. it derives from :class:`dnf.Plugin`,
252. it is made available in a Python module stored in one of the :attr:`.Conf.pluginpath`,
263. provides its own :attr:`~.Plugin.name` and :meth:`~.Plugin.__init__`.
27
28When DNF CLI runs it loads the plugins found in the paths during the CLI's initialization.
29
30.. class:: dnf.Plugin
31
32  The base class all DNF plugins must derive from.
33
34  .. attribute:: name
35
36    The plugin must set this class variable to a string identifying the plugin. The string can only contain alphanumeric characters and underscores.
37
38  .. staticmethod:: read_config(conf)
39
40    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.
41
42  .. method:: __init__(base, cli)
43
44    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).
45
46  .. method:: pre_config()
47
48    This hook is called before configuring the repos.
49
50  .. method:: config()
51
52    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.
53
54  .. method:: resolved()
55
56    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`.
57
58  .. method:: sack()
59
60    This hook is called immediately after :attr:`.Base.sack` is initialized with data from all the enabled repos.
61
62  .. method:: pre_transaction()
63
64    This hook is called just before transaction execution. This means after a successful transaction test. RPMDB is locked during that time.
65
66  .. method:: transaction()
67
68    This hook is called immediately after a successful transaction.
69    Plugins that were removed or obsoleted by the transaction will not run the transaction hook.
70
71.. method:: register_command(command_class)
72
73    A class decorator for automatic command registration.
74
75    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::
76
77        import dnf
78
79        @dnf.plugin.register_command
80        class HelloWorldCommand(dnf.cli.Command):
81            aliases = ('hello-world',)
82            summary = 'The example command'
83
84            def run(self):
85                print('Hello world!')
86
87    To run the command::
88
89        $ dnf hello-world
90        Hello world!
91
92
93You may want to see the comparison with `yum plugin hook API`_.
94
95.. _yum plugin hook API: http://dnf.readthedocs.org/en/latest/api_vs_yum.html
96