[gnome-builder] fpaste: add simple fpaste uploader



commit 63b4385d0a99bcd2b4aaec171d0047b4231784a9
Author: Christian Hergert <chergert redhat com>
Date:   Sun Dec 13 20:42:59 2015 -0800

    fpaste: add simple fpaste uploader
    
    Someone please take this over and make a nice paste uploader. We probably
    need to allow for configuring the target host, username, etc.
    
    All of this should be doable now that we have IdePreferencesAddin.
    
    Also, we should make a GtkSourceLanguage→fpaste language mapping.

 configure.ac                              |    1 +
 plugins/Makefile.am                       |    1 +
 plugins/fpaste/Makefile.am                |   19 +++++
 plugins/fpaste/configure.ac               |   12 +++
 plugins/fpaste/fpaste.plugin              |    9 +++
 plugins/fpaste/fpaste_plugin/__init__.py  |  113 +++++++++++++++++++++++++++++
 plugins/fpaste/fpaste_plugin/gtk/menus.ui |   16 ++++
 7 files changed, 171 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index c8d937b..f31d05d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -235,6 +235,7 @@ m4_include([plugins/ctags/configure.ac])
 m4_include([plugins/devhelp/configure.ac])
 m4_include([plugins/device-manager/configure.ac])
 m4_include([plugins/file-search/configure.ac])
+m4_include([plugins/fpaste/configure.ac])
 m4_include([plugins/git/configure.ac])
 m4_include([plugins/gnome-code-assistance/configure.ac])
 m4_include([plugins/html-completion/configure.ac])
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 24bae6c..bda42fb 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -6,6 +6,7 @@ SUBDIRS = \
        ctags \
        devhelp \
        file-search \
+       fpaste \
        git \
        gnome-code-assistance \
        html-completion \
diff --git a/plugins/fpaste/Makefile.am b/plugins/fpaste/Makefile.am
new file mode 100644
index 0000000..b9dd964
--- /dev/null
+++ b/plugins/fpaste/Makefile.am
@@ -0,0 +1,19 @@
+if ENABLE_FPASTE_PLUGIN
+
+EXTRA_DIST = $(plugin_DATA)
+
+plugindir = $(libdir)/gnome-builder/plugins
+dist_plugin_DATA = fpaste.plugin
+
+moduledir = $(libdir)/gnome-builder/plugins/fpaste_plugin
+dist_module_DATA = fpaste_plugin/__init__.py
+
+resourcegtkdir = $(datadir)/gnome-builder/plugins/fpaste_plugin/gtk
+dist_resourcegtk_DATA = fpaste_plugin/gtk/menus.ui
+
+endif
+
+GITIGNOREFILES = fpaste_plugin/__pycache__
+
+-include $(top_srcdir)/git.mk
+
diff --git a/plugins/fpaste/configure.ac b/plugins/fpaste/configure.ac
new file mode 100644
index 0000000..fe89c88
--- /dev/null
+++ b/plugins/fpaste/configure.ac
@@ -0,0 +1,12 @@
+# --enable-fpaste-plugin=yes/no
+AC_ARG_ENABLE([fpaste-plugin],
+              [AS_HELP_STRING([--enable-fpaste-plugin=@<:@yes/no@:>@],
+                              [Build with support for pasting code to fpaste.org])],
+              [enable_fpaste_plugin=$enableval],
+              [enable_fpaste_plugin=yes])
+
+# for if ENABLE_FPASTE_PLUGIN in Makefile.am
+AM_CONDITIONAL(ENABLE_FPASTE_PLUGIN, test x$enable_python_scripting = xyes && test x$enable_fpaste_plugin = 
xyes)
+
+# Ensure our makefile is generated by autoconf
+AC_CONFIG_FILES([plugins/fpaste/Makefile])
diff --git a/plugins/fpaste/fpaste.plugin b/plugins/fpaste/fpaste.plugin
new file mode 100644
index 0000000..fe739f3
--- /dev/null
+++ b/plugins/fpaste/fpaste.plugin
@@ -0,0 +1,9 @@
+[Plugin]
+Module=fpaste_plugin
+Loader=python3
+Name=Fedora Paste
+Description=Paste code to fpaste.org
+Authors=Christian Hergert <christian hergert me>
+Copyright=Copyright © 2015 Christian Hergert
+Depends=editor
+Builtin=true
diff --git a/plugins/fpaste/fpaste_plugin/__init__.py b/plugins/fpaste/fpaste_plugin/__init__.py
new file mode 100644
index 0000000..e547f72
--- /dev/null
+++ b/plugins/fpaste/fpaste_plugin/__init__.py
@@ -0,0 +1,113 @@
+#!/usr/bin/env python3
+
+#
+# fpaste_plugin.py
+#
+# Copyright (C) 2015 Christian Hergert <chris dronelabs com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+from gettext import gettext as _
+
+from gi.repository import Gio
+from gi.repository import GLib
+from gi.repository import GObject
+from gi.repository import Gdk
+from gi.repository import Gtk
+from gi.repository import Ide
+
+import json
+import threading
+import urllib.parse
+import urllib.request
+
+USER_AGENT = 'gnome-builder-fpaste/0.1'
+BASE_URL = 'https://paste.fedoraproject.org'
+
+class FpasteEditorViewAddin(GObject.Object, Ide.EditorViewAddin):
+    def do_load(self, view):
+        self.view = view
+        action = Gio.SimpleAction(name='send-to-fpaste')
+        action.connect('activate', self.send_to_faste)
+        self.view.get_action_group('view').add_action(action)
+
+    def do_unload(self, view):
+        self.view.get_action_group('view').remove_action('send-to-fpaste')
+        self.view = None
+
+    def get_text(self):
+        buf = self.view.get_document()
+        if buf.get_has_selection():
+            # XXX: .get_selection_bounds() looks broken
+            begin = buf.get_iter_at_mark(buf.get_insert())
+            end = buf.get_iter_at_mark(buf.get_selection_bound())
+        else:
+            begin = buf.get_start_iter()
+            end = buf.get_end_iter()
+        return begin.get_slice(end)
+
+    def send_to_faste(self, action, param):
+        text = self.get_text()
+        self._send_to_fpaste(text)
+
+    def _send_to_fpaste(self, text, options=None):
+        if options is None:
+            options = {}
+
+        text = self.get_text()
+
+        params = urllib.parse.urlencode({
+            'paste_lang': options.get('language', ''),
+            'paste_data': text,
+            'api_submit': True,
+            'mode': 'json'
+        })
+
+        req = urllib.request.Request(BASE_URL,
+                                     data=params.encode(),
+                                     headers={'User-Agent': USER_AGENT})
+
+        Uploader(req).start()
+
+class Uploader(threading.Thread):
+    def __init__(self, request):
+        super().__init__()
+        self.request = request
+
+    def run(self):
+        f = urllib.request.urlopen(self.request)
+        GLib.timeout_add(0, self.complete, f)
+        print("Got "+repr(f))
+
+    def complete(self, stream):
+        try:
+            response = json.loads(stream.read().decode())
+            result = response.get('result', {})
+            message = str(result)
+            dialog = Gtk.MessageDialog(buttons=Gtk.ButtonsType.CLOSE, text=message)
+            if 'id' in result:
+                uri = BASE_URL + '/' + str(result['id'] + '/')
+                if result.get('hash', None):
+                    uri += str(result['hash']) + '/'
+                dialog.props.text = _("The following URL has been copied to the clipboard")
+                label = Gtk.LinkButton(visible=True, label=uri, uri=uri, margin=12)
+                Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD).set_text(uri, len(uri))
+                dialog.get_message_area().add(label)
+            dialog.run()
+            dialog.destroy()
+        except Exception as ex:
+            print(ex)
+        finally:
+            return False
diff --git a/plugins/fpaste/fpaste_plugin/gtk/menus.ui b/plugins/fpaste/fpaste_plugin/gtk/menus.ui
new file mode 100644
index 0000000..0de5b05
--- /dev/null
+++ b/plugins/fpaste/fpaste_plugin/gtk/menus.ui
@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+<interface>
+  <menu id="ide-source-view-popup-menu">
+    <section id="ide-source-view-popup-menu-selection-section">
+      <submenu id="ide-source-view-popup-menu-selection-submenu">
+        <section id="ide-source-view-popup-menu-selection-send-to-section">
+          <attribute name="after">ide-source-view-popup-menu-line-section</attribute>
+          <item>
+            <attribute name="label" translatable="yes">Send to Fpaste.org</attribute>
+            <attribute name="action">view.send-to-fpaste</attribute>
+          </item>
+        </section>
+      </submenu>
+    </section>
+  </menu>
+</interface>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]