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
27 - def __new__(newtype, string=u"", encoding=None, errors=None):
28 if isinstance(string, list):
29 if not string:
30 raise ValueError("multistring must contain at least one string")
31 mainstring = string[0]
32 newstring = multistring.__new__(newtype, string[0], encoding, errors)
33 newstring.strings = [newstring] + [autoencode.autoencode.__new__(autoencode.autoencode, altstring, encoding, errors) for altstring in string[1:]]
34 else:
35 newstring = autoencode.autoencode.__new__(newtype, string, encoding, errors)
36 newstring.strings = [newstring]
37 return newstring
38
40 super(multistring, self).__init__(*args, **kwargs)
41 if not hasattr(self, "strings"):
42 self.strings = []
43
45 if isinstance(otherstring, multistring):
46 parentcompare = cmp(autoencode.autoencode(self), otherstring)
47 if parentcompare:
48 return parentcompare
49 else:
50 return cmp(self.strings[1:], otherstring.strings[1:])
51 elif isinstance(otherstring, autoencode.autoencode):
52 return cmp(autoencode.autoencode(self), otherstring)
53 elif isinstance(otherstring, unicode):
54 return cmp(unicode(self), otherstring)
55 elif isinstance(otherstring, str):
56 return cmp(str(self), otherstring)
57 else:
58 return cmp(type(self), type(otherstring))
59
60 - def __ne__(self, otherstring):
61 return self.__cmp__(otherstring) != 0
62
63 - def __eq__(self, otherstring):
64 return self.__cmp__(otherstring) == 0
65
69
70 - def replace(self, old, new, count=None):
71 if count is None:
72 newstr = multistring(super(multistring, self).replace(old, new), self.encoding)
73 else:
74 newstr = multistring(super(multistring, self).replace(old, new, count), self.encoding)
75 for s in self.strings[1:]:
76 if count is None:
77 newstr.strings.append(s.replace(old, new))
78 else:
79 newstr.strings.append(s.replace(old, new, count))
80 return newstr
81