diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index 2ed2173d87..f17b909ea7 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -31,7 +31,7 @@ if False: # Prevent pyflakes from complaining winutil, winutilerror, __appname__, islinux, __version__ fcntl, win32event, isfrozen, __author__ - winerror, win32api, isbsd + winerror, win32api, isbsd, config_dir _mt_inited = False def _init_mimetypes(): @@ -699,69 +699,6 @@ if isosx: traceback.print_exc() def ipython(user_ns=None): - old_argv = sys.argv - sys.argv = ['ipython'] - 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 + from calibre.utils.ipython import ipython + ipython(user_ns=user_ns) diff --git a/src/calibre/constants.py b/src/calibre/constants.py index 0b50c7d2ac..2170db4587 100644 --- a/src/calibre/constants.py +++ b/src/calibre/constants.py @@ -153,3 +153,12 @@ else: 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 + + diff --git a/src/calibre/gui2/init.py b/src/calibre/gui2/init.py index cccf76e713..95e8c79382 100644 --- a/src/calibre/gui2/init.py +++ b/src/calibre/gui2/init.py @@ -5,14 +5,14 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import functools, sys, os +import functools from PyQt4.Qt import Qt, QStackedWidget, QMenu, \ QSize, QSizePolicy, QStatusBar, QLabel, QFont from calibre.utils.config import prefs -from calibre.constants import isosx, __appname__, preferred_encoding, \ - __version__ +from calibre.constants import (isosx, __appname__, preferred_encoding, + get_version) from calibre.gui2 import config, is_widescreen, gprefs from calibre.gui2.library.views import BooksView, DeviceBooksView from calibre.gui2.widgets import Splitter @@ -187,11 +187,7 @@ class StatusBar(QStatusBar): # {{{ self.clearMessage() def get_version(self): - 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 + return get_version() def show_message(self, msg, timeout=0): self.showMessage(msg, timeout) diff --git a/src/calibre/utils/ipython.py b/src/calibre/utils/ipython.py new file mode 100644 index 0000000000..d53a74a5c2 --- /dev/null +++ b/src/calibre/utils/ipython.py @@ -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 ' +__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) +