mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
py3: More unicode_literals and str() porting
This commit is contained in:
parent
50af7ba51f
commit
85fbb87b82
@ -1,4 +1,4 @@
|
|||||||
from __future__ import print_function
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ Device drivers.
|
|||||||
|
|
||||||
import sys, time, pprint
|
import sys, time, pprint
|
||||||
from functools import partial
|
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)
|
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)
|
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):
|
def strptime(src):
|
||||||
src = src.strip()
|
src = src.strip()
|
||||||
src = src.split()
|
src = src.split()
|
||||||
src[0] = str(DAY_MAP[src[0][:-1]])+','
|
src[0] = unicode_type(DAY_MAP[src[0][:-1]])+','
|
||||||
src[2] = str(MONTH_MAP[src[2]])
|
src[2] = unicode_type(MONTH_MAP[src[2]])
|
||||||
return time.strptime(' '.join(src), '%w, %d %m %Y %H:%M:%S %Z')
|
return time.strptime(' '.join(src), '%w, %d %m %Y %H:%M:%S %Z')
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import print_function
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
"""
|
"""
|
||||||
@ -15,6 +16,7 @@ from calibre.devices.errors import ArgumentError, DeviceError, DeviceLocked
|
|||||||
from calibre.customize.ui import device_plugins
|
from calibre.customize.ui import device_plugins
|
||||||
from calibre.devices.scanner import DeviceScanner
|
from calibre.devices.scanner import DeviceScanner
|
||||||
from calibre.utils.config import device_prefs
|
from calibre.utils.config import device_prefs
|
||||||
|
from polyglot.builtins import unicode_type
|
||||||
from polyglot.io import PolyglotBytesIO
|
from polyglot.io import PolyglotBytesIO
|
||||||
|
|
||||||
MINIMUM_COL_WIDTH = 12 # : Minimum width of columns in ls output
|
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 ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0):
|
||||||
def col_split(l, cols): # split list l into columns
|
def col_split(l, cols): # split list l into columns
|
||||||
rows = len(l) / cols
|
rows = len(l) // cols
|
||||||
if len(l) % cols:
|
if len(l) % cols:
|
||||||
rows += 1
|
rows += 1
|
||||||
m = []
|
m = []
|
||||||
@ -122,7 +124,7 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0):
|
|||||||
maxlen = 0
|
maxlen = 0
|
||||||
if ll: # Calculate column width for size column
|
if ll: # Calculate column width for size column
|
||||||
for file in files:
|
for file in files:
|
||||||
size = len(str(file.size))
|
size = len(unicode_type(file.size))
|
||||||
if human_readable_size:
|
if human_readable_size:
|
||||||
file = FileFormatter(file)
|
file = FileFormatter(file)
|
||||||
size = len(file.human_readable_size)
|
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)
|
lsoutput.append(name)
|
||||||
lscoloutput.append(name)
|
lscoloutput.append(name)
|
||||||
if ll:
|
if ll:
|
||||||
size = str(file.size)
|
size = unicode_type(file.size)
|
||||||
if human_readable_size:
|
if human_readable_size:
|
||||||
size = file.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:
|
if not ll and len(lsoutput) > 0:
|
||||||
trytable = []
|
trytable = []
|
||||||
for colwidth in range(MINIMUM_COL_WIDTH, cols):
|
for colwidth in range(MINIMUM_COL_WIDTH, cols):
|
||||||
trycols = int(cols/colwidth)
|
trycols = int(cols//colwidth)
|
||||||
trytable = col_split(lsoutput, trycols)
|
trytable = col_split(lsoutput, trycols)
|
||||||
works = True
|
works = True
|
||||||
for row in trytable:
|
for row in trytable:
|
||||||
@ -241,7 +243,7 @@ def main():
|
|||||||
print("Filesystem\tSize \tUsed \tAvail \tUse%")
|
print("Filesystem\tSize \tUsed \tAvail \tUse%")
|
||||||
for i in range(3):
|
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]),
|
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':
|
elif command == 'eject':
|
||||||
dev.eject()
|
dev.eject()
|
||||||
elif command == "books":
|
elif command == "books":
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
"""
|
"""
|
||||||
@ -6,6 +8,8 @@ Defines the errors that the device drivers generate.
|
|||||||
G{classtree ProtocolError}
|
G{classtree ProtocolError}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from polyglot.builtins import unicode_type
|
||||||
|
|
||||||
|
|
||||||
class ProtocolError(Exception):
|
class ProtocolError(Exception):
|
||||||
""" The base class for all exceptions in this package """
|
""" The base class for all exceptions in this package """
|
||||||
@ -91,7 +95,7 @@ class DeviceBusy(ProtocolError):
|
|||||||
def __init__(self, uerr=""):
|
def __init__(self, uerr=""):
|
||||||
ProtocolError.__init__(
|
ProtocolError.__init__(
|
||||||
self, "Device is in use by another application:"
|
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):
|
def __str__(self):
|
||||||
if self.query and self.response:
|
if self.query and self.response:
|
||||||
return "Got unexpected response:\n" + \
|
return "Got unexpected response:\n" + \
|
||||||
"query:\n"+str(self.query.query)+"\n"+\
|
"query:\n"+unicode_type(self.query.query)+"\n"+\
|
||||||
"expected:\n"+str(self.query.response)+"\n" +\
|
"expected:\n"+unicode_type(self.query.response)+"\n" +\
|
||||||
"actual:\n"+str(self.response)
|
"actual:\n"+unicode_type(self.response)
|
||||||
if self.desc:
|
if self.desc:
|
||||||
return self.desc
|
return self.desc
|
||||||
return "Unknown control error occurred"
|
return "Unknown control error occurred"
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
import os
|
import os
|
||||||
@ -423,7 +425,7 @@ class DevicePlugin(Plugin):
|
|||||||
:meth`books(oncard='cardb')`).
|
:meth`books(oncard='cardb')`).
|
||||||
|
|
||||||
'''
|
'''
|
||||||
raise NotImplementedError
|
raise NotImplementedError()
|
||||||
|
|
||||||
def delete_books(self, paths, end_session=True):
|
def delete_books(self, paths, end_session=True):
|
||||||
'''
|
'''
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
__license__ = 'GPL 3'
|
__license__ = 'GPL 3'
|
||||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
@ -21,4 +21,3 @@ def mime_type_ext(ext):
|
|||||||
|
|
||||||
def mime_type_path(path):
|
def mime_type_path(path):
|
||||||
return _mt(path)
|
return _mt(path)
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# 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'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from __future__ import print_function
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
'''
|
'''
|
||||||
@ -107,7 +107,7 @@ class LinuxScanner(object):
|
|||||||
self.ok = os.path.exists(self.base)
|
self.ok = os.path.exists(self.base)
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
ans = set([])
|
ans = set()
|
||||||
if not self.ok:
|
if not self.ok:
|
||||||
raise RuntimeError('DeviceScanner requires the /sys filesystem to work.')
|
raise RuntimeError('DeviceScanner requires the /sys filesystem to work.')
|
||||||
|
|
||||||
@ -128,41 +128,41 @@ class LinuxScanner(object):
|
|||||||
# Ignore USB HUBs
|
# Ignore USB HUBs
|
||||||
if read(os.path.join(base, 'bDeviceClass')) == b'09':
|
if read(os.path.join(base, 'bDeviceClass')) == b'09':
|
||||||
continue
|
continue
|
||||||
except:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
dev.append(int(b'0x'+read(ven), 16))
|
dev.append(int(b'0x'+read(ven), 16))
|
||||||
except:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
dev.append(int(b'0x'+read(prod), 16))
|
dev.append(int(b'0x'+read(prod), 16))
|
||||||
except:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
dev.append(int(b'0x'+read(bcd), 16))
|
dev.append(int(b'0x'+read(bcd), 16))
|
||||||
except:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
dev.append(read(man).decode('utf-8'))
|
dev.append(read(man).decode('utf-8'))
|
||||||
except:
|
except Exception:
|
||||||
dev.append(u'')
|
dev.append(u'')
|
||||||
try:
|
try:
|
||||||
dev.append(read(prod_string).decode('utf-8'))
|
dev.append(read(prod_string).decode('utf-8'))
|
||||||
except:
|
except Exception:
|
||||||
dev.append(u'')
|
dev.append(u'')
|
||||||
try:
|
try:
|
||||||
dev.append(read(serial).decode('utf-8'))
|
dev.append(read(serial).decode('utf-8'))
|
||||||
except:
|
except Exception:
|
||||||
dev.append(u'')
|
dev.append(u'')
|
||||||
|
|
||||||
dev = USBDevice(*dev)
|
dev = USBDevice(*dev)
|
||||||
try:
|
try:
|
||||||
dev.busnum = int(read(os.path.join(base, 'busnum')))
|
dev.busnum = int(read(os.path.join(base, 'busnum')))
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
dev.devnum = int(read(os.path.join(base, 'devnum')))
|
dev.devnum = int(read(os.path.join(base, 'devnum')))
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
ans.add(dev)
|
ans.add(dev)
|
||||||
return ans
|
return ans
|
||||||
|
@ -1,23 +1,26 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# 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'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import os, re
|
import os, re
|
||||||
|
|
||||||
from polyglot.builtins import unicode_type
|
from polyglot.builtins import unicode_type, as_bytes, as_unicode
|
||||||
|
|
||||||
|
|
||||||
def node_mountpoint(node):
|
def node_mountpoint(node):
|
||||||
|
|
||||||
def de_mangle(raw):
|
if isinstance(node, unicode_type):
|
||||||
return raw.replace('\\040', ' ').replace('\\011', '\t').replace('\\012',
|
node = node.encode('utf-8')
|
||||||
'\n').replace('\\0134', '\\')
|
|
||||||
|
|
||||||
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()
|
line = line.split()
|
||||||
if line[0] == node:
|
if line[0] == node:
|
||||||
return de_mangle(line[1])
|
return de_mangle(line[1])
|
||||||
@ -53,9 +56,9 @@ class UDisks(object):
|
|||||||
return unicode_type(d.FilesystemMount('',
|
return unicode_type(d.FilesystemMount('',
|
||||||
['auth_no_user_interaction', 'rw', 'noexec', 'nosuid',
|
['auth_no_user_interaction', 'rw', 'noexec', 'nosuid',
|
||||||
'nodev', 'uid=%d'%os.geteuid(), 'gid=%d'%os.getegid()]))
|
'nodev', 'uid=%d'%os.geteuid(), 'gid=%d'%os.getegid()]))
|
||||||
except:
|
except Exception:
|
||||||
# May be already mounted, check
|
# May be already mounted, check
|
||||||
mp = node_mountpoint(str(device_node_path))
|
mp = node_mountpoint(unicode_type(device_node_path))
|
||||||
if mp is None:
|
if mp is None:
|
||||||
raise
|
raise
|
||||||
return mp
|
return mp
|
||||||
@ -103,8 +106,8 @@ class UDisks2(object):
|
|||||||
try:
|
try:
|
||||||
device = bd.Get(self.BLOCK, 'Device',
|
device = bd.Get(self.BLOCK, 'Device',
|
||||||
dbus_interface='org.freedesktop.DBus.Properties')
|
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:
|
except Exception:
|
||||||
device = None
|
device = None
|
||||||
|
|
||||||
if device == device_node_path:
|
if device == device_node_path:
|
||||||
@ -120,7 +123,7 @@ class UDisks2(object):
|
|||||||
try:
|
try:
|
||||||
device = bd.Get(self.BLOCK, 'Device',
|
device = bd.Get(self.BLOCK, 'Device',
|
||||||
dbus_interface='org.freedesktop.DBus.Properties')
|
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:
|
except:
|
||||||
device = None
|
device = None
|
||||||
if device == device_node_path:
|
if device == device_node_path:
|
||||||
@ -133,15 +136,15 @@ class UDisks2(object):
|
|||||||
mount_options = ['rw', 'noexec', 'nosuid',
|
mount_options = ['rw', 'noexec', 'nosuid',
|
||||||
'nodev', 'uid=%d'%os.geteuid(), 'gid=%d'%os.getegid()]
|
'nodev', 'uid=%d'%os.geteuid(), 'gid=%d'%os.getegid()]
|
||||||
try:
|
try:
|
||||||
return unicode_type(d.Mount(
|
return as_unicode(d.Mount(
|
||||||
{
|
{
|
||||||
'auth.no_user_interaction':True,
|
'auth.no_user_interaction':True,
|
||||||
'options':','.join(mount_options)
|
'options':','.join(mount_options)
|
||||||
},
|
},
|
||||||
dbus_interface=self.FILESYSTEM))
|
dbus_interface=self.FILESYSTEM))
|
||||||
except:
|
except Exception:
|
||||||
# May be already mounted, check
|
# May be already mounted, check
|
||||||
mp = node_mountpoint(str(device_node_path))
|
mp = node_mountpoint(unicode_type(device_node_path))
|
||||||
if mp is None:
|
if mp is None:
|
||||||
raise
|
raise
|
||||||
return mp
|
return mp
|
||||||
|
@ -100,7 +100,7 @@ def create_upload_path(mdata, fname, template, sanitize,
|
|||||||
opts = config().parse()
|
opts = config().parse()
|
||||||
if not isinstance(template, unicode_type):
|
if not isinstance(template, unicode_type):
|
||||||
template = template.decode('utf-8')
|
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)
|
id_ = mdata.get('id', fname)
|
||||||
extra_components = get_components(template, mdata, id_,
|
extra_components = get_components(template, mdata, id_,
|
||||||
timefmt=opts.send_timefmt, length=maxlen-len(app_id)-1,
|
timefmt=opts.send_timefmt, length=maxlen-len(app_id)-1,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user