VMM: Post-2to3 fixes. v0.7.x
authorPascal Volk <user@localhost.localdomain.org>
Sun, 25 Nov 2012 17:06:17 +0000
branchv0.7.x
changeset 653 cf07e4468934
parent 652 68d715ce6e1c
child 654 f2463a64e1d7
VMM: Post-2to3 fixes. Added, (re)moved some (en|de)code() calls.
VirtualMailManager/cli/__init__.py
VirtualMailManager/cli/subcommands.py
VirtualMailManager/common.py
VirtualMailManager/domain.py
VirtualMailManager/ext/postconf.py
VirtualMailManager/handler.py
VirtualMailManager/mailbox.py
--- a/VirtualMailManager/cli/__init__.py	Sun Nov 25 14:37:09 2012 +0000
+++ b/VirtualMailManager/cli/__init__.py	Sun Nov 25 17:06:17 2012 +0000
@@ -31,7 +31,8 @@
     """Writes a line for each arg of *args*, encoded in the current
     ENCODING, to stdout.
     """
-    _std_write('\n'.join(a.encode(ENCODING, 'replace') for a in args) + '\n')
+    _std_write('\n'.join(arg.encode(ENCODING, 'replace').decode()
+               for arg in args) + '\n')
 
 
 def w_err(code, *args):
@@ -40,7 +41,8 @@
     This function optionally interrupts the program execution if *code*
     does not equal to 0. *code* will be used as the system exit status.
     """
-    _err_write('\n'.join(a.encode(ENCODING, 'replace') for a in args) + '\n')
+    _err_write('\n'.join(arg.encode(ENCODING, 'replace').decode()
+               for arg in args) + '\n')
     if code:
         os.sys.exit(code)
 
--- a/VirtualMailManager/cli/subcommands.py	Sun Nov 25 14:37:09 2012 +0000
+++ b/VirtualMailManager/cli/subcommands.py	Sun Nov 25 17:06:17 2012 +0000
@@ -110,7 +110,7 @@
     def __init__(self, argv, handler, command):
         """Create a new RunContext"""
         self.argc = len(argv)
-        self.args = [str(arg, ENCODING) for arg in argv]
+        self.args = argv[:]  # will be moved to argparse
         self.cget = handler.cfg_dget
         self.hdlr = handler
         self.scmd = command
@@ -320,8 +320,7 @@
         q_limit = 'Storage: %(bytes)s; Messages: %(messages)s'
         if not details:
             info['bytes'] = human_size(info['bytes'])
-            info['messages'] = locale.format('%d', info['messages'],
-                                             True).decode(ENCODING, 'replace')
+            info['messages'] = locale.format('%d', info['messages'], True)
             info['quota limit/user'] = q_limit % info
             _print_info(ctx, info, _('Domain'))
         else:
@@ -778,7 +777,7 @@
     # Python 2.5.4 on FreeBSD
         _('version'), __version__, _('from'),
         strftime(locale.nl_langinfo(locale.D_FMT),
-            strptime(__date__, '%Y-%m-%d')).decode(ENCODING, 'replace'),
+            strptime(__date__, '%Y-%m-%d')),
         os.sys.version.split()[0], _('on'), os.uname()[0],
         __copyright__, prog,
         _('is free software and comes with ABSOLUTELY NO WARRANTY.')))
@@ -943,10 +942,8 @@
         }
     else:
         q_usage = {
-            'used': locale.format('%d', used, True).decode(ENCODING,
-                                                           'replace'),
-            'limit': locale.format('%d', limit, True).decode(ENCODING,
-                                                             'replace'),
+            'used': locale.format('%d', used, True),
+            'limit': locale.format('%d', limit, True),
         }
     if limit:
         q_usage['percent'] = locale.format('%6.2f', 100. / limit * used, True)
--- a/VirtualMailManager/common.py	Sun Nov 25 14:37:09 2012 +0000
+++ b/VirtualMailManager/common.py	Sun Nov 25 17:06:17 2012 +0000
@@ -87,7 +87,7 @@
             # TP: e.g.: '%(size)s %(prefix)s' -> '118.30 MiB'
             return _('%(size)s %(prefix)s') % {
                     'size': locale.format('%.2f', float(size) / multiply,
-                                          True).decode(ENCODING, 'replace'),
+                                          True),
                     'prefix': prefix}
 
 
@@ -197,7 +197,8 @@
     level = (version >> 8) & 0x0F
     serial = version & 0xFF
 
-    levels = dict(list(zip(list(_version_level.values()), list(_version_level.keys()))))
+    levels = dict(list(zip(list(_version_level.values()),
+                  list(_version_level.keys()))))
     if level == 0xF and not serial:
         version_string = '%u.%u.%u' % (major, minor, patch)
     elif level in levels and not patch:
--- a/VirtualMailManager/domain.py	Sun Nov 25 14:37:09 2012 +0000
+++ b/VirtualMailManager/domain.py	Sun Nov 25 17:06:17 2012 +0000
@@ -499,7 +499,7 @@
 
     """
     if not RE_DOMAIN.match(domainname):
-        domainname = domainname.encode('idna')
+        domainname = domainname.encode('idna').decode()
     if len(domainname) > 255:
         raise DomErr(_('The domain name is too long'), DOMAIN_TOO_LONG)
     if not RE_DOMAIN.match(domainname):
--- a/VirtualMailManager/ext/postconf.py	Sun Nov 25 14:37:09 2012 +0000
+++ b/VirtualMailManager/ext/postconf.py	Sun Nov 25 17:06:17 2012 +0000
@@ -53,7 +53,7 @@
         stderr = Popen((self._bin, '-e', parameter + '=' + str(value)),
                        stderr=PIPE).communicate()[1]
         if stderr:
-            raise VMMError(stderr.strip(), VMM_ERROR)
+            raise VMMError(stderr.strip().decode(), VMM_ERROR)
 
     def read(self, parameter, expand_vars=True):
         """Returns the parameters value.
@@ -107,8 +107,8 @@
         stdout, stderr = Popen([self._bin, '-h', parameter], stdout=PIPE,
                                stderr=PIPE).communicate()
         if stderr:
-            raise VMMError(stderr.strip(), VMM_ERROR)
-        return stdout.strip()
+            raise VMMError(stderr.strip().decode(), VMM_ERROR)
+        return stdout.strip().decode()
 
     def _read_multi(self, parameters):
         """Ask postconf for multiple configuration parameters. Returns a dict
@@ -117,9 +117,9 @@
         cmd.extend(parameter[1:] for parameter in parameters)
         stdout, stderr = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate()
         if stderr:
-            raise VMMError(stderr.strip(), VMM_ERROR)
+            raise VMMError(stderr.strip().decode(), VMM_ERROR)
         par_val = {}
-        for line in stdout.splitlines():
+        for line in stdout.decode().splitlines():
             par, val = line.split(' = ')
             par_val[par] = val
         return par_val
--- a/VirtualMailManager/handler.py	Sun Nov 25 14:37:09 2012 +0000
+++ b/VirtualMailManager/handler.py	Sun Nov 25 17:06:17 2012 +0000
@@ -283,7 +283,7 @@
         """
         if lisdir(directory):
             return Popen([self._cfg.dget('bin.du'), "-hs", directory],
-                         stdout=PIPE).communicate()[0].split('\t')[0]
+                         stdout=PIPE).communicate()[0].decode().split('\t')[0]
         else:
             self._warnings.append(_('No such directory: %s') % directory)
             return 0
@@ -525,7 +525,7 @@
         dominfo = dom.get_info()
         if dominfo['domain name'].startswith('xn--'):
             dominfo['domain name'] += ' (%s)' % \
-                                      dominfo['domain name'].decode('idna')
+                         dominfo['domain name'].encode('utf-8').decode('idna')
         if details is None:
             return dominfo
         elif details == 'accounts':
--- a/VirtualMailManager/mailbox.py	Sun Nov 25 14:37:09 2012 +0000
+++ b/VirtualMailManager/mailbox.py	Sun Nov 25 17:06:17 2012 +0000
@@ -29,13 +29,14 @@
 
 def _mbase64_encode(inp, dest):
     if inp:
-        mb64 = b2a_base64(''.join(inp).encode('utf-16be'))
+        mb64 = b2a_base64(''.join(inp).encode('utf-16be')).decode()
         dest.append('&%s-' % mb64.rstrip('\n=').replace('/', ','))
         del inp[:]
 
 
 def _mbase64_to_unicode(mb64):
-    return str(a2b_base64(mb64.replace(',', '/') + '==='), 'utf-16be')
+    return str(a2b_base64(mb64.replace(',', '/').encode() + b'==='),
+               'utf-16be')
 
 
 def utf8_to_mutf7(src):
@@ -256,7 +257,7 @@
         stderr = process.communicate()[1]
         if process.returncode:
             e_msg = _('Failed to create mailboxes: %r\n') % mailboxes
-            raise VMMError(e_msg + stderr.strip(), VMM_ERROR)
+            raise VMMError(e_msg + stderr.strip().decode(), VMM_ERROR)
 
     def create(self):
         """Create a dbox INBOX"""