libgweather r376 - in trunk: . python



Author: danw
Date: Thu Sep  4 12:44:21 2008
New Revision: 376
URL: http://svn.gnome.org/viewvc/libgweather?rev=376&view=rev

Log:
	* python/*: create python bindings for GWeatherLocation,
	GWeatherTimezone, GWeatherLocationEntry, and GWeatherTimezoneMenu.
	As with the C library, these are unstable. #547824

	* configure.in: Add --enable-python (off by default)

	* acinclude.m4: stuff needed for that


Added:
   trunk/acinclude.m4
   trunk/python/   (props changed)
   trunk/python/Makefile.am
   trunk/python/bad__init__.py
   trunk/python/demo.py
   trunk/python/gweather.defs
   trunk/python/gweather.override
   trunk/python/gweathermodule.c
Modified:
   trunk/ChangeLog
   trunk/Makefile.am
   trunk/configure.in

Modified: trunk/Makefile.am
==============================================================================
--- trunk/Makefile.am	(original)
+++ trunk/Makefile.am	Thu Sep  4 12:44:21 2008
@@ -1,4 +1,4 @@
-SUBDIRS = po po-locations libgweather data
+SUBDIRS = po po-locations libgweather data python
 
 EXTRA_DIST = \
 	MAINTAINERS

Added: trunk/acinclude.m4
==============================================================================
--- (empty file)
+++ trunk/acinclude.m4	Thu Sep  4 12:44:21 2008
@@ -0,0 +1,66 @@
+## this one is commonly used with AM_PATH_PYTHONDIR ...
+dnl AM_CHECK_PYMOD(MODNAME [,SYMBOL [,ACTION-IF-FOUND [,ACTION-IF-NOT-FOUND]]])
+dnl Check if a module containing a given symbol is visible to python.
+AC_DEFUN([AM_CHECK_PYMOD],
+[AC_REQUIRE([AM_PATH_PYTHON])
+py_mod_var=`echo $1['_']$2 | sed 'y%./+-%__p_%'`
+AC_MSG_CHECKING(for ifelse([$2],[],,[$2 in ])python module $1)
+AC_CACHE_VAL(py_cv_mod_$py_mod_var, [
+ifelse([$2],[], [prog="
+import sys
+try:
+        import $1
+except ImportError:
+        sys.exit(1)
+except:
+        sys.exit(0)
+sys.exit(0)"], [prog="
+import $1
+$1.$2"])
+if $PYTHON -c "$prog" 1>&AC_FD_CC 2>&AC_FD_CC
+  then
+    eval "py_cv_mod_$py_mod_var=yes"
+  else
+    eval "py_cv_mod_$py_mod_var=no"
+  fi
+])
+py_val=`eval "echo \`echo '$py_cv_mod_'$py_mod_var\`"`
+if test "x$py_val" != xno; then
+  AC_MSG_RESULT(yes)
+  ifelse([$3], [],, [$3
+])dnl
+else
+  AC_MSG_RESULT(no)
+  ifelse([$4], [],, [$4
+])dnl
+fi
+])
+
+dnl a macro to check for ability to create python extensions
+dnl  AM_CHECK_PYTHON_HEADERS([ACTION-IF-POSSIBLE], [ACTION-IF-NOT-POSSIBLE])
+dnl function also defines PYTHON_INCLUDES
+AC_DEFUN([AM_CHECK_PYTHON_HEADERS],
+[AC_REQUIRE([AM_PATH_PYTHON])
+AC_MSG_CHECKING(for headers required to compile python extensions)
+dnl deduce PYTHON_INCLUDES
+py_prefix=`$PYTHON -c "import sys; print sys.prefix"`
+py_exec_prefix=`$PYTHON -c "import sys; print sys.exec_prefix"`
+if test -x "$PYTHON-config"; then
+PYTHON_INCLUDES=`$PYTHON-config --includes 2>/dev/null`
+else
+PYTHON_INCLUDES="-I${py_prefix}/include/python${PYTHON_VERSION}"
+if test "$py_prefix" != "$py_exec_prefix"; then
+  PYTHON_INCLUDES="$PYTHON_INCLUDES -I${py_exec_prefix}/include/python${PYTHON_VERSION}"
+fi
+fi
+AC_SUBST(PYTHON_INCLUDES)
+dnl check if the headers exist:
+save_CPPFLAGS="$CPPFLAGS"
+CPPFLAGS="$CPPFLAGS $PYTHON_INCLUDES"
+AC_TRY_CPP([#include <Python.h>],dnl
+[AC_MSG_RESULT(found)
+$1],dnl
+[AC_MSG_RESULT(not found)
+$2])
+CPPFLAGS="$save_CPPFLAGS"
+])

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Thu Sep  4 12:44:21 2008
@@ -163,6 +163,24 @@
 AC_DEFINE_UNQUOTED(ZONEINFO_DIR, "$ZONEINFO_DIR", [zoneinfo directory])
 
 dnl ***************************************************************************
+dnl *** Python bindings
+dnl ***************************************************************************
+AC_ARG_ENABLE(python,
+        [AC_HELP_STRING([--enable-python],
+                        [Build libgweather python bindings])],
+        [enable_python=yes],
+        [enable_python=no])
+if test "$enable_python" = "yes"; then
+   AM_PATH_PYTHON()
+   AM_CHECK_PYTHON_HEADERS(,enable_python=no)
+   PKG_CHECK_MODULES(PYGOBJECT, pygobject-2.0)
+   pygtk_defsdir=`$PKG_CONFIG --variable=defsdir pygtk-2.0`
+   PYGTK_DEFS="$pygtk_defsdir/gtk.defs"
+   AC_SUBST(PYGTK_DEFS)
+fi
+AM_CONDITIONAL(BUILD_PYTHON, test "x$enable_python" = "xyes")
+
+dnl ***************************************************************************
 dnl *** Honour aclocal flags                                                ***
 dnl ***************************************************************************
 ACLOCAL_AMFLAGS="\${ACLOCAL_FLAGS}"
@@ -186,6 +204,7 @@
 libgweather/gweather.pc
 libgweather/gweather-uninstalled.pc
 data/Makefile
+python/Makefile
 ])
 
 if test "x$enable_big_xml" = "xyes"; then

Added: trunk/python/Makefile.am
==============================================================================
--- (empty file)
+++ trunk/python/Makefile.am	Thu Sep  4 12:44:21 2008
@@ -0,0 +1,33 @@
+if BUILD_PYTHON
+INCLUDES =			\
+	$(PYTHON_INCLUDES)	\
+	$(PYGOBJECT_CFLAGS)	\
+	$(GTK_CFLAGS)
+
+pyunstabledir = $(pyexecdir)/gweather/I_KNOW_THIS_IS_UNSTABLE
+pyunstable_LTLIBRARIES = gweather.la
+gweather_la_LDFLAGS = -module -avoid-version -export-symbols-regex initgweather
+gweather_la_LIBADD = ../libgweather/libgweather.la
+gweather_la_SOURCES = gweathermodule.c
+nodist_gweather_la_SOURCES = gweather.c
+gweather.c: gweather.defs *.override
+CLEANFILES = gweather.c
+
+EXTRA_DIST = *.override bad__init__.py demo.py
+
+.defs.c:
+	(cd $(srcdir)\
+	 && pygtk-codegen-2.0 \
+		--register $(PYGTK_DEFS) \
+		--override $*.override \
+		--prefix py$* $*.defs) > gen-$*.c \
+	&& cp gen-$*.c $*.c \
+	&& rm -f gen-$*.c
+
+install-exec-hook:
+	$(MKDIR_P) "$(DESTDIR)$(pyunstabledir)"
+	$(INSTALL_SCRIPT) bad__init__.py "$(DESTDIR)$(pyexecdir)/gweather/__init__.py"
+	touch $(DESTDIR)$(pyunstabledir)/__init__.py
+	chmod a+x $(DESTDIR)$(pyunstabledir)/__init__.py
+
+endif

Added: trunk/python/bad__init__.py
==============================================================================
--- (empty file)
+++ trunk/python/bad__init__.py	Thu Sep  4 12:44:21 2008
@@ -0,0 +1,11 @@
+# The correct idiom is
+# from gweather.I_KNOW_THIS_IS_UNSTABLE import gweather
+
+def location_new_world(use_regions):
+    raise ImportError("libgweather should only be used if you understand that it's subject to change, and is not supported as a fixed API/ABI or as part of the platform")
+
+def location_entry_new(top):
+    raise ImportError("libgweather should only be used if you understand that it's subject to change, and is not supported as a fixed API/ABI or as part of the platform")
+
+def timezone_menu_new(top):
+    raise ImportError("libgweather should only be used if you understand that it's subject to change, and is not supported as a fixed API/ABI or as part of the platform")

Added: trunk/python/demo.py
==============================================================================
--- (empty file)
+++ trunk/python/demo.py	Thu Sep  4 12:44:21 2008
@@ -0,0 +1,7 @@
+from gweather.I_KNOW_THIS_IS_UNSTABLE import gweather
+
+for loc in gweather.location_new_world(False).get_children():
+    print loc.get_name()
+    for zone in loc.get_timezones():
+        if zone.get_name() is not None:
+            print "  %s" % zone.get_name()

Added: trunk/python/gweather.defs
==============================================================================
--- (empty file)
+++ trunk/python/gweather.defs	Thu Sep  4 12:44:21 2008
@@ -0,0 +1,326 @@
+;; -*- scheme -*-
+; boxed definitions ...
+
+(define-boxed Location
+  (in-module "GWeather")
+  (c-name "GWeatherLocation")
+  (gtype-id "GWEATHER_TYPE_LOCATION")
+)
+
+(define-boxed Timezone
+  (in-module "GWeather")
+  (c-name "GWeatherTimezone")
+  (gtype-id "GWEATHER_TYPE_TIMEZONE")
+)
+
+; interface definitions ...
+
+; object definitions ...
+
+(define-object LocationEntry
+  (in-module "GWeather")
+  (parent "GtkEntry")
+  (c-name "GWeatherLocationEntry")
+  (gtype-id "GWEATHER_TYPE_LOCATION_ENTRY")
+)
+
+(define-object TimezoneMenu
+  (in-module "GWeather")
+  (parent "GtkComboBox")
+  (c-name "GWeatherTimezoneMenu")
+  (gtype-id "GWEATHER_TYPE_TIMEZONE_MENU")
+)
+
+; pointer definitions ...
+
+;; Enumerations and Flags ...
+
+(define-enum LocationLevel
+  (in-module "GWeather")
+  (c-name "GWeatherLocationLevel")
+  (gtype-id "GWEATHER_TYPE_LOCATION_LEVEL")
+  (values
+    '("world" "GWEATHER_LOCATION_WORLD")
+    '("region" "GWEATHER_LOCATION_REGION")
+    '("country" "GWEATHER_LOCATION_COUNTRY")
+    '("adm1" "GWEATHER_LOCATION_ADM1")
+    '("adm2" "GWEATHER_LOCATION_ADM2")
+    '("city" "GWEATHER_LOCATION_CITY")
+    '("weather-station" "GWEATHER_LOCATION_WEATHER_STATION")
+  )
+)
+
+
+;; From gweather-enum-types.h
+
+(define-function g_weather_location_level_get_type
+  (c-name "g_weather_location_level_get_type")
+  (return-type "GType")
+)
+
+
+
+;; From gweather-location.h
+
+(define-function location_get_type
+  (c-name "gweather_location_get_type")
+  (return-type "GType")
+)
+
+(define-function location_new_world
+  (c-name "gweather_location_new_world")
+  (return-type "GWeatherLocation*")
+  (parameters
+    '("gboolean" "use_regions")
+  )
+)
+
+(define-method ref
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_ref")
+  (return-type "GWeatherLocation*")
+)
+
+(define-method unref
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_unref")
+  (return-type "none")
+)
+
+(define-method get_name
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_name")
+  (return-type "const-char*")
+)
+
+(define-method get_sort_name
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_sort_name")
+  (return-type "const-char*")
+)
+
+(define-method get_level
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_level")
+  (return-type "GWeatherLocationLevel")
+)
+
+(define-method get_parent
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_parent")
+  (return-type "GWeatherLocation*")
+)
+
+(define-method get_children
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_children")
+  (return-type "GWeatherLocation**")
+)
+
+(define-method free_children
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_free_children")
+  (return-type "none")
+  (parameters
+    '("GWeatherLocation**" "children")
+  )
+)
+
+(define-method has_coords
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_has_coords")
+  (return-type "gboolean")
+)
+
+(define-method get_coords
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_coords")
+  (return-type "none")
+  (parameters
+    '("double*" "latitude")
+    '("double*" "longitude")
+  )
+)
+
+(define-method get_distance
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_distance")
+  (return-type "double")
+  (parameters
+    '("GWeatherLocation*" "loc2")
+  )
+)
+
+(define-method get_country
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_country")
+  (return-type "const-char*")
+)
+
+(define-method get_timezone
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_timezone")
+  (return-type "GWeatherTimezone*")
+)
+
+(define-method get_timezones
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_timezones")
+  (return-type "GWeatherTimezone**")
+)
+
+(define-method free_timezones
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_free_timezones")
+  (return-type "none")
+  (parameters
+    '("GWeatherTimezone**" "zones")
+  )
+)
+
+(define-method get_code
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_code")
+  (return-type "const-char*")
+)
+
+(define-method get_city_name
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_city_name")
+  (return-type "char*")
+)
+
+(define-method get_weather
+  (of-object "GWeatherLocation")
+  (c-name "gweather_location_get_weather")
+  (return-type "WeatherInfo*")
+)
+
+
+
+;; From gweather-timezone.h
+
+(define-function timezone_get_type
+  (c-name "gweather_timezone_get_type")
+  (return-type "GType")
+)
+
+(define-method get_name
+  (of-object "GWeatherTimezone")
+  (c-name "gweather_timezone_get_name")
+  (return-type "const-char*")
+)
+
+(define-method get_tzid
+  (of-object "GWeatherTimezone")
+  (c-name "gweather_timezone_get_tzid")
+  (return-type "const-char*")
+)
+
+(define-method get_offset
+  (of-object "GWeatherTimezone")
+  (c-name "gweather_timezone_get_offset")
+  (return-type "int")
+)
+
+(define-method has_dst
+  (of-object "GWeatherTimezone")
+  (c-name "gweather_timezone_has_dst")
+  (return-type "gboolean")
+)
+
+(define-method get_dst_offset
+  (of-object "GWeatherTimezone")
+  (c-name "gweather_timezone_get_dst_offset")
+  (return-type "int")
+)
+
+(define-method ref
+  (of-object "GWeatherTimezone")
+  (c-name "gweather_timezone_ref")
+  (return-type "GWeatherTimezone*")
+)
+
+(define-method unref
+  (of-object "GWeatherTimezone")
+  (c-name "gweather_timezone_unref")
+  (return-type "none")
+)
+
+
+
+;; From location-entry.h
+
+(define-function location_entry_get_type
+  (c-name "gweather_location_entry_get_type")
+  (return-type "GType")
+)
+
+(define-function location_entry_new
+  (c-name "gweather_location_entry_new")
+  (is-constructor-of "GWeatherLocationEntry")
+  (return-type "GtkWidget*")
+  (properties
+    '("top")
+  )
+)
+
+(define-method set_location
+  (of-object "GWeatherLocationEntry")
+  (c-name "gweather_location_entry_set_location")
+  (return-type "none")
+  (parameters
+    '("GWeatherLocation*" "loc")
+  )
+)
+
+(define-method get_location
+  (of-object "GWeatherLocationEntry")
+  (c-name "gweather_location_entry_get_location")
+  (return-type "GWeatherLocation*")
+)
+
+(define-method set_city
+  (of-object "GWeatherLocationEntry")
+  (c-name "gweather_location_entry_set_city")
+  (return-type "none")
+  (parameters
+    '("const-char*" "city_name")
+    '("const-char*" "code")
+  )
+)
+
+
+
+;; From timezone-menu.h
+
+(define-function timezone_menu_get_type
+  (c-name "gweather_timezone_menu_get_type")
+  (return-type "GType")
+)
+
+(define-function timezone_menu_new
+  (c-name "gweather_timezone_menu_new")
+  (is-constructor-of "GWeatherTimezoneMenu")
+  (return-type "GtkWidget*")
+  (properties
+    '("top")
+  )
+)
+
+(define-method set_tzid
+  (of-object "GWeatherTimezoneMenu")
+  (c-name "gweather_timezone_menu_set_tzid")
+  (return-type "none")
+  (parameters
+    '("const-char*" "tzid")
+  )
+)
+
+(define-method get_tzid
+  (of-object "GWeatherTimezoneMenu")
+  (c-name "gweather_timezone_menu_get_tzid")
+  (return-type "const-char*")
+)
+
+

Added: trunk/python/gweather.override
==============================================================================
--- (empty file)
+++ trunk/python/gweather.override	Thu Sep  4 12:44:21 2008
@@ -0,0 +1,104 @@
+/* -*- Mode: C; c-basic-offset: 4 -*-
+ *
+ * gweather.override: overrides for gweather
+ */
+%%
+headers
+#define NO_IMPORT_PYGOBJECT
+#include "pygobject.h"
+
+#define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
+#include <libgweather/gweather-location.h>
+#include <libgweather/gweather-timezone.h>
+#include <libgweather/location-entry.h>
+#include <libgweather/timezone-menu.h>
+#include <libgweather/gweather-enum-types.h>
+
+%%
+modulename gweather
+%%
+import gtk.Entry as PyGtkEntry_Type
+import gtk.ComboBox as PyGtkComboBox_Type
+%%
+ignore
+  gweather_location_get_weather
+  gweather_location_free_children
+  gweather_location_free_timezones
+%%
+ignore-glob
+  *_get_type
+  *_ref
+  *_unref
+%%
+override gweather_location_get_children
+static PyObject *
+_wrap_gweather_location_get_children (PyGObject *self, PyObject *args)
+{
+    GWeatherLocation *loc, **children;
+    int i;
+    PyObject *pychildren, *pychild;
+
+    loc = pyg_boxed_get (self, GWeatherLocation);
+    children = gweather_location_get_children (loc);
+    for (i = 0; children[i]; i++)
+	;
+
+    pychildren = PyList_New (i);
+    for (i = 0; children[i]; i++) {
+	pychild = pyg_boxed_new (GWEATHER_TYPE_LOCATION, children[i], TRUE, TRUE);
+	PyList_SetItem (pychildren, i, pychild);
+    }
+
+    gweather_location_free_children (loc, children);
+    return pychildren;
+}
+%%
+override gweather_location_get_timezones
+static PyObject *
+_wrap_gweather_location_get_timezones (PyGObject *self, PyObject *args)
+{
+    GWeatherLocation *loc;
+    GWeatherTimezone **zones;
+    int i;
+    PyObject *pyzones, *pyzone;
+
+    loc = pyg_boxed_get (self, GWeatherLocation);
+    zones = gweather_location_get_timezones (loc);
+    for (i = 0; zones[i]; i++)
+	;
+
+    pyzones = PyList_New (i);
+    for (i = 0; zones[i]; i++) {
+	pyzone = pyg_boxed_new (GWEATHER_TYPE_TIMEZONE, zones[i], TRUE, TRUE);
+	PyList_SetItem (pyzones, i, pyzone);
+    }
+
+    gweather_location_free_timezones (loc, zones);
+    return pyzones;
+}
+%%
+override gweather_location_get_coords
+static PyObject *
+_wrap_gweather_location_get_coords (PyGObject *self, PyObject *args)
+{
+    GWeatherLocation *loc;
+    double latitude, longitude;
+    PyObject *ret;
+
+    loc = pyg_boxed_get (self, GWeatherLocation);
+    if (!gweather_location_has_coords (loc)) {
+	Py_INCREF (Py_None);
+	return Py_None;
+    }
+
+    gweather_location_get_coords (loc, &latitude, &longitude);
+    ret = PyTuple_New (2);
+    PyTuple_SetItem (ret, 0, PyFloat_FromDouble (latitude));
+    PyTuple_SetItem (ret, 1, PyFloat_FromDouble (longitude));
+    return ret;
+}
+%%
+new-constructor GWEATHER_TYPE_LOCATION_ENTRY
+%%
+new-constructor GWEATHER_TYPE_TIMEZONE_MENU
+%%

Added: trunk/python/gweathermodule.c
==============================================================================
--- (empty file)
+++ trunk/python/gweathermodule.c	Thu Sep  4 12:44:21 2008
@@ -0,0 +1,45 @@
+/* -*- Mode: C; c-basic-offset: 4 -*-
+ *
+ * gweathermodule.c: module wrapping gweather.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* include this first, before NO_IMPORT_PYGOBJECT is defined */
+#include <pygobject.h>
+
+void pygweather_register_classes (PyObject *d);
+void pygweather_add_constants(PyObject *module, const gchar *strip_prefix);
+void _pygweather_register_boxed_types(void);	
+
+extern PyMethodDef pygweather_functions[];
+
+DL_EXPORT(void)
+initgweather(void)
+{
+    PyObject *m, *d;
+
+    init_pygobject ();
+    g_thread_init (NULL);
+
+    m = Py_InitModule ("gweather", pygweather_functions);
+    d = PyModule_GetDict (m);
+    pygweather_register_classes (d);
+    pygweather_add_constants(m, "GWEATHER_");
+}



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