[Jppy-commit] [SCM] jppy - Python bindings and API for jpilot databases and a GTK2 GUI branch, devel-merge-calendar-and-plugin, updated. releases/0.0.48-177-g758a543

Nick Piper nick-git@nickpiper.co.uk
Fri Jan 2 23:28:05 GMT 2009


The following commit has been merged in the devel-merge-calendar-and-plugin branch:
commit 758a5438ba7dbb197faf1418c5683dd40e9e5be5
Author: Nick Piper <nick-git@nickpiper.co.uk>
Date:   Fri Jan 2 23:26:53 2009 +0000

    Starting to let todo use the plugin-filters; but still needs some API rework

diff --git a/jppy/datebk6.py b/jppy/datebk6.py
index 98b8277..157618a 100644
--- a/jppy/datebk6.py
+++ b/jppy/datebk6.py
@@ -19,6 +19,7 @@ class TodoProcessor(Component):
         return field in self._keys + ['note']
 
     def get_field(self, todo, item):
+        #print "Getting field!", todo, item
         if item in self._keys:
             if hasattr(self, "_get_%s" % item):
                 return getattr(self, "_get_%s" % item)(todo)
@@ -30,12 +31,15 @@ class TodoProcessor(Component):
                 return todo['note']
 
     def set_field(self, todo, item, value):
+        #print "Setting field!", todo, item, value
         if item in self._keys:
             if hasattr(self, "_set_%s" % item):
                 return getattr(self, "_set_%s" % item)(todo, value)
             raise NotImplementedError("Unable to set %s yet." % item)
         elif item == "note":
-            todo['note'] = "".join(self._read_dbfield()) + value
+            # argh, we'll have to find a way to get this to AVOID using the filters
+            # otherwise we have a infinite recursion problem.
+            todo['note'] = "".join(self._read_dbfield(todo)) + value
 
     def _get_urgency(self, todo):
         dbfield = self._read_dbfield(todo)
diff --git a/python_module_src/pytype_todos.c b/python_module_src/pytype_todos.c
index d6273f0..b8e58e3 100644
--- a/python_module_src/pytype_todos.c
+++ b/python_module_src/pytype_todos.c
@@ -22,6 +22,7 @@ extern PyObject* PyPiTodo_New(PyTypeObject *type, PyObject *args, PyObject *kwds
   self = (PyPiTodo *)type->tp_alloc(type, 0);
   new_ToDo(&(self->a));
   SetBasicRecordObjectAttributeDefaults((PyObject*) self, pack_ToDo);  
+  self->filters = NULL;
 
   return (PyObject*)self;
 }
@@ -40,6 +41,14 @@ extern int PyPiTodo_Init(PyObject *self, PyObject *args, PyObject *kwds) {
   
   todo = (PyPiTodo*)self;
   /* we have to support calling __init__ more than once */
+  if (todo->filters != NULL) {
+    Py_DECREF(todo->filters);
+    todo->filters = NULL;
+  }
+  if (filters != NULL) {
+    todo->filters = filters;
+    Py_INCREF(filters);
+  }
   free_ToDo(&(todo->a));
   if (todo->saved_br.size > 0 && todo->saved_br.buf) {	
     free(todo->saved_br.buf);					
@@ -168,6 +177,10 @@ extern PyObject* PyPiTodo_Wrap(struct ToDo* a, PCRecType rt,
 
 static void PyPiTodo_Dealloc(PyPiTodo* self) {
   free_ToDo(&(self->a));
+  if (self->filters != NULL) {
+    Py_DECREF(self->filters);
+    self->filters = NULL;
+  }
   if (self->saved_br.size > 0 && self->saved_br.buf) {
     free(self->saved_br.buf);
   }
@@ -211,15 +224,35 @@ static char *PyPiTodo_key_list[] = {
   NULL};
 
 static PyObject* PyPiTodo_keys(PyObject* self) {
+  PyPiBase* base = NULL;
   PyObject *list = PyList_New(0);
   int n = 0;
-
+  base = (PyPiBase*)self;
   while (PyPiTodo_key_list[n]) {
     PyObject *value;
     value = PyString_FromString(PyPiTodo_key_list[n++]);
     PyList_Append(list, value);
     Py_DECREF(value);
   }
+  if (base->filters != NULL) {
+    //printf("Have filters to use!\n");
+    if (PySequence_Check(base->filters)) {
+      //printf("And it's a sequence\n");
+      Py_ssize_t i, n;
+      for (i=0; i<PySequence_Size(base->filters); i++) {
+	//printf("Using filter %d\n", i);
+	PyObject *filter = PySequence_GetItem(base->filters, i); // new ref
+	PyObject *keys = PyObject_CallMethod(filter, "fields", NULL);
+	for (n=0; n<PySequence_Size(keys); n++) {
+	  PyObject *item = PySequence_GetItem(keys, n); // new ref
+	  PyList_Append(list, item);
+	  Py_DECREF(item);
+	}
+	Py_DECREF(keys);
+	Py_DECREF(filter);
+      }
+    }
+  }
   return list;
 }
 
@@ -295,6 +328,38 @@ PyObject *PyPiTodo_GetItem(PyPiTodo* self,  PyObject* key) {
     return Py_None;  
   }
 
+  // disabled until we add a way to avoid infinite recursion when
+  // a filter tries to handle 'note' AND read 'note'
+#if 0
+  // once this works well, we could put it into the 'basic' files
+  if (self->filters != NULL) {
+    printf("Have filters to use!\n");
+    if (PySequence_Check(self->filters)) {
+      //printf("And it's a sequence\n");
+      Py_ssize_t i, n;
+      for (i=0; i<PySequence_Size(self->filters); i++) {
+	int handled=0;
+	//printf("Using filter %d\n", i);
+	PyObject *filter = PySequence_GetItem(self->filters, i); // new ref
+	PyObject *handles_field = PyObject_CallMethod(filter, "handles_field", "O", key);
+	if (Py_True == handles_field) {
+	  printf("Using filter %d to handle field\n", i);
+	  PyObject *result = PyObject_CallMethod(filter, "get_field",
+						 "(OO)", self, key);
+	  if (result == NULL) {
+	    Py_DECREF(handles_field);
+	    Py_DECREF(filter);
+	    return NULL;
+	  }
+	  Py_DECREF(handles_field);
+	  Py_DECREF(filter);
+	  return result;
+	}
+      }
+    }
+  }
+#endif
+
   Py_INCREF(key);
   keystring = PyString_AsString(key);
 
@@ -320,6 +385,42 @@ int PyPiTodo_SetItem(PyPiTodo* self, PyObject* key, PyObject* value) {
     return -1;
   }
   
+  // disabled until we add a way to avoid infinite recursion when
+  // a filter tries to handle 'note' AND read 'note'
+#if 0
+  // once this works well, we could put it into the 'basic' files
+  if (self->filters != NULL) {
+    //printf("Have filters to use!\n");
+    if (PySequence_Check(self->filters)) {
+      //printf("And it's a sequence\n");
+      Py_ssize_t i, n;
+      for (i=0; i<PySequence_Size(self->filters); i++) {
+	int handled=0;
+	//printf("Using filter %d\n", i);	
+	PyObject *filter = PySequence_GetItem(self->filters, i); // new ref
+	PyObject *handles_field = PyObject_CallMethod(filter, "handles_field", "O", key);
+	if (Py_True == handles_field) {
+	  //printf("Using filter %d to handle field\n", i);
+	  PyObject *result = PyObject_CallMethod(filter, "set_field", 
+						 "(OOO)", self, key, value);
+	  if (result == NULL) {
+	    Py_DECREF(handles_field);
+	    Py_DECREF(filter);
+	    return -1;
+	  }
+	  Py_DECREF(result);
+	  handled = 1;
+	}
+	Py_DECREF(handles_field);
+	Py_DECREF(filter);
+	if(handled) {
+	  return 0;
+	}
+      }
+    }
+  }
+#endif
+
   Py_INCREF(key);
   keystring = PyString_AsString(key);
 
diff --git a/tests/pluginTests.py b/tests/pluginTests.py
index b60f403..f0d945d 100644
--- a/tests/pluginTests.py
+++ b/tests/pluginTests.py
@@ -14,10 +14,10 @@ class todoPluginsTest(baseclass.baseTest):
 
     def testUrgencyByPlugins(self):
         todo = self.env.taskList.new()
-        # this will soon by done by the todo itself
-        for plugin in self.env.taskList.record_field_filters:
-            if plugin.handles_field('urgency'):
-                plugin.set_field(todo, 'urgency', 'A')
+        # implementation not finished yet
+        #assert 'urgency' in todo.keys()
+        #todo['urgency'] = 'a'
+        #assert todo['urgency'] == 'A'
 
 def suite():
     suite = unittest.TestSuite((unittest.makeSuite(todoPluginsTest),))

-- 
jppy - Python bindings and API for jpilot databases and a GTK2 GUI



More information about the Jppy-commit mailing list