3333import os
3434import sys
3535from locale import LC_ALL , getpreferredencoding , setlocale
36+ from xmlrpclib import ServerProxy , Error as XMLRPCError
37+ from string import Template
38+ from urllib import quote as urlquote
3639
3740import gobject
3841import gtk
4245from bpython .formatter import theme_map
4346import bpython .args
4447
48+ py3 = sys .version_info [0 ] == 3
4549
4650_COLORS = dict (b = 'blue' , c = 'cyan' , g = 'green' , m = 'magenta' , r = 'red' ,
4751 w = 'white' , y = 'yellow' , k = 'black' , d = 'black' )
@@ -136,8 +140,13 @@ class Statusbar(gtk.Statusbar):
136140 def __init__ (self ):
137141 gtk .Statusbar .__init__ (self )
138142
139- context_id = self .get_context_id ('StatusBar' )
140- # self.push(context_id, text)
143+ self .context_id = self .get_context_id ('StatusBar' )
144+
145+ def message (self , s , n = 3 ):
146+ self .push (self .context_id , s )
147+
148+ def prompt (self , s ):
149+ pass
141150
142151
143152class SuggestionWindow (gtk .Window ):
@@ -281,6 +290,7 @@ def __init__(self, interpreter, config):
281290 self .modify_base ('normal' , gtk .gdk .color_parse (_COLORS [self .config .color_gtk_scheme ['background' ]]))
282291
283292 self .text_buffer = self .get_buffer ()
293+ self .statusbar = Statusbar ()
284294 tags = dict ()
285295 for (name , value ) in self .config .color_gtk_scheme .iteritems ():
286296 tag = tags [name ] = self .text_buffer .create_tag (name )
@@ -563,6 +573,68 @@ def on_suggestion_selection_changed(self, selection, word):
563573 self .get_cursor_iter ())
564574 self .text_buffer .insert_at_cursor (word )
565575
576+ def pastebin (self , widget ):
577+ """Upload to a pastebin and display the URL in the status bar."""
578+
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 )
618+
619+ def write (self , s ):
620+ """For overriding stdout defaults"""
621+ if '\x04 ' in s :
622+ for block in s .split ('\x04 ' ):
623+ self .write (block )
624+ return
625+ if s .rstrip () and '\x03 ' in s :
626+ t = s .split ('\x03 ' )[1 ]
627+ else :
628+ t = s
629+
630+ if not py3 and isinstance (t , unicode ):
631+ t = t .encode (getpreferredencoding ())
632+
633+ self .echo (s )
634+ self .s_hist .append (s .rstrip ())
635+
636+
637+
566638 def prompt (self , more ):
567639 """
568640 Show the appropriate Python prompt.
@@ -618,6 +690,12 @@ def set_cursor_to_valid_insert_position(self):
618690 if line_start_iter .compare (cursor_iter ) > 0 :
619691 self .text_buffer .place_cursor (line_start_iter )
620692
693+ @property
694+ def stdout_hist (self ):
695+ bounds = self .text_buffer .get_bounds ()
696+ text = self .text_buffer .get_text (bounds [0 ], bounds [1 ])
697+ return text
698+
621699 def writetb (self , lines ):
622700 with ExceptionManager (ExceptionDialog ,
623701 'An error occured while trying to display '
@@ -693,7 +771,11 @@ def main(args=None):
693771
694772 filem = gtk .MenuItem ("File" )
695773 filem .set_submenu (filemenu )
696-
774+
775+ pastebin = gtk .MenuItem ("Pastebin" )
776+ pastebin .connect ("activate" , repl_widget .pastebin )
777+ filemenu .append (pastebin )
778+
697779 exit = gtk .MenuItem ("Exit" )
698780 exit .connect ("activate" , gtk .main_quit )
699781 filemenu .append (exit )
@@ -711,7 +793,7 @@ def main(args=None):
711793 sw .add (repl_widget )
712794 container .add (sw )
713795
714- sb = Statusbar ()
796+ sb = repl_widget . statusbar
715797 container .pack_end (sb , expand = False )
716798
717799 parent .show_all ()
0 commit comments