X Tutup
Skip to content

Commit 00e7cd6

Browse files
committed
Make bpython-gtk embeddable.
This closes bpython#73.
1 parent 1af2e61 commit 00e7cd6

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

bpython/args.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def parse(args, extras=None):
2828
'A full description of what these options are for',
2929
[optparse.Option('-f', action='store_true', dest='f', help='Explode'),
3030
optparse.Option('-l', action='store_true', dest='l', help='Love')]))
31-
31+
3232
3333
Return a tuple of (config, options, exec_args) wherein "config" is the
3434
config object either parsed from a default/specified config file or default
@@ -56,16 +56,17 @@ def parse(args, extras=None):
5656
if extras is not None:
5757
extras_group = OptionGroup(parser, extras[0], extras[1])
5858
for option in extras[2]:
59-
extras_group.option_list.append(option)
59+
extras_group.add_option(option)
6060
parser.add_option_group(extras_group)
6161

6262
all_args = set(parser._short_opt.keys() + parser._long_opt.keys())
63-
if args and not all_args.intersection(args):
63+
if args and not all_args.intersection(arg.split('=')[0] for arg in args):
6464
# Just let Python handle this
6565
os.execv(sys.executable, [sys.executable] + args)
6666
else:
6767
# Split args in bpython args and args for the executed file
68-
real_args = list(takewhile(lambda arg: arg in all_args, args))
68+
real_args = list(takewhile(lambda arg: arg.split('=')[0] in all_args,
69+
args))
6970
exec_args = args[len(real_args):]
7071

7172
options, args = parser.parse_args(real_args)

bpython/gtk_.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929

3030
from __future__ import with_statement
3131
import inspect
32-
import sys
32+
import optparse
3333
import os
34+
import sys
3435
from locale import LC_ALL, getpreferredencoding, setlocale
3536

3637
import gobject
3738
import gtk
3839
import pango
3940

4041
from bpython import importcompletion, repl
41-
from bpython.config import Struct, loadini
4242
from bpython.formatter import theme_map
4343
import bpython.args
4444

@@ -623,40 +623,49 @@ def init_import_completion():
623623
def main(args=None):
624624

625625
setlocale(LC_ALL, '')
626-
config = Struct()
627626

628-
config, options, exec_args = bpython.args.parse(args)
629-
630-
loadini(config, '~/.bpython/config', options.config)
627+
gtk_options = ('gtk-specific options',
628+
"Options specific to bpython's Gtk+ front end",
629+
[optparse.Option('--socket-id', dest='socket_id',
630+
type='int', help='Embed bpython')])
631+
config, options, exec_args = bpython.args.parse(args, gtk_options)
631632

632633
interpreter = repl.Interpreter(None, getpreferredencoding())
633634
repl_widget = ReplWidget(interpreter, config)
634635

635-
# sys.stderr = repl_widget
636+
sys.stderr = repl_widget
636637
sys.stdout = repl_widget
637638

638-
gobject.idle_add(init_import_completion)
639+
# repl.startup()
639640

640-
window = gtk.Window()
641+
gobject.idle_add(init_import_completion)
641642

642-
# branding
643+
if not options.socket_id:
644+
print options.socket_id
645+
parent = gtk.Window()
646+
parent.connect('delete-event', lambda widget, event: gtk.main_quit())
643647

644-
# fix icon to be distributed and loaded from the correct path
645-
icon = gtk.gdk.pixbuf_new_from_file(os.path.join(os.path.dirname(__file__),
646-
'logo.png'))
648+
# branding
649+
# fix icon to be distributed and loaded from the correct path
650+
icon = gtk.gdk.pixbuf_new_from_file(os.path.join(os.path.dirname(__file__),
651+
'logo.png'))
647652

648-
window.set_title('bpython')
649-
window.set_icon(icon)
650-
window.resize(600, 300)
653+
parent.set_title('bpython')
654+
parent.set_icon(icon)
655+
parent.resize(600, 300)
656+
else:
657+
parent = gtk.Plug(options.socket_id)
658+
parent.connect('destroy', gtk.main_quit)
651659

652660
# read from config
653661

654662
sw = gtk.ScrolledWindow()
655663
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
656664
sw.add(repl_widget)
657-
window.add(sw)
658-
window.show_all()
659-
window.connect('delete-event', lambda widget, event: gtk.main_quit())
665+
parent.add(sw)
666+
parent.show_all()
667+
parent.connect('delete-event', lambda widget, event: gtk.main_quit())
668+
660669
gtk.main()
661670

662671

0 commit comments

Comments
 (0)
X Tutup