[gnome-builder/wip/commands] commands: experimental python command



commit ec2c898f321a788ca44d722a0f86bce9d852ad83
Author: Christian Hergert <christian hergert me>
Date:   Tue Oct 7 11:27:36 2014 -0700

    commands: experimental python command

 build/autotools/autoconf.d/50_dependencies.post-am |    1 +
 src/commands/gb-command-bar.c                      |    7 ++
 src/commands/gb-commands-internal.c                |    8 ++-
 src/commands/gb-python-command.c                   |   95 ++++++++++++++++++++
 src/commands/gb-python-command.h                   |   56 ++++++++++++
 src/gnome-builder.mk                               |    6 +-
 src/main.c                                         |    6 ++
 7 files changed, 175 insertions(+), 4 deletions(-)
---
diff --git a/build/autotools/autoconf.d/50_dependencies.post-am 
b/build/autotools/autoconf.d/50_dependencies.post-am
index a927b6b..c0e8664 100644
--- a/build/autotools/autoconf.d/50_dependencies.post-am
+++ b/build/autotools/autoconf.d/50_dependencies.post-am
@@ -3,3 +3,4 @@ PKG_CHECK_MODULES(GIO, gio-2.0 >= 2.40.0)
 PKG_CHECK_MODULES(GTKSOURCEVIEW, gtksourceview-3.0 >= 3.13.91)
 PKG_CHECK_MODULES(DEVHELP, libdevhelp-3.0 >= 3.13.90)
 PKG_CHECK_MODULES(GGIT, libgit2-glib-1.0 >= 0.0.22)
+PKG_CHECK_MODULES(PYTHON, python >= 2.7)
diff --git a/src/commands/gb-command-bar.c b/src/commands/gb-command-bar.c
index 2d25d1d..31475d5 100644
--- a/src/commands/gb-command-bar.c
+++ b/src/commands/gb-command-bar.c
@@ -70,8 +70,15 @@ static void
 gb_command_bar_push_task (GbCommandBar  *bar,
                           GbCommandTask *task)
 {
+  const gchar *command_text;
+  const gchar *result_text;
+
   g_return_if_fail (GB_IS_COMMAND_BAR (bar));
   g_return_if_fail (GB_IS_COMMAND_TASK (task));
+
+  command_text = gb_command_task_get_command_text (task);
+  result_text = gb_command_task_get_result_text (task);
+  g_print ("                    %s = %s\n", command_text, result_text);
 }
 
 static void
diff --git a/src/commands/gb-commands-internal.c b/src/commands/gb-commands-internal.c
index 5b3c70b..4b81030 100644
--- a/src/commands/gb-commands-internal.c
+++ b/src/commands/gb-commands-internal.c
@@ -26,6 +26,7 @@
 #include "gb-commands-internal.h"
 #include "gb-editor-tab.h"
 #include "gb-editor-workspace.h"
+#include "gb-python-command.h"
 
 typedef void (*TextCommandFunc) (GbCommand        *command,
                                  GVariant         *parameters,
@@ -151,14 +152,13 @@ void
 gb_commands_internal_init (void)
 {
   GbCommandManager *manager;
+  GbCommand *command;
   guint i;
   
   manager = gb_command_manager_get_default ();
 
   for (i = 0; i < G_N_ELEMENTS (gTextCommands); i++)
     {
-      GbCommand *command;
-
       command = g_object_new (GB_TYPE_COMMAND,
                               "name", gTextCommands [i].name,
                               "description", gTextCommands [i].description,
@@ -170,4 +170,8 @@ gb_commands_internal_init (void)
       gb_command_manager_register (manager, command);
       g_object_unref (command);
     }
+
+  command = gb_python_command_new ();
+  gb_command_manager_register (manager, command);
+  g_object_unref (command);
 }
diff --git a/src/commands/gb-python-command.c b/src/commands/gb-python-command.c
new file mode 100644
index 0000000..648ce09
--- /dev/null
+++ b/src/commands/gb-python-command.c
@@ -0,0 +1,95 @@
+/* gb-python-command.c
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * 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/>.
+ */
+
+#include <Python.h>
+#include <glib/gi18n.h>
+
+#include "gb-python-command.h"
+
+G_DEFINE_TYPE (GbPythonCommand, gb_python_command, GB_TYPE_COMMAND)
+
+GbCommand *
+gb_python_command_new (void)
+{
+  return g_object_new (GB_TYPE_PYTHON_COMMAND, NULL);
+}
+
+static GbCommandTask *
+gb_python_command_execute (GbCommand   *command,
+                           GVariant    *parameters,
+                           GbWorkbench *workbench)
+{
+  const gchar *command_text = "hex(10)";
+  GbCommandTask *task;
+  PyObject *ret;
+  PyObject *locals;
+  gchar *result_text = NULL;
+
+  g_return_val_if_fail (GB_IS_PYTHON_COMMAND (command), NULL);
+  g_return_val_if_fail (GB_IS_WORKBENCH (workbench), NULL);
+
+  locals = PyDict_New ();
+
+  if ((ret = PyRun_String (command_text, Py_single_input,
+                           PyEval_GetBuiltins (), locals)))
+    {
+      if (!PyString_Check (ret))
+        {
+          PyObject *tmp = ret;
+
+          ret = PyObject_Str (tmp);
+          Py_DECREF (tmp);
+        }
+
+      if (ret)
+        {
+          result_text = g_strdup (PyString_AS_STRING (ret));
+          Py_DECREF (ret);
+        }
+    }
+  else
+    PyErr_Print ();
+
+  task = g_object_new (GB_TYPE_COMMAND_TASK,
+                       "active", FALSE,
+                       "command-text", command_text,
+                       "progress", 1.0,
+                       "result-text", result_text,
+                       NULL);
+
+  Py_DECREF (locals);
+  g_free (result_text);
+
+  return task;
+}
+
+static void
+gb_python_command_class_init (GbPythonCommandClass *klass)
+{
+  GbCommandClass *command_class = GB_COMMAND_CLASS (klass);
+
+  command_class->execute = gb_python_command_execute;
+}
+
+static void
+gb_python_command_init (GbPythonCommand *self)
+{
+  gb_command_set_name (GB_COMMAND (self), "python");
+  gb_command_set_description (GB_COMMAND (self),
+                              _("Run a command in Python."));
+}
diff --git a/src/commands/gb-python-command.h b/src/commands/gb-python-command.h
new file mode 100644
index 0000000..2b2e0d2
--- /dev/null
+++ b/src/commands/gb-python-command.h
@@ -0,0 +1,56 @@
+/* gb-python-command.h
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * 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/>.
+ */
+
+#ifndef GB_PYTHON_COMMAND_H
+#define GB_PYTHON_COMMAND_H
+
+#include "gb-command.h"
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_PYTHON_COMMAND            (gb_python_command_get_type())
+#define GB_PYTHON_COMMAND(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_PYTHON_COMMAND, 
GbPythonCommand))
+#define GB_PYTHON_COMMAND_CONST(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_PYTHON_COMMAND, 
GbPythonCommand const))
+#define GB_PYTHON_COMMAND_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GB_TYPE_PYTHON_COMMAND, 
GbPythonCommandClass))
+#define GB_IS_PYTHON_COMMAND(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GB_TYPE_PYTHON_COMMAND))
+#define GB_IS_PYTHON_COMMAND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GB_TYPE_PYTHON_COMMAND))
+#define GB_PYTHON_COMMAND_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GB_TYPE_PYTHON_COMMAND, 
GbPythonCommandClass))
+
+typedef struct _GbPythonCommand        GbPythonCommand;
+typedef struct _GbPythonCommandClass   GbPythonCommandClass;
+typedef struct _GbPythonCommandPrivate GbPythonCommandPrivate;
+
+struct _GbPythonCommand
+{
+  GbCommand parent;
+
+  /*< private >*/
+  GbPythonCommandPrivate *priv;
+};
+
+struct _GbPythonCommandClass
+{
+  GbCommandClass parent;
+};
+
+GType      gb_python_command_get_type (void) G_GNUC_CONST;
+GbCommand *gb_python_command_new      (void);
+
+G_END_DECLS
+
+#endif /* GB_PYTHON_COMMAND_H */
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 58919c5..17360b2 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -19,6 +19,8 @@ libgnome_builder_la_SOURCES = \
        src/commands/gb-command-task.h \
        src/commands/gb-commands-internal.c \
        src/commands/gb-commands-internal.h \
+       src/commands/gb-python-command.c \
+       src/commands/gb-python-command.h \
        src/devhelp/gb-devhelp-navigation-item.c \
        src/devhelp/gb-devhelp-navigation-item.h \
        src/devhelp/gb-devhelp-tab.c \
@@ -133,7 +135,7 @@ libgnome_builder_la_LIBADD = \
        $(GIO_LIBS) \
        $(GTKSOURCEVIEW_LIBS) \
        $(GTK_LIBS) \
-       $(WEBKIT_LIBS) \
+       $(PYTHON_LIBS) \
        -lm
 
 libgnome_builder_la_CFLAGS = \
@@ -143,7 +145,7 @@ libgnome_builder_la_CFLAGS = \
        $(GTKSOURCEVIEW_CFLAGS) \
        $(GTK_CFLAGS) \
        $(MAINTAINER_CFLAGS) \
-       $(WEBKIT_CFLAGS) \
+       $(PYTHON_CFLAGS) \
        -I$(top_srcdir)/src/animation \
        -I$(top_srcdir)/src/app \
        -I$(top_srcdir)/src/commands \
diff --git a/src/main.c b/src/main.c
index 4834713..8142f21 100644
--- a/src/main.c
+++ b/src/main.c
@@ -16,6 +16,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <Python.h>
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
@@ -33,6 +34,9 @@ main (int   argc,
 
   g_set_prgname ("gnome-builder");
   g_set_application_name (_("Builder"));
+  Py_SetProgramName ("gnome-builder");
+
+  Py_InitializeEx (0);
 
   gb_log_init (TRUE, NULL);
   ggit_init ();
@@ -47,5 +51,7 @@ main (int   argc,
 
   gb_log_shutdown ();
 
+  Py_Finalize ();
+
   return ret;
 }


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