py3: Replace use of StringIO

This commit is contained in:
Kovid Goyal 2019-04-01 18:34:50 +05:30
parent e072941c88
commit a754bf46f1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 14 additions and 12 deletions

View File

@ -7,14 +7,15 @@ Provides a command-line interface to ebook devices.
For usage information run the script. For usage information run the script.
""" """
import StringIO, sys, time, os import sys, time, os
from optparse import OptionParser from optparse import OptionParser
from calibre import __version__, __appname__, human_readable, fsync from calibre import __version__, __appname__, human_readable, fsync, prints
from calibre.devices.errors import ArgumentError, DeviceError, DeviceLocked 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.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
@ -127,13 +128,13 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0):
c += 1 c += 1
return rowwidths return rowwidths
output = StringIO.StringIO() output = PolyglotBytesIO()
if path.endswith("/") and len(path) > 1: if path.endswith("/") and len(path) > 1:
path = path[:-1] path = path[:-1]
dirs = dev.list(path, recurse) dirs = dev.list(path, recurse)
for dir in dirs: for dir in dirs:
if recurse: if recurse:
print(dir[0] + ":", file=output) prints(dir[0] + ":", file=output)
lsoutput, lscoloutput = [], [] lsoutput, lscoloutput = [], []
files = dir[1] files = dir[1]
maxlen = 0 maxlen = 0
@ -154,7 +155,7 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0):
size = str(file.size) size = str(file.size)
if human_readable_size: if human_readable_size:
size = file.human_readable_size size = file.human_readable_size
print(file.mode_string, ("%"+str(maxlen)+"s")%size, file.modification_time, name, file=output) prints(file.mode_string, ("%"+str(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):
@ -176,10 +177,10 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0):
for r in range(len(trytable)): for r in range(len(trytable)):
for c in range(len(trytable[r])): for c in range(len(trytable[r])):
padding = rowwidths[c] - len(trytable[r][c]) padding = rowwidths[c] - len(trytable[r][c])
print(trytablecol[r][c], "".ljust(padding), end=' ', file=output) prints(trytablecol[r][c], "".ljust(padding), end=' ', file=output)
print(file=output) prints(file=output)
print(file=output) prints(file=output)
listing = output.getvalue().rstrip()+ "\n" listing = output.getvalue().rstrip().decode('utf-8') + "\n"
output.close() output.close()
return listing return listing

View File

@ -10,7 +10,7 @@ __docformat__ = 'restructuredtext en'
import os import os
import re import re
import StringIO import io
from copy import deepcopy from copy import deepcopy
from calibre import my_unichr, prepare_string_for_xml from calibre import my_unichr, prepare_string_for_xml
@ -558,8 +558,9 @@ class PML_HTMLizer(object):
else: else:
indent_state['et'] = False indent_state['et'] = False
# Must use StringIO, cStringIO does not support unicode if isinstance(line, bytes):
line = StringIO.StringIO(line) line = line.decode('utf-8')
line = io.StringIO(line)
parsed.append(self.start_line()) parsed.append(self.start_line())
c = line.read(1) c = line.read(1)