pygobject r935 - in trunk: . gio tests



Author: jmatthew
Date: Sun Aug 10 03:28:03 2008
New Revision: 935
URL: http://svn.gnome.org/viewvc/pygobject?rev=935&view=rev

Log:
2008-08-10  Jonathan Matthew  <jonathan d14n org>

	Bug 547067 â add File.replace_contents, replace_contents_async,
	replace_contents_finish.

	* gio/gfile.override:
	* gio/gio.defs:
	* tests/test_gio.py:
	Add overrides, docs, and tests for File.replace_contents,
	replace_contents_async, and replace_contents_finish.


Modified:
   trunk/ChangeLog
   trunk/gio/gfile.override
   trunk/gio/gio.defs
   trunk/tests/test_gio.py

Modified: trunk/gio/gfile.override
==============================================================================
--- trunk/gio/gfile.override	(original)
+++ trunk/gio/gfile.override	Sun Aug 10 03:28:03 2008
@@ -972,14 +972,163 @@
     Py_INCREF(Py_None);
     return Py_None;
 }
+%%
+override g_file_replace_contents kwargs
+static PyObject *
+_wrap_g_file_replace_contents(PyGObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *kwlist[] = { "contents", "etag", "make_backup",
+                              "flags", "cancellable", NULL };
+    GCancellable *cancellable;
+    PyGObject *pycancellable = NULL;
+    GFileCreateFlags flags = G_FILE_CREATE_NONE;
+    PyObject *py_flags = NULL;
+    gsize length;
+    gboolean make_backup = FALSE;
+    char *contents;
+    char *etag = NULL;
+    char *new_etag = NULL;
+    GError *error = NULL;
+    gboolean ret;
+    PyObject *py_ret;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+                                     "s#|zbOO:File.replace_contents",
+                                     kwlist,
+                                     &contents,
+                                     &length,
+                                     &etag,
+                                     &make_backup,
+                                     &flags,
+                                     &cancellable))
+    {
+        return NULL;
+    }
+
+    if (py_flags && pyg_flags_get_value(G_TYPE_FILE_CREATE_FLAGS,
+                                        py_flags, (gpointer)&flags))
+        return NULL;
+
+    if (!pygio_check_cancellable(pycancellable, &cancellable))
+        return NULL;
+
+    ret = g_file_replace_contents(G_FILE(self->obj), contents, length, etag,
+                                  make_backup, flags, &new_etag, cancellable,
+                                  &error);
+
+    if (pyg_error_check(&error))
+        return NULL;
+
+    if (ret) {
+        py_ret = PyString_FromString(new_etag);
+    } else {
+        py_ret = Py_None;
+        Py_INCREF(py_ret);
+    }
+
+    g_free(new_etag);
+    return py_ret;
+}
+%%
+override g_file_replace_contents_finish kwargs
+static PyObject *
+_wrap_g_file_replace_contents_finish(PyGObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *kwlist[] = { "result", NULL };
+    PyGObject *res;
+    gchar *etag_out = NULL;
+    GError *error = NULL;
+    gboolean ret;
+    PyObject *py_ret;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+                                     "O!:File.replace_contents_finish",
+                                      kwlist,
+                                      &PyGAsyncResult_Type,
+                                      &res))
+        return NULL;
+
+    ret = g_file_replace_contents_finish(G_FILE(self->obj),
+                                         G_ASYNC_RESULT(res->obj), &etag_out,
+                                         &error);
+
+    if (pyg_error_check(&error))
+        return NULL;
+
+    if (ret) {
+        py_ret = PyString_FromString(etag_out);
+        return py_ret;
+    } else {
+        py_ret = Py_None;
+        Py_INCREF(py_ret);
+    }
+
+    g_free(etag_out);
+    return py_ret;
+}
+%%
+override g_file_replace_contents_async kwargs
+static PyObject *
+_wrap_g_file_replace_contents_async(PyGObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *kwlist[] = { "contents", "callback", "etag", "make_backup",
+                              "flags", "cancellable", "user_data", NULL };
+    GCancellable *cancellable;
+    PyGObject *pycancellable = NULL;
+    PyGIONotify *notify;
+    GFileCreateFlags flags = G_FILE_CREATE_NONE;
+    PyObject *py_flags = NULL;
+    gsize length;
+    gboolean make_backup = FALSE;
+    char *contents;
+    char *etag = NULL;
+
+    notify = g_slice_new0(PyGIONotify);
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+                                     "s#O|zbOOO:File.replace_contents_async",
+                                      kwlist,
+                                      &contents,
+                                      &length,
+                                      &notify->callback,
+                                      &etag,
+                                      &make_backup,
+                                      &py_flags,
+                                      &pycancellable,
+                                      &notify->data))
+    {
+      g_slice_free(PyGIONotify, notify);
+      return NULL;
+    }
+
+    if (py_flags && pyg_flags_get_value(G_TYPE_FILE_CREATE_FLAGS,
+                                        py_flags, (gpointer)&flags))
+        return NULL;
+
+    if (!pygio_check_cancellable(pycancellable, &cancellable))
+        return NULL;
+
+    Py_INCREF(notify->callback);
+    Py_XINCREF(notify->data);
+    g_file_replace_contents_async(G_FILE(self->obj),
+                                  contents,
+                                  length,
+                                  etag,
+                                  make_backup,
+                                  flags,
+                                  cancellable,
+                                  (GAsyncReadyCallback)async_result_callback_marshal,
+                                  notify);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
 /* GFile.eject_mountable */
 /* GFile.find_enclosing_mount_async */
-/* GFile.replace_contents_async */
 /* GFile.set_attributes_async */
 /* GFile.set_display_name_async */
 /* GFile.load_partial_contents_async: No ArgType for GFileReadMoreCallback */
 /* GFile.move: No ArgType for GFileProgressCallback */
 /* GFile.set_attributes_finish: No ArgType for GFileInfo** */
 /* GFile.load_partial_contents_finish: No ArgType for char** */
-/* GFile.replace_contents: No ArgType for char** */
-/* GFile.replace_contents_finish: No ArgType for char** */

Modified: trunk/gio/gio.defs
==============================================================================
--- trunk/gio/gio.defs	(original)
+++ trunk/gio/gio.defs	Sun Aug 10 03:28:03 2008
@@ -2263,6 +2263,18 @@
 )
 
 (define-method replace_contents
+  (docstring
+  "F.replace_contents(contents, [etag, [make_backup, [flags, [cancellable]]]])\n"
+  "-> etag_out\n"
+  "\n"
+  "Replaces the content of the file, returning the new etag value for the\n"
+  "file. If an etag is specified, any existing file must have that etag, or\n"
+  "the error gio.IO_ERROR_WRONG_ETAG will be returned.\n"
+  "If make_backup is True, this method will attempt to make a backup of the\n"
+  "file. If cancellable is not None, then the operation can be cancelled by\n"
+  "triggering the cancellable object from another thread. If the operation\n"
+  "was cancelled, the error gio.IO_ERROR_CANCELLED will be returned.\n"
+  )
   (of-object "GFile")
   (c-name "g_file_replace_contents")
   (return-type "gboolean")
@@ -2279,6 +2291,20 @@
 )
 
 (define-method replace_contents_async
+  (docstring
+  "F.replace_contents_async(contents, callback, [etag, [make_backup, [flags,\n"
+  "                         [cancellable]]]]) -> etag_out\n"
+  "\n"
+  "Starts an asynchronous replacement of the file with the given contents.\n"
+  "For more details, see F.replace_contents() which is the synchronous\n"
+  "version of this call.\n\n"
+  "When the load operation has completed, callback will be called with\n"
+  "user data. To finish the operation, call F.replace_contents_finish() with\n"
+  "the parameter 'res' returned by the callback.\n\n"
+  "If cancellable is not None, then the operation can be cancelled by\n"
+  "triggering the cancellable object from another thread. If the operation\n"
+  "was cancelled, the error gio.IO_ERROR_CANCELLED will be returned.\n"
+  )
   (of-object "GFile")
   (c-name "g_file_replace_contents_async")
   (return-type "none")
@@ -2295,6 +2321,11 @@
 )
 
 (define-method replace_contents_finish
+  (docstring
+  "F.replace_contents_finish(res) -> etag_out\n\n"
+  "Finishes an asynchronous replacement of the file's contents.\n"
+  "The new entity tag for the file is returned.\n"
+  )
   (of-object "GFile")
   (c-name "g_file_replace_contents_finish")
   (return-type "gboolean")

Modified: trunk/tests/test_gio.py
==============================================================================
--- trunk/tests/test_gio.py	(original)
+++ trunk/tests/test_gio.py	Sun Aug 10 03:28:03 2008
@@ -373,6 +373,47 @@
                                               10, gio.FILE_QUERY_INFO_NONE)
                 self.assertEqual(ret, True)
 
+    def testReplaceContents(self):
+        self.file.replace_contents("testing replace_contents")
+        cont, leng, etag = self.file.load_contents()
+        self.assertEqual(cont, "testing replace_contents")
+
+        caught = False
+        try:
+            self.file.replace_contents("this won't work", etag="wrong")
+        except gio.Error, e:
+            self.assertEqual(e.code, gio.ERROR_WRONG_ETAG)
+            caught = True
+        self.failUnless(caught)
+
+        self.file.replace_contents("testing replace_contents again", etag=etag)
+        cont, leng, etag = self.file.load_contents()
+        self.assertEqual(cont, "testing replace_contents again")
+
+        self.file.replace_contents("testing replace_contents yet again", etag=None)
+        cont, leng, etag = self.file.load_contents()
+        self.assertEqual(cont, "testing replace_contents yet again")
+
+    def testReplaceContentsAsync(self):
+
+        def callback(contents, result):
+            try:
+                newetag = contents.replace_contents_finish(result)
+                cont, leng, etag = self.file.load_contents()
+                self.assertEqual(cont, "testing replace_contents_async")
+                self.assertEqual(leng, 30)
+                self.assertEqual(etag, newetag)
+                self.assertNotEqual(newetag, '')
+            finally:
+                loop.quit()
+
+        canc = gio.Cancellable()
+        self.file.replace_contents_async("testing replace_contents_async", callback, cancellable=canc)
+
+        loop = glib.MainLoop()
+        loop.run()
+
+    
 
 class TestGFileEnumerator(unittest.TestCase):
     def setUp(self):



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