103 lines
3.0 KiB
Python
103 lines
3.0 KiB
Python
# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
|
|
__all__ = ('UnicodeF',
|
|
'LowerF', 'UpperF', 'StripF',
|
|
'StrF',
|
|
)
|
|
|
|
import re
|
|
|
|
from ulib.base.uio import _u, _s
|
|
|
|
from base import Format
|
|
|
|
class UnicodeF(Format):
|
|
RE_a = (re.compile(ur'[äàâ]'), u'a'); RE_A = (re.compile(ur'[ÄÀÂ]'), u'A')
|
|
RE_e = (re.compile(ur'[ëéèê]'), u'e'); RE_E = (re.compile(ur'[ËÉÈÊ]'), u'E')
|
|
RE_i = (re.compile(ur'[ïî]'), u'i'); RE_I = (re.compile(ur'[ÏÎ]'), u'I')
|
|
RE_o = (re.compile(ur'[öô]'), u'o'); RE_O = (re.compile(ur'[ÖÔ]'), u'O')
|
|
RE_u = (re.compile(ur'[üûù]'), u'u'); RE_U = (re.compile(ur'[ÜÛÙ]'), u'U')
|
|
RE_y = (re.compile(ur'[ÿŷ]'), u'y'); RE_Y = (re.compile(ur'[ŸŶ]'), u'Y')
|
|
RE_c = (re.compile(ur'[ç]'), u'c'); RE_C = (re.compile(ur'[Ç]'), u'C')
|
|
|
|
def strip_accents(cls, s):
|
|
for re_x, x in (cls.RE_A, cls.RE_a,
|
|
cls.RE_E, cls.RE_e,
|
|
cls.RE_I, cls.RE_i,
|
|
cls.RE_O, cls.RE_o,
|
|
cls.RE_U, cls.RE_u,
|
|
cls.RE_Y, cls.RE_y,
|
|
cls.RE_C, cls.RE_c,
|
|
):
|
|
s = re_x.sub(x, s)
|
|
return s
|
|
strip_accents = classmethod(strip_accents)
|
|
|
|
def parse(self, s):
|
|
u"""Retourner la chaine en unicode, ou None si s==None
|
|
|
|
@return: une chaine ou None
|
|
@rtype: unicode
|
|
"""
|
|
if s is None: return None
|
|
else: return _u(s)
|
|
|
|
def format(self, o):
|
|
u"""Retourner une chaine vide si o==None, ou o dans le cas contraire.
|
|
|
|
@rtype: unicode
|
|
"""
|
|
if o is None: return u""
|
|
else: return o
|
|
|
|
class LowerF(UnicodeF):
|
|
def parse(self, s):
|
|
u"""Retourner la chaine s en minuscule, ou None si s==None
|
|
|
|
@return: une chaine ou None
|
|
@rtype: unicode
|
|
"""
|
|
s = UnicodeF.parse(self, s)
|
|
if s is None: return None
|
|
else: return s.lower()
|
|
|
|
class UpperF(UnicodeF):
|
|
def parse(self, s):
|
|
u"""Retourner la chaine s en majuscule, ou None si s==None
|
|
|
|
@return: une chaine ou None
|
|
@rtype: unicode
|
|
"""
|
|
s = UnicodeF.parse(self, s)
|
|
if s is None: return None
|
|
else: return s.upper()
|
|
|
|
class StripF(UnicodeF):
|
|
def parse(self, s):
|
|
u"""Retourner la chaine s strippée, ou None si s==None
|
|
|
|
@return: une chaine ou None
|
|
@rtype: unicode
|
|
"""
|
|
s = UnicodeF.parse(self, s)
|
|
if s is None: return None
|
|
else: return s.strip()
|
|
|
|
class StrF(Format):
|
|
def parse(self, s):
|
|
u"""Retourner la chaine en str, ou None si s==None
|
|
|
|
@return: une chaine ou None
|
|
@rtype: str
|
|
"""
|
|
if s is None: return None
|
|
else: return _s(s)
|
|
|
|
def format(self, o):
|
|
u"""Retourner une chaine vide si o==None, ou o dans le cas contraire.
|
|
|
|
@rtype: str
|
|
"""
|
|
if o is None: return ""
|
|
else: return o
|