Plugins/WizardPlugins/QRegularExpressionWizard/QRegularExpressionWizardServer.py

changeset 2748
3731148a7cdf
parent 2747
68b920f307ff
child 2791
a9577f248f04
child 3005
3953ddfb991d
equal deleted inserted replaced
2747:68b920f307ff 2748:3731148a7cdf
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 2
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 5
6 """
7 Module implementing the PyQt5 server part of the QRegularExpression wizzard.
8 """
9
6 import json 10 import json
7 import sys 11 import sys
8 12
9 def printerr(string):
10 sys.stderr.write(string)
11 sys.stderr.flush()
12 13
13 def rxValidate(regexp, options): 14 def rxValidate(regexp, options):
14 """ 15 """
15 Function to validate the given regular expression. 16 Function to validate the given regular expression.
16 17
50 errorOffset = 0 51 errorOffset = 0
51 52
52 return valid, error, errorOffset 53 return valid, error, errorOffset
53 54
54 55
55 if __name__ == "__main__": 56 def rxExecute(regexp, options, text, startpos):
57 """
58 Function to execute the given regular expression for a given text.
59
60 @param regexp regular expression to validate (string)
61 @param options list of options (list of string)
62 @param text text to execute on (string)
63 @param startpos start position for the execution (integer)
64 @return tuple of a flag indicating a successful match (boolean) and
65 a list of captures containing the complete match as matched string
66 (string), match start (integer), match end (integer) and match length
67 (integer) for each entry
68 """
69 valid, error, errorOffset = rxValidate(regexp, options)
70 if not valid:
71 return valid, error, errorOffset
72
73 from PyQt5.QtCore import QRegularExpression
74 rxOptions = QRegularExpression.NoPatternOption
75 if "CaseInsensitiveOption" in options:
76 rxOptions |= QRegularExpression.CaseInsensitiveOption
77 if "MultilineOption" in options:
78 rxOptions |= QRegularExpression.MultilineOption
79 if "DotMatchesEverythingOption" in options:
80 rxOptions |= QRegularExpression.DotMatchesEverythingOption
81 if "ExtendedPatternSyntaxOption" in options:
82 rxOptions |= QRegularExpression.ExtendedPatternSyntaxOption
83 if "InvertedGreedinessOption" in options:
84 rxOptions |= QRegularExpression.InvertedGreedinessOption
85 if "UseUnicodePropertiesOption" in options:
86 rxOptions |= QRegularExpression.UseUnicodePropertiesOption
87 if "DontCaptureOption" in options:
88 rxOptions |= QRegularExpression.DontCaptureOption
89
90 matched = False
91 captures = []
92 re = QRegularExpression(regexp, rxOptions)
93 match = re.match(text, startpos)
94 if match.hasMatch():
95 matched = True
96 for index in range(match.lastCapturedIndex() + 1):
97 captures.append([
98 match.captured(index),
99 match.capturedStart(index),
100 match.capturedEnd(index),
101 match.capturedLength(index)
102 ])
103
104 return matched, captures
105
106
107 def main():
108 """
109 Function containing the main routine.
110 """
56 while True: 111 while True:
57 commandStr = sys.stdin.readline() 112 commandStr = sys.stdin.readline()
58 try: 113 try:
59 commandDict = json.loads(commandStr) 114 commandDict = json.loads(commandStr)
60 responseDict = {"error": ""} 115 responseDict = {"error": ""}
61 printerr(str(commandDict))
62 if "command" in commandDict: 116 if "command" in commandDict:
63 command = commandDict["command"] 117 command = commandDict["command"]
64 if command == "exit": 118 if command == "exit":
65 break 119 break
66 elif command == "available": 120 elif command == "available":
73 valid, error, errorOffset = rxValidate(commandDict["regexp"], 127 valid, error, errorOffset = rxValidate(commandDict["regexp"],
74 commandDict["options"]) 128 commandDict["options"])
75 responseDict["valid"] = valid 129 responseDict["valid"] = valid
76 responseDict["errorMessage"] = error 130 responseDict["errorMessage"] = error
77 responseDict["errorOffset"] = errorOffset 131 responseDict["errorOffset"] = errorOffset
132 elif command == "execute":
133 valid, error, errorOffset = rxValidate(commandDict["regexp"],
134 commandDict["options"])
135 if not valid:
136 responseDict["valid"] = valid
137 responseDict["errorMessage"] = error
138 responseDict["errorOffset"] = errorOffset
139 else:
140 matched, captures = rxExecute(commandDict["regexp"],
141 commandDict["options"], commandDict["text"],
142 commandDict["startpos"])
143 responseDict["matched"] = matched
144 responseDict["captures"] = captures
78 except ValueError as err: 145 except ValueError as err:
79 responseDict = {"error": str(err)} 146 responseDict = {"error": str(err)}
80 except Exception as err: 147 except Exception as err:
81 responseDict = {"error": str(err)} 148 responseDict = {"error": str(err)}
82 responseStr = json.dumps(responseDict) 149 responseStr = json.dumps(responseDict)
83 sys.stdout.write(responseStr) 150 sys.stdout.write(responseStr)
84 sys.stdout.flush() 151 sys.stdout.flush()
85 152
86 sys.exit(0) 153 sys.exit(0)
154
155
156 if __name__ == "__main__":
157 main()

eric ide

mercurial