[gen] More compact code.

This commit is contained in:
Gaetan Delannay 2014-03-05 17:04:43 +01:00
parent f629f2b323
commit 90af2e5698
2 changed files with 9 additions and 11 deletions

View file

@ -586,21 +586,14 @@ class String(Field):
break break
if error: return obj.translate('bad_select_value') if error: return obj.translate('bad_select_value')
accents = {'é':'e','è':'e','ê':'e','ë':'e','à':'a','â':'a','ä':'a',
'ù':'u','û':'u','ü':'u','î':'i','ï':'i','ô':'o','ö':'o',
'ç':'c', 'Ç':'C',
'Ù':'U','Û':'U','Ü':'U','Î':'I','Ï':'I','Ô':'O','Ö':'O',
'É':'E','È':'E','Ê':'E','Ë':'E','À':'A','Â':'A','Ä':'A'}
def applyTransform(self, value): def applyTransform(self, value):
'''Applies a transform as required by self.transform on single '''Applies a transform as required by self.transform on single
value p_value.''' value p_value.'''
if self.transform in ('uppercase', 'lowercase'): if self.transform in ('uppercase', 'lowercase'):
# For those transforms, I will remove any accent, because # For those transforms, I will remove any accent, because, most of
# (1) 'é'.upper() or 'Ê'.lower() has no effect; # the time, if the user wants to apply such effect, it is for ease
# (2) most of the time, if the user wants to apply such effect, it # of data manipulation, so I guess without accent.
# is for ease of data manipulation, so I guess without accent. value = sutils.normalizeString(value, usage='noAccents')
for c, n in self.accents.iteritems():
if c in value: value = value.replace(c, n)
# Apply the transform # Apply the transform
if self.transform == 'lowercase': return value.lower() if self.transform == 'lowercase': return value.lower()
elif self.transform == 'uppercase': return value.upper() elif self.transform == 'uppercase': return value.upper()

View file

@ -216,6 +216,7 @@ def normalizeString(s, usage='fileName'):
* alphanum: it removes any non-alphanumeric char; * alphanum: it removes any non-alphanumeric char;
* alpha: it removes any non-letter char. * alpha: it removes any non-letter char.
''' '''
strNeeded = isinstance(s, str)
# We work in unicode. Convert p_s to unicode if not unicode. # We work in unicode. Convert p_s to unicode if not unicode.
if isinstance(s, str): s = s.decode('utf-8') if isinstance(s, str): s = s.decode('utf-8')
elif not isinstance(s, unicode): s = unicode(s) elif not isinstance(s, unicode): s = unicode(s)
@ -236,8 +237,12 @@ def normalizeString(s, usage='fileName'):
res = '' res = ''
for char in s: for char in s:
if rex.match(char): res += char if rex.match(char): res += char
elif usage == 'noAccents':
res = s
else: else:
res = s res = s
# Re-code the result as a str if a str was given.
if strNeeded: res = res.encode('utf-8')
return res return res
def normalizeText(s): def normalizeText(s):