]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/stxview/StructuredText/ts_regex.py
implemented dummy wxPalette for wxGTK, so that it no longer pops up assertion failure...
[wxWidgets.git] / wxPython / samples / stxview / StructuredText / ts_regex.py
1 ##############################################################################
2 #
3 # Zope Public License (ZPL) Version 1.0
4 # -------------------------------------
5 #
6 # Copyright (c) Digital Creations. All rights reserved.
7 #
8 # This license has been certified as Open Source(tm).
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are
12 # met:
13 #
14 # 1. Redistributions in source code must retain the above copyright
15 # notice, this list of conditions, and the following disclaimer.
16 #
17 # 2. Redistributions in binary form must reproduce the above copyright
18 # notice, this list of conditions, and the following disclaimer in
19 # the documentation and/or other materials provided with the
20 # distribution.
21 #
22 # 3. Digital Creations requests that attribution be given to Zope
23 # in any manner possible. Zope includes a "Powered by Zope"
24 # button that is installed by default. While it is not a license
25 # violation to remove this button, it is requested that the
26 # attribution remain. A significant investment has been put
27 # into Zope, and this effort will continue if the Zope community
28 # continues to grow. This is one way to assure that growth.
29 #
30 # 4. All advertising materials and documentation mentioning
31 # features derived from or use of this software must display
32 # the following acknowledgement:
33 #
34 # "This product includes software developed by Digital Creations
35 # for use in the Z Object Publishing Environment
36 # (http://www.zope.org/)."
37 #
38 # In the event that the product being advertised includes an
39 # intact Zope distribution (with copyright and license included)
40 # then this clause is waived.
41 #
42 # 5. Names associated with Zope or Digital Creations must not be used to
43 # endorse or promote products derived from this software without
44 # prior written permission from Digital Creations.
45 #
46 # 6. Modified redistributions of any form whatsoever must retain
47 # the following acknowledgment:
48 #
49 # "This product includes software developed by Digital Creations
50 # for use in the Z Object Publishing Environment
51 # (http://www.zope.org/)."
52 #
53 # Intact (re-)distributions of any official Zope release do not
54 # require an external acknowledgement.
55 #
56 # 7. Modifications are encouraged but must be packaged separately as
57 # patches to official Zope releases. Distributions that do not
58 # clearly separate the patches from the original work must be clearly
59 # labeled as unofficial distributions. Modifications which do not
60 # carry the name Zope may be packaged in any form, as long as they
61 # conform to all of the clauses above.
62 #
63 #
64 # Disclaimer
65 #
66 # THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
67 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
69 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
70 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
71 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
72 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
73 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
74 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
75 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
76 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 # SUCH DAMAGE.
78 #
79 #
80 # This software consists of contributions made by Digital Creations and
81 # many individuals on behalf of Digital Creations. Specific
82 # attributions are listed in the accompanying credits file.
83 #
84 ##############################################################################
85 """Provide a thread-safe interface to regex
86 """
87 import regex, regsub #, Sync
88 from regex import *
89 from regsub import split, sub, gsub, splitx, capwords
90
91 try:
92 import thread
93 except:
94 class allocate_lock:
95 def acquire(*args): pass
96 def release(*args): pass
97
98 else:
99 class SafeFunction:
100 _l=thread.allocate_lock()
101 _a=_l.acquire
102 _r=_l.release
103
104 def __init__(self, f):
105 self._f=f
106
107 def __call__(self, *args, **kw):
108 self._a()
109 try: return apply(self._f, args, kw)
110 finally: self._r()
111
112 split=SafeFunction(split)
113 sub=SafeFunction(sub)
114 gsub=SafeFunction(gsub)
115 splitx=SafeFunction(splitx)
116 capwords=SafeFunction(capwords)
117
118 allocate_lock=thread.allocate_lock
119
120 class compile:
121
122 _r=None
123 groupindex=None
124
125 def __init__(self, *args):
126 self._r=r=apply(regex.compile,args)
127 self._init(r)
128
129 def _init(self, r):
130 lock=allocate_lock()
131 self.__a=lock.acquire
132 self.__r=lock.release
133 self.translate=r.translate
134 self.givenpat=r.givenpat
135 self.realpat=r.realpat
136
137 def match(self, string, pos=0):
138 self.__a()
139 try: return self._r.match(string, pos)
140 finally: self.__r()
141
142 def search(self, string, pos=0):
143 self.__a()
144 try: return self._r.search(string, pos)
145 finally: self.__r()
146
147 def search_group(self, str, group, pos=0):
148 """Search a string for a pattern.
149
150 If the pattern was not found, then None is returned,
151 otherwise, the location where the pattern was found,
152 as well as any specified group are returned.
153 """
154 self.__a()
155 try:
156 r=self._r
157 l=r.search(str, pos)
158 if l < 0: return None
159 return l, apply(r.group, group)
160 finally: self.__r()
161
162 def match_group(self, str, group, pos=0):
163 """Match a pattern against a string
164
165 If the string does not match the pattern, then None is
166 returned, otherwise, the length of the match, as well
167 as any specified group are returned.
168 """
169 self.__a()
170 try:
171 r=self._r
172 l=r.match(str, pos)
173 if l < 0: return None
174 return l, apply(r.group, group)
175 finally: self.__r()
176
177 def search_regs(self, str, pos=0):
178 """Search a string for a pattern.
179
180 If the pattern was not found, then None is returned,
181 otherwise, the 'regs' attribute of the expression is
182 returned.
183 """
184 self.__a()
185 try:
186 r=self._r
187 r.search(str, pos)
188 return r.regs
189 finally: self.__r()
190
191 def match_regs(self, str, pos=0):
192 """Match a pattern against a string
193
194 If the string does not match the pattern, then None is
195 returned, otherwise, the 'regs' attribute of the expression is
196 returned.
197 """
198 self.__a()
199 try:
200 r=self._r
201 r.match(str, pos)
202 return r.regs
203 finally: self.__r()
204
205 class symcomp(compile):
206
207 def __init__(self, *args):
208 self._r=r=apply(regex.symcomp,args)
209 self._init(r)
210 self.groupindex=r.groupindex
211
212
213
214
215