Source

pida-hacks / pida-plugins / pastebin / pastebin.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# -*- coding: utf-8 -*- 

import gtk, gobject

from pygtkhelpers.ui.objectlist import ObjectList, Column
from pygtkhelpers.proxy import proxy_for
# PIDA Imports
from pida.core.service import Service
from pida.core.actions import ActionsConfig
from pida.ui.actions import PidaRememberToggle
from pida.ui.views import PidaView

from pida.utils.web import fetch_url

# locale
from pida.core.locale import Locale
locale = Locale('pastebin')
_ = locale.gettext


class Bin(object):
    """ A Pastebin """

    PASTE_URL = None

    def __init__(self, svc):
        self.svc = svc

    def create_data_dict(self, title, name, content, syntax):
        """
        Has to return a dict containing the POST data to send to the pastebin.
        Override this in individual pastebins.
        """

    @classmethod
    def get_syntax_items(cls):
        """
        Override to return a list of syntax item tuples (label, value)
        """

    def post(self, *args):
        self.args = args
        fetch_url(self.PASTE_URL, self.on_posted, self.create_data_dict(*args))

    def on_posted(self, url, content):
        self.svc._view.stop_pulse()
        if url:
            # pasting was successfully
            self.svc.new_paste_complete(url, *self.args)
        else:
            # an error occured when pasting
            self.svc.new_paste_failed(content)


class LodgeIt(Bin):

    PASTE_URL = 'http://paste.pocoo.org'

    def create_data_dict(self, title, name, content, syntax):
        return {
            'code':     content,
            'language': syntax
        }

    @classmethod
    def get_syntax_items(cls):
        return [
            ('Text', 'text'),
            ('Python', 'python'),
            ('Python Console Sessions', 'pycon'),
            ('Python Tracebacks', 'pycon'),
            ('PHP', 'html+php'),
            ('Django / Jinja Templates', 'html+django'),
            ('Mako Templates', 'html+mako'),
            ('Myghty Templates', 'html+myghty'),
            ('Apache Config (.htaccess)', 'apache'),
            ('Bash', 'bash'),
            ('Batch (.bat)', 'bat'),
            ('C', 'c'),
            ('C++', 'cpp'),
            ('C#', 'csharp'),
            ('CSS', 'css'),
            ('D', 'd'),
            ('MiniD', 'minid'),
            ('Smarty', 'smarty'),
            ('HTML', 'html'),
            ('Genshi Templates', 'html+genshi'),
            ('JavaScript', 'js'),
            ('Java', 'java'),
            ('JSP', 'jsp'),
            ('Lua', 'lua'),
            ('Haskell', 'haskell'),
            ('Scheme', 'scheme'),
            ('Ruby', 'ruby'),
            ('eRuby / rhtml', 'rhtml'),
            ('TeX / LaTeX', 'tex'),
            ('XML', 'xml'),
            ('reStructuredText', 'rst'),
            ('IRC Logs', 'irc'),
            ('Unified DIff', 'diff'),
            ('Vim Scripts', 'vim')
        ]


class Dpaste(Bin):

    PASTE_URL = 'http://dpaste.com/'

    def create_data_dict(self, title, name, content, syntax):
        return dict(
            poster = name,
            title = title,
            content = content,
            language = syntax,
        )

    @classmethod
    def get_syntax_items(cls):
        return [
            ('Python', 'Python'),
            ('Python Interactive / Traceback', 'PythonConsole'),
            ('SQL', 'Sql'),
            ('HTML / Django Template', 'DjangoTemplate'),
            ('JavaScript', 'JScript'),
            ('CSS', 'Css'),
            ('XML', 'Xml'),
            ('Diff', 'Diff'),
            ('Ruby', 'Ruby'),
            ('Ruby HTML (ERB)', 'Rhtml'),
            ('Haskell', 'Haskell'),
            ('Apache Configuration', 'Apache'),
            ('Bash Script', 'Bash'),
            ('Plain Text', ''),
        ]


class Rafb(Bin):

    PASTE_URL = 'http://www.rafb.net/paste/paste.php'

    def create_data_dict(self, title, name, content, syntax):
        return dict(
            text=content,
            nick=name,
            desc=title,
            lang=syntax,
            cvt_tabs=4,
            submit=_('Paste')
        )

    @classmethod
    def get_syntax_items(cls):
        return [
            ('C89', 'C89'),
            ('C', 'C'),
            ('C++', 'C++'), 
            ('C#', 'C#'),
            ('Java', 'Java'),
            ('Pascal', 'Pascal'),
            ('Perl', 'Perl'),
            ('PHP', 'PHP'),
            ('PL/I', 'PL/I'),
            ('Python', 'Python'),
            ('Ruby', 'Ruby'),
            ('SQL', 'SQL'),
            ('VB', 'VB'),
            ('Plain Text', 'Plain Text')
        ]

class Twisted(Bin):

    PASTE_URL = 'http://deadbeefbabe.org/paste/freeform_post!!addPasting'

    def create_data_dict(self, title, name, content, syntax):
        return dict(
            author=name,
            text=content,
            addPasting='addPasting',
            _charset_='',
        )

    @classmethod
    def get_syntax_items(cls):
        return [('Python', '')]


class HPaste(Bin):
    PASTE_URL="http://hpaste.org/fastcgi/hpaste.fcgi/save"
    
    def create_data_dict(self, title, name, content, syntax):
        return dict(
            content=content,
            author=name,
            title=title,
            save='save',      
            language=syntax,
            channel='none',
        )
    
    @classmethod
    def get_syntax_items(cls):
        return [
            ("apacheconf","ApacheConf"),
            ("BBCode", "bbcode"),
            ("Bash", "bash"),
            ("C", "c"),
            ("C#", "csharp"),
            ("C++", "cpp"),
            ("CSS", "css"),
            ("Clojure", "clojure"),
            ("Common Lisp", "common-lisp"),
            ("D", "d"),
            ("HTML", "html"),
            ("Haskell", "haskell"),
            ("INI", "ini"),
            ("Io", "io"),
            ("Java", "java"),
            ("JavaScript", "js"),
            ("Lighttpd configuration file", "lighty"),
            ("Lua", "lua"),
            ("Makefile", "make"),
            ("Objective-C", "objective-c"),
            ("PHP", "php"),
            ("Perl", "perl"),
            ("Python", "python"),
            ("Python 3", "python3"),
            ("Python 3.0 Traceback", "py3tb"),
            ("Python Traceback", "pytb"),
            ("Python console session", "pycon"),
            ("Raw token data","raw"),
            ("Ruby", "rb"),
            ("SQL", "sql"),
            ("Scala", "scala"),
            ("Text only", "text"),
            ("VimL", "vim"),
            ("XML", "xml"),
            ("YAML", "yaml"),
            ("reStructuredText", "rst"),
            ("sqlite3con", "sqlite3"),
        ]


pastebin_types = [
    ('DPaste', Dpaste),
    ('Rafb.net', Rafb),
    ('LodgeIt', LodgeIt),
    ('HPaste', HPaste),
    #('Twisted', Twisted), #Broken for some reason
]


class PastebinEditorView(PidaView):

    key = 'pastebin.editor'
    gladefile = 'paste_editor'
    locale = locale
    label_text = _('Paste Editor')
    icon_name = gtk.STOCK_PASTE

    def create_ui(self):
        self.paste_location.set_choices(self.svc.get_pastebin_types(), None)
        self.paste_proxy = proxy_for(self.paste_location)
        self.syntax_proxy = proxy_for(self.paste_syntax)

    def on_paste_proxy__changed(self, proxy, value):
        if value is not None:
            self.paste_syntax.set_choices(value.get_syntax_items(), None)

    def on_post_button__clicked(self, button):
        paste_type = self.paste_location.read()
        self.svc.commence_paste(paste_type, *self.read_values())

    def on_cancel_button__clicked(self, button):
        self.svc.cancel_paste()

    def read_values(self):
        return (self.paste_title.get_text(),
                self.paste_name.get_text(),
                self.paste_content.get_buffer().get_text(
                    self.paste_content.get_buffer().get_start_iter(),
                    self.paste_content.get_buffer().get_end_iter(),
                ),
                self.syntax_proxy.read(),
        )

    def can_be_closed(self):
        self.svc.cancel_paste()

class PasteHistoryView(PidaView):

    key = 'pastebin.history'

    label_text = _('Paste History')
    icon_name = gtk.STOCK_PASTE

    #glade_file_name = 'paste-history.glade'

    def create_ui(self):
        self.history_tree = ObjectList(
            [Column('markup', use_markup=True, expand=True)])
        self.history_tree.set_headers_visible(False)
        self.add_main_widget(self.history_tree)
        self.x11_clipboard = gtk.Clipboard(selection="PRIMARY")
        self.gnome_clipboard = gtk.Clipboard(selection="CLIPBOARD")
        self.history_tree.connect('item-right-clicked', self.on_paste_rclick)
        self.__pulse_bar = gtk.ProgressBar()
        self.add_main_widget(self.__pulse_bar, expand=False)
        # only show pulse bar if working
        self.__pulse_bar.hide()
        self.__pulse_bar.set_size_request(-1, 12)
        self.__pulse_bar.set_pulse_step(0.01)
        self.history_tree.show_all()

    @property
    def tree_selected(self):
        return self.history_tree.selected_item

    def set(self, pastes):
        '''Sets the paste list to the tree view.
           First reset it, then rebuild it.
        '''
        self.history_tree.clear()
        self.history_tree.expand(pastes)
        self.tree_selected = None

    def add_paste(self, item):
        self.history_tree.append(item)

    def copy_current_paste(self):
        '''Callback function bound to the toolbar button view that copies the
        selected paste'''
        if self.tree_selected != None:
            self.x11_clipboard.set_text(self.tree_selected.get_url())
            self.gnome_clipboard.set_text(self.tree_selected.get_url())

    def view_current_paste(self):
        '''Callback function bound to the toolbar button view that shows the
        selected paste'''
        if self.tree_selected != None:
            self.service.boss.call_command('pastemanager','view_paste',
                paste=self.tree_selected)
        else:
            print _("ERROR: No paste selected")

    def remove_current_paste(self):
        '''Callback function bound to the toolbar button delete that removes the
        selected paste'''
        if self.tree_selected != None:
            self.service.boss.call_command('pastemanager','delete_paste',
                paste=self.tree_selected)
        else:
            print _("ERROR: No paste selected")


    def cb_paste_db_clicked(self, ol, item):
        """
        Callback function called when an item is double clicked, and copy it
        to the gnome/gtk clipboard
        """
        if item is not None:
            self.svc.boss.cmd('browseweb', 'browse', url=item.url)
            # self.__gnome_clipboard.set_text(self.__tree_selected.get_url())
            # aa: view the paste

    def cb_paste_m_clicked(self,paste,tree_item):
        '''Callback function called when an item is middle clicked, and copy it
        to the mouse buffer clipboard'''
        if self.__tree_selected != None:
            self.__x11_clipboard.set_text(self.__tree_selected.get_url())

    def cb_paste_r_clicked(self, paste, tree_item, event):
        menu = gtk.Menu()
        sensitives = (tree_item is not None)
        for action in ['pastemanager+new_paste',
                        None,
                       'pastemanager+remove_paste',
                       'pastemanager+view_paste',
                        None,
                        'pastemanager+copy_url_to_clipboard']:
            if action is None:
                menu.append(gtk.SeparatorMenuItem())
            else:
                act = self.service.action_group.get_action(action)
                if 'new_paste' not in action:
                    act.set_sensitive(sensitives)
                mi = gtk.ImageMenuItem()
                act.connect_proxy(mi)
                mi.show()
                menu.append(mi)
        menu.show_all()
        menu.popup(None, None, None, event.button, event.time)

    def on_paste_rclick(self, ol, item, event):
        self.svc.boss.cmd('contexts', 'popup_menu', context='url-menu',
                          url=item.url, event=event)

    def start_pulse(self):
        '''Starts the pulse'''
        self._pulsing = True
        self.__pulse_bar.show()
        gobject.timeout_add(100, self._pulse)

    def stop_pulse(self):
        self.__pulse_bar.hide()
        self._pulsing = False

    def _pulse(self):
        self.__pulse_bar.pulse()
        return self._pulsing

    def can_be_closed(self):
        self.svc.get_action('show_pastes').set_active(False)


class PastebinActionsConfig(ActionsConfig):


    actions = [
        gtk.Action('new_paste',
            _('Upload Text Snippet'),
            _('Upload a text snippet to a pastebin'),
            gtk.STOCK_PASTE),
        PidaRememberToggle('show_pastes',
            _('Paste History'),
            _('Show the paste history viewer'),
            gtk.STOCK_PASTE),
    ]

    accels = {
        'show_pastes': '<Shift><Control>0',
    }

    def on_new_paste(self, action):
        self.svc.new_paste()

    def on_show_pastes(self, action):
        if action.get_active():
            self.svc.show_pastes()
        else:
            self.svc.hide_pastes()

class PasteItem(object):

    def __init__(self, url, *args):
        self.url = url
        self.title, self.name, self.content, self.syntax = args

    @property
    def markup(self):
        return ('<b>%s</b> (<span foreground="#0000c0">%s</span>)\n%s' %
                    (self.title, self.syntax, self.url))


# Service class
class Pastebin(Service):
    """Describe your Service Here""" 

    actions_config = PastebinActionsConfig

    def pre_start(self):
        self._editor = PastebinEditorView(self)
        self._view = PasteHistoryView(self)

    def new_paste(self):
        self.boss.cmd('window', 'add_view', paned='Plugin',
                      view=self._editor)
        self.get_action('new_paste').set_sensitive(False)

    def show_pastes(self):
        self.boss.cmd('window', 'add_view', paned='Plugin', view=self._view)

    def hide_pastes(self):
        self.boss.cmd('window', 'remove_view', view=self._view)

    def commence_paste(self, paste_type, *args):
        p = paste_type(self)
        p.post(*args)
        self.ensure_view_visible()
        self._view.start_pulse()
        self._close_paste_editor()

    def cancel_paste(self):
        self._close_paste_editor()

    def _close_paste_editor(self):
        self.boss.cmd('window', 'remove_view', view=self._editor)
        self.get_action('new_paste').set_sensitive(True)

    def new_paste_complete(self, url, *args):
        self._view.add_paste(PasteItem(url, *args))
        self.ensure_view_visible()

    def new_paste_failed(self, response):
        self.boss.cmd('notify', 'notify', title=response,
            data=_('An error occured when pasting.\n'
                   'Maybe you should try to use another pastebin?'))

        # show editor field
        self.new_paste()


    def ensure_view_visible(self):
        act = self.get_action('show_pastes')
        if not act.get_active():
            act.set_active(True)

    def get_pastebin_types(self):
        return pastebin_types

    def stop(self):
        if not self.get_action('new_paste').get_sensitive():
            self._close_paste_editor()
        if self.get_action('show_pastes').get_active():
            self.hide_pastes()

# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: