Debian Codegraph @

clevercsv/_regexes.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 from typing import Dict
4 from typing import List
5 from typing import Pattern
6
7 import regex
8
9 ##########################################
10 # Regular expressions for number formats #
11 ##########################################
12
13 PATTERN_NUMBER_1: Pattern[str] = regex.compile(
14 r"^(?=[+-\.\d])"
15 r"[+-]?"
16 r"(?:0|[1-9]\d*)?"
17 r"("
18 r"("
19 r"(?P<dot>((?<=\d)\.|\.(?=\d)))?"
20 r"("
21 r"?(dot)(?P<yes_dot>\d*(\d*[eE][+-]?\d+)?)"
22 r"|"
23 r"(?P<no_dot>((?<=\d)[eE][+-]?\d+)?)"
24 r")"
25 r")"
26 r"|"
27 r"("
28 r"(?P<comma>,)?"
29 r"(?(comma)(?P<yes_comma>\d+(\d+[eE][+-]?\d+)?)"
30 r"|"
31 r"(?P<no_comma>((?<=\d)[eE][+-]?\d+)?)"
32 r")"
33 r")"
34 r")"
35 r"$"
36 )
37
38 PATTERN_NUMBER_2: Pattern[str] = regex.compile(
39 r"[+-]?(?:[1-9]|[1-9]\d{0,2})(?:\,\d{3})+\.\d*"
40 )
41
42 PATTERN_NUMBER_3: Pattern[str] = regex.compile(
43 r"[+-]?(?:[1-9]|[1-9]\d{0,2})(?:\.\d{3})+\,\d*"
44 )
45
46 ##############################################
47 # Regular expressions for url, email, and ip #
48 ##############################################
49
50 PATTERN_URL: Pattern[str] = regex.compile(
51 r"("
52 r"(https?|ftp):\/\/(?!\-)"
53 r")?"
54 r"("
55 r"((?:[\p{L}\p{N}-]+\.)+([a-z]{2,}|local)(\.[a-z]{2,3})?)"
56 r"|"
57 r"localhost(\:\d{1,5})?"
58 r"|"
59 r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\:\d{1,5})?)"
60 r")"
61 r"(\/[\p{L}\p{N}_\/()~?=&%\-\#\.:]*)?"
62 r"(\.[a-z]+)?"
63 )
64
65 PATTERN_EMAIL: Pattern[str] = regex.compile(
66 r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
67 )
68
69 PATTERN_IPV4: Pattern[str] = regex.compile(r"(?:\d{1,3}\.){3}\d{1,3}")
70
71 #################################################
72 # Regular expressions related to time notations #
73 #################################################
74
75 PATTERN_TIME_HHMMSSZZ: Pattern[str] = regex.compile(
76 r"(0[0-9]|1[0-9]|2[0-3])"
77 r":"
78 r"([0-5][0-9])"
79 r":"
80 r"([0-5][0-9])"
81 r"[+-]"
82 r"([0-1][0-9])"
83 r":"
84 r"([0-5][0-9])"
85 )
86
87 PATTERN_TIME_HHMMSS: Pattern[str] = regex.compile(
88 r"(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"
89 )
90
91 PATTERN_TIME_HHMM_1: Pattern[str] = regex.compile(
92 r"(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])"
93 )
94
95 PATTERN_TIME_HHMM_2: Pattern[str] = regex.compile(
96 r"(0[0-9]|1[0-9]|2[0-3])([0-5][0-9])"
97 )
98
99 PATTERN_TIME_HH: Pattern[str] = regex.compile(
100 r"(0[0-9]|1[0-9]|2[0-3])([0-5][0-9])"
101 )
102
103 PATTERN_TIME_HMM: Pattern[str] = regex.compile(
104 r"([0-9]|1[0-9]|2[0-3]):([0-5][0-9])"
105 )
106
107 ###############################
108 # Regular expression for date #
109 ###############################
110
111 # Regex for various date formats. See
112 # https://github.com/alan-turing-institute/CleverCSV/blob/master/notes/date_regex/dateregex_annotated.txt
113 # for an explanation.
114 PATTERN_DATE: Pattern[str] = regex.compile(
115 r"("
116 r"(0[1-9]|1[0-2])"
117 r"("
118 r"(0[1-9]|[12]\d|3[01])"
119 r"([12]\d{3}|\d{2})"
120 r"|"
121 r"(?P<sep1>[-\/. ])"
122 r"(0?[1-9]|[12]\d|3[01])"
123 r"(?P=sep1)"
124 r"([12]\d{3}|\d{2})"
125 r")"
126 r"|"
127 r"(0[1-9]|[12]\d|3[01])"
128 r"("
129 r"(0[1-9]|1[0-2])"
130 r"([12]\d{3}|\d{2})"
131 r"|"
132 r"(?P<sep2>[-\/. ])"
133 r"(0?[1-9]|1[0-2])"
134 r"(?P=sep2)"
135 r"([12]\d{3}|\d{2})"
136 r")"
137 r"|"
138 r"([12]\d{3}|\d{2})"
139 r"("
140 r"(?P<sep3>[-\/. ])"
141 r"(0?[1-9]|1[0-2])"
142 r"(?P=sep3)"
143 r"(0?[1-9]|[12]\d|3[01])"
144 r"|"
145 r"年(0?[1-9]|1[0-2])月(0?[1-9]|[12]\d|3[01])日"
146 r"|"
147 r"년(0?[1-9]|1[0-2])월(0?[1-9]|[12]\d|3[01])일"
148 r"|"
149 r"(0[1-9]|1[0-2])"
150 r"(0[1-9]|[12]\d|3[01])"
151 r")"
152 r"|"
153 r"("
154 r"([1-9]|1[0-2])"
155 r"(?P<sep4>[-\/. ])"
156 r"(0?[1-9]|[12]\d|3[01])"
157 r"(?P=sep4)([12]\d{3}|\d{2})"
158 r"|"
159 r"([1-9]|[12]\d|3[01])"
160 r"(?P<sep5>[-\/. ])"
161 r"(0?[1-9]|1[0-2])"
162 r"(?P=sep5)([12]\d{3}|\d{2})"
163 r")"
164 r")"
165 )
166
167 #############################################
168 # Regular expressions for alphanumeric text #
169 #############################################
170
171 # Used this site: https://unicode-search.net/unicode-namesearch.pl
172 # Specials allowed in unicode_alphanum regex if is_quoted = False
173 SPECIALS_ALLOWED: List[str] = [
174 "-",
175 "_",
176 # Periods
177 "\u002e",
178 "\u06d4",
179 "\u3002",
180 "\ufe52",
181 "\uff0e",
182 "\uff61",
183 # Parentheses
184 "\u0028",
185 "\u0029",
186 "\u27ee",
187 "\u27ef",
188 "\uff08",
189 "\uff09",
190 # Question marks
191 "\u003F",
192 "\u00BF",
193 "\u037E",
194 "\u055E",
195 "\u061F",
196 "\u1367",
197 "\u1945",
198 "\u2047",
199 "\u2048",
200 "\u2049",
201 "\u2CFA",
202 "\u2CFB",
203 "\u2E2E",
204 "\uA60F",
205 "\uA6F7",
206 "\uFE16",
207 "\uFE56",
208 "\uFF1F",
209 chr(69955), # chakma question mark
210 chr(125279), # adlam initial question mark
211 # Exclamation marks
212 "\u0021",
213 "\u00A1",
214 "\u01C3",
215 "\u055C",
216 "\u07F9",
217 "\u109F",
218 "\u1944",
219 "\u203C",
220 "\u2048",
221 "\u2049",
222 "\uAA77",
223 "\uFE15",
224 "\uFE57",
225 "\uFF01",
226 chr(125278), # adlam initial exclamation mark
227 ]
228
229 # Additional specials allowed in unicode_alphanum_quoted regex
230 QUOTED_SPECIALS_ALLOWED: List[str] = [
231 ",",
232 "\u060C",
233 "\u1363",
234 "\u1802",
235 "\u1808",
236 "\uFF0C",
237 "\uFE50",
238 ]
239
240 ALPHANUM_SPECIALS: str = regex.escape(r"".join(SPECIALS_ALLOWED))
241
242 # Regex for alphanumeric text
243 PATTERN_ALPHANUM: Pattern[str] = regex.compile(
244 r"("
245 r"\p{N}?\p{L}+"
246 r"["
247 r"\p{N}\p{L}\ " + ALPHANUM_SPECIALS + r"]*"
248 r"|"
249 r"\p{L}?"
250 r"["
251 r"\p{N}\p{L}\ " + ALPHANUM_SPECIALS + r"]+"
252 r")"
253 )
254
255 ALPANUM_QUOTED_SPECIALS: str = regex.escape(
256 r"".join(SPECIALS_ALLOWED) + r"".join(QUOTED_SPECIALS_ALLOWED)
257 )
258 # Regex for alphanumeric text in quoted strings
259 PATTERN_ALPHANUM_QUOTED: Pattern[str] = regex.compile(
260 r"("
261 r"\p{N}?\p{L}+"
262 r"["
263 r"\p{N}\p{L}\ " + ALPANUM_QUOTED_SPECIALS + r"]*"
264 r"|"
265 r"\p{L}?"
266 r"["
267 r"\p{N}\p{L}\ " + ALPANUM_QUOTED_SPECIALS + r"]+"
268 r")"
269 )
270
271 ###################################
272 # Regular expression for currency #
273 ###################################
274
275 PATTERN_CURRENCY: Pattern[str] = regex.compile(r"\p{Sc}\s?(.*)")
276
277 #####################################
278 # Regular expression for unix paths #
279 #####################################
280
281 PATTERN_UNIX_PATH: Pattern[str] = regex.compile(
282 r"[~.]?(?:\/[a-zA-Z0-9\.\-\_]+)+\/?"
283 )
284
285 ################################################
286 # Map of regular expresions for type detection #
287 ################################################
288
289 DEFAULT_TYPE_REGEXES: Dict[str, Pattern[str]] = {
290 "number_1": PATTERN_NUMBER_1,
291 "number_2": PATTERN_NUMBER_2,
292 "number_3": PATTERN_NUMBER_3,
293 "url": PATTERN_URL,
294 "email": PATTERN_EMAIL,
295 "ipv4": PATTERN_IPV4,
296 "unicode_alphanum": PATTERN_ALPHANUM,
297 "unicode_alphanum_quoted": PATTERN_ALPHANUM_QUOTED,
298 "time_hhmmss": PATTERN_TIME_HHMMSS,
299 "time_hhmm": PATTERN_TIME_HHMM_1,
300 "time_HHMM": PATTERN_TIME_HHMM_2,
301 "time_HH": PATTERN_TIME_HH,
302 "time_hmm": PATTERN_TIME_HMM,
303 "time_hhmmsszz": PATTERN_TIME_HHMMSSZZ,
304 "currency": PATTERN_CURRENCY,
305 "unix_path": PATTERN_UNIX_PATH,
306 "date": PATTERN_DATE,
307 }