-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleHTMLFilter.py
More file actions
189 lines (172 loc) · 6.23 KB
/
SimpleHTMLFilter.py
File metadata and controls
189 lines (172 loc) · 6.23 KB
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
import copy
from html.parser import HTMLParser
from functools import partial
class SimpleHTMLFilter(HTMLParser):
""" Example:
<form name="form1" action="/action" method="post">
<p name="message">Available Options</p>
<input type="hidden" name="task" value="uid">
<table>
<tr align="left">
<td><b>Select an option: </b></td>
<td>
<select name="app">
<option value="value 1">value 1
<option value="value 2">value 2
</select>
</td>
</tr>
</table>
<p><input type="submit" name="submit" value="submit"></p>
</form>
def match_attr(to_, from_):
return all([item in to_ for item in from_])
P = lambda from_: partial(match_attr, from_=from_)
{
# match 'form' tag has name = 'form1' then output its 'action'
# attribute as 'post_url'
'tag': 'form',
'matcher': P([('name', 'form1')])
'out': [('action', 'post_url')],
'children':
(
# for descendant, match 'select' tag has name = 'app'
{
'tag': 'select',
'matcher': P([('name', 'app')])
'children':
(
# for descendants match any 'option' tag and output
# 'value' attribute as 'app'
{
'tag': 'option',
'out': [('value', 'app')]
}
)
},
# for descendants match 'input' tag has name = 'task',
# output 'value' attribute as 'task'
{
'tag': 'input',
'matcher': P([('name', 'task')])
'out': [('value', 'task')]
},
# for descendants match 'p' tag has name = 'message',
# output its data as 'message'
{
'tag': 'p',
'matcher': P([('name', 'message')])
'children':
(
{
'out': 'message'
}
)
}
)
}"""
def __init__(self, filters):
HTMLParser.__init__(self)
HTMLParser.reset(self)
self._stack = []
self._flag_pool = {}
self._value_pool = {}
self._starttag_handlers = []
self._endtag_handlers = []
self._data_handlers = []
self._install_filters(filters)
def reset(self):
HTMLParser.reset(self)
self._stack = []
self._flag_pool = {}
self._value_pool = {}
@staticmethod
def _generate_flag(pre_flag, flag):
if not pre_flag:
pre_flag = 'filter'
return '%s::%s' % (pre_flag, flag)
def _install_filters(self, filters, ptype=None, pre_flag=None):
"""
HtmlFilter = {
'tag':
- tag handler: tag name
- data handler: None
'matcher': matcher function that will be called with element attrs
'out':
- attributes: (attr_name, saved_name), ...
- data: saved_name
'descendants': filters, ...
}
"""
assert ptype in ('ATTR', 'DATA', None)
for i in range(len(filters)):
html_filter = filters[i]
assert id(html_filter) not in self._stack, \
'Recursive filters detected'
self._stack.append(id(html_filter))
tag = html_filter.get('tag', None)
matcher = html_filter.get('matcher', None)
output = html_filter.get('out', [])
descendants = html_filter.get('descendants', [])
if not matcher:
matcher = lambda x: True
new_flag = self._generate_flag(pre_flag, i)
if tag: # Attributes filter
if descendants:
self._install_filters(descendants, 'ATTR', new_flag)
self._starttag_handlers.append(
partial(self._start_tag, tag_=tag, matcher=matcher,
output=output, pre_flag=pre_flag,
pos_flag=new_flag))
self._endtag_handlers.append(
partial(self._end_tag, tag_=tag, flag=new_flag))
else: # Data filter
if descendants:
self._install_filters(descendants, 'DATA', new_flag)
self._data_handlers.append(
partial(self._handle_data, output=output, matcher=matcher,
pre_flag=pre_flag, pos_flag=new_flag))
self._stack.remove(id(html_filter))
def _start_tag(self, tag, attrs, tag_, matcher, output,
pre_flag=None, pos_flag=None):
if pre_flag and not self._flag_pool.get(pre_flag, False):
return
if tag == tag_:
if matcher(attrs):
self._flag_pool.update({pos_flag: True})
if output:
attrs = dict(attrs)
for item in output:
key, name = item[0], item[1]
value = attrs.get(key, None)
values = self._value_pool.get(name, [])
values.append(value)
self._value_pool.update({name: values})
def _end_tag(self, tag, tag_, flag):
if flag and (tag == tag_):
self._flag_pool.pop(flag, None)
def _handle_data(self, data, output, matcher, pre_flag=None, pos_flag=None):
if pre_flag and not self._flag_pool.get(pre_flag, False):
return
if matcher(data):
self._flag_pool.update({pos_flag: True})
if output:
values = self._value_pool.get(output, [])
values.append(data)
self._value_pool.update({output: values})
def handle_starttag(self, tag, attrs):
for handler in self._starttag_handlers:
handler(tag, attrs)
def handle_endtag(self, tag):
for handler in self._endtag_handlers:
handler(tag)
def handle_data(self, data):
for handler in self._data_handlers:
handler(data)
def validate(self):
for values in self._value_pool.values():
if not values or values[0] is None:
return False
return True
def dump(self):
return copy.deepcopy(self._value_pool)