summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohan Dahlin <johan@gnome.org>2008-04-07 12:37:27 +0000
committerJohan Dahlin <johan@src.gnome.org>2008-04-07 12:37:27 +0000
commit98f1db3549f7dc056f892ceec3e81d9ee7285ce1 (patch)
treeef9d5510bd72f7a7a0e8c1c70247278dbd566550
parent901eeaf675aece3c2ba199a1532dbe07d80b51c5 (diff)
downloadpygobject-98f1db3549f7dc056f892ceec3e81d9ee7285ce1.tar.gz
pygobject-98f1db3549f7dc056f892ceec3e81d9ee7285ce1.tar.xz
pygobject-98f1db3549f7dc056f892ceec3e81d9ee7285ce1.zip
Make gio.File() (calling on an interface) a factory for creating files.
2008-04-07 Johan Dahlin <johan@gnome.org> * gio/__init__.py: * gio/gfile.override: * gio/gio-types.defs: * gio/gio.override: * tests/test_gio.py: Make gio.File() (calling on an interface) a factory for creating files. Add tests and a docstring. svn path=/trunk/; revision=764
-rw-r--r--ChangeLog10
-rw-r--r--gio/__init__.py8
-rw-r--r--gio/gfile.override90
-rw-r--r--gio/gio-types.defs10
-rw-r--r--gio/gio.override3
-rw-r--r--tests/test_gio.py19
6 files changed, 139 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index d9457c1..a4cf8b9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2008-04-07 Johan Dahlin <johan@gnome.org>
+ * gio/__init__.py:
+ * gio/gfile.override:
+ * gio/gio-types.defs:
+ * gio/gio.override:
+ * tests/test_gio.py:
+ Make gio.File() (calling on an interface) a factory for creating
+ files. Add tests and a docstring.
+
+2008-04-07 Johan Dahlin <johan@gnome.org>
+
* gio/Makefile.am (pygiodir): Install gio in the right location
2008-04-06 Johan Dahlin <johan@gnome.org>
diff --git a/gio/__init__.py b/gio/__init__.py
index 2a21d88..5735161 100644
--- a/gio/__init__.py
+++ b/gio/__init__.py
@@ -27,11 +27,17 @@ try:
except ImportError:
pass
+from gobject import GObjectMeta
from _gio import *
-
+from _gio import _file_init, _install_file_meta
try:
import unix
unix # pyflakes
except ImportError:
unix = None
del _gio
+
+class GFileMeta(GObjectMeta):
+ __call__ = _file_init
+
+_install_file_meta(GFileMeta)
diff --git a/gio/gfile.override b/gio/gfile.override
index a876cb8..96af9e0 100644
--- a/gio/gfile.override
+++ b/gio/gfile.override
@@ -20,6 +20,70 @@
* USA
*/
%%
+define _install_file_meta
+static PyObject *
+_wrap__install_file_meta(PyObject *self, PyObject *args)
+{
+ PyObject *metaclass;
+
+ if (!PyArg_ParseTuple(args, "O", &metaclass))
+ return NULL;
+
+ Py_INCREF(metaclass);
+ PyGFile_Type.ob_type = (PyTypeObject*)metaclass;
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+%%
+define _file_init kwargs
+static PyObject*
+_wrap__file_init(PyGObject *self, PyObject *args, PyObject *kwargs)
+{
+ GFile *file;
+ Py_ssize_t n_args, n_kwargs;
+ char *arg;
+
+ n_args = PyTuple_Size(args);
+ n_kwargs = kwargs != NULL ? PyDict_Size(kwargs) : 0;
+ if (n_args == 1 && n_kwargs == 0) {
+ if (!PyArg_ParseTuple(args, "s:GFile", &arg))
+ return NULL;
+ file = g_file_new_for_commandline_arg(arg);
+ } else if (n_args == 0 && n_kwargs == 1) {
+ if (PyDict_GetItemString(kwargs, "path")) {
+ char *kwlist[] = { "uri", NULL };
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "s:GFile", kwlist, &arg))
+ return NULL;
+ file = g_file_new_for_path(arg);
+ } else if (PyDict_GetItemString(kwargs, "uri")) {
+ char *kwlist[] = { "path", NULL };
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "s:GFile", kwlist, &arg))
+ return NULL;
+ file = g_file_new_for_uri(arg);
+ } else {
+ PyErr_Format(PyExc_TypeError,
+ "GFile() got an unexpected keyword argument '%s'",
+ "unknown");
+ return NULL;
+ }
+ } else {
+ PyErr_Format(PyExc_TypeError,
+ "GFile() takes exactly 1 argument (%d given)",
+ n_args + n_kwargs);
+ return NULL;
+ }
+
+ if (!file) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "could not create GFile object");
+ return NULL;
+ }
+ return pygobject_new((GObject *)file);
+}
+%%
override g_file_read_async kwargs
static PyObject *
_wrap_g_file_read_async(PyGObject *self, PyObject *args, PyObject *kwargs)
@@ -73,3 +137,29 @@ _wrap_g_file_read_async(PyGObject *self, PyObject *args, PyObject *kwargs)
Py_INCREF(Py_None);
return Py_None;
}
+/* GFile.append_to_async */
+/* GFile.create_async */
+/* GFile.enumerate_children_async */
+/* GFile.eject_mountable */
+/* GFile.find_enclosing_mount_async */
+/* GFile.load_contents_async */
+/* GFile.mount_enclosing_volume */
+/* GFile.mount_mountable */
+/* GFile.query_info_async */
+/* GFile.replace_async */
+/* GFile.replace_contents_async */
+/* GFile.set_attributes_async */
+/* GFile.set_display_name_async */
+/* GFile.unmount_mountable */
+/* GFile.load_partial_contents_async: No ArgType for GFileReadMoreCallback */
+/* GFile.copy: No ArgType for GFileProgressCallback */
+/* GFile.move: No ArgType for GFileProgressCallback */
+/* GFile.query_settable_attributes: No ArgType for GFileAttributeInfoList* */
+/* GFile.query_writable_namespaces: No ArgType for GFileAttributeInfoList* */
+/* GFile.set_attribute: No ArgType for gpointer */
+/* GFile.set_attributes_finish: No ArgType for GFileInfo** */
+/* GFile.load_contents: No ArgType for char** */
+/* GFile.load_contents_finish: No ArgType for char** */
+/* GFile.load_partial_contents_finish: No ArgType for char** */
+/* GFile.replace_contents: No ArgType for char** */
+/* GFile.replace_contents_finish: No ArgType for char** */
diff --git a/gio/gio-types.defs b/gio/gio-types.defs
index bb43ba3..cf3a2f2 100644
--- a/gio/gio-types.defs
+++ b/gio/gio-types.defs
@@ -15,6 +15,16 @@
)
(define-interface File
+ (docstring
+"File(arg, path=None, uri=None) -> gio.File subclass\n"
+"\n"
+"If arg is specified; creates a GFile with the given argument from the\n"
+"command line. The value of arg can be either a URI, an absolute path\n"
+"or a relative path resolved relative to the current working directory.\n"
+"If path is specified, create a file from an absolute or relative path.\n"
+"If uri is specified, create a file from a URI.\n\n"
+"This operation never fails, but the returned object might not \n"
+"support any I/O operation if arg points to a malformed path.")
(in-module "gio")
(c-name "GFile")
(gtype-id "G_TYPE_FILE")
diff --git a/gio/gio.override b/gio/gio.override
index c7d1655..702382d 100644
--- a/gio/gio.override
+++ b/gio/gio.override
@@ -89,6 +89,9 @@ ignore-glob
*free
g_simple_async_result_new_error
g_simple_async_report_error_in_idle
+ g_file_new_from_commandline_arg
+ g_file_new_from_path
+ g_file_new_from_uri
%%
override g_drive_get_volumes noargs
static PyObject *
diff --git a/tests/test_gio.py b/tests/test_gio.py
index 205b312..26ff2e8 100644
--- a/tests/test_gio.py
+++ b/tests/test_gio.py
@@ -32,6 +32,25 @@ class TestFile(unittest.TestCase):
loop = gobject.MainLoop()
loop.run()
+ def testConstructor(self):
+ for gfile in [gio.File("/"),
+ gio.File("file:///"),
+ gio.File(uri="file:///"),
+ gio.File(path="/"),
+ gio.File(u"/"),
+ gio.File(path=u"/")]:
+ self.failUnless(isinstance(gfile, gio.File))
+ self.assertEquals(gfile.get_path(), "/")
+ self.assertEquals(gfile.get_uri(), "file:///")
+
+ def testConstructorError(self):
+ self.assertRaises(TypeError, gio.File)
+ self.assertRaises(TypeError, gio.File, 1)
+ self.assertRaises(TypeError, gio.File, "foo", "bar")
+ self.assertRaises(TypeError, gio.File, foo="bar")
+ self.assertRaises(TypeError, gio.File, uri=1)
+ self.assertRaises(TypeError, gio.File, path=1)
+
class TestGFileEnumerator(unittest.TestCase):
def setUp(self):