1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Supports a hybrid Unicode string that can also have a list of alternate strings in the strings attribute"""
23
24 from translate.misc import autoencode
25
26
28
29 - def __new__(newtype, string=u"", encoding=None, errors=None):
30 if isinstance(string, list):
31 if not string:
32 raise ValueError("multistring must contain at least one string")
33 mainstring = string[0]
34 newstring = multistring.__new__(newtype, string[0], encoding, errors)
35 newstring.strings = [newstring] + [autoencode.autoencode.__new__(autoencode.autoencode, altstring, encoding, errors) for altstring in string[1:]]
36 else:
37 newstring = autoencode.autoencode.__new__(newtype, string, encoding, errors)
38 newstring.strings = [newstring]
39 return newstring
40
42 super(multistring, self).__init__()
43 if not hasattr(self, "strings"):
44 self.strings = []
45
47 if isinstance(otherstring, multistring):
48 parentcompare = cmp(autoencode.autoencode(self), otherstring)
49 if parentcompare:
50 return parentcompare
51 else:
52 return cmp(self.strings[1:], otherstring.strings[1:])
53 elif isinstance(otherstring, autoencode.autoencode):
54 return cmp(autoencode.autoencode(self), otherstring)
55 elif isinstance(otherstring, unicode):
56 return cmp(unicode(self), otherstring)
57 elif isinstance(otherstring, str):
58 return cmp(str(self), otherstring)
59 elif isinstance(otherstring, list) and otherstring:
60 return cmp(self, multistring(otherstring))
61 else:
62 return cmp(type(self), type(otherstring))
63
64 - def __ne__(self, otherstring):
65 return self.__cmp__(otherstring) != 0
66
67 - def __eq__(self, otherstring):
68 return self.__cmp__(otherstring) == 0
69
73
74 - def replace(self, old, new, count=None):
75 if count is None:
76 newstr = multistring(super(multistring, self).replace(old, new), self.encoding)
77 else:
78 newstr = multistring(super(multistring, self).replace(old, new, count), self.encoding)
79 for s in self.strings[1:]:
80 if count is None:
81 newstr.strings.append(s.replace(old, new))
82 else:
83 newstr.strings.append(s.replace(old, new, count))
84 return newstr
85