12 |
12 |
13 class AdBlockSearchTreeNode: |
13 class AdBlockSearchTreeNode: |
14 """ |
14 """ |
15 Class implementing the AdBlock search tree node. |
15 Class implementing the AdBlock search tree node. |
16 """ |
16 """ |
|
17 |
17 def __init__(self): |
18 def __init__(self): |
18 """ |
19 """ |
19 Constructor |
20 Constructor |
20 """ |
21 """ |
21 self.char = '' |
22 self.char = "" |
22 self.rule = None |
23 self.rule = None |
23 self.children = {} |
24 self.children = {} |
24 |
25 |
25 |
26 |
26 class AdBlockSearchTree: |
27 class AdBlockSearchTree: |
27 """ |
28 """ |
28 Class implementing the AdBlock search tree. |
29 Class implementing the AdBlock search tree. |
29 """ |
30 """ |
|
31 |
30 def __init__(self): |
32 def __init__(self): |
31 """ |
33 """ |
32 Constructor |
34 Constructor |
33 """ |
35 """ |
34 self.__root = AdBlockSearchTreeNode() |
36 self.__root = AdBlockSearchTreeNode() |
35 |
37 |
36 def clear(self): |
38 def clear(self): |
37 """ |
39 """ |
38 Public method to clear the search tree. |
40 Public method to clear the search tree. |
39 """ |
41 """ |
40 self.__deleteNode(self.__root) |
42 self.__deleteNode(self.__root) |
41 self.__root = AdBlockSearchTreeNode() |
43 self.__root = AdBlockSearchTreeNode() |
42 |
44 |
43 def add(self, rule): |
45 def add(self, rule): |
44 """ |
46 """ |
45 Public method to add a rule to the search tree. |
47 Public method to add a rule to the search tree. |
46 |
48 |
47 @param rule rule to be added |
49 @param rule rule to be added |
48 @type AdBlockRule |
50 @type AdBlockRule |
49 @return flag indicating a successful addition |
51 @return flag indicating a successful addition |
50 @rtype bool |
52 @rtype bool |
51 """ |
53 """ |
52 if rule.ruleType() != AdBlockRuleType.StringContainsMatchRule: |
54 if rule.ruleType() != AdBlockRuleType.StringContainsMatchRule: |
53 return False |
55 return False |
54 |
56 |
55 filterString = rule.matchString() |
57 filterString = rule.matchString() |
56 |
58 |
57 if len(filterString) <= 0: |
59 if len(filterString) <= 0: |
58 return False |
60 return False |
59 |
61 |
60 node = self.__root |
62 node = self.__root |
61 |
63 |
62 for filterChar in filterString: |
64 for filterChar in filterString: |
63 try: |
65 try: |
64 nextNode = node.children[filterChar] |
66 nextNode = node.children[filterChar] |
65 except KeyError: |
67 except KeyError: |
66 nextNode = AdBlockSearchTreeNode() |
68 nextNode = AdBlockSearchTreeNode() |
67 nextNode.char = filterChar |
69 nextNode.char = filterChar |
68 node.children[filterChar] = nextNode |
70 node.children[filterChar] = nextNode |
69 node = nextNode |
71 node = nextNode |
70 |
72 |
71 node.rule = rule |
73 node.rule = rule |
72 |
74 |
73 return True |
75 return True |
74 |
76 |
75 def find(self, request, domain, urlString): |
77 def find(self, request, domain, urlString): |
76 """ |
78 """ |
77 Public method to find a matching rule. |
79 Public method to find a matching rule. |
78 |
80 |
79 @param request URL request to be matched |
81 @param request URL request to be matched |
80 @type QWebEngineUrlRequestInfo |
82 @type QWebEngineUrlRequestInfo |
81 @param domain domain of the URL |
83 @param domain domain of the URL |
82 @type str |
84 @type str |
83 @param urlString requested URL as a lowercase string |
85 @param urlString requested URL as a lowercase string |
84 @type str |
86 @type str |
85 @return reference to the matched rule |
87 @return reference to the matched rule |
86 @rtype AdBlockRule |
88 @rtype AdBlockRule |
87 """ |
89 """ |
88 length = len(urlString) |
90 length = len(urlString) |
89 |
91 |
90 if length <= 0: |
92 if length <= 0: |
91 return None |
93 return None |
92 |
94 |
93 for index in range(length): |
95 for index in range(length): |
94 rule = self.__prefixSearch(request, domain, urlString, |
96 rule = self.__prefixSearch( |
95 urlString[index:], length - index) |
97 request, domain, urlString, urlString[index:], length - index |
|
98 ) |
96 if rule: |
99 if rule: |
97 return rule |
100 return rule |
98 |
101 |
99 return None |
102 return None |
100 |
103 |
101 def __deleteNode(self, node): |
104 def __deleteNode(self, node): |
102 """ |
105 """ |
103 Private method to delete a search tree node. |
106 Private method to delete a search tree node. |
104 |
107 |
105 @param node reference to the node to be deleted |
108 @param node reference to the node to be deleted |
106 @type AdBlockSearchTreeNode |
109 @type AdBlockSearchTreeNode |
107 """ |
110 """ |
108 if not node: |
111 if not node: |
109 return |
112 return |
110 |
113 |
111 for key in node.children: |
114 for key in node.children: |
112 self.__deleteNode(node.children[key]) |
115 self.__deleteNode(node.children[key]) |
113 |
116 |
114 node.children = {} |
117 node.children = {} |
115 node = None |
118 node = None |
116 |
119 |
117 def __prefixSearch(self, request, domain, urlString, string, length): |
120 def __prefixSearch(self, request, domain, urlString, string, length): |
118 """ |
121 """ |
119 Private method to perform a prefix search. |
122 Private method to perform a prefix search. |
120 |
123 |
121 @param request URL request to be matched |
124 @param request URL request to be matched |
122 @type QWebEngineUrlRequestInfo |
125 @type QWebEngineUrlRequestInfo |
123 @param domain domain of the URL |
126 @param domain domain of the URL |
124 @type str |
127 @type str |
125 @param urlString requested URL as a lowercase string |
128 @param urlString requested URL as a lowercase string |