Sublime Text 2 : PrivatePaste module

Download | Vote Up (1) | Vote Down (0)
# Sublime Text 2 PrivatePaste Plugin !
# Create a file in your Sublime Text 2 plugin folder named privatepaste.py
# (by default /home/USERNAME/.config/sublime-text-2/Packages/User) 
# Add a shortcut "Preferences > Key Bindings - User" with the following values
# { "keys": ["ctrl+&"], "command": "private_paste"} (change the "keys" value !)
#
# <3 Sublime Text 2, Enjoy :)
# By ex0ns 
# Thank's to Kallimero, Fr0g & AppleStorm !

import sublime, sublime_plugin, urllib, urllib2, re, threading, os.path

extensions = {"" : "No Formatting", "cpp" : "C++", "py" : "Python", "c" : "C", "css" : "CSS", "java" : "Java",
              "js" : "JavaScript", "rb" : "Ruby", "asm" : "NASM", "html" : "HTML", "php" : "PHP", "pl" : "Perl" }
class PrivatePasteCommand(sublime_plugin.TextCommand):
        def run(self, edit):
                if self.view.sel()[0].empty():
                        self.code = self.getCode(self.view.file_name())
                else:
                        self.code = self.view.substr(self.view.sel()[0])
                self.extension =  os.path.splitext(self.view.file_name())[1][1:]
                print self.extension
                thread = threading.Thread(target=self.backgroundJob)
                thread.start()

        def getCode(self, file):
                return open(file).read()

        def backgroundJob(self):
                try:
                  host = "http://privatepaste.com/save"
                  params = urllib.urlencode([("paste_content", self.code ),
                                             ("formatting", extensions[self.extension] if self.extension in extensions else "No Formatting" ),
                                             ("line_number", "on"),
                                             ("expire", "3600"),
                                             ("secure_past", "off"),
                                             ("secure_past_key", "")
                                            ])
                  headers = {"Content-type": "application/x-www-form-urlencoded"}
                  req = urllib2.urlopen(urllib2.Request(host, params, headers)).read();
                  pasteid = re.search("<title>privatepaste.com :: Paste ID (.*)</title>", req)
                  sublime.error_message("http://privatepaste.com/" + pasteid.group(1))
                  #sublime.set_clipboard("http://privatepaste.com/" + pasteid.group(1))
                except:
                  sublime.error_message("Error while sending information")


ex0ns


Be the first to give feedback !

Please login to comment !