Source

paste2 / gedit-commander-module / paste2.py

import urllib
import gtk

import commander.commands as commands
import commander.commands.completion
import commander.commands.result
import commander.commands.exceptions

__commander_module__ = True

def __default__(view, entry):
    """paste2: paste selection or hole file"""
    buf = view.get_buffer()
    bounds = buf.get_selection_bounds()

    # nothing selected -> select all
    if not bounds:
        bounds = buf.get_bounds()

    text = bounds[0].get_text(bounds[1])
 
    params = urllib.urlencode({'content': text})
    f = urllib.urlopen("http://p2.karottika.de/", params)
    url = f.read().strip()

    lang = buf.get_language()
    if lang:
        url += '?%s' % lang.get_id()

    prret = (yield commands.result.Prompt("Copy URL to Clipboard? [Y/n]", ('yes','no')))
    if prret[0].strip().lower() in ['y', 'ye', 'yes', '']:
        clipboard = gtk.Clipboard(selection='CLIPBOARD')
        clipboard.set_text(url, len(url))
       
    entry.info_show('<u>%s</u>' % url, True)
    yield commands.result.DONE