X Tutup
Skip to content

Commit 2d1370c

Browse files
committed
merged simon's something
2 parents 917471c + e12f926 commit 2d1370c

File tree

8 files changed

+143
-61
lines changed

8 files changed

+143
-61
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ include AUTHORS
33
include CHANGELOG
44
include LICENSE
55
include data/bpython
6+
include data/bpython-gtk
67
include data/bpython.desktop
78
include doc/*.[0-9]
89
include sample-config
910
include *.theme
11+
include bpython/logo.png

bpython/args.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
Module to handle command line argument parsing, for all front-ends.
3+
"""
4+
5+
import os
6+
import sys
7+
import code
8+
from optparse import OptionParser, OptionGroup
9+
from itertools import takewhile
10+
11+
from bpython import __version__
12+
from bpython.config import loadini, Struct, migrate_rc
13+
14+
def parse(args, extras=None):
15+
"""Receive an argument list - if None, use sys.argv - parse all args and
16+
take appropriate action. Also receive optional extra options: this should
17+
be a tuple of (title, description, options)
18+
title: The title for the option group
19+
description: A full description of the option group
20+
options: A list of optparse.Option objects to be added to the
21+
group
22+
23+
e.g.:
24+
25+
parse(['-i', '-m', 'foo.py'],
26+
('Front end-specific options',
27+
'A full description of what these options are for',
28+
[optparse.Option('-f', action='store_true', dest='f', help='Explode'),
29+
optparse.Option('-l', action='store_true', dest='l', help='Love')]))
30+
31+
32+
Return a tuple of (config, options, exec_args) wherein "config" is the
33+
config object either parsed from a default/specified config file or default
34+
config options, "options" is the parsed options from
35+
OptionParser.parse_args, and "exec_args" are the args (if any) to be parsed
36+
to the executed file (if any).
37+
"""
38+
if args is None:
39+
args = sys.argv[1:]
40+
41+
parser = OptionParser(usage='Usage: %prog [options] [file [args]]\n'
42+
'NOTE: If bpython sees an argument it does '
43+
'not know, execution falls back to the '
44+
'regular Python interpreter.')
45+
parser.add_option('--config', '-c', default='~/.bpython/config',
46+
help='use CONFIG instead of default config file')
47+
parser.add_option('--interactive', '-i', action='store_true',
48+
help='Drop to bpython shell after running file '
49+
'instead of exiting')
50+
parser.add_option('--quiet', '-q', action='store_true',
51+
help="Don't flush the output to stdout.")
52+
parser.add_option('--version', '-V', action='store_true',
53+
help='print version and exit')
54+
55+
if extras is not None:
56+
extras_group = OptionGroup(parser, extras[0], extras[1])
57+
for option in extras[2]:
58+
extras_group.option_list.append(option)
59+
parser.add_option_group(extras_group)
60+
61+
all_args = set(parser._short_opt.keys() + parser._long_opt.keys())
62+
if args and not all_args.intersection(args):
63+
# Just let Python handle this
64+
os.execv(sys.executable, [sys.executable] + args)
65+
else:
66+
# Split args in bpython args and args for the executed file
67+
real_args = list(takewhile(lambda arg: arg in all_args, args))
68+
exec_args = args[len(real_args):]
69+
70+
options, args = parser.parse_args(real_args)
71+
72+
if options.version:
73+
print 'bpython version', __version__,
74+
print 'on top of Python', sys.version.split()[0]
75+
print ('(C) 2008-2009 Bob Farrell, Andreas Stuehrk et al. '
76+
'See AUTHORS for detail.')
77+
raise SystemExit
78+
79+
if not (sys.stdin.isatty() and sys.stdout.isatty()):
80+
interpreter = code.InteractiveInterpreter()
81+
interpreter.runsource(sys.stdin.read())
82+
raise SystemExit
83+
84+
path = os.path.expanduser('~/.bpythonrc')
85+
# migrating old configuration file
86+
if os.path.isfile(path):
87+
migrate_rc(path)
88+
config = Struct()
89+
90+
loadini(config, options.config)
91+
92+
return config, options, exec_args
93+
94+
def exec_code(interpreter, args):
95+
"""
96+
Helper to execute code in a given interpreter. args should be a [faked]
97+
sys.argv
98+
"""
99+
with open(args[0], 'r') as sourcefile:
100+
code_obj = compile(sourcefile.read(), args[0], 'exec')
101+
old_argv, sys.argv = sys.argv, args
102+
interpreter.runcode(code_obj)
103+
sys.argv = old_argv

bpython/cli.py

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@
5757
from bpython import importcompletion
5858

5959
# This for config
60-
from bpython.config import Struct, loadini, migrate_rc
60+
from bpython.config import Struct, migrate_rc
6161

6262
# This for keys
6363
from bpython.keys import key_dispatch
6464

65-
from bpython import __version__
6665
from bpython.pager import page
6766
from bpython.repl import Interpreter, Repl
67+
import bpython.args
6868

6969

7070
def log(x):
@@ -1551,11 +1551,7 @@ def main_curses(scr, args, config, interactive=True, locals_=None):
15511551
repl.startup()
15521552

15531553
if args:
1554-
with open(args[0], 'r') as sourcefile:
1555-
code_obj = compile(sourcefile.read(), args[0], 'exec')
1556-
old_argv, sys.argv = sys.argv, args
1557-
interpreter.runcode(code_obj)
1558-
sys.argv = old_argv
1554+
bpython.args.exec_code(interpreter, args)
15591555
if not interactive:
15601556
curses.raw(False)
15611557
return repl.getstdout()
@@ -1580,53 +1576,9 @@ def main_curses(scr, args, config, interactive=True, locals_=None):
15801576
def main(args=None, locals_=None):
15811577
global stdscr
15821578

1583-
if args is None:
1584-
args = sys.argv[1:]
1585-
1586-
parser = OptionParser(usage='Usage: %prog [options] [file [args]]\n'
1587-
'NOTE: If bpython sees an argument it does '
1588-
'not know, execution falls back to the '
1589-
'regular Python interpreter.')
1590-
parser.add_option('--config', '-c', default='~/.bpython/config',
1591-
help='use CONFIG instead of default config file')
1592-
parser.add_option('--interactive', '-i', action='store_true',
1593-
help='Drop to bpython shell after running file '
1594-
'instead of exiting')
1595-
parser.add_option('--quiet', '-q', action='store_true',
1596-
help="Don't flush the output to stdout.")
1597-
parser.add_option('--version', '-V', action='store_true',
1598-
help='print version and exit')
1599-
1600-
all_args = set(parser._short_opt.keys() + parser._long_opt.keys())
1601-
if args and not all_args.intersection(args):
1602-
# Just let Python handle this
1603-
os.execv(sys.executable, [sys.executable] + args)
1604-
else:
1605-
# Split args in bpython args and args for the executed file
1606-
real_args = list(takewhile(lambda arg: arg in all_args, args))
1607-
exec_args = args[len(real_args):]
1608-
1609-
options, args = parser.parse_args(real_args)
1610-
1611-
if options.version:
1612-
print 'bpython version', __version__,
1613-
print 'on top of Python', sys.version.split()[0]
1614-
print '(C) 2008-2009 Bob Farrell et al. See AUTHORS for detail.'
1615-
return
1616-
1617-
if not (sys.stdin.isatty() and sys.stdout.isatty()):
1618-
interpreter = code.InteractiveInterpreter()
1619-
interpreter.runsource(sys.stdin.read())
1620-
return
1621-
16221579
setlocale(LC_ALL, '')
16231580

1624-
path = os.path.expanduser('~/.bpythonrc')
1625-
# migrating old configuration file
1626-
if os.path.isfile(path):
1627-
migrate_rc(path)
1628-
config = Struct()
1629-
loadini(config, options.config)
1581+
config, options, exec_args = bpython.args.parse(args)
16301582

16311583
# Save stdin, stdout and stderr for later restoration
16321584
orig_stdin = sys.stdin

bpython/gtk_.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from __future__ import with_statement
3131
import inspect
3232
import sys
33+
import os
3334
from locale import LC_ALL, getpreferredencoding, setlocale
3435

3536
import gobject
@@ -39,10 +40,11 @@
3940
from bpython import importcompletion, repl
4041
from bpython.config import Struct, loadini
4142
from bpython.formatter import theme_map
43+
import bpython.args
4244

4345

4446
_COLORS = dict(b='blue', c='cyan', g='green', m='magenta', r='red',
45-
w='white', y='yellow', k='black')
47+
w='white', y='yellow', k='black', d='black')
4648

4749

4850
class ArgspecFormatter(object):
@@ -129,7 +131,7 @@ def __nonzero__(self):
129131

130132
class SuggestionWindow(gtk.Window):
131133
"""
132-
The window where suggestiosn are displayed.
134+
The window where suggestions are displayed.
133135
"""
134136
__gsignals__ = dict(expose_event=None,
135137
selection_changed=(gobject.SIGNAL_RUN_LAST, None,
@@ -263,6 +265,9 @@ def __init__(self, interpreter, config):
263265
self.list_win.connect('selection-changed',
264266
self.on_suggestion_selection_changed)
265267
self.list_win.hide()
268+
269+
self.modify_base('normal', gtk.gdk.color_parse(_COLORS[self.config.color_scheme['background']]))
270+
266271
self.text_buffer = self.get_buffer()
267272
tags = dict()
268273
for (name, value) in self.config.color_scheme.iteritems():
@@ -616,17 +621,18 @@ def init_import_completion():
616621

617622

618623
def main(args=None):
619-
if args is None:
620-
args = sys.argv[1:]
621624

622625
setlocale(LC_ALL, '')
623626
config = Struct()
624-
loadini(config, '~/.bpython/config')
627+
628+
config, options, exec_args = bpython.args.parse(args)
629+
630+
loadini(config, '~/.bpython/config', options.config)
625631

626632
interpreter = repl.Interpreter(None, getpreferredencoding())
627633
repl_widget = ReplWidget(interpreter, config)
628634

629-
sys.stderr = repl_widget
635+
# sys.stderr = repl_widget
630636
sys.stdout = repl_widget
631637

632638
# repl.startup()
@@ -636,7 +642,16 @@ def main(args=None):
636642
window = gtk.Window()
637643

638644
# branding
645+
646+
# fix icon to be distributed and loaded from the correct path
647+
icon = gtk.gdk.pixbuf_new_from_file(os.path.join(os.path.dirname(__file__),
648+
'logo.png'))
649+
639650
window.set_title('bpython')
651+
window.set_icon(icon)
652+
window.resize(600, 300)
653+
654+
# read from config
640655

641656
sw = gtk.ScrolledWindow()
642657
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

bpython/logo.png

12.9 KB
Loading

bpython/repl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# THE SOFTWARE.
2222
#
2323

24+
# G*RLLCRA RHTNSAHTNSOG()*(@****
2425
from __future__ import with_statement
2526
import code
2627
import codecs
@@ -530,7 +531,7 @@ def complete(self, tab=False):
530531
return False
531532
else:
532533
# remove duplicates
533-
self.matches = matches
534+
self.matches = sorted(set(matches))
534535

535536

536537
if len(self.matches) == 1 and not self.config.auto_display_list:

data/bpython-gtk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
from bpython.gtk_ import main
5+
6+
sys.exit(main())

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
setup(
3131
name="bpython",
3232
version = __version__,
33-
author = "Robert Anthony Farrell et al.",
33+
author = "Bob Farrell, Andreas Stuehrk et al.",
3434
author_email = "robertanthonyfarrell@gmail.com",
3535
description = "Fancy Interface to the Python Interpreter",
3636
license = "MIT/X",
@@ -46,12 +46,15 @@
4646
(os.path.join(man_dir, 'man5'), ['doc/bpython-config.5']),
4747
('share/applications', ['data/bpython.desktop'])
4848
],
49+
package_data = {'bpython': ['logo.png']},
4950
entry_points = {
5051
'console_scripts': [
5152
'bpython = bpython.cli:main',
53+
'bpython-gtk = bpython.gtk_:main',
5254
],
5355
},
54-
scripts = ([] if using_setuptools else ['data/bpython']),
56+
scripts = ([] if using_setuptools else ['data/bpython',
57+
'data/bpython-gtk']),
5558
cmdclass=dict(build_py=build_py)
5659
)
5760

0 commit comments

Comments
 (0)
X Tutup