mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Support IPython >=0.11
This commit is contained in:
parent
e2d051cfc0
commit
fa6f3c1502
@ -31,7 +31,7 @@ if False:
|
|||||||
# Prevent pyflakes from complaining
|
# Prevent pyflakes from complaining
|
||||||
winutil, winutilerror, __appname__, islinux, __version__
|
winutil, winutilerror, __appname__, islinux, __version__
|
||||||
fcntl, win32event, isfrozen, __author__
|
fcntl, win32event, isfrozen, __author__
|
||||||
winerror, win32api, isbsd
|
winerror, win32api, isbsd, config_dir
|
||||||
|
|
||||||
_mt_inited = False
|
_mt_inited = False
|
||||||
def _init_mimetypes():
|
def _init_mimetypes():
|
||||||
@ -699,69 +699,6 @@ if isosx:
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
def ipython(user_ns=None):
|
def ipython(user_ns=None):
|
||||||
old_argv = sys.argv
|
from calibre.utils.ipython import ipython
|
||||||
sys.argv = ['ipython']
|
ipython(user_ns=user_ns)
|
||||||
if user_ns is None:
|
|
||||||
user_ns = locals()
|
|
||||||
ipydir = os.path.join(config_dir, ('_' if iswindows else '.')+'ipython')
|
|
||||||
os.environ['IPYTHONDIR'] = ipydir
|
|
||||||
if not os.path.exists(ipydir):
|
|
||||||
os.makedirs(ipydir)
|
|
||||||
for x in ('', '.ini'):
|
|
||||||
rc = os.path.join(ipydir, 'ipythonrc'+x)
|
|
||||||
if not os.path.exists(rc):
|
|
||||||
open(rc, 'wb').write(' ')
|
|
||||||
UC = '''
|
|
||||||
import IPython.ipapi
|
|
||||||
ip = IPython.ipapi.get()
|
|
||||||
|
|
||||||
# You probably want to uncomment this if you did %upgrade -nolegacy
|
|
||||||
import ipy_defaults
|
|
||||||
|
|
||||||
import os, re, sys
|
|
||||||
|
|
||||||
def main():
|
|
||||||
# Handy tab-completers for %cd, %run, import etc.
|
|
||||||
# Try commenting this out if you have completion problems/slowness
|
|
||||||
import ipy_stock_completers
|
|
||||||
|
|
||||||
# uncomment if you want to get ipython -p sh behaviour
|
|
||||||
# without having to use command line switches
|
|
||||||
|
|
||||||
import ipy_profile_sh
|
|
||||||
|
|
||||||
|
|
||||||
# Configure your favourite editor?
|
|
||||||
# Good idea e.g. for %edit os.path.isfile
|
|
||||||
|
|
||||||
import ipy_editors
|
|
||||||
|
|
||||||
# Choose one of these:
|
|
||||||
|
|
||||||
#ipy_editors.scite()
|
|
||||||
#ipy_editors.scite('c:/opt/scite/scite.exe')
|
|
||||||
#ipy_editors.komodo()
|
|
||||||
#ipy_editors.idle()
|
|
||||||
# ... or many others, try 'ipy_editors??' after import to see them
|
|
||||||
|
|
||||||
# Or roll your own:
|
|
||||||
#ipy_editors.install_editor("c:/opt/jed +$line $file")
|
|
||||||
|
|
||||||
ipy_editors.kate()
|
|
||||||
|
|
||||||
o = ip.options
|
|
||||||
# An example on how to set options
|
|
||||||
#o.autocall = 1
|
|
||||||
o.system_verbose = 0
|
|
||||||
o.confirm_exit = 0
|
|
||||||
|
|
||||||
main()
|
|
||||||
'''
|
|
||||||
uc = os.path.join(ipydir, 'ipy_user_conf.py')
|
|
||||||
if not os.path.exists(uc):
|
|
||||||
open(uc, 'wb').write(UC)
|
|
||||||
from IPython.Shell import IPShellEmbed
|
|
||||||
ipshell = IPShellEmbed(user_ns=user_ns)
|
|
||||||
ipshell()
|
|
||||||
sys.argv = old_argv
|
|
||||||
|
|
||||||
|
@ -153,3 +153,12 @@ else:
|
|||||||
atexit.register(cleanup_cdir)
|
atexit.register(cleanup_cdir)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
def get_version():
|
||||||
|
'''Return version string that indicates if we are running in a dev env'''
|
||||||
|
dv = os.environ.get('CALIBRE_DEVELOP_FROM', None)
|
||||||
|
v = __version__
|
||||||
|
if getattr(sys, 'frozen', False) and dv and os.path.abspath(dv) in sys.path:
|
||||||
|
v += '*'
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,14 +5,14 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import functools, sys, os
|
import functools
|
||||||
|
|
||||||
from PyQt4.Qt import Qt, QStackedWidget, QMenu, \
|
from PyQt4.Qt import Qt, QStackedWidget, QMenu, \
|
||||||
QSize, QSizePolicy, QStatusBar, QLabel, QFont
|
QSize, QSizePolicy, QStatusBar, QLabel, QFont
|
||||||
|
|
||||||
from calibre.utils.config import prefs
|
from calibre.utils.config import prefs
|
||||||
from calibre.constants import isosx, __appname__, preferred_encoding, \
|
from calibre.constants import (isosx, __appname__, preferred_encoding,
|
||||||
__version__
|
get_version)
|
||||||
from calibre.gui2 import config, is_widescreen, gprefs
|
from calibre.gui2 import config, is_widescreen, gprefs
|
||||||
from calibre.gui2.library.views import BooksView, DeviceBooksView
|
from calibre.gui2.library.views import BooksView, DeviceBooksView
|
||||||
from calibre.gui2.widgets import Splitter
|
from calibre.gui2.widgets import Splitter
|
||||||
@ -187,11 +187,7 @@ class StatusBar(QStatusBar): # {{{
|
|||||||
self.clearMessage()
|
self.clearMessage()
|
||||||
|
|
||||||
def get_version(self):
|
def get_version(self):
|
||||||
dv = os.environ.get('CALIBRE_DEVELOP_FROM', None)
|
return get_version()
|
||||||
v = __version__
|
|
||||||
if getattr(sys, 'frozen', False) and dv and os.path.abspath(dv) in sys.path:
|
|
||||||
v += '*'
|
|
||||||
return v
|
|
||||||
|
|
||||||
def show_message(self, msg, timeout=0):
|
def show_message(self, msg, timeout=0):
|
||||||
self.showMessage(msg, timeout)
|
self.showMessage(msg, timeout)
|
||||||
|
113
src/calibre/utils/ipython.py
Normal file
113
src/calibre/utils/ipython.py
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
|
from __future__ import (unicode_literals, division, absolute_import,
|
||||||
|
print_function)
|
||||||
|
|
||||||
|
__license__ = 'GPL v3'
|
||||||
|
__copyright__ = '2012, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
|
import sys, os
|
||||||
|
from calibre.constants import iswindows, config_dir, get_version
|
||||||
|
|
||||||
|
ipydir = os.path.join(config_dir, ('_' if iswindows else '.')+'ipython')
|
||||||
|
|
||||||
|
def old_ipython(user_ns=None): # {{{
|
||||||
|
old_argv = sys.argv
|
||||||
|
sys.argv = ['ipython']
|
||||||
|
if user_ns is None:
|
||||||
|
user_ns = locals()
|
||||||
|
os.environ['IPYTHONDIR'] = ipydir
|
||||||
|
if not os.path.exists(ipydir):
|
||||||
|
os.makedirs(ipydir)
|
||||||
|
for x in ('', '.ini'):
|
||||||
|
rc = os.path.join(ipydir, 'ipythonrc'+x)
|
||||||
|
if not os.path.exists(rc):
|
||||||
|
open(rc, 'wb').write(' ')
|
||||||
|
UC = '''
|
||||||
|
import IPython.ipapi
|
||||||
|
ip = IPython.ipapi.get()
|
||||||
|
|
||||||
|
# You probably want to uncomment this if you did %upgrade -nolegacy
|
||||||
|
import ipy_defaults
|
||||||
|
|
||||||
|
import os, re, sys
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# Handy tab-completers for %cd, %run, import etc.
|
||||||
|
# Try commenting this out if you have completion problems/slowness
|
||||||
|
import ipy_stock_completers
|
||||||
|
|
||||||
|
# uncomment if you want to get ipython -p sh behaviour
|
||||||
|
# without having to use command line switches
|
||||||
|
|
||||||
|
import ipy_profile_sh
|
||||||
|
|
||||||
|
|
||||||
|
# Configure your favourite editor?
|
||||||
|
# Good idea e.g. for %edit os.path.isfile
|
||||||
|
|
||||||
|
import ipy_editors
|
||||||
|
|
||||||
|
# Choose one of these:
|
||||||
|
|
||||||
|
#ipy_editors.scite()
|
||||||
|
#ipy_editors.scite('c:/opt/scite/scite.exe')
|
||||||
|
#ipy_editors.komodo()
|
||||||
|
#ipy_editors.idle()
|
||||||
|
# ... or many others, try 'ipy_editors??' after import to see them
|
||||||
|
|
||||||
|
# Or roll your own:
|
||||||
|
#ipy_editors.install_editor("c:/opt/jed +$line $file")
|
||||||
|
|
||||||
|
ipy_editors.kate()
|
||||||
|
|
||||||
|
o = ip.options
|
||||||
|
# An example on how to set options
|
||||||
|
#o.autocall = 1
|
||||||
|
o.system_verbose = 0
|
||||||
|
o.confirm_exit = 0
|
||||||
|
|
||||||
|
main()
|
||||||
|
'''
|
||||||
|
uc = os.path.join(ipydir, 'ipy_user_conf.py')
|
||||||
|
if not os.path.exists(uc):
|
||||||
|
open(uc, 'wb').write(UC)
|
||||||
|
from IPython.Shell import IPShellEmbed
|
||||||
|
ipshell = IPShellEmbed(user_ns=user_ns)
|
||||||
|
ipshell()
|
||||||
|
sys.argv = old_argv
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
def ipython(user_ns=None):
|
||||||
|
try:
|
||||||
|
import IPython
|
||||||
|
from IPython.config.loader import Config
|
||||||
|
except ImportError:
|
||||||
|
return old_ipython(user_ns=user_ns)
|
||||||
|
if not user_ns:
|
||||||
|
user_ns = {}
|
||||||
|
c = Config()
|
||||||
|
c.InteractiveShellApp.exec_lines = [
|
||||||
|
'from __future__ import division, absolute_import, unicode_literals, print_function',
|
||||||
|
]
|
||||||
|
c.TerminalInteractiveShell.confirm_exit = False
|
||||||
|
c.PromptManager.in_template = (r'{color.LightGreen}calibre '
|
||||||
|
'{color.LightBlue}[{color.LightCyan}%s{color.LightBlue}]'
|
||||||
|
r'{color.Green}|\#> '%get_version())
|
||||||
|
c.PromptManager.in2_template = r'{color.Green}|{color.LightGreen}\D{color.Green}> '
|
||||||
|
c.PromptManager.out_template = r'<\#> '
|
||||||
|
c.TerminalInteractiveShell.banner1 = ('Welcome to the interactive calibre'
|
||||||
|
' shell!\n\n')
|
||||||
|
c.PromptManager.justify = True
|
||||||
|
c.TerminalIPythonApp.ipython_dir = ipydir
|
||||||
|
os.environ['IPYTHONDIR'] = ipydir
|
||||||
|
|
||||||
|
c.InteractiveShell.separate_in = ''
|
||||||
|
c.InteractiveShell.separate_out = ''
|
||||||
|
c.InteractiveShell.separate_out2 = ''
|
||||||
|
|
||||||
|
c.PrefilterManager.multi_line_specials = True
|
||||||
|
|
||||||
|
IPython.embed(config=c, user_ns=user_ns)
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user