| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,8 +58,8 @@ | |||
| 58 | 58 | # This for keys | |
| 59 | 59 | from bpython.keys import key_dispatch | |
| 60 | 60 | ||
| 61 | + from bpython import repl | ||
| 61 | 62 | from bpython.pager import page | |
| 62 | - from bpython.repl import Interpreter, Repl | ||
| 63 | 63 | import bpython.args | |
| 64 | 64 | ||
| 65 | 65 | ||
@@ -233,10 +233,25 @@ def make_colors(config): | |||
| 233 | 233 | return c | |
| 234 | 234 | ||
| 235 | 235 | ||
| 236 | - class CLIRepl(Repl): | ||
| 236 | + class CLIInteraction(repl.Interaction): | ||
| 237 | + def __init__(self, config, statusbar=None): | ||
| 238 | + repl.Interaction.__init__(self, config, statusbar) | ||
| 239 | + | ||
| 240 | + def confirm(self, q): | ||
| 241 | + """Ask for yes or no and return boolean""" | ||
| 242 | + return self.statusbar.prompt(q).lower().startswith('y') | ||
| 243 | + | ||
| 244 | + def notify(self, s, n=10): | ||
| 245 | + return self.statusbar.message(s, n) | ||
| 246 | + | ||
| 247 | + def file_prompt(self, s): | ||
| 248 | + return self.statusbar.prompt(s) | ||
| 249 | + | ||
| 250 | + | ||
| 251 | + class CLIRepl(repl.Repl): | ||
| 237 | 252 | ||
| 238 | 253 | def __init__(self, scr, interp, statusbar, config, idle=None): | |
| 239 | - Repl.__init__(self, interp, config) | ||
| 254 | + repl.Repl.__init__(self, interp, config) | ||
| 240 | 255 | interp.writetb = self.writetb | |
| 241 | 256 | self.scr = scr | |
| 242 | 257 | self.stdout_hist = '' | |
@@ -251,6 +266,7 @@ def __init__(self, scr, interp, statusbar, config, idle=None): | |||
| 251 | 266 | self.s = '' | |
| 252 | 267 | self.statusbar = statusbar | |
| 253 | 268 | self.formatter = BPythonFormatter(config.color_scheme) | |
| 269 | + self.interact = CLIInteraction(self.config, statusbar=self.statusbar) | ||
| 254 | 270 | ||
| 255 | 271 | def addstr(self, s): | |
| 256 | 272 | """Add a string to the current input line and figure out | |
@@ -334,7 +350,7 @@ def clear_current_line(self): | |||
| 334 | 350 | """Called when a SyntaxError occured in the interpreter. It is | |
| 335 | 351 | used to prevent autoindentation from occuring after a | |
| 336 | 352 | traceback.""" | |
| 337 | - Repl.clear_current_line(self) | ||
| 353 | + repl.Repl.clear_current_line(self) | ||
| 338 | 354 | self.s = '' | |
| 339 | 355 | ||
| 340 | 356 | def clear_wrapped_lines(self): | |
@@ -359,7 +375,7 @@ def complete(self, tab=False): | |||
| 359 | 375 | return | |
| 360 | 376 | ||
| 361 | 377 | if self.config.auto_display_list or tab: | |
| 362 | - self.list_win_visible = Repl.complete(self, tab) | ||
| 378 | + self.list_win_visible = repl.Repl.complete(self, tab) | ||
| 363 | 379 | if self.list_win_visible: | |
| 364 | 380 | try: | |
| 365 | 381 | self.show_list(self.matches, self.argspec) | |
@@ -900,7 +916,7 @@ def push(self, s, insert_into_history=True): | |||
| 900 | 916 | # curses.raw(True) prevents C-c from causing a SIGINT | |
| 901 | 917 | curses.raw(False) | |
| 902 | 918 | try: | |
| 903 | - return Repl.push(self, s, insert_into_history) | ||
| 919 | + return repl.Repl.push(self, s, insert_into_history) | ||
| 904 | 920 | except SystemExit: | |
| 905 | 921 | # Avoid a traceback on e.g. quit() | |
| 906 | 922 | self.do_exit = True | |
@@ -998,10 +1014,7 @@ def resize(self): | |||
| 998 | 1014 | self.statusbar.resize(refresh=False) | |
| 999 | 1015 | self.redraw() | |
| 1000 | 1016 | ||
| 1001 | - def ask_confirmation(self, q): | ||
| 1002 | - """Ask for yes or no and return boolean""" | ||
| 1003 | - return self.statusbar.prompt(q).lower().startswith('y') | ||
| 1004 | - | ||
| 1017 | + | ||
| 1005 | 1018 | def getstdout(self): | |
| 1006 | 1019 | """This method returns the 'spoofed' stdout buffer, for writing to a | |
| 1007 | 1020 | file or sending to a pastebin or whatever.""" | |
@@ -1293,7 +1306,7 @@ def tab(self, back=False): | |||
| 1293 | 1306 | return True | |
| 1294 | 1307 | ||
| 1295 | 1308 | def undo(self, n=1): | |
| 1296 | - Repl.undo(self, n) | ||
| 1309 | + repl.Repl.undo(self, n) | ||
| 1297 | 1310 | ||
| 1298 | 1311 | # This will unhighlight highlighted parens | |
| 1299 | 1312 | self.print_line(self.s) | |
@@ -1613,6 +1626,7 @@ def main_curses(scr, args, config, interactive=True, locals_=None, | |||
| 1613 | 1626 | global stdscr | |
| 1614 | 1627 | global DO_RESIZE | |
| 1615 | 1628 | global colors | |
| 1629 | + global repl | ||
| 1616 | 1630 | DO_RESIZE = False | |
| 1617 | 1631 | ||
| 1618 | 1632 | old_sigwinch_handler = signal.signal(signal.SIGWINCH, | |
@@ -1639,10 +1653,10 @@ def main_curses(scr, args, config, interactive=True, locals_=None, | |||
| 1639 | 1653 | if locals_ is None: | |
| 1640 | 1654 | sys.modules['__main__'] = ModuleType('__main__') | |
| 1641 | 1655 | locals_ = sys.modules['__main__'].__dict__ | |
| 1642 | - interpreter = Interpreter(locals_, getpreferredencoding()) | ||
| 1656 | + interpreter = repl.Interpreter(locals_, getpreferredencoding()) | ||
| 1643 | 1657 | ||
| 1644 | - repl = CLIRepl(main_win, interpreter, statusbar, config, idle) | ||
| 1645 | - repl._C = cols | ||
| 1658 | + clirepl = CLIRepl(main_win, interpreter, statusbar, config, idle) | ||
| 1659 | + clirepl._C = cols | ||
| 1646 | 1660 | ||
| 1647 | 1661 | sys.stdin = FakeStdin(repl) | |
| 1648 | 1662 | sys.stdout = repl | |
@@ -1652,18 +1666,18 @@ def main_curses(scr, args, config, interactive=True, locals_=None, | |||
| 1652 | 1666 | bpython.args.exec_code(interpreter, args) | |
| 1653 | 1667 | if not interactive: | |
| 1654 | 1668 | curses.raw(False) | |
| 1655 | - return repl.getstdout() | ||
| 1669 | + return clirepl.getstdout() | ||
| 1656 | 1670 | else: | |
| 1657 | 1671 | sys.path.insert(0, '') | |
| 1658 | - repl.startup() | ||
| 1672 | + clirepl.startup() | ||
| 1659 | 1673 | ||
| 1660 | 1674 | if banner is not None: | |
| 1661 | - repl.write(banner) | ||
| 1662 | - repl.write('\n') | ||
| 1663 | - repl.repl() | ||
| 1675 | + clirepl.write(banner) | ||
| 1676 | + clirepl.write('\n') | ||
| 1677 | + clirepl.repl() | ||
| 1664 | 1678 | if config.hist_length: | |
| 1665 | 1679 | histfilename = os.path.expanduser(config.hist_file) | |
| 1666 | - repl.rl_history.save(histfilename, getpreferredencoding()) | ||
| 1680 | + clirepl.rl_history.save(histfilename, getpreferredencoding()) | ||
| 1667 | 1681 | ||
| 1668 | 1682 | main_win.erase() | |
| 1669 | 1683 | main_win.refresh() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -140,14 +140,11 @@ class Statusbar(gtk.Statusbar): | |||
| 140 | 140 | def __init__(self): | |
| 141 | 141 | gtk.Statusbar.__init__(self) | |
| 142 | 142 | ||
| 143 | - self.context_id = self.get_context_id('StatusBar') | ||
| 143 | + self.context_id = self.get_context_id('Statusbar') | ||
| 144 | 144 | ||
| 145 | 145 | def message(self, s, n=3): | |
| 146 | 146 | self.push(self.context_id, s) | |
| 147 | 147 | ||
| 148 | - def prompt(self, s): | ||
| 149 | - pass | ||
| 150 | - | ||
| 151 | 148 | ||
| 152 | 149 | class SuggestionWindow(gtk.Window): | |
| 153 | 150 | """ | |
@@ -267,6 +264,20 @@ def update_matches(self, matches): | |||
| 267 | 264 | self.view.set_property('visible', bool(matches)) | |
| 268 | 265 | ||
| 269 | 266 | ||
| 267 | + class GTKInteraction(repl.Interaction): | ||
| 268 | + def __init__(self, config, statusbar): | ||
| 269 | + repl.Interaction.__init__(self, config, statusbar) | ||
| 270 | + | ||
| 271 | + def confirm(self, q): | ||
| 272 | + dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO, q) | ||
| 273 | + response = True if dialog.run() == gtk.RESPONSE_YES else False | ||
| 274 | + dialog.destroy() | ||
| 275 | + return response | ||
| 276 | + | ||
| 277 | + def notify(self, s, n=10): | ||
| 278 | + self.statusbar.message(s) | ||
| 279 | + | ||
| 280 | + | ||
| 270 | 281 | class ReplWidget(gtk.TextView, repl.Repl): | |
| 271 | 282 | __gsignals__ = dict(button_press_event=None, | |
| 272 | 283 | focus_in_event=None, | |
@@ -290,7 +301,7 @@ def __init__(self, interpreter, config): | |||
| 290 | 301 | self.modify_base('normal', gtk.gdk.color_parse(_COLORS[self.config.color_gtk_scheme['background']])) | |
| 291 | 302 | ||
| 292 | 303 | self.text_buffer = self.get_buffer() | |
| 293 | - self.statusbar = Statusbar() | ||
| 304 | + self.interact = GTKInteraction(self.config, Statusbar()) | ||
| 294 | 305 | tags = dict() | |
| 295 | 306 | for (name, value) in self.config.color_gtk_scheme.iteritems(): | |
| 296 | 307 | tag = tags[name] = self.text_buffer.create_tag(name) | |
@@ -574,12 +585,6 @@ def on_suggestion_selection_changed(self, selection, word): | |||
| 574 | 585 | self.text_buffer.insert_at_cursor(word) | |
| 575 | 586 | ||
| 576 | 587 | ||
| 577 | - def ask_confirmation(self, q): | ||
| 578 | - dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO, q) | ||
| 579 | - response = True if dialog.run() == gtk.RESPONSE_YES else False | ||
| 580 | - dialog.destroy() | ||
| 581 | - return response | ||
| 582 | - | ||
| 583 | 588 | def do_paste(self, widget): | |
| 584 | 589 | self.pastebin() | |
| 585 | 590 | ||
@@ -772,7 +777,7 @@ def main(args=None): | |||
| 772 | 777 | sw.add(repl_widget) | |
| 773 | 778 | container.add(sw) | |
| 774 | 779 | ||
| 775 | - sb = repl_widget.statusbar | ||
| 780 | + sb = repl_widget.interact.statusbar | ||
| 776 | 781 | container.pack_end(sb, expand=False) | |
| 777 | 782 | ||
| 778 | 783 | parent.show_all() | |
@@ -788,7 +793,6 @@ def main(args=None): | |||
| 788 | 793 | repl_widget.rl_history.save(histfilename, getpreferredencoding()) | |
| 789 | 794 | return 0 | |
| 790 | 795 | ||
| 791 | - | ||
| 792 | 796 | if __name__ == '__main__': | |
| 793 | 797 | from bpython.gtk_ import main | |
| 794 | 798 | main() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -236,6 +236,23 @@ def update(self, current_word='', matches=[]): | |||
| 236 | 236 | self.index = -1 | |
| 237 | 237 | ||
| 238 | 238 | ||
| 239 | + class Interaction(object): | ||
| 240 | + def __init__(self, config, statusbar=None): | ||
| 241 | + self.config = config | ||
| 242 | + | ||
| 243 | + if statusbar: | ||
| 244 | + self.statusbar = statusbar | ||
| 245 | + | ||
| 246 | + def confirm(self, s): | ||
| 247 | + raise NotImplementedError | ||
| 248 | + | ||
| 249 | + def notify(self, s, n=10): | ||
| 250 | + raise NotImplementedError | ||
| 251 | + | ||
| 252 | + def file_prompt(self, s): | ||
| 253 | + raise NotImplementedError | ||
| 254 | + | ||
| 255 | + | ||
| 239 | 256 | class Repl(object): | |
| 240 | 257 | """Implements the necessary guff for a Python-repl-alike interface | |
| 241 | 258 | ||
@@ -302,6 +319,7 @@ def __init__(self, interp, config): | |||
| 302 | 319 | self.list_win_visible = False | |
| 303 | 320 | self._C = {} | |
| 304 | 321 | self.prev_block_finished = 0 | |
| 322 | + self.interact = Interaction(self.config) | ||
| 305 | 323 | # previous pastebin content to prevent duplicate pastes, filled on call | |
| 306 | 324 | # to repl.pastebin | |
| 307 | 325 | self.prev_pastebin_content = '' | |
@@ -615,9 +633,9 @@ def write2file(self): | |||
| 615 | 633 | buffer to disk.""" | |
| 616 | 634 | ||
| 617 | 635 | try: | |
| 618 | - fn = self.statusbar.prompt('Save to file (Esc to cancel): ') | ||
| 636 | + fn = self.interact.file_prompt('Save to file (Esc to cancel): ') | ||
| 619 | 637 | except ValueError: | |
| 620 | - self.statusbar.message("Save cancelled.") | ||
| 638 | + self.interact.notify("Save cancelled.") | ||
| 621 | 639 | return | |
| 622 | 640 | ||
| 623 | 641 | if fn.startswith('~'): | |
@@ -630,9 +648,9 @@ def write2file(self): | |||
| 630 | 648 | f.write(s) | |
| 631 | 649 | f.close() | |
| 632 | 650 | except IOError: | |
| 633 | - self.statusbar.message("Disk write error for file '%s'." % (fn, )) | ||
| 651 | + self.interact.notify("Disk write error for file '%s'." % (fn, )) | ||
| 634 | 652 | else: | |
| 635 | - self.statusbar.message('Saved to %s' % (fn, )) | ||
| 653 | + self.interact.notify('Saved to %s' % (fn, )) | ||
| 636 | 654 | ||
| 637 | 655 | def pastebin(self, s=None): | |
| 638 | 656 | """Upload to a pastebin and display the URL in the status bar.""" | |
@@ -641,31 +659,31 @@ def pastebin(self, s=None): | |||
| 641 | 659 | s = self.getstdout() | |
| 642 | 660 | ||
| 643 | 661 | if (self.config.pastebin_confirm and | |
| 644 | - not self.ask_confirmation("Pastebin buffer? (y/N) ")): | ||
| 645 | - self.statusbar.message("Pastebin aborted") | ||
| 662 | + not self.interact.confirm("Pastebin buffer? (y/N) ")): | ||
| 663 | + self.interact.notify("Pastebin aborted") | ||
| 646 | 664 | return | |
| 647 | 665 | ||
| 648 | 666 | pasteservice = ServerProxy(self.config.pastebin_url) | |
| 649 | 667 | ||
| 650 | 668 | if s == self.prev_pastebin_content: | |
| 651 | - self.statusbar.message('Duplicate pastebin. Previous URL: ' + | ||
| 652 | - self.prev_pastebin_url) | ||
| 669 | + self.interact.notify('Duplicate pastebin. Previous URL: ' + | ||
| 670 | + self.prev_pastebin_url) | ||
| 653 | 671 | return | |
| 654 | 672 | ||
| 655 | 673 | self.prev_pastebin_content = s | |
| 656 | 674 | ||
| 657 | - self.statusbar.message('Posting data to pastebin...') | ||
| 675 | + self.interact.notify('Posting data to pastebin...') | ||
| 658 | 676 | try: | |
| 659 | 677 | paste_id = pasteservice.pastes.newPaste('pycon', s) | |
| 660 | 678 | except XMLRPCError, e: | |
| 661 | - self.statusbar.message('Upload failed: %s' % (str(e), ) ) | ||
| 679 | + self.interact.notify('Upload failed: %s' % (str(e), ) ) | ||
| 662 | 680 | return | |
| 663 | 681 | ||
| 664 | 682 | paste_url_template = Template(self.config.pastebin_show_url) | |
| 665 | 683 | paste_id = urlquote(paste_id) | |
| 666 | 684 | paste_url = paste_url_template.safe_substitute(paste_id=paste_id) | |
| 667 | 685 | self.prev_pastebin_url = paste_url | |
| 668 | - self.statusbar.message('Pastebin URL: %s' % (paste_url, ), 10) | ||
| 686 | + self.interact.notify('Pastebin URL: %s' % (paste_url, ), 10) | ||
| 669 | 687 | ||
| 670 | 688 | def push(self, s, insert_into_history=True): | |
| 671 | 689 | """Push a line of code onto the buffer so it can process it all | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -384,6 +384,17 @@ def render(self, size, focus=False): | |||
| 384 | 384 | canvas.cursor = cursor | |
| 385 | 385 | return canvas | |
| 386 | 386 | ||
| 387 | + class URWIDInteraction(repl.Interaction): | ||
| 388 | + def __init__(self, config, statusbar=None): | ||
| 389 | + repl.Interaction.__init__(self, config, statusbar) | ||
| 390 | + | ||
| 391 | + def confirm(self, q): | ||
| 392 | + """Ask for yes or no and return boolean""" | ||
| 393 | + return self.statusbar.prompt(q).lower().startswith('y') | ||
| 394 | + | ||
| 395 | + def notify(self, s, n=10): | ||
| 396 | + return self.statusbar.message(s, n) | ||
| 397 | + | ||
| 387 | 398 | ||
| 388 | 399 | class URWIDRepl(repl.Repl): | |
| 389 | 400 | ||
@@ -400,6 +411,8 @@ def __init__(self, event_loop, palette, interpreter, config): | |||
| 400 | 411 | config.pastebin_key, config.last_output_key, | |
| 401 | 412 | config.show_source_key)) | |
| 402 | 413 | ||
| 414 | + self.interact = URWIDInteraction(self.config, self.statusbar) | ||
| 415 | + | ||
| 403 | 416 | self.tooltip = urwid.ListBox(urwid.SimpleListWalker([])) | |
| 404 | 417 | self.tooltip.grid = None | |
| 405 | 418 | self.overlay = Tooltip(self.listbox, self.tooltip) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments