summaryrefslogtreecommitdiffstats
path: root/pyfirstaidkit/dependency.py
blob: 73100bd680c361947d9ed99386f0e9dddd0212ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File name: dependency.py
# Date:      2008/04/18
# Author:    Martin Sivak
#
# Copyright (C) Red Hat 2008
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# in a file called COPYING along with this program; if not, write to
# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
# 02139, USA.

import logging
import copy
from errors import *

Logger=logging.getLogger("firstaidkit")

class Dependencies(object):
    """Encapsulate flags used to control the dependencies between plugins"""
    def __init__(self):
        self._provide = None
        self._known = set()
        self.reset()

    def provide(self, id, setactionflag = True):
        """Add flag"""
        Logger.info("Setting dependency flag %s", id)
        self._provide.add(id)
        #Action flags denote activity happening on some regular flag
        if setactionflag: self._provide.add(id+"?")

    def unprovide(self, id, setactionflag = True):
        """Remove flag"""
        Logger.info("Resetting dependency flag %s", id)
        try:
            self._provide.remove(id)
        except KeyError: #not there
            pass
        if setactionflag: self._provide.add(id+"?")

    donotprovide = unprovide #alias
    failed = unprovide #alias

    def require(self, id):
        """Return True if flag is present, otherwise false"""
        return id in self._provide

    def introduce(self, s):
        """Notifies the system about dep names used in the plugins.

        This allows us to list them in help"""
        self._known = self._known.union(s)

    def known(self):
        """Returns list of known flags"""
        return list(self._known.union(self._provide))

    def valid(self):
        """Returns list of valid/provided flags"""
        return list(self._provide)

    def reset(self):
        self._provide = set()