mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
py3: port use of raw_input
This commit is contained in:
parent
40d631f39a
commit
1fed6604e0
@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
|
import sys
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
from calibre.db.legacy import LibraryDatabase
|
from calibre.db.legacy import LibraryDatabase
|
||||||
|
from polyglot.builtins import raw_input
|
||||||
|
|
||||||
readonly = False
|
readonly = False
|
||||||
version = 0 # change this if you change signature of implementation()
|
version = 0 # change this if you change signature of implementation()
|
||||||
@ -37,9 +39,16 @@ columns with the custom_columns command.
|
|||||||
return parser
|
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):
|
def do_remove_custom_column(db, label, force):
|
||||||
if not force:
|
if not force:
|
||||||
q = raw_input(
|
q = input_unicode(
|
||||||
_('You will lose all data in the column: %s.'
|
_('You will lose all data in the column: %s.'
|
||||||
' Are you sure (y/n)? ') % label
|
' Are you sure (y/n)? ') % label
|
||||||
)
|
)
|
||||||
|
@ -11,7 +11,7 @@ import sys, os, functools
|
|||||||
from calibre.utils.config import OptionParser
|
from calibre.utils.config import OptionParser
|
||||||
from calibre.constants import iswindows
|
from calibre.constants import iswindows
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
from polyglot.builtins import exec_path
|
from polyglot.builtins import exec_path, raw_input
|
||||||
|
|
||||||
|
|
||||||
def get_debug_executable():
|
def get_debug_executable():
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
# #
|
# #
|
||||||
#########################################################################
|
#########################################################################
|
||||||
import sys, os
|
import sys, os
|
||||||
|
from polyglot.builtins import raw_input
|
||||||
# , codecs
|
# , codecs
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import pdb, socket, inspect, sys, select, os, atexit, time
|
|||||||
from calibre import prints
|
from calibre import prints
|
||||||
from calibre.utils.ipc import eintr_retry_call
|
from calibre.utils.ipc import eintr_retry_call
|
||||||
from calibre.constants import cache_dir
|
from calibre.constants import cache_dir
|
||||||
from polyglot.builtins import range
|
from polyglot.builtins import range, raw_input
|
||||||
|
|
||||||
PROMPT = b'(debug) '
|
PROMPT = b'(debug) '
|
||||||
QUESTION = b'\x00\x01\x02'
|
QUESTION = b'\x00\x01\x02'
|
||||||
@ -121,6 +121,8 @@ def cli(port=4444):
|
|||||||
p = pdb.Pdb()
|
p = pdb.Pdb()
|
||||||
readline.set_completer(p.complete)
|
readline.set_completer(p.complete)
|
||||||
readline.parse_and_bind("tab: complete")
|
readline.parse_and_bind("tab: complete")
|
||||||
|
stdin = getattr(sys.stdin, 'buffer', sys.stdin)
|
||||||
|
stdout = getattr(sys.stdout, 'buffer', sys.stdout)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@ -133,15 +135,19 @@ def cli(port=4444):
|
|||||||
recvd = recvd[:-len(PROMPT)]
|
recvd = recvd[:-len(PROMPT)]
|
||||||
if recvd.startswith(QUESTION):
|
if recvd.startswith(QUESTION):
|
||||||
recvd = recvd[len(QUESTION):]
|
recvd = recvd[len(QUESTION):]
|
||||||
sys.stdout.write(recvd)
|
stdout.write(recvd)
|
||||||
raw = sys.stdin.readline() or b'n'
|
raw = stdin.readline() or b'n'
|
||||||
else:
|
else:
|
||||||
sys.stdout.write(recvd)
|
stdout.write(recvd)
|
||||||
raw = b''
|
raw = b''
|
||||||
try:
|
try:
|
||||||
raw = raw_input(PROMPT) + b'\n'
|
raw = raw_input(PROMPT.decode('utf-8'))
|
||||||
except (EOFError, KeyboardInterrupt):
|
except (EOFError, KeyboardInterrupt):
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
|
if not isinstance(raw, bytes):
|
||||||
|
raw = raw.encode('utf-8')
|
||||||
|
raw += b'\n'
|
||||||
if not raw:
|
if not raw:
|
||||||
raw = b'quit\n'
|
raw = b'quit\n'
|
||||||
eintr_retry_call(sock.send, raw)
|
eintr_retry_call(sock.send, raw)
|
||||||
|
@ -9,7 +9,7 @@ from functools import partial
|
|||||||
|
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from polyglot.builtins import iteritems
|
from polyglot.builtins import iteritems, raw_input
|
||||||
|
|
||||||
# Manage users CLI {{{
|
# Manage users CLI {{{
|
||||||
|
|
||||||
@ -21,7 +21,10 @@ def manage_users_cli(path=None):
|
|||||||
|
|
||||||
def get_input(prompt):
|
def get_input(prompt):
|
||||||
prints(prompt, end=' ')
|
prints(prompt, end=' ')
|
||||||
return raw_input().decode(enc)
|
ans = raw_input()
|
||||||
|
if isinstance(ans, bytes):
|
||||||
|
ans = ans.decode(enc)
|
||||||
|
return ans
|
||||||
|
|
||||||
def choice(
|
def choice(
|
||||||
question=_('What do you want to do?'), choices=(), default=None, banner=''):
|
question=_('What do you want to do?'), choices=(), default=None, banner=''):
|
||||||
|
@ -9,11 +9,11 @@ from binascii import hexlify
|
|||||||
from collections import Counter
|
from collections import Counter
|
||||||
|
|
||||||
from calibre import prints
|
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_base import prefs, StringConfig, create_global_prefs
|
||||||
from calibre.utils.config import JSONConfig
|
from calibre.utils.config import JSONConfig
|
||||||
from calibre.utils.filenames import samefile
|
from calibre.utils.filenames import samefile
|
||||||
from polyglot.builtins import iteritems
|
from polyglot.builtins import iteritems, raw_input
|
||||||
|
|
||||||
|
|
||||||
# Export {{{
|
# Export {{{
|
||||||
@ -386,6 +386,13 @@ def cli_report(*args, **kw):
|
|||||||
pass
|
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):
|
def run_exporter(export_dir=None, args=None):
|
||||||
if args:
|
if args:
|
||||||
if len(args) < 2:
|
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)
|
export(export_dir, progress1=cli_report, progress2=cli_report, library_paths=libraries)
|
||||||
return
|
return
|
||||||
|
|
||||||
export_dir = export_dir or raw_input(
|
export_dir = export_dir or input_unicode(
|
||||||
'Enter path to an empty folder (all exported data will be saved inside it): ').decode(
|
'Enter path to an empty folder (all exported data will be saved inside it): ').rstrip('\r')
|
||||||
filesystem_encoding).rstrip('\r')
|
|
||||||
if not os.path.exists(export_dir):
|
if not os.path.exists(export_dir):
|
||||||
os.makedirs(export_dir)
|
os.makedirs(export_dir)
|
||||||
if not os.path.isdir(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)
|
raise SystemExit('%s is not empty' % export_dir)
|
||||||
library_paths = {}
|
library_paths = {}
|
||||||
for lpath, lus in iteritems(all_known_libraries()):
|
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
|
library_paths[lpath] = lus
|
||||||
if library_paths:
|
if library_paths:
|
||||||
export(export_dir, progress1=cli_report, progress2=cli_report, library_paths=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():
|
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):
|
if not os.path.isdir(export_dir):
|
||||||
raise SystemExit('%s is not a folder' % export_dir)
|
raise SystemExit('%s is not a folder' % export_dir)
|
||||||
try:
|
try:
|
||||||
@ -435,7 +441,7 @@ def run_importer():
|
|||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
raise SystemExit(err.message)
|
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):
|
if not os.path.exists(import_dir):
|
||||||
os.makedirs(import_dir)
|
os.makedirs(import_dir)
|
||||||
if not os.path.isdir(import_dir):
|
if not os.path.isdir(import_dir):
|
||||||
|
@ -16,7 +16,7 @@ from itertools import islice
|
|||||||
from calibre import detect_ncpus as cpu_count, as_unicode
|
from calibre import detect_ncpus as cpu_count, as_unicode
|
||||||
from calibre.constants import plugins, filesystem_encoding
|
from calibre.constants import plugins, filesystem_encoding
|
||||||
from calibre.utils.icu import primary_sort_key, primary_find, primary_collator
|
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
|
from polyglot.queue import Queue
|
||||||
|
|
||||||
DEFAULT_LEVEL1 = '/'
|
DEFAULT_LEVEL1 = '/'
|
||||||
@ -335,13 +335,20 @@ else:
|
|||||||
return string[pos:pos + chs]
|
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):
|
def main(basedir=None, query=None):
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
from calibre.utils.terminal import ColoredStream
|
from calibre.utils.terminal import ColoredStream
|
||||||
if basedir is None:
|
if basedir is None:
|
||||||
try:
|
try:
|
||||||
basedir = raw_input('Enter directory to scan [%s]: ' % os.getcwdu()
|
basedir = input_unicode('Enter directory to scan [%s]: ' % os.getcwdu()
|
||||||
).decode(sys.stdin.encoding).strip() or os.getcwdu()
|
).strip() or os.getcwdu()
|
||||||
except (EOFError, KeyboardInterrupt):
|
except (EOFError, KeyboardInterrupt):
|
||||||
return
|
return
|
||||||
m = FilesystemMatcher(basedir)
|
m = FilesystemMatcher(basedir)
|
||||||
@ -349,7 +356,7 @@ def main(basedir=None, query=None):
|
|||||||
while True:
|
while True:
|
||||||
if query is None:
|
if query is None:
|
||||||
try:
|
try:
|
||||||
query = raw_input('Enter query: ').decode(sys.stdin.encoding)
|
query = input_unicode('Enter query: ')
|
||||||
except (EOFError, KeyboardInterrupt):
|
except (EOFError, KeyboardInterrupt):
|
||||||
break
|
break
|
||||||
if not query:
|
if not query:
|
||||||
|
@ -22,7 +22,7 @@ from calibre.utils.filenames import atomic_rename
|
|||||||
from calibre.utils.terminal import ANSIStream
|
from calibre.utils.terminal import ANSIStream
|
||||||
from duktape import Context, JSError, to_python
|
from duktape import Context, JSError, to_python
|
||||||
from lzma.xz import compress, decompress
|
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
|
from polyglot.queue import Empty, Queue
|
||||||
|
|
||||||
COMPILER_PATH = 'rapydscript/compiler.js.xz'
|
COMPILER_PATH = 'rapydscript/compiler.js.xz'
|
||||||
|
@ -36,6 +36,7 @@ if is_py3:
|
|||||||
unicode_type = str
|
unicode_type = str
|
||||||
string_or_bytes = str, bytes
|
string_or_bytes = str, bytes
|
||||||
long_type = int
|
long_type = int
|
||||||
|
raw_input = input
|
||||||
|
|
||||||
def iteritems(d):
|
def iteritems(d):
|
||||||
return iter(d.items())
|
return iter(d.items())
|
||||||
@ -72,6 +73,7 @@ else:
|
|||||||
string_or_bytes = unicode, bytes
|
string_or_bytes = unicode, bytes
|
||||||
long_type = long
|
long_type = long
|
||||||
exec_path = execfile
|
exec_path = execfile
|
||||||
|
raw_input = builtins['raw_input']
|
||||||
|
|
||||||
def iteritems(d):
|
def iteritems(d):
|
||||||
return d.iteritems()
|
return d.iteritems()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user