Correction des problmes d'encodages

darcs-hash:20071209042950-af139-11d9db44163a8349fcf095862ac33efa1aa1d9e9.gz
This commit is contained in:
Jeremie Dimino 2007-12-09 05:29:50 +01:00
parent 093598cd46
commit bb74d66be4

View file

@ -35,12 +35,28 @@ except:
sys.exit(1) sys.exit(1)
sys.path.append("/usr/scripts/gestion") sys.path.append("/usr/scripts/gestion")
from affich_tools import cprint from affich_tools import cprint, encoding
from unicode2ascii import unicode2ascii from unicode2ascii import unicode2ascii
def to_utf8(str):
""" Decode un str ou un unicode vers un str en UTF-8. """
if isinstance(str, unicode):
return str.encode("UTF-8")
else:
try:
# Si c'est une chaine brute, on commend par essayer
# de la décoder comme une chaine en UTF-8
str.decode("UTF-8")
return str
except:
try:
return str.decode(encoding).encode("UTF-8")
except:
return str
def darcs(args): def darcs(args):
""" Invoque darcs et renvoie sa sortie. """ """ Invoque darcs et renvoie sa sortie. """
return commands.getoutput("env DARCS_DONT_ESCAPE_8BIT=1 darcs " + args) return to_utf8(commands.getoutput("env DARCS_DONT_ESCAPE_8BIT=1 darcs " + args))
def get_patch_properties(): def get_patch_properties():
""" Récupère les informations a propos du dernier patch. """ """ Récupère les informations a propos du dernier patch. """
@ -54,8 +70,8 @@ def get_patch_properties():
'shortrepo': os.path.basename(cwd), 'shortrepo': os.path.basename(cwd),
'date': prop.attrib['local_date'], 'date': prop.attrib['local_date'],
'hash': prop.attrib['hash'], 'hash': prop.attrib['hash'],
'name': prop.findtext('name').encode("UTF-8"), 'name': prop.findtext('name'),
'comment': prop.findtext('comment').encode("UTF-8"), 'comment': prop.findtext('comment'),
'diff': diff, 'diff': diff,
'changes': darcs("changes --match='hash %s' --summary" % hash) } 'changes': darcs("changes --match='hash %s' --summary" % hash) }
@ -86,16 +102,26 @@ peut contenir les variables suivantes:
si template est None, DEFAULT_TEMPLATE est utilisé. si template est None, DEFAULT_TEMPLATE est utilisé.
""" """
patch_props["recipient"] = ", ".join(recipient) patch_props["recipient"] = ", ".join(recipient)
mail = email.message_from_string((template or DEFAULT_TEMPLATE) % patch_props) # On met toutes valeurs en string, en UTF-8
if not mail['Content-Transfer-Encoding']: for key, val in patch_props.items():
mail['Content-Transfer-Encoding'] = 'quoted-printable' patch_props[key] = to_utf8(val)
if not mail.get_charset(): template = to_utf8(template or DEFAULT_TEMPLATE)
mail.set_charset("UTF-8") mail = email.message_from_string(template % patch_props)
# On met le titre en ascii sinon c'est atroce pour le filtrage # On met le titre en ascii sinon c'est atroce pour le filtrage
# automatique # automatique
subject = unicode2ascii(mail['Subject'].decode("UTF-8")) subject = mail['Subject']
if subject:
subject = unicode2ascii(subject.decode("UTF-8"))
mail.replace_header('Subject', subject) mail.replace_header('Subject', subject)
if not mail['Content-Transfer-Encoding']:
mail['Content-Transfer-Encoding'] = 'quoted-printable'
charset = mail.get_charset()
if not charset:
charset = "UTF-8"
mail.set_charset("UTF-8")
rawmail = mail.as_string() rawmail = mail.as_string()
if charset != "UTF-8":
rawmail = rawmail.decode("UTF-8").encode(charset)
for to in recipient: for to in recipient:
smtp.sendmail(patch_props['author'], to, rawmail) smtp.sendmail(patch_props['author'], to, rawmail)