Skip to content

Commit af0b29a

Browse files
committed
Better pastebin abstractions, thanks for the ideas Andy
1 parent 5b08740 commit af0b29a

File tree

4 files changed

+24
-53
lines changed

4 files changed

+24
-53
lines changed

bpython/cli.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#!/usr/bin/env python
2-
#
1+
32
# The MIT License
43
#
54
# Copyright (c) 2008 Bob Farrell
@@ -999,6 +998,10 @@ def resize(self):
999998
self.statusbar.resize(refresh=False)
1000999
self.redraw()
10011000

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+
10021005
def getstdout(self):
10031006
"""This method returns the 'spoofed' stdout buffer, for writing to a
10041007
file or sending to a pastebin or whatever."""

bpython/gtk_.py

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -573,48 +573,15 @@ def on_suggestion_selection_changed(self, selection, word):
573573
self.get_cursor_iter())
574574
self.text_buffer.insert_at_cursor(word)
575575

576-
def pastebin(self, widget):
577-
"""Upload to a pastebin and display the URL in the status bar."""
576+
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
578582

579-
# FIXME cleanup
580-
response = False
581-
582-
if self.config.pastebin_confirm:
583-
dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO,
584-
"Pastebin buffer?")
585-
response = True if dialog.run() == gtk.RESPONSE_YES else False
586-
dialog.destroy()
587-
else:
588-
response = True
589-
590-
if not response:
591-
self.statusbar.message("Pastebin aborted")
592-
return
593-
# end FIXME
594-
595-
pasteservice = ServerProxy(self.config.pastebin_url)
596-
597-
s = self.stdout_hist
598-
599-
if s == self.prev_pastebin_content:
600-
self.statusbar.message('Duplicate pastebin. Previous URL: ' +
601-
self.prev_pastebin_url)
602-
return
603-
604-
self.prev_pastebin_content = s
605-
606-
self.statusbar.message('Posting data to pastebin...')
607-
try:
608-
paste_id = pasteservice.pastes.newPaste('pycon', s)
609-
except XMLRPCError, e:
610-
self.statusbar.message('Upload failed: %s' % (str(e), ) )
611-
return
612-
613-
paste_url_template = Template(self.config.pastebin_show_url)
614-
paste_id = urlquote(paste_id)
615-
paste_url = paste_url_template.safe_substitute(paste_id=paste_id)
616-
self.prev_pastebin_url = paste_url
617-
self.statusbar.message('Pastebin URL: %s' % (paste_url, ), 10)
583+
def do_paste(self, widget):
584+
self.pastebin()
618585

619586
def write(self, s):
620587
"""For overriding stdout defaults"""
@@ -690,8 +657,7 @@ def set_cursor_to_valid_insert_position(self):
690657
if line_start_iter.compare(cursor_iter) > 0:
691658
self.text_buffer.place_cursor(line_start_iter)
692659

693-
@property
694-
def stdout_hist(self):
660+
def getstdout(self):
695661
bounds = self.text_buffer.get_bounds()
696662
text = self.text_buffer.get_text(bounds[0], bounds[1])
697663
return text
@@ -773,7 +739,7 @@ def main(args=None):
773739
filem.set_submenu(filemenu)
774740

775741
pastebin = gtk.MenuItem("Pastebin")
776-
pastebin.connect("activate", repl_widget.pastebin)
742+
pastebin.connect("activate", repl_widget.do_paste)
777743
filemenu.append(pastebin)
778744

779745
exit = gtk.MenuItem("Exit")

bpython/repl.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -634,20 +634,19 @@ def write2file(self):
634634
else:
635635
self.statusbar.message('Saved to %s' % (fn, ))
636636

637-
def pastebin(self):
637+
def pastebin(self, s=None):
638638
"""Upload to a pastebin and display the URL in the status bar."""
639639

640+
if s is None:
641+
s = self.getstdout()
642+
640643
if (self.config.pastebin_confirm and
641-
not self.statusbar.prompt("Pastebin buffer? (y/N) "
642-
).lower().startswith('y'
643-
)):
644+
not self.ask_confirmation("Pastebin buffer? (y/N) ")):
644645
self.statusbar.message("Pastebin aborted")
645646
return
646647

647648
pasteservice = ServerProxy(self.config.pastebin_url)
648649

649-
s = self.getstdout()
650-
651650
if s == self.prev_pastebin_content:
652651
self.statusbar.message('Duplicate pastebin. Previous URL: ' +
653652
self.prev_pastebin_url)

bpython/urwid.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,10 @@ def getstdout(self):
581581

582582
return self.stdout_hist + '\n'
583583

584-
584+
def ask_confirmation(self, q):
585+
"""Ask for yes or no and return boolean"""
586+
return self.statusbar.prompt(q).lower().startswith('y')
587+
585588
def reevaluate(self):
586589
"""Clear the buffer, redraw the screen and re-evaluate the history"""
587590

0 commit comments

Comments
 (0)