py3: port use of raw_input

This commit is contained in:
Kovid Goyal 2019-03-26 14:45:36 +05:30
parent 40d631f39a
commit 1fed6604e0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
9 changed files with 56 additions and 22 deletions

View File

@ -4,8 +4,10 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
from calibre import prints
from calibre.db.legacy import LibraryDatabase
from polyglot.builtins import raw_input
readonly = False
version = 0 # change this if you change signature of implementation()
@ -37,9 +39,16 @@ columns with the custom_columns command.
return parser
def input_unicode(prompt):
ans = raw_input(prompt)
if isinstance(ans, bytes):
ans = ans.decode(sys.stdin.encoding)
return ans
def do_remove_custom_column(db, label, force):
if not force:
q = raw_input(
q = input_unicode(
_('You will lose all data in the column: %s.'
' Are you sure (y/n)? ') % label
)

View File

@ -11,7 +11,7 @@ import sys, os, functools
from calibre.utils.config import OptionParser
from calibre.constants import iswindows
from calibre import prints
from polyglot.builtins import exec_path
from polyglot.builtins import exec_path, raw_input
def get_debug_executable():

View File

@ -11,6 +11,7 @@
# #
#########################################################################
import sys, os
from polyglot.builtins import raw_input
# , codecs

View File

@ -11,7 +11,7 @@ import pdb, socket, inspect, sys, select, os, atexit, time
from calibre import prints
from calibre.utils.ipc import eintr_retry_call
from calibre.constants import cache_dir
from polyglot.builtins import range
from polyglot.builtins import range, raw_input
PROMPT = b'(debug) '
QUESTION = b'\x00\x01\x02'
@ -121,6 +121,8 @@ def cli(port=4444):
p = pdb.Pdb()
readline.set_completer(p.complete)
readline.parse_and_bind("tab: complete")
stdin = getattr(sys.stdin, 'buffer', sys.stdin)
stdout = getattr(sys.stdout, 'buffer', sys.stdout)
try:
while True:
@ -133,15 +135,19 @@ def cli(port=4444):
recvd = recvd[:-len(PROMPT)]
if recvd.startswith(QUESTION):
recvd = recvd[len(QUESTION):]
sys.stdout.write(recvd)
raw = sys.stdin.readline() or b'n'
stdout.write(recvd)
raw = stdin.readline() or b'n'
else:
sys.stdout.write(recvd)
stdout.write(recvd)
raw = b''
try:
raw = raw_input(PROMPT) + b'\n'
raw = raw_input(PROMPT.decode('utf-8'))
except (EOFError, KeyboardInterrupt):
pass
else:
if not isinstance(raw, bytes):
raw = raw.encode('utf-8')
raw += b'\n'
if not raw:
raw = b'quit\n'
eintr_retry_call(sock.send, raw)

View File

@ -9,7 +9,7 @@ from functools import partial
from calibre import prints
from calibre.constants import preferred_encoding
from polyglot.builtins import iteritems
from polyglot.builtins import iteritems, raw_input
# Manage users CLI {{{
@ -21,7 +21,10 @@ def manage_users_cli(path=None):
def get_input(prompt):
prints(prompt, end=' ')
return raw_input().decode(enc)
ans = raw_input()
if isinstance(ans, bytes):
ans = ans.decode(enc)
return ans
def choice(
question=_('What do you want to do?'), choices=(), default=None, banner=''):

View File

@ -9,11 +9,11 @@ from binascii import hexlify
from collections import Counter
from calibre import prints
from calibre.constants import config_dir, iswindows, filesystem_encoding
from calibre.constants import config_dir, iswindows
from calibre.utils.config_base import prefs, StringConfig, create_global_prefs
from calibre.utils.config import JSONConfig
from calibre.utils.filenames import samefile
from polyglot.builtins import iteritems
from polyglot.builtins import iteritems, raw_input
# Export {{{
@ -386,6 +386,13 @@ def cli_report(*args, **kw):
pass
def input_unicode(prompt):
ans = raw_input(prompt)
if isinstance(ans, bytes):
ans = ans.decode(sys.stdin.encoding)
return ans
def run_exporter(export_dir=None, args=None):
if args:
if len(args) < 2:
@ -407,9 +414,8 @@ def run_exporter(export_dir=None, args=None):
export(export_dir, progress1=cli_report, progress2=cli_report, library_paths=libraries)
return
export_dir = export_dir or raw_input(
'Enter path to an empty folder (all exported data will be saved inside it): ').decode(
filesystem_encoding).rstrip('\r')
export_dir = export_dir or input_unicode(
'Enter path to an empty folder (all exported data will be saved inside it): ').rstrip('\r')
if not os.path.exists(export_dir):
os.makedirs(export_dir)
if not os.path.isdir(export_dir):
@ -418,7 +424,7 @@ def run_exporter(export_dir=None, args=None):
raise SystemExit('%s is not empty' % export_dir)
library_paths = {}
for lpath, lus in iteritems(all_known_libraries()):
if raw_input('Export the library %s [y/n]: ' % lpath).strip().lower() == b'y':
if input_unicode('Export the library %s [y/n]: ' % lpath).strip().lower() == 'y':
library_paths[lpath] = lus
if library_paths:
export(export_dir, progress1=cli_report, progress2=cli_report, library_paths=library_paths)
@ -427,7 +433,7 @@ def run_exporter(export_dir=None, args=None):
def run_importer():
export_dir = raw_input('Enter path to folder containing previously exported data: ').decode(filesystem_encoding).rstrip('\r')
export_dir = input_unicode('Enter path to folder containing previously exported data: ').rstrip('\r')
if not os.path.isdir(export_dir):
raise SystemExit('%s is not a folder' % export_dir)
try:
@ -435,7 +441,7 @@ def run_importer():
except ValueError as err:
raise SystemExit(err.message)
import_dir = raw_input('Enter path to an empty folder (all libraries will be created inside this folder): ').decode(filesystem_encoding).rstrip('\r')
import_dir = input_unicode('Enter path to an empty folder (all libraries will be created inside this folder): ').rstrip('\r')
if not os.path.exists(import_dir):
os.makedirs(import_dir)
if not os.path.isdir(import_dir):

View File

@ -16,7 +16,7 @@ from itertools import islice
from calibre import detect_ncpus as cpu_count, as_unicode
from calibre.constants import plugins, filesystem_encoding
from calibre.utils.icu import primary_sort_key, primary_find, primary_collator
from polyglot.builtins import iteritems, itervalues, map, unicode_type, range, zip
from polyglot.builtins import iteritems, itervalues, map, unicode_type, range, zip, raw_input
from polyglot.queue import Queue
DEFAULT_LEVEL1 = '/'
@ -335,13 +335,20 @@ else:
return string[pos:pos + chs]
def input_unicode(prompt):
ans = raw_input(prompt)
if isinstance(ans, bytes):
ans = ans.decode(sys.stdin.encoding)
return ans
def main(basedir=None, query=None):
from calibre import prints
from calibre.utils.terminal import ColoredStream
if basedir is None:
try:
basedir = raw_input('Enter directory to scan [%s]: ' % os.getcwdu()
).decode(sys.stdin.encoding).strip() or os.getcwdu()
basedir = input_unicode('Enter directory to scan [%s]: ' % os.getcwdu()
).strip() or os.getcwdu()
except (EOFError, KeyboardInterrupt):
return
m = FilesystemMatcher(basedir)
@ -349,7 +356,7 @@ def main(basedir=None, query=None):
while True:
if query is None:
try:
query = raw_input('Enter query: ').decode(sys.stdin.encoding)
query = input_unicode('Enter query: ')
except (EOFError, KeyboardInterrupt):
break
if not query:

View File

@ -22,7 +22,7 @@ from calibre.utils.filenames import atomic_rename
from calibre.utils.terminal import ANSIStream
from duktape import Context, JSError, to_python
from lzma.xz import compress, decompress
from polyglot.builtins import itervalues, range, exec_path
from polyglot.builtins import itervalues, range, exec_path, raw_input
from polyglot.queue import Empty, Queue
COMPILER_PATH = 'rapydscript/compiler.js.xz'

View File

@ -36,6 +36,7 @@ if is_py3:
unicode_type = str
string_or_bytes = str, bytes
long_type = int
raw_input = input
def iteritems(d):
return iter(d.items())
@ -72,6 +73,7 @@ else:
string_or_bytes = unicode, bytes
long_type = long
exec_path = execfile
raw_input = builtins['raw_input']
def iteritems(d):
return d.iteritems()