forked from osm-fr/osmose-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhone.py
285 lines (241 loc) · 13 KB
/
Phone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Francois Gouget fgouget free.fr 2017 ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###########################################################################
from plugins.Plugin import Plugin
from modules.Stablehash import stablehash64
import re
class Phone(Plugin):
PHONE_TAGS = set((u"contact:fax", u"contact:phone", u"fax", u"phone"))
def init(self, logger):
Plugin.init(self, logger)
self.code = self.father.config.options.get("phone_code")
if not self.code:
return False
self.size = self.father.config.options.get("phone_len")
if self.size and not isinstance(self.size, list):
self.size = [self.size]
self.size_short = self.father.config.options.get("phone_len_short")
if self.size_short and not isinstance(self.size_short, list):
self.size_short = [self.size_short]
self.format = self.father.config.options.get("phone_format")
self.international_prefix = self.father.config.options.get("phone_international")
self.local_prefix = self.father.config.options.get("phone_local_prefix")
self.values_separators = self.father.config.options.get("phone_values_separators", [' / ', ' - ', ','])
self.suffix_separators = self.father.config.options.get("suffix_separators")
if self.format:
self.errors[30920] = self.def_class(item = 3092, level = 2, tags = ['value', 'fix:chair'],
title = T_('Phone number does not match the expected format'))
if self.local_prefix:
self.errors[30921] = self.def_class(item = 3092, level = 2, tags = ['value', 'fix:chair'],
title = T_f('Extra "{0}" after international code', self.local_prefix))
if self.size_short:
self.errors[30922] = self.def_class(item = 3092, level = 2, tags = ['value', 'fix:chair'],
title = T_f('Local short code can not be internationalized'))
self.errors[30923] = self.def_class(item = 3092, level = 3, tags = ['value', 'fix:chair'],
title = T_('Missing international prefix'))
self.errors[30924] = self.def_class(item = 3092, level = 3, tags = ['value', 'fix:chair'],
title = T_('Bad international prefix'))
self.errors[30925] = self.def_class(item = 3092, level = 3, tags = ['value', 'fix:chair'],
title = T_('Unallowed char in phone number'))
self.errors[30926] = self.def_class(item = 3092, level = 3, tags = ['value', 'fix:chair'],
title = T_('Bad separator for multiple values'))
country = self.father.config.options.get("country")
if self.code and self.local_prefix:
# Regular numbers must not have a local_prefix (aka "0") after +[code]
self.InternationalAndLocalPrefix = re.compile(r"^[+]%s[- ./]*%s((?:[- ./]*[0-9])+)$" % (self.code, self.local_prefix))
else:
self.InternationalAndLocalPrefix = None
if self.size_short:
# Short numbers cannot be internationalized
self.BadShort = re.compile(r"^[+]%s[- ./]*([0-9]{%s,%s})$" % (self.code, min(self.size_short), max(self.size_short)))
else:
self.BadShort = None
if self.international_prefix:
self.InternationalPrefix = re.compile(r"^%s(.*)" % self.international_prefix)
else:
self.InternationalPrefix = None
if self.local_prefix:
if country and country.startswith("FR"):
# Local numbers to internationalize. Note that in addition to
# short numbers this also skips special numbers starting with 08
# or 09 since these may or may not be callable from abroad.
self.MissingInternationalPrefix = re.compile(r"^%s[- ./]*([1-7](:?[- ./]*[0-9]){%s,%s})$" % (self.local_prefix, min(self.size) - 1, max(self.size) - 1))
elif self.size:
self.MissingInternationalPrefix = re.compile(r"^%s[- ./]*((:?[0-9][- ./]*){%s,%s}[0-9])$" % (self.local_prefix, min(self.size) - len(self.local_prefix) - 1, max(self.size) - len(self.local_prefix) - 1))
else:
self.MissingInternationalPrefix = re.compile(r"^%s[- ./]*((:?[0-9][- ./]*)+[0-9])$" % (self.local_prefix))
else:
self.MissingInternationalPrefix = re.compile(r"^((:?[0-9][- ./]*){%s,%s}[0-9])$" % (min(self.size) - 1, max(self.size) - 1))
if self.format:
self.Format = re.compile(self.format % self.code)
else:
self.Format = None
def check(self, tags):
err = []
for tag in self.PHONE_TAGS:
if tag not in tags:
continue
phone = tags[tag]
if u';' in phone:
continue # Ignore multiple phone numbers
if self.suffix_separators is not None:
phone = phone.split(self.suffix_separators, 1)[0]
if self.values_separators:
p = phone
for sep in self.values_separators:
if sep in phone:
phone = phone.replace(sep, '; ')
if p != phone:
phone = phone.replace(' ', ' ')
err.append({"class": 30926, "subclass": stablehash64(tag), "text": {'en': u'='.join([tag, phone])}, "fix": {tag: phone.replace(' / ', '; ').replace(' - ', '; ').replace(',', ';')}})
continue
phone_test = phone
for c in '+0123456789 -./()':
phone_test = phone_test.replace(c, '')
if len(phone_test) > 0:
err.append({"class": 30925, "subclass": stablehash64(tag), "text": T_f(u"Not allowed char \"{0}\" in phone number tag \"{1}\"", phone_test, tag)})
continue
# Before local prefix
if self.InternationalPrefix:
r = self.InternationalPrefix.match(phone)
if r:
err.append({"class": 30924, "subclass": stablehash64(tag), "text": {'en': u'='.join([tag, phone])}, "fix": {tag: "+" + r.group(1)}})
continue
if self.InternationalAndLocalPrefix:
r = self.InternationalAndLocalPrefix.match(phone)
if r:
err.append({"class": 30921, "subclass": stablehash64(tag), "text": {'en': u'='.join([tag, phone])}, "fix": {tag: "+" + self.code + " " + r.group(1)}})
continue
if self.MissingInternationalPrefix:
r = self.MissingInternationalPrefix.match(phone)
if r:
err.append({"class": 30923, "subclass": stablehash64(tag), "text": {'en': u'='.join([tag, phone])}, "fix": {tag: "+" + self.code + " " + r.group(1)}})
continue
if self.BadShort:
r = self.BadShort.match(phone)
if r:
err.append({"class": 30922, "subclass": stablehash64(tag), "text": {'en': u'='.join([tag, phone])}, "fix": {tag: r.group(1)}})
continue
# Last
if self.Format:
r = self.Format.match(phone)
if not r:
err.append({"class": 30920, "subclass": stablehash64(tag), "text": {'en': u'='.join([tag, phone])}})
continue
return err
def node(self, _data, tags):
return self.check(tags)
def way(self, _data, tags, _nds):
return self.check(tags)
def relation(self, _data, tags, _members):
return self.check(tags)
###########################################################################
from plugins.Plugin import TestPluginCommon
class Test(TestPluginCommon):
def test_FR(self):
p = Phone(None)
class _config:
options = {"country": "FR", "phone_code": "33", "phone_len": 9, "phone_len_short": [4, 6], "phone_format": r"^([+]%s([- ./]*[0-9]){8}[0-9])|[0-9]{4}|[0-9]{6}$", "phone_international": "00", "phone_local_prefix": "0"}
class father:
config = _config()
p.father = father()
p.init(None)
for (bad, good) in (
(u"+330102030405", u"+33 102030405"),
(u"0033 102030405", u"+33 102030405"),
(u"12 / 13", u"12; 13"),
# Preserve formatting
(u"+33 0102030405", u"+33 102030405"),
(u"+33 01 02 03 04 05", u"+33 1 02 03 04 05"),
(u"+33 3631", u"3631"),
(u"0102030405", u"+33 102030405"),
(u"01 02 03 04 05", u"+33 1 02 03 04 05"),
):
# Check the bad number's error and fix
err = p.node(None, {"phone": bad})
self.check_err(err, ("phone='%s'" % bad))
self.assertEqual(err[0]["fix"]["phone"], good)
# The correct number does not need fixing
assert not p.node(None, {"phone": good}), ("phone='%s'" % good)
# Verify we got no error for other correct numbers
for good in (u"3631", u"118987", u"1;2"):
assert not p.node(None, {"phone": good}), ("phone='%s'" % good)
assert len(p.node(None, {"phone": "09.72.42.42.42", "fax": "09.72.42.42.42"})) == 2
def test_NC(self):
p = Phone(None)
class _config:
options = {"country": "NC", "phone_code": "687", "phone_len": 6, "phone_format": r"^[+]%s([- ./]*[0-9]){5}[0-9]$", "phone_international": "00"}
class father:
config = _config()
p.father = father()
p.init(None)
for (bad, good) in (
(u"43 43 43", u"+687 43 43 43"),
(u"434343", u"+687 434343"),
(u"00687297969", u"+687297969"),
):
# Check the bad number's error and fix
err = p.node(None, {"phone": bad})
self.check_err(err, ("phone='%s'" % bad))
self.assertEqual(err[0]["fix"]["phone"], good)
# The correct number does not need fixing
assert not p.node(None, {"phone": good}), ("phone='%s'" % good)
# Verify we got error for other correct numbers
for bad in (u"3631"):
assert p.node(None, {"phone": bad}), ("phone='%s'" % bad)
def test_CA(self):
p = Phone(None)
class _config:
options = {"country": "CA", "phone_code": "1", "phone_len": 10, "phone_format": r"^[+]%s[- ][0-9]{3}[- ][0-9]{3}[- ][0-9]{4}$"}
class father:
config = _config()
p.father = father()
p.init(None)
for (bad, good) in (
(u"800-555-0000", u"+1 800-555-0000"),
):
# Check the bad number's error and fix
err = p.node(None, {"phone": bad})
self.check_err(err, ("phone='%s'" % bad))
self.assertEqual(err[0]["fix"]["phone"], good)
# The correct number does not need fixing
assert not p.node(None, {"phone": good}), ("phone='%s'" % good)
# Verify we got error for other correct numbers
for bad in (u"3631", u"(123) 123-4567", "+1 123 1234567"):
assert p.node(None, {"phone": bad}), ("phone='%s'" % bad)
def test_ES(self):
p = Phone(None)
class _config:
options = {"country": "ES", "phone_code": "34", "phone_len": 9, "phone_len_short": [3, 4, 5], "phone_international": "00"}
class father:
config = _config()
p.father = father()
p.init(None)
for (bad, good) in (
(u"923 555 000", u"+34 923 555 000"),
(u"923 55 50 00", u"+34 923 55 50 00"),
(u"923555000", u"+34 923555000"),
(u"0034923555000", u"+34923555000"),
):
# Check the bad number's error and fix
err = p.node(None, {"phone": bad})
self.check_err(err, ("phone='%s'" % bad))
self.assertEqual(err[0]["fix"]["phone"], good)
# The correct number does not need fixing
assert not p.node(None, {"phone": good}), ("phone='%s'" % good)