Allow using calibre-debug to run scripts without using the -e option

This commit is contained in:
Kovid Goyal 2013-02-28 11:55:49 +05:30
parent 1e6e0fa16e
commit 30828dfed9

View File

@ -23,10 +23,9 @@ It also contains interfaces to various bits of calibre that do not have
dedicated command line tools, such as font subsetting, tweaking ebooks and so dedicated command line tools, such as font subsetting, tweaking ebooks and so
on. on.
''') ''')
parser.add_option('-c', '--command', help='Run python code.', default=None) parser.add_option('-c', '--command', help='Run python code.')
parser.add_option('-e', '--exec-file', default=None, help='Run the python code in file.') parser.add_option('-e', '--exec-file', help='Run the python code in file.')
parser.add_option('-f', '--subset-font', default=False, parser.add_option('-f', '--subset-font', help='Subset the specified font')
action='store_true', help='Subset the specified font')
parser.add_option('-d', '--debug-device-driver', default=False, action='store_true', parser.add_option('-d', '--debug-device-driver', default=False, action='store_true',
help='Debug the specified device driver.') help='Debug the specified device driver.')
parser.add_option('-g', '--gui', default=False, action='store_true', parser.add_option('-g', '--gui', default=False, action='store_true',
@ -174,30 +173,24 @@ def run_debug_gui(logpath):
from calibre.gui2.main import main from calibre.gui2.main import main
main(['__CALIBRE_GUI_DEBUG__', logpath]) main(['__CALIBRE_GUI_DEBUG__', logpath])
def run_script(path, args):
# Load all user defined plugins so the script can import from the
# calibre_plugins namespace
import calibre.customize.ui as dummy
dummy
sys.argv = [path] + args
ef = os.path.abspath(path)
base = os.path.dirname(ef)
sys.path.insert(0, base)
g = globals()
g['__name__'] = '__main__'
g['__file__'] = ef
execfile(ef, g)
def main(args=sys.argv): def main(args=sys.argv):
from calibre.constants import debug from calibre.constants import debug
debug() debug()
if len(args) > 2 and args[1] in ('-e', '--exec-file'):
# Load all plugins user defined plugins so the script can import from the
# calibre_plugins namespace
import calibre.customize.ui as dummy
dummy
sys.argv = [args[2]] + args[3:]
ef = os.path.abspath(args[2])
base = os.path.dirname(ef)
sys.path.insert(0, base)
g = globals()
g['__name__'] = '__main__'
g['__file__'] = ef
execfile(ef, g)
return
if len(args) > 1 and args[1] in ('-f', '--subset-font'):
from calibre.utils.fonts.sfnt.subset import main
main(['subset-font']+args[2:])
return
opts, args = option_parser().parse_args(args) opts, args = option_parser().parse_args(args)
if opts.gui: if opts.gui:
@ -258,6 +251,13 @@ def main(args=sys.argv):
elif opts.shutdown_running_calibre: elif opts.shutdown_running_calibre:
from calibre.gui2.main import shutdown_other from calibre.gui2.main import shutdown_other
shutdown_other() shutdown_other()
elif opts.subset_font:
from calibre.utils.fonts.sfnt.subset import main
main(['subset-font']+[opts.subset_font]+args[1:])
elif opts.exec_file:
run_script(opts.exec_file, args[1:])
elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'py', 'recipe'}:
run_script(args[1], args[2:])
else: else:
from calibre import ipython from calibre import ipython
ipython() ipython()