mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
parent
d5a02c0ec7
commit
9f7c0f3911
@ -6,11 +6,12 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
import os, json, sys, errno, re
|
import os, json, sys, errno, re, atexit
|
||||||
from threading import local
|
from threading import local
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
from calibre.constants import cache_dir, iswindows
|
||||||
from calibre.utils.terminal import ANSIStream, colored
|
from calibre.utils.terminal import ANSIStream, colored
|
||||||
from calibre.constants import cache_dir
|
|
||||||
|
|
||||||
COMPILER_PATH = 'rapydscript/compiler.js'
|
COMPILER_PATH = 'rapydscript/compiler.js'
|
||||||
|
|
||||||
@ -172,11 +173,13 @@ class Repl(object):
|
|||||||
|
|
||||||
LINE_CONTINUATION_CHARS = r'\:'
|
LINE_CONTINUATION_CHARS = r'\:'
|
||||||
|
|
||||||
def __init__(self, ps1=colored('>>> ', fg='green'), ps2=colored('... ', fg='green'), show_js=False, libdir=None):
|
def __init__(self, ps1='>>> ', ps2='... ', show_js=False, libdir=None):
|
||||||
from duktape import Context, undefined, JSError, to_python
|
from duktape import Context, undefined, JSError, to_python
|
||||||
self.lines = []
|
self.lines = []
|
||||||
self.libdir = libdir
|
self.libdir = libdir
|
||||||
self.ps1, self.ps2 = ps1, ps2
|
self.ps1, self.ps2 = ps1, ps2
|
||||||
|
if not iswindows:
|
||||||
|
self.ps1, self.ps2 = colored(self.ps1, fg='green'), colored(self.ps2, fg='green')
|
||||||
self.ctx = Context()
|
self.ctx = Context()
|
||||||
self.ctx.g.show_js = show_js
|
self.ctx.g.show_js = show_js
|
||||||
self.undefined = undefined
|
self.undefined = undefined
|
||||||
@ -188,7 +191,7 @@ class Repl(object):
|
|||||||
self.readline = readline
|
self.readline = readline
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
self.output = ANSIStream(sys.stderr)
|
self.output = ANSIStream(sys.stdout)
|
||||||
|
|
||||||
def resetbuffer(self):
|
def resetbuffer(self):
|
||||||
self.lines = []
|
self.lines = []
|
||||||
@ -207,12 +210,15 @@ class Repl(object):
|
|||||||
self.prints(colored('Welcome to the RapydScript REPL! Press Ctrl+D to quit.\n'
|
self.prints(colored('Welcome to the RapydScript REPL! Press Ctrl+D to quit.\n'
|
||||||
'Use show_js = True to have the REPL print out the'
|
'Use show_js = True to have the REPL print out the'
|
||||||
' compiled javascript before executing it.\n', bold=True))
|
' compiled javascript before executing it.\n', bold=True))
|
||||||
history = os.path.join(cache_dir(), 'pyj-repl-history.txt')
|
if hasattr(self, 'readline'):
|
||||||
try:
|
history = os.path.join(cache_dir(), 'pyj-repl-history.txt')
|
||||||
self.readline.read_history_file(history)
|
self.readline.parse_and_bind("tab: complete")
|
||||||
except EnvironmentError as e:
|
try:
|
||||||
if e.errno != errno.ENOENT:
|
self.readline.read_history_file(history)
|
||||||
raise
|
except EnvironmentError as e:
|
||||||
|
if e.errno != errno.ENOENT:
|
||||||
|
raise
|
||||||
|
atexit.register(partial(self.readline.write_history_file, history))
|
||||||
more = False
|
more = False
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@ -224,8 +230,7 @@ class Repl(object):
|
|||||||
prompt += lw
|
prompt += lw
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.prints(prompt, end='')
|
line = raw_input(prompt).decode(self.enc)
|
||||||
line = raw_input().decode(self.enc)
|
|
||||||
except EOFError:
|
except EOFError:
|
||||||
self.prints()
|
self.prints()
|
||||||
break
|
break
|
||||||
@ -238,7 +243,6 @@ class Repl(object):
|
|||||||
self.prints("\nKeyboardInterrupt")
|
self.prints("\nKeyboardInterrupt")
|
||||||
self.resetbuffer()
|
self.resetbuffer()
|
||||||
more = False
|
more = False
|
||||||
self.readline.write_history_file(history)
|
|
||||||
|
|
||||||
def push(self, line):
|
def push(self, line):
|
||||||
self.lines.append(line)
|
self.lines.append(line)
|
||||||
@ -274,7 +278,7 @@ class Repl(object):
|
|||||||
if self.ctx.g.show_js:
|
if self.ctx.g.show_js:
|
||||||
self.prints(colored('Compiled Javascript:', fg='green'), js, sep='\n')
|
self.prints(colored('Compiled Javascript:', fg='green'), js, sep='\n')
|
||||||
try:
|
try:
|
||||||
result = self.ctx.eval(js)
|
result = self.ctx.eval(js, fname='line')
|
||||||
except self.JSError as e:
|
except self.JSError as e:
|
||||||
self.prints(e.message)
|
self.prints(e.message)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user