forked from osm-fr/osmose-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Name_UpperCase.py
90 lines (77 loc) · 3.84 KB
/
Name_UpperCase.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
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Frédéric Rodrigo 2016 ##
## ##
## 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
import regex as re
# Whitelist of allowed capitals by country code
UpperCase_WhiteList = {
"FR": ["CNFPT", "COSEC", "EHPAD", "MACIF", "MEDEF", "URSSAF"]
}
class Name_UpperCase(Plugin):
not_for = ["CU"]
def init(self, logger):
Plugin.init(self, logger)
self.errors[803] = self.def_class(item = 5010, level = 1, tags = ['name', 'fix:chair'],
title = T_('Name with uppercase'),
detail = T_(
'''Word in capital letters.'''))
self.UpperTitleCase = re.compile(u".*[\p{Lu}\p{Lt}]{5,}")
self.RomanNumber = re.compile(u".*[IVXCDLM]{5,}")
if "country" in self.father.config.options:
country = self.father.config.options.get("country")[:2]
self.whitelist = UpperCase_WhiteList.get(country, None)
else:
self.whitelist = None
def node(self, data, tags):
err = []
if u"name" in tags:
# first check if the name *might* match
if self.UpperTitleCase.match(tags[u"name"]) and not self.RomanNumber.match(tags[u"name"]):
if self.whitelist is None:
err.append({"class": 803, "text":{"en":tags[u"name"]}})
else:
# Check if we match the whitelist and if so re-try
name = " ".join(i for i in tags[u"name"].split() if not i in self.whitelist)
if self.UpperTitleCase.match(name) and not self.RomanNumber.match(name):
err.append({"class": 803, "text":{"en":tags[u"name"]}})
return err
def way(self, data, tags, nds):
return self.node(data, tags)
###########################################################################
from plugins.Plugin import TestPluginCommon
class Test(TestPluginCommon):
def test(self):
a = Name_UpperCase(None)
class _config:
options = {"country": "FR"}
class father:
config = _config()
a.father = father()
a.init(None)
for t in [{u"name": u"COL TRÈS HAUTTT"},
{u"name": u"EHPAD MAGEUSCULE"},
{u"name": u"AÇDZÞΣSSὩΙST"},
]:
self.check_err(a.node(None, t), t)
self.check_err(a.way(None, t, None), t)
for t in [{u"name": u"Col des Champs XIIVVVIM"},
{u"name": u"EHPAD La Madelon"},
{u"name": u"ƻאᎯᚦ京"},
]:
assert not a.node(None, t), t