diff --git a/src/calibre/devices/__init__.py b/src/calibre/devices/__init__.py index a9a7e184a7..a32fd45c09 100644 --- a/src/calibre/devices/__init__.py +++ b/src/calibre/devices/__init__.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' @@ -8,7 +8,7 @@ Device drivers. import sys, time, pprint from functools import partial -from polyglot.builtins import zip +from polyglot.builtins import zip, unicode_type DAY_MAP = dict(Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6) MONTH_MAP = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8, Sep=9, Oct=10, Nov=11, Dec=12) @@ -19,8 +19,8 @@ INVERSE_MONTH_MAP = dict(zip(MONTH_MAP.values(), MONTH_MAP.keys())) def strptime(src): src = src.strip() src = src.split() - src[0] = str(DAY_MAP[src[0][:-1]])+',' - src[2] = str(MONTH_MAP[src[2]]) + src[0] = unicode_type(DAY_MAP[src[0][:-1]])+',' + src[2] = unicode_type(MONTH_MAP[src[2]]) return time.strptime(' '.join(src), '%w, %d %m %Y %H:%M:%S %Z') diff --git a/src/calibre/devices/cli.py b/src/calibre/devices/cli.py index 412fa985ed..4f2dd5c676 100755 --- a/src/calibre/devices/cli.py +++ b/src/calibre/devices/cli.py @@ -1,4 +1,5 @@ -from __future__ import print_function +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' """ @@ -15,6 +16,7 @@ from calibre.devices.errors import ArgumentError, DeviceError, DeviceLocked from calibre.customize.ui import device_plugins from calibre.devices.scanner import DeviceScanner from calibre.utils.config import device_prefs +from polyglot.builtins import unicode_type from polyglot.io import PolyglotBytesIO MINIMUM_COL_WIDTH = 12 # : Minimum width of columns in ls output @@ -92,7 +94,7 @@ def info(dev): def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0): def col_split(l, cols): # split list l into columns - rows = len(l) / cols + rows = len(l) // cols if len(l) % cols: rows += 1 m = [] @@ -122,7 +124,7 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0): maxlen = 0 if ll: # Calculate column width for size column for file in files: - size = len(str(file.size)) + size = len(unicode_type(file.size)) if human_readable_size: file = FileFormatter(file) size = len(file.human_readable_size) @@ -134,14 +136,14 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0): lsoutput.append(name) lscoloutput.append(name) if ll: - size = str(file.size) + size = unicode_type(file.size) if human_readable_size: size = file.human_readable_size - prints(file.mode_string, ("%"+str(maxlen)+"s")%size, file.modification_time, name, file=output) + prints(file.mode_string, ("%"+unicode_type(maxlen)+"s")%size, file.modification_time, name, file=output) if not ll and len(lsoutput) > 0: trytable = [] for colwidth in range(MINIMUM_COL_WIDTH, cols): - trycols = int(cols/colwidth) + trycols = int(cols//colwidth) trytable = col_split(lsoutput, trycols) works = True for row in trytable: @@ -241,7 +243,7 @@ def main(): print("Filesystem\tSize \tUsed \tAvail \tUse%") for i in range(3): print("%-10s\t%s\t%s\t%s\t%s"%(where[i], human_readable(total[i]), human_readable(total[i]-free[i]), human_readable(free[i]), - str(0 if total[i]==0 else int(100*(total[i]-free[i])/(total[i]*1.)))+"%")) + unicode_type(0 if total[i]==0 else int(100*(total[i]-free[i])/(total[i]*1.)))+"%")) elif command == 'eject': dev.eject() elif command == "books": diff --git a/src/calibre/devices/errors.py b/src/calibre/devices/errors.py index 8cf4e127cd..95877fc72c 100644 --- a/src/calibre/devices/errors.py +++ b/src/calibre/devices/errors.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' """ @@ -6,6 +8,8 @@ Defines the errors that the device drivers generate. G{classtree ProtocolError} """ +from polyglot.builtins import unicode_type + class ProtocolError(Exception): """ The base class for all exceptions in this package """ @@ -91,7 +95,7 @@ class DeviceBusy(ProtocolError): def __init__(self, uerr=""): ProtocolError.__init__( self, "Device is in use by another application:" - "\nUnderlying error:" + str(uerr) + "\nUnderlying error:" + unicode_type(uerr) ) @@ -134,9 +138,9 @@ class ControlError(ProtocolError): def __str__(self): if self.query and self.response: return "Got unexpected response:\n" + \ - "query:\n"+str(self.query.query)+"\n"+\ - "expected:\n"+str(self.query.response)+"\n" +\ - "actual:\n"+str(self.response) + "query:\n"+unicode_type(self.query.query)+"\n"+\ + "expected:\n"+unicode_type(self.query.response)+"\n" +\ + "actual:\n"+unicode_type(self.response) if self.desc: return self.desc return "Unknown control error occurred" diff --git a/src/calibre/devices/interface.py b/src/calibre/devices/interface.py index d8f1ebd5f7..b74e2555fd 100644 --- a/src/calibre/devices/interface.py +++ b/src/calibre/devices/interface.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' import os @@ -423,7 +425,7 @@ class DevicePlugin(Plugin): :meth`books(oncard='cardb')`). ''' - raise NotImplementedError + raise NotImplementedError() def delete_books(self, paths, end_session=True): ''' diff --git a/src/calibre/devices/mime.py b/src/calibre/devices/mime.py index 80ee3391c2..0762ea7809 100644 --- a/src/calibre/devices/mime.py +++ b/src/calibre/devices/mime.py @@ -1,4 +1,4 @@ -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL 3' __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' @@ -21,4 +21,3 @@ def mime_type_ext(ext): def mime_type_path(path): return _mt(path) - diff --git a/src/calibre/devices/misc.py b/src/calibre/devices/misc.py index f6fd6f940b..a191292d84 100644 --- a/src/calibre/devices/misc.py +++ b/src/calibre/devices/misc.py @@ -1,6 +1,6 @@ #!/usr/bin/env python2 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' diff --git a/src/calibre/devices/scanner.py b/src/calibre/devices/scanner.py index 3b8a141300..33e0287b24 100644 --- a/src/calibre/devices/scanner.py +++ b/src/calibre/devices/scanner.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' ''' @@ -107,7 +107,7 @@ class LinuxScanner(object): self.ok = os.path.exists(self.base) def __call__(self): - ans = set([]) + ans = set() if not self.ok: raise RuntimeError('DeviceScanner requires the /sys filesystem to work.') @@ -128,41 +128,41 @@ class LinuxScanner(object): # Ignore USB HUBs if read(os.path.join(base, 'bDeviceClass')) == b'09': continue - except: + except Exception: continue try: dev.append(int(b'0x'+read(ven), 16)) - except: + except Exception: continue try: dev.append(int(b'0x'+read(prod), 16)) - except: + except Exception: continue try: dev.append(int(b'0x'+read(bcd), 16)) - except: + except Exception: continue try: dev.append(read(man).decode('utf-8')) - except: + except Exception: dev.append(u'') try: dev.append(read(prod_string).decode('utf-8')) - except: + except Exception: dev.append(u'') try: dev.append(read(serial).decode('utf-8')) - except: + except Exception: dev.append(u'') dev = USBDevice(*dev) try: dev.busnum = int(read(os.path.join(base, 'busnum'))) - except: + except Exception: pass try: dev.devnum = int(read(os.path.join(base, 'devnum'))) - except: + except Exception: pass ans.add(dev) return ans diff --git a/src/calibre/devices/udisks.py b/src/calibre/devices/udisks.py index 83bf8147b5..2efd9ca104 100644 --- a/src/calibre/devices/udisks.py +++ b/src/calibre/devices/udisks.py @@ -1,23 +1,26 @@ #!/usr/bin/env python2 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai +from __future__ import absolute_import, division, print_function, unicode_literals -from __future__ import print_function __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' import os, re -from polyglot.builtins import unicode_type +from polyglot.builtins import unicode_type, as_bytes, as_unicode def node_mountpoint(node): - def de_mangle(raw): - return raw.replace('\\040', ' ').replace('\\011', '\t').replace('\\012', - '\n').replace('\\0134', '\\') + if isinstance(node, unicode_type): + node = node.encode('utf-8') - for line in open('/proc/mounts').readlines(): + def de_mangle(raw): + return raw.replace(b'\\040', b' ').replace(b'\\011', b'\t').replace(b'\\012', + b'\n').replace(b'\\0134', b'\\').decode('utf-8') + + for line in open('/proc/mounts', 'rb').readlines(): line = line.split() if line[0] == node: return de_mangle(line[1]) @@ -53,9 +56,9 @@ class UDisks(object): return unicode_type(d.FilesystemMount('', ['auth_no_user_interaction', 'rw', 'noexec', 'nosuid', 'nodev', 'uid=%d'%os.geteuid(), 'gid=%d'%os.getegid()])) - except: + except Exception: # May be already mounted, check - mp = node_mountpoint(str(device_node_path)) + mp = node_mountpoint(unicode_type(device_node_path)) if mp is None: raise return mp @@ -103,8 +106,8 @@ class UDisks2(object): try: device = bd.Get(self.BLOCK, 'Device', dbus_interface='org.freedesktop.DBus.Properties') - device = bytearray(device).replace(b'\x00', b'').decode('utf-8') - except: + device = bytearray(as_bytes(device)).replace(b'\x00', b'').decode('utf-8') + except Exception: device = None if device == device_node_path: @@ -120,7 +123,7 @@ class UDisks2(object): try: device = bd.Get(self.BLOCK, 'Device', dbus_interface='org.freedesktop.DBus.Properties') - device = bytearray(device).replace(b'\x00', b'').decode('utf-8') + device = bytearray(as_bytes(device)).replace(b'\x00', b'').decode('utf-8') except: device = None if device == device_node_path: @@ -133,15 +136,15 @@ class UDisks2(object): mount_options = ['rw', 'noexec', 'nosuid', 'nodev', 'uid=%d'%os.geteuid(), 'gid=%d'%os.getegid()] try: - return unicode_type(d.Mount( + return as_unicode(d.Mount( { 'auth.no_user_interaction':True, 'options':','.join(mount_options) }, dbus_interface=self.FILESYSTEM)) - except: + except Exception: # May be already mounted, check - mp = node_mountpoint(str(device_node_path)) + mp = node_mountpoint(unicode_type(device_node_path)) if mp is None: raise return mp diff --git a/src/calibre/devices/utils.py b/src/calibre/devices/utils.py index 2240874393..bd349d0803 100644 --- a/src/calibre/devices/utils.py +++ b/src/calibre/devices/utils.py @@ -100,7 +100,7 @@ def create_upload_path(mdata, fname, template, sanitize, opts = config().parse() if not isinstance(template, unicode_type): template = template.decode('utf-8') - app_id = str(getattr(mdata, 'application_id', '')) + app_id = unicode_type(getattr(mdata, 'application_id', '')) id_ = mdata.get('id', fname) extra_components = get_components(template, mdata, id_, timefmt=opts.send_timefmt, length=maxlen-len(app_id)-1,