TextMate dpaster

For the past couple years, I've switched back and forth between TextMate and VIM as my primary editor. Lately, I've been on a huge TextMate kick and decided to see what all I could do to customize it. Python, being my primary language, is what I wanted to write my scripts in. However, most of the TextMate bundles are written in Ruby (a language I don't at all understand -- it looks crazy to me). After much digging around in the source of some of these bundles and pecking through Ruby to see how it's interacting with TextMate, I was able come up with a little script.

The second point I'd like to make it that I use dpaste.com (a code pastebin) on a daily basis. I know TextMate has built in support for pasting to its own pastebin site, but I don't like it as well at dpaste (also, dpaste is a django site, a plus for me).

I decided to set out and write my own script to paste code to dpaste.com from within TextMate. Right now, it's far from perfect. Every time I look at it, I see parts that I know I should handle better for errors, etc. But it works for me right now and I have more pressing issues to deal with.

So here is the code.

#!/usr/bin/python 

import os, sys, urllib, re, commands

class dpaster(object):
    def __init__(self):
        self.mode_conversion = {'Python': 'Python', 'Python Django': 'Python', 'SQL': 'Sql', 'HTML (Django)': 'DjangoTemplate', 'JavaScript': 'JScript', 'CSS': 'Css', 'XML': 'Xml', 'Diff': 'Diff', 'Ruby': 'Ruby', 'Ruby on Rails': 'Rhtml', 'Apache': 'Apache', 'Schell Script (Bash)': 'Bash', 'Plain Text':''}
        self.TM_SELECTED_TEXT = os.environ.get('TM_SELECTED_TEXT', '')
        self.TM_SUPPORT_PATH = os.environ.get('TM_SUPPORT_PATH', '')
        self.TM_MODE = os.environ.get('TM_MODE', '')
        if self.TM_SELECTED_TEXT and self.TM_SUPPORT_PATH and self.TM_MODE:
            self.TM_SUPPORT_PATH = os.path.join(self.TM_SUPPORT_PATH, "lib")
            if not self.TM_SUPPORT_PATH in os.environ:
                sys.path.insert(0, self.TM_SUPPORT_PATH)
        else:
            self.dialog("Error!", "You need to select text first.")
            raise "Required environment variables are not present"
        if self.TM_MODE in self.mode_conversion:
            self.TM_MODE = self.mode_conversion[self.TM_MODE]
        else:
            self.dialog("Warning!", "dpaste.com does not currently support this language.\nPasting as Plain Text.")
            self.TM_MODE = ''
        
        self.url = "dpaste.com"
        self.id_regex = re.compile('dpaste: #(\d+)')
    
    def ping(self):
        ping_re = re.compile('0% packet loss')
        if ping_re.search(commands.getstatusoutput('ping -c 1 %s' % self.url)[1]):
            return True
        return False
    
    def paste(self):
        data = urllib.urlencode({'content': self.TM_SELECTED_TEXT, 'language':self.TM_MODE, 'title': '', 'poster':'TextMate Script'})
        req = urllib.urlopen('http://%s' % self.url, data)
        self.post_url = "http://dpaste.com/%s/" % self.id_regex.search(req.read()).groups()[0]
    
    def copy(self):
        os.system("echo %s | pbcopy" % self.post_url)
    
    def dialog(self, message, text):
        os.system("$DIALOG -ep '{messageTitle = \"%s\"; informativeText = \"%s\"; alertStyle = informational;}'" % (message, text))
    
    def successDialog(self):
        message = "dpaste.com URL"
        text = "%s\n\nThis URL has been copied to your clipboard." % self.post_url
        self.dialog(message, text)
        
if __name__ == '__main__':
    dpaster = dpaster()
    if dpaster.ping():
        dpaster.paste()
        dpaster.copy()
        dpaster.successDialog()

To make this work, just go into your Bundle Editor, create a New Command and paste this code into the Commands text area. Set the Output to Discard and then setup a keyboard shortcut key -- I chose Ctrl + Option + Command + d. Enjoy!

Posted by Sean Stoops on March 20, 2008

bundle, django, dpaste, paste, python, textmate


Comments

No one has said anything yet...

Post a Comment

(not displayed)

Comment:

The SigB Links:

External Links:

Archives

RSS feeds

A Django project.
Made with vim