]> git.decadent.org.uk Git - dak.git/blob - daklib/textutils.py
move fix_maintainer support routines to textutils
[dak.git] / daklib / textutils.py
1 #!/usr/bin/env python
2 # vim:set et ts=4 sw=4:
3
4 """Text utility functions
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2000, 2001, 2002, 2003, 2004, 2005, 2006  James Troup <james@nocrew.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 import codecs
26 import email.Header
27
28 from dak_exceptions import *
29 from regexes import re_parse_maintainer
30
31 ################################################################################
32
33 def force_to_utf8(s):
34     """
35     Forces a string to UTF-8.  If the string isn't already UTF-8,
36     it's assumed to be ISO-8859-1.
37     """
38     try:
39         unicode(s, 'utf-8')
40         return s
41     except UnicodeError:
42         latin1_s = unicode(s,'iso8859-1')
43         return latin1_s.encode('utf-8')
44
45 def rfc2047_encode(s):
46     """
47     Encodes a (header) string per RFC2047 if necessary.  If the
48     string is neither ASCII nor UTF-8, it's assumed to be ISO-8859-1.
49     """
50     try:
51         codecs.lookup('ascii')[1](s)
52         return s
53     except UnicodeError:
54         pass
55     try:
56         codecs.lookup('utf-8')[1](s)
57         h = email.Header.Header(s, 'utf-8', 998)
58         return str(h)
59     except UnicodeError:
60         h = email.Header.Header(s, 'iso-8859-1', 998)
61         return str(h)
62
63 ################################################################################
64
65 # <Culus> 'The standard sucks, but my tool is supposed to interoperate
66 #          with it. I know - I'll fix the suckage and make things
67 #          incompatible!'
68
69 def fix_maintainer(maintainer):
70     """
71     Parses a Maintainer or Changed-By field and returns:
72       1. an RFC822 compatible version,
73       2. an RFC2047 compatible version,
74       3. the name
75       4. the email
76
77     The name is forced to UTF-8 for both 1. and 3..  If the name field
78     contains '.' or ',' (as allowed by Debian policy), 1. and 2. are
79     switched to 'email (name)' format.
80
81     """
82     maintainer = maintainer.strip()
83     if not maintainer:
84         return ('', '', '', '')
85
86     if maintainer.find("<") == -1:
87         email = maintainer
88         name = ""
89     elif (maintainer[0] == "<" and maintainer[-1:] == ">"):
90         email = maintainer[1:-1]
91         name = ""
92     else:
93         m = re_parse_maintainer.match(maintainer)
94         if not m:
95             raise ParseMaintError, "Doesn't parse as a valid Maintainer field."
96         name = m.group(1)
97         email = m.group(2)
98
99     # Get an RFC2047 compliant version of the name
100     rfc2047_name = rfc2047_encode(name)
101
102     # Force the name to be UTF-8
103     name = force_to_utf8(name)
104
105     if name.find(',') != -1 or name.find('.') != -1:
106         rfc822_maint = "%s (%s)" % (email, name)
107         rfc2047_maint = "%s (%s)" % (email, rfc2047_name)
108     else:
109         rfc822_maint = "%s <%s>" % (name, email)
110         rfc2047_maint = "%s <%s>" % (rfc2047_name, email)
111
112     if email.find("@") == -1 and email.find("buildd_") != 0:
113         raise ParseMaintError, "No @ found in email address part."
114
115     return (rfc822_maint, rfc2047_maint, name, email)
116
117 ################################################################################