Helpviewer/GreaseMonkey/GreaseMonkeyUrlMatcher.py

changeset 1953
26aa6fd94dc2
child 2302
f29e9405c851
equal deleted inserted replaced
1952:af4103f0e93f 1953:26aa6fd94dc2
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the GreaseMonkey URL matcher.
8 """
9
10 import re
11
12 from PyQt4.QtCore import Qt, QRegExp
13
14
15 def wildcardMatch(string, pattern):
16 """
17 Module function implementing a special wildcard matcher.
18
19 @param string string to match (string)
20 @param pattern pattern to be used (string)
21 """
22 stringSize = len(string)
23
24 startsWithWildcard = pattern.startswith("*")
25 endsWithWildcard = pattern.endswith("*")
26
27 parts = pattern.split("*")
28 pos = 0
29
30 if startsWithWildcard:
31 pos = string.find(parts[1])
32 if pos == -1:
33 return False
34
35 for part in parts:
36 pos = string.find(part, pos)
37 if pos == -1:
38 return False
39
40 if not endsWithWildcard and stringSize - pos != len(parts[-1]):
41 return False
42
43 return True
44
45
46 class GreaseMonkeyUrlMatcher(object):
47 """
48 Class implementing the GreaseMonkey URL matcher.
49 """
50 def __init__(self, pattern):
51 """
52 Constructor
53
54 @param pattern pattern to be used for the matching (string)
55 """
56 self.__pattern = pattern
57 self.__matchString = ""
58 self.__regExp = QRegExp()
59 self.__useRegExp = False
60
61 self.__parsePattern(self.__pattern)
62
63 def pattern(self):
64 """
65 Public method to get the match pattern.
66
67 @return match pattern (string)
68 """
69 return self.__pattern
70
71 def match(self, urlString):
72 """
73 Public method to match the given URL.
74
75 @param urlString URL to match (string)
76 """
77 if self.__useRegExp:
78 return self.__regExp.indexIn(urlString) != -1
79 else:
80 return wildcardMatch(urlString, self.__matchString)
81
82 def __parsePattern(self, pattern):
83 """
84 Private method to parse the match pattern.
85
86 @param pattern match pattern to be used (string)
87 """
88 if pattern.startswith("/") and pattern.endswith("/"):
89 pattern = pattern[1:-1]
90
91 self.__regExp = QRegExp(pattern, Qt.CaseInsensitive)
92 self.__useRegExp = True
93 elif ".tld" in pattern:
94 pattern = re.sub(r"(\W)", r"\\\1", pattern) # escape special symbols
95 pattern = re.sub(r"\*+", "*", pattern) # remove multiple wildcards
96 pattern = re.sub(r"^\\\|", "^", pattern) # process anchor at expression
97 # start
98 pattern = re.sub(r"\\\|$", "$", pattern) # process anchor at expression
99 # end
100 pattern = re.sub(r"\\\*", ".*", pattern) # replace wildcards by .*
101 pattern = re.sub(r"\.tld", r"\.[a-z.]{2,6}") # replace domain pattern
102
103 self.__useRegExp = True
104 self.__regExp = QRegExp(pattern, Qt.CaseInsensitive)
105 else:
106 self.__matchString = pattern

eric ide

mercurial