summaryrefslogtreecommitdiffstats
path: root/pyfirstaidkit/issue.py
blob: 2913943f17981474a07f70b5778ef0da0f7c8dc4 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File name: issue.py
# Date:      2008/03/14
# Author:    Martin Sivak <msivak at redhat dot com>
#
# 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.

class SimpleIssue(object):
    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.reset()

    def reset(self):
        """Reset the object's state"""
        self._checked = False
        self._happened = False
        self._fixed = False

    def set(self, happened = None, fixed = None, checked = None, reporting = None, **kwreportingargs):
        """Set the state of this issue and send a report (if reporting is not None)"""
        if happened:
            self._happened = happened
        if fixed:
            self._fixed = fixed
        if checked:
            self._checked = checked
        if reporting:
            reporting.issue(issue = self, **kwreportingargs)

    def happened(self):
        """Get the 'issue happened' flag.
Return values:
    True - YES it happened
    False - NO, it is OK
    None - I don't know, there was an error"""
        #if the issue was fixed or not checked, the check si needed
        if not self._checked or self._fixed:
            return None
        else:
            return self._happened
        
    def fixed(self):
        """Get the 'issue fixed' flag.
Return values:
    True - YES it is fixed
    False - NO, it is still broken
    None - I don't know"""
        #if the issue was not checked, the check si needed
        if not self._checked:
            return None
        else:
            #issue didn't happened or is fixed -> True
            return not self._happened or self._fixed

    def __str__(self):
        s = []
        if self._fixed:
            s.append("Fixed")
        elif self._happened and self._checked:
            s.append("Detected")
        elif self._checked:
            s.append("No problem with")
        else:
            s.append("Waiting for check on")

        s.append(self.name)

        if self._happened and self._checked:
            s.append("--")
            s.append(self.description)

        return " ".join(s)

    def str(self):
        return self.__str__()

class Issue(SimpleIssue):
    name = "Parent issue"
    description = "This happens when you use the wrong object in the issues list"

    def __init__(self, plugin, reporting = None):
        SimpleIssue.__init__(self, self.name, self.description)
        self._plugin = plugin
        self._reporting = reporting

    def detect(self):
        """Detect if this situation happened and store some information about it, so we can fix it
Return values:
    True - check OK
    False - check Failed
    None - no result, please continue with the operation"""

        #if the issue was fixed. the check is worthless
        #if it was checked, no need to do the check again
        if self._checked or self._fixed:
            return not self._fixed and self._checked

        return None #no error, please do the check (so the child-class knows to actually do something)

    def fix(self):
        """Fix the situation if needed
Return values:
    True - fix OK
    False - fix Failed
    None - no result, please continue with the operation"""

        #if the issue was fixed. no need to do the fix again
        #if it was not checked, the check si needed too
        if not self._checked or self._fixed:
            return self._fixed and self._checked

        return None #no fix error, please do the fix (so the child-class knows to actually do something)