Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mwlib/sanitychecker.py
blob: 78d25b8c29228d182d67b4adbf848771ac5980f0 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright (c) 2007-2008 PediaPress GmbH
# See README.txt for additional licensing information.
"""
class for defining DTD-Like Rules for the tree
"""
from advtree import Article

from mwlib.log import Log
log = Log("sanitychecker")

# -----------------------------------------------------------
# Constraints
# -----------------------------------------------------------

class ConstraintBase(object):
    def __init__(self, *klasses):
        self.klasses = klasses

    def test(self, nodes): 
        return True,None # passed

    def __repr__(self):
        return "%s(%s)" %(self.__class__.__name__, ", ".join(k.__name__ for k in self.klasses))


class Forbid(ConstraintBase):
    "forbid any of the classes"
    def test(self, nodes): 
        for n in nodes:
            if n.__class__ in self.klasses:
                return False, n
        return True, None
    

class Allow(ConstraintBase):
    "allow only these classes"
    def test(self, nodes): 
        for n in nodes:
            if not n.__class__ in self.klasses:
                return False, n
        return True, None


class Require(ConstraintBase):
    "require any of these classes"
    def test(self, nodes): 
        for n in nodes:
            if n.__class__ in self.klasses:
                return True, n
        return False, None
  
class Equal(ConstraintBase):
    "node classes and their order must be equal to these klasses"
    def test(self, nodes): 
        if len(nodes) != len(self.klasses):
            return False, None # FIXME what could we report?
        for i,n in enumerate(nodes):
            if n.__class__ !=  self.klasses[i]:
                return False, n
        return True, None
    

# -----------------------------------------------------------
# Rules regarding [Children, AllChildren, Parents, ...]
# -----------------------------------------------------------

class RuleBase:
    def __init__(self, klass, constraint):
        self.klass = klass
        self.constraint = constraint
    
    def _tocheck(self, node):
       return [] 

    def test(self, node):
        if node.__class__ == self.klass:
            return self.constraint.test( self._tocheck(node) )
        return True, None

    def __repr__(self):
        return "%s(%s, %r)" %(self.__class__.__name__, self.klass.__name__, self.constraint)

class ChildrenOf(RuleBase):
    def _tocheck(self, node):
        return node.children

class AllChildrenOf(RuleBase):
    def _tocheck(self, node):
        return node.getAllChildren()

class ParentsOf(RuleBase):
    def _tocheck(self, node):
        return node.parents

class ParentOf(RuleBase):
    def _tocheck(self, node):
        if node.parent:
            return [node.parent]
        return []

class SiblingsOf(RuleBase):
    def _tocheck(self, node):
        return node.siblings



# example custom rules

class RequireChild(RuleBase):

    def __init__(self, klass):
        self.klass = klass

    def __repr__(self):
        return "%s(%s)" %(self.__class__.__name__, self.klass.__name__)

    def test(self, node):
        if node.__class__ == self.klass:
            if not len(node.children):
                return False, node
        return True, None




# -----------------------------------------------------------
# Callbacks
# -----------------------------------------------------------
"""
callbacks get called if added to rules
callback return values should be:
 * True if it modified the tree and the sanity check needs to restart
 * False if the tree is left unmodified
"""
class SanityException(Exception):
    pass    

def exceptioncb(rule, node=None, parentnode=None):
    raise SanityException("%r  err:%r" %(rule, node or parentnode) )

def warncb(rule, node=None, parentnode=None):
    log.warn("%r node:%r parent:%r" %(rule, node, parentnode))
    return False

def removecb(rule, node=None, parentnode=None):
    assert node and node.parent
    node.parent.removeChild(node)
    return True



# -----------------------------------------------------------
# Container for sanity rules
# -----------------------------------------------------------

class SanityChecker(object):

    def __init__(self):
        self.rules = []

    def addRule(self, rule, actioncb=exceptioncb):
        self.rules.append((rule, actioncb))
    
    def check(self, tree):
        """ 
        check each node with each rule
        on failure call callback
        """
        modified = True
        while modified:
            modified = False
            for node in tree.allchildren():
                #if node.__class__ == Article:
                #    log.info("checking article:", node.caption.encode('utf-8'))
                for r,cb in self.rules:
                    passed, errnode = r.test(node)
                    if not passed and cb:
                        if cb(r, errnode or node):
                            modified = True
                            break
                if modified:
                    break

def demo(tree):
    "for documentation only, see tests for more demos"
    from mwlib.advtree import Table, Row, Cell, Text, ImageLink, PreFormatted

    sc = SanityChecker()
    rules = [ChildrenOf(Table, Allow(Row)),
             ChildrenOf(Row, Allow(Cell)),
             AllChildrenOf(Cell, Require(Text, ImageLink)),
             AllChildrenOf(Cell, Forbid(PreFormatted)),
             ChildrenOf(PreFormatted, Equal(Text)),
             ]
    
    def mycb(rule, node=None, parentnode=None):
        print "failed", rule, node or parentnode
        modifiedtree = False
        return modifiedtree

    for r in rules:
        sc.addRule( r, mycb)
    #sc.check(anytree)