34 return path |
34 return path |
35 |
35 |
36 |
36 |
37 def get_unicode(string): |
37 def get_unicode(string): |
38 """Converts `string` to `unicode`, if necessary.""" |
38 """Converts `string` to `unicode`, if necessary.""" |
39 if isinstance(string, unicode): |
39 if isinstance(string, str): |
40 return string |
40 return string |
41 return unicode(string, ENCODING, 'replace') |
41 return str(string, ENCODING, 'replace') |
42 |
42 |
43 |
43 |
44 def lisdir(path): |
44 def lisdir(path): |
45 """Checks if `path` is a directory. Doesn't follow symbolic links. |
45 """Checks if `path` is a directory. Doesn't follow symbolic links. |
46 Returns bool. |
46 Returns bool. |
58 Throws a `VMMError` if the `binary` isn't a file or is not |
58 Throws a `VMMError` if the `binary` isn't a file or is not |
59 executable. |
59 executable. |
60 """ |
60 """ |
61 binary = expand_path(binary) |
61 binary = expand_path(binary) |
62 if not os.path.isfile(binary): |
62 if not os.path.isfile(binary): |
63 raise VMMError(_(u"No such file: '%s'") % get_unicode(binary), |
63 raise VMMError(_("No such file: '%s'") % get_unicode(binary), |
64 NO_SUCH_BINARY) |
64 NO_SUCH_BINARY) |
65 if not os.access(binary, os.X_OK): |
65 if not os.access(binary, os.X_OK): |
66 raise VMMError(_(u"File is not executable: '%s'") % |
66 raise VMMError(_("File is not executable: '%s'") % |
67 get_unicode(binary), NOT_EXECUTABLE) |
67 get_unicode(binary), NOT_EXECUTABLE) |
68 return binary |
68 return binary |
69 |
69 |
70 |
70 |
71 def human_size(size): |
71 def human_size(size): |
72 """Converts the `size` in bytes in human readable format.""" |
72 """Converts the `size` in bytes in human readable format.""" |
73 if not isinstance(size, (long, int)): |
73 if not isinstance(size, int): |
74 try: |
74 try: |
75 size = long(size) |
75 size = int(size) |
76 except ValueError: |
76 except ValueError: |
77 raise TypeError("'size' must be a positive long or int.") |
77 raise TypeError("'size' must be a positive long or int.") |
78 if size < 0: |
78 if size < 0: |
79 raise ValueError("'size' must be a positive long or int.") |
79 raise ValueError("'size' must be a positive long or int.") |
80 if size < 1024: |
80 if size < 1024: |
81 return str(size) |
81 return str(size) |
82 # TP: abbreviations of gibibyte, tebibyte kibibyte and mebibyte |
82 # TP: abbreviations of gibibyte, tebibyte kibibyte and mebibyte |
83 prefix_multiply = ((_(u'TiB'), 1 << 40), (_(u'GiB'), 1 << 30), |
83 prefix_multiply = ((_('TiB'), 1 << 40), (_('GiB'), 1 << 30), |
84 (_(u'MiB'), 1 << 20), (_(u'KiB'), 1 << 10)) |
84 (_('MiB'), 1 << 20), (_('KiB'), 1 << 10)) |
85 for prefix, multiply in prefix_multiply: |
85 for prefix, multiply in prefix_multiply: |
86 if size >= multiply: |
86 if size >= multiply: |
87 # TP: e.g.: '%(size)s %(prefix)s' -> '118.30 MiB' |
87 # TP: e.g.: '%(size)s %(prefix)s' -> '118.30 MiB' |
88 return _(u'%(size)s %(prefix)s') % { |
88 return _('%(size)s %(prefix)s') % { |
89 'size': locale.format('%.2f', float(size) / multiply, |
89 'size': locale.format('%.2f', float(size) / multiply, |
90 True).decode(ENCODING, 'replace'), |
90 True).decode(ENCODING, 'replace'), |
91 'prefix': prefix} |
91 'prefix': prefix} |
92 |
92 |
93 |
93 |
95 """Converts the string `size` to a long (size in bytes). |
95 """Converts the string `size` to a long (size in bytes). |
96 |
96 |
97 The string `size` can be suffixed with *b* (bytes), *k* (kilobytes), |
97 The string `size` can be suffixed with *b* (bytes), *k* (kilobytes), |
98 *M* (megabytes) or *G* (gigabytes). |
98 *M* (megabytes) or *G* (gigabytes). |
99 """ |
99 """ |
100 if not isinstance(size, basestring) or not size: |
100 if not isinstance(size, str) or not size: |
101 raise TypeError('size must be a non empty string.') |
101 raise TypeError('size must be a non empty string.') |
102 if size[-1].upper() in ('B', 'K', 'M', 'G'): |
102 if size[-1].upper() in ('B', 'K', 'M', 'G'): |
103 try: |
103 try: |
104 num = int(size[:-1]) |
104 num = int(size[:-1]) |
105 except ValueError: |
105 except ValueError: |
106 raise ValueError('Not a valid integer value: %r' % size[:-1]) |
106 raise ValueError('Not a valid integer value: %r' % size[:-1]) |
107 unit = size[-1].upper() |
107 unit = size[-1].upper() |
108 if unit == 'B': |
108 if unit == 'B': |
109 return num |
109 return num |
110 elif unit == 'K': |
110 elif unit == 'K': |
111 return num << 10L |
111 return num << 10 |
112 elif unit == 'M': |
112 elif unit == 'M': |
113 return num << 20L |
113 return num << 20 |
114 else: |
114 else: |
115 return num << 30L |
115 return num << 30 |
116 else: |
116 else: |
117 try: |
117 try: |
118 num = int(size) |
118 num = int(size) |
119 except ValueError: |
119 except ValueError: |
120 raise ValueError('Not a valid size value: %r' % size) |
120 raise ValueError('Not a valid size value: %r' % size) |
134 `maillocation` : VirtualMailManager.maillocation.MailLocation |
134 `maillocation` : VirtualMailManager.maillocation.MailLocation |
135 a MailLocation object |
135 a MailLocation object |
136 """ |
136 """ |
137 if transport.transport in ('virtual', 'virtual:') and \ |
137 if transport.transport in ('virtual', 'virtual:') and \ |
138 not maillocation.postfix: |
138 not maillocation.postfix: |
139 raise VMMError(_(u"Invalid transport '%(transport)s' for mailbox " |
139 raise VMMError(_("Invalid transport '%(transport)s' for mailbox " |
140 u"format '%(mbfmt)s'.") % |
140 "format '%(mbfmt)s'.") % |
141 {'transport': transport.transport, |
141 {'transport': transport.transport, |
142 'mbfmt': maillocation.mbformat}, INVALID_MAIL_LOCATION) |
142 'mbfmt': maillocation.mbformat}, INVALID_MAIL_LOCATION) |
143 |
143 |
144 |
144 |
145 def version_hex(version_string): |
145 def version_hex(version_string): |
187 Raises a `ValueError` if *version* is an incorrect int version. |
187 Raises a `ValueError` if *version* is an incorrect int version. |
188 """ |
188 """ |
189 global _version_cache |
189 global _version_cache |
190 if version in _version_cache: |
190 if version in _version_cache: |
191 return _version_cache[version] |
191 return _version_cache[version] |
192 if not isinstance(version, (int, long)): |
192 if not isinstance(version, int): |
193 raise TypeError('Argument is not a int/long: %r', version) |
193 raise TypeError('Argument is not a int/long: %r', version) |
194 major = (version >> 28) & 0xFF |
194 major = (version >> 28) & 0xFF |
195 minor = (version >> 20) & 0xFF |
195 minor = (version >> 20) & 0xFF |
196 patch = (version >> 12) & 0xFF |
196 patch = (version >> 12) & 0xFF |
197 level = (version >> 8) & 0x0F |
197 level = (version >> 8) & 0x0F |
198 serial = version & 0xFF |
198 serial = version & 0xFF |
199 |
199 |
200 levels = dict(zip(_version_level.values(), _version_level.keys())) |
200 levels = dict(list(zip(list(_version_level.values()), list(_version_level.keys())))) |
201 if level == 0xF and not serial: |
201 if level == 0xF and not serial: |
202 version_string = '%u.%u.%u' % (major, minor, patch) |
202 version_string = '%u.%u.%u' % (major, minor, patch) |
203 elif level in levels and not patch: |
203 elif level in levels and not patch: |
204 version_string = '%u.%u.%s%u' % (major, minor, levels[level], serial) |
204 version_string = '%u.%u.%s%u' % (major, minor, levels[level], serial) |
205 else: |
205 else: |
212 def format_domain_default(domaindata): |
212 def format_domain_default(domaindata): |
213 """Format info output when the value displayed is the domain default.""" |
213 """Format info output when the value displayed is the domain default.""" |
214 # TP: [domain default] indicates that a user's setting is the same as |
214 # TP: [domain default] indicates that a user's setting is the same as |
215 # configured in the user's domain. |
215 # configured in the user's domain. |
216 # e.g.: [ 0.84%] 42/5,000 [domain default] |
216 # e.g.: [ 0.84%] 42/5,000 [domain default] |
217 return _(u'%s [domain default]') % domaindata |
217 return _('%s [domain default]') % domaindata |
218 |
218 |
219 |
219 |
220 def search_addresses(dbh, typelimit=None, lpattern=None, llike=False, |
220 def search_addresses(dbh, typelimit=None, lpattern=None, llike=False, |
221 dpattern=None, dlike=False): |
221 dpattern=None, dlike=False): |
222 """'Search' for addresses by *pattern* in the database. |
222 """'Search' for addresses by *pattern* in the database. |