src/eric7/Plugins/WizardPlugins/QRegularExpressionWizard/QRegularExpressionWizardServer.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9482
a2bc06a54d9d
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
12 12
13 13
14 def rxValidate(regexp, options): 14 def rxValidate(regexp, options):
15 """ 15 """
16 Function to validate the given regular expression. 16 Function to validate the given regular expression.
17 17
18 @param regexp regular expression to validate (string) 18 @param regexp regular expression to validate (string)
19 @param options list of options (list of string) 19 @param options list of options (list of string)
20 @return tuple of flag indicating validity (boolean), error 20 @return tuple of flag indicating validity (boolean), error
21 string (string) and error offset (integer) 21 string (string) and error offset (integer)
22 """ 22 """
23 try: 23 try:
24 from PyQt6.QtCore import QRegularExpression 24 from PyQt6.QtCore import QRegularExpression
25
25 rxOptions = QRegularExpression.PatternOption.NoPatternOption 26 rxOptions = QRegularExpression.PatternOption.NoPatternOption
26 if "CaseInsensitiveOption" in options: 27 if "CaseInsensitiveOption" in options:
27 rxOptions |= QRegularExpression.PatternOption.CaseInsensitiveOption 28 rxOptions |= QRegularExpression.PatternOption.CaseInsensitiveOption
28 if "MultilineOption" in options: 29 if "MultilineOption" in options:
29 rxOptions |= QRegularExpression.PatternOption.MultilineOption 30 rxOptions |= QRegularExpression.PatternOption.MultilineOption
30 if "DotMatchesEverythingOption" in options: 31 if "DotMatchesEverythingOption" in options:
31 rxOptions |= ( 32 rxOptions |= QRegularExpression.PatternOption.DotMatchesEverythingOption
32 QRegularExpression.PatternOption.DotMatchesEverythingOption
33 )
34 if "ExtendedPatternSyntaxOption" in options: 33 if "ExtendedPatternSyntaxOption" in options:
35 rxOptions |= ( 34 rxOptions |= QRegularExpression.PatternOption.ExtendedPatternSyntaxOption
36 QRegularExpression.PatternOption.ExtendedPatternSyntaxOption
37 )
38 if "InvertedGreedinessOption" in options: 35 if "InvertedGreedinessOption" in options:
39 rxOptions |= ( 36 rxOptions |= QRegularExpression.PatternOption.InvertedGreedinessOption
40 QRegularExpression.PatternOption.InvertedGreedinessOption
41 )
42 if "UseUnicodePropertiesOption" in options: 37 if "UseUnicodePropertiesOption" in options:
43 rxOptions |= ( 38 rxOptions |= QRegularExpression.PatternOption.UseUnicodePropertiesOption
44 QRegularExpression.PatternOption.UseUnicodePropertiesOption
45 )
46 if "DontCaptureOption" in options: 39 if "DontCaptureOption" in options:
47 rxOptions |= QRegularExpression.PatternOption.DontCaptureOption 40 rxOptions |= QRegularExpression.PatternOption.DontCaptureOption
48 41
49 error = "" 42 error = ""
50 errorOffset = -1 43 errorOffset = -1
51 re = QRegularExpression(regexp, rxOptions) 44 re = QRegularExpression(regexp, rxOptions)
52 valid = re.isValid() 45 valid = re.isValid()
53 if not valid: 46 if not valid:
55 errorOffset = re.patternErrorOffset() 48 errorOffset = re.patternErrorOffset()
56 except ImportError: 49 except ImportError:
57 valid = False 50 valid = False
58 error = "ImportError" 51 error = "ImportError"
59 errorOffset = 0 52 errorOffset = 0
60 53
61 return valid, error, errorOffset 54 return valid, error, errorOffset
62 55
63 56
64 def rxExecute(regexp, options, text, startpos): 57 def rxExecute(regexp, options, text, startpos):
65 """ 58 """
66 Function to execute the given regular expression for a given text. 59 Function to execute the given regular expression for a given text.
67 60
68 @param regexp regular expression to validate (string) 61 @param regexp regular expression to validate (string)
69 @param options list of options (list of string) 62 @param options list of options (list of string)
70 @param text text to execute on (string) 63 @param text text to execute on (string)
71 @param startpos start position for the execution (integer) 64 @param startpos start position for the execution (integer)
72 @return tuple of a flag indicating a successful match (boolean) and 65 @return tuple of a flag indicating a successful match (boolean) and
75 (integer) for each entry 68 (integer) for each entry
76 """ 69 """
77 valid, error, errorOffset = rxValidate(regexp, options) 70 valid, error, errorOffset = rxValidate(regexp, options)
78 if not valid: 71 if not valid:
79 return valid, error, errorOffset 72 return valid, error, errorOffset
80 73
81 from PyQt6.QtCore import QRegularExpression 74 from PyQt6.QtCore import QRegularExpression
75
82 rxOptions = QRegularExpression.PatternOption.NoPatternOption 76 rxOptions = QRegularExpression.PatternOption.NoPatternOption
83 if "CaseInsensitiveOption" in options: 77 if "CaseInsensitiveOption" in options:
84 rxOptions |= QRegularExpression.PatternOption.CaseInsensitiveOption 78 rxOptions |= QRegularExpression.PatternOption.CaseInsensitiveOption
85 if "MultilineOption" in options: 79 if "MultilineOption" in options:
86 rxOptions |= QRegularExpression.PatternOption.MultilineOption 80 rxOptions |= QRegularExpression.PatternOption.MultilineOption
87 if "DotMatchesEverythingOption" in options: 81 if "DotMatchesEverythingOption" in options:
88 rxOptions |= ( 82 rxOptions |= QRegularExpression.PatternOption.DotMatchesEverythingOption
89 QRegularExpression.PatternOption.DotMatchesEverythingOption
90 )
91 if "ExtendedPatternSyntaxOption" in options: 83 if "ExtendedPatternSyntaxOption" in options:
92 rxOptions |= ( 84 rxOptions |= QRegularExpression.PatternOption.ExtendedPatternSyntaxOption
93 QRegularExpression.PatternOption.ExtendedPatternSyntaxOption
94 )
95 if "InvertedGreedinessOption" in options: 85 if "InvertedGreedinessOption" in options:
96 rxOptions |= QRegularExpression.PatternOption.InvertedGreedinessOption 86 rxOptions |= QRegularExpression.PatternOption.InvertedGreedinessOption
97 if "UseUnicodePropertiesOption" in options: 87 if "UseUnicodePropertiesOption" in options:
98 rxOptions |= ( 88 rxOptions |= QRegularExpression.PatternOption.UseUnicodePropertiesOption
99 QRegularExpression.PatternOption.UseUnicodePropertiesOption
100 )
101 if "DontCaptureOption" in options: 89 if "DontCaptureOption" in options:
102 rxOptions |= QRegularExpression.PatternOption.DontCaptureOption 90 rxOptions |= QRegularExpression.PatternOption.DontCaptureOption
103 91
104 matched = False 92 matched = False
105 captures = [] 93 captures = []
106 re = QRegularExpression(regexp, rxOptions) 94 re = QRegularExpression(regexp, rxOptions)
107 match = re.match(text, startpos) 95 match = re.match(text, startpos)
108 if match.hasMatch(): 96 if match.hasMatch():
109 matched = True 97 matched = True
110 for index in range(match.lastCapturedIndex() + 1): 98 for index in range(match.lastCapturedIndex() + 1):
111 captures.append([ 99 captures.append(
112 match.captured(index), 100 [
113 match.capturedStart(index), 101 match.captured(index),
114 match.capturedEnd(index), 102 match.capturedStart(index),
115 match.capturedLength(index) 103 match.capturedEnd(index),
116 ]) 104 match.capturedLength(index),
117 105 ]
106 )
107
118 return matched, captures 108 return matched, captures
119 109
120 110
121 def main(): 111 def main():
122 """ 112 """
131 command = commandDict["command"] 121 command = commandDict["command"]
132 if command == "exit": 122 if command == "exit":
133 break 123 break
134 elif command == "available": 124 elif command == "available":
135 try: 125 try:
136 import PyQt6 # __IGNORE_WARNING__ 126 import PyQt6 # __IGNORE_WARNING__
127
137 responseDict["available"] = True 128 responseDict["available"] = True
138 except ImportError: 129 except ImportError:
139 responseDict["available"] = False 130 responseDict["available"] = False
140 elif command == "validate": 131 elif command == "validate":
141 valid, error, errorOffset = rxValidate( 132 valid, error, errorOffset = rxValidate(
142 commandDict["regexp"], commandDict["options"]) 133 commandDict["regexp"], commandDict["options"]
134 )
143 responseDict["valid"] = valid 135 responseDict["valid"] = valid
144 responseDict["errorMessage"] = error 136 responseDict["errorMessage"] = error
145 responseDict["errorOffset"] = errorOffset 137 responseDict["errorOffset"] = errorOffset
146 elif command == "execute": 138 elif command == "execute":
147 valid, error, errorOffset = rxValidate( 139 valid, error, errorOffset = rxValidate(
148 commandDict["regexp"], commandDict["options"]) 140 commandDict["regexp"], commandDict["options"]
141 )
149 if not valid: 142 if not valid:
150 responseDict["valid"] = valid 143 responseDict["valid"] = valid
151 responseDict["errorMessage"] = error 144 responseDict["errorMessage"] = error
152 responseDict["errorOffset"] = errorOffset 145 responseDict["errorOffset"] = errorOffset
153 else: 146 else:
154 matched, captures = rxExecute( 147 matched, captures = rxExecute(
155 commandDict["regexp"], commandDict["options"], 148 commandDict["regexp"],
156 commandDict["text"], commandDict["startpos"]) 149 commandDict["options"],
150 commandDict["text"],
151 commandDict["startpos"],
152 )
157 responseDict["matched"] = matched 153 responseDict["matched"] = matched
158 responseDict["captures"] = captures 154 responseDict["captures"] = captures
159 except ValueError as err: 155 except ValueError as err:
160 responseDict = {"error": str(err)} 156 responseDict = {"error": str(err)}
161 except Exception as err: 157 except Exception as err:
162 responseDict = {"error": str(err)} 158 responseDict = {"error": str(err)}
163 responseStr = json.dumps(responseDict) 159 responseStr = json.dumps(responseDict)
164 sys.stdout.write(responseStr) 160 sys.stdout.write(responseStr)
165 sys.stdout.flush() 161 sys.stdout.flush()
166 162
167 sys.exit(0) 163 sys.exit(0)
168 164
169 165
170 if __name__ == "__main__": 166 if __name__ == "__main__":
171 main() 167 main()

eric ide

mercurial