[Thuban-commits] r2744 - branches/WIP-pyshapelib-bramz/libraries/pyshapelib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Mar 15 14:48:59 CET 2007


Author: bramz
Date: 2007-03-15 14:48:58 +0100 (Thu, 15 Mar 2007)
New Revision: 2744

Modified:
   branches/WIP-pyshapelib-bramz/libraries/pyshapelib/ChangeLog
   branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c
   branches/WIP-pyshapelib-bramz/libraries/pyshapelib/shapelibmodule.c
Log:
shapelibmodule.c, dbflibmodule.c: added some Unicode support for the filenames (no internal encoding for DBFFile yet).  It now should similar Unicode support Python's file() (concerning the filename, that is).

Modified: branches/WIP-pyshapelib-bramz/libraries/pyshapelib/ChangeLog
===================================================================
--- branches/WIP-pyshapelib-bramz/libraries/pyshapelib/ChangeLog	2007-03-14 20:53:53 UTC (rev 2743)
+++ branches/WIP-pyshapelib-bramz/libraries/pyshapelib/ChangeLog	2007-03-15 13:48:58 UTC (rev 2744)
@@ -1,3 +1,9 @@
+2007-03-15	Bram de Greve <bram.degreve at intec.ugent.be>
+
+	* shapelibmodule.c, dbflibmodule.c: added some Unicode support for the
+	filenames (no internal encoding for DBFFile yet).  It now should similar
+	Unicode support Python's file() (concerning the filename, that is).
+
 2007-03-14  Bram de Greve <bram.degreve at intec.ugent.be>
 
 	* shapelibmodule.c: added support for shapetypes with Z and M values

Modified: branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c
===================================================================
--- branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c	2007-03-14 20:53:53 UTC (rev 2743)
+++ branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c	2007-03-15 13:48:58 UTC (rev 2744)
@@ -43,9 +43,12 @@
 		PyErr_Format(PyExc_TypeError, "dbflib.DBFFile.__init__ takes no keyword arguments");
 		return -1;
 	}
-	if (!PyArg_ParseTuple(args, "s|s", &file, &mode)) return -1;
+
+	if (!PyArg_ParseTuple(args, "et|s:__init__", Py_FileSystemDefaultEncoding, &file, &mode)) return -1;
 	
 	self->handle = DBFOpen(file, mode);
+	PyMem_Free(file);
+	
 	return self->handle ? 0 : -1;
 }
 
@@ -79,7 +82,7 @@
 	char field_name[12];
 	int field, width = 0, decimals = 0, field_type;
 	
-	if (!PyArg_ParseTuple(args, "i", &field)) return NULL;
+	if (!PyArg_ParseTuple(args, "i:field_info", &field)) return NULL;
 	
 	field_name[0] = '\0';
 	field_type = DBFGetFieldInfo(self->handle, field, field_name, &width, &decimals);
@@ -95,7 +98,7 @@
 	int type, width, decimals;
 	int field;
 	
-	if (!PyArg_ParseTuple(args, "siii", &name, &type, &width, &decimals)) return NULL;
+	if (!PyArg_ParseTuple(args, "siii:add_field", &name, &type, &width, &decimals)) return NULL;
 	
 	field = DBFAddField(self->handle, name, (DBFFieldType)type, width, decimals);
 	
@@ -170,7 +173,7 @@
 {
 	int record, field;
 
-	if (!PyArg_ParseTuple(args, "ii", &record, &field)) return NULL;
+	if (!PyArg_ParseTuple(args, "ii:read_field", &record, &field)) return NULL;
 	
 	if (record < 0 || record >= DBFGetRecordCount(self->handle))
 	{
@@ -206,7 +209,7 @@
 	PyObject *dict;
 	PyObject *value = NULL;
 
-	if (!PyArg_ParseTuple(args, "i", &record)) return NULL;
+	if (!PyArg_ParseTuple(args, "i:read_record", &record)) return NULL;
 
 	if (record < 0 || record >= DBFGetRecordCount(self->handle))
 	{
@@ -312,7 +315,7 @@
 	PyObject* value;
 	int type;
 
-	if (!PyArg_ParseTuple(args, "iiO", &record, &field, &value)) return NULL;
+	if (!PyArg_ParseTuple(args, "iiO:write_field", &record, &field, &value)) return NULL;
 	
 	if (field < 0 || field >= DBFGetFieldCount(self->handle))
 	{
@@ -339,7 +342,7 @@
 	char name[12];
 	PyObject* value = NULL;
 	
-	if (!PyArg_ParseTuple(args, "iO", &record, &record_object)) return NULL;
+	if (!PyArg_ParseTuple(args, "iO:write_record", &record, &record_object)) return NULL;
 	
 	num_fields = DBFGetFieldCount(self->handle);
 	
@@ -418,27 +421,46 @@
 
 static struct PyMethodDef dbffile_methods[] = 
 {
-	{"close", (PyCFunction)dbffile_close, METH_NOARGS, "close DBFFile"},
-	{"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS, "return number of fields currently defined"},
-	{"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS, "return number of records that currently exist"},
-	{"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS, 
+	{"close", (PyCFunction)dbffile_close, METH_NOARGS, 
+		"close()\n"
+		"close DBFFile"},
+	{"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS, 
+		"field_count()\n"
+		"returns number of fields currently defined"},
+	{"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS, 
+		"record_count()\n"
+		"returns number of records that currently exist"},
+	{"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS,
+		"field_info(field_index)\n"
 		"returns info of a field as a tuple (type, name, width, decimals) with:\n"
 		"-type: the type of the field corresponding to the integer value of one of the constants FTString, FTInteger, ...\n"
 		"-name: the name of the field as a string\n"
 		"-width: the width of the field as a number of characters\n"
 		"-decimals: the number of decimal digits" },
 	{"add_field", (PyCFunction)dbffile_add_field, METH_VARARGS,
+		"add_field(type, name, width, decimals)\n"
 		"adds a new field and returns field index if successful\n"
 		"-type: the type of the field corresponding to the integer value of one of the constants FTString, FTInteger, ...\n"
 		"-name: the name of the field as a string\n"
 		"-width: the width of the field as a number of characters\n"
 		"-decimals: the number of decimal digits" },
-	{"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS, "return the value of one field of a record"},
-	{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, "return an entire record as a dict of field names and values"},
-	{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, "write a single field of a record"},
-	{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, "write an entire record as a dict or a sequence"},
+	{"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS, 
+		"read_attribute(record_index, field_index)\n"
+		"return the value of one field of a record"},
+	{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, 
+		"read_record(record_index)\n"
+		"return an entire record as a dict of field names and values"},
+	{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, 
+		"write_field(record_index, field_index, new_value)\n"
+		"write a single field of a record"},
+	{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, 
+		"write_record(record_index, record)\n"
+		"write an entire record as a dict or a sequence\n"
+		"record can either be a dictionary in which case the keys are used as field names, "
+		"or a sequence that must have an item for every field (length = field_count())"},
 #if HAVE_UPDATE_HEADER
-	{"commit", (PyCFunction)dbffile_read_record, METH_NOARGS, NULL},
+	{"commit", (PyCFunction)dbffile_read_record, METH_NOARGS, 
+		"commit()"},
 #endif
 	{NULL}
 };
@@ -470,7 +492,7 @@
 	char* file;
 	DBFFileObject* result;
 	
-	if (!PyArg_ParseTuple(args, "s", &file)) return NULL;
+	if (!PyArg_ParseTuple(args, "et:create", Py_FileSystemDefaultEncoding, &file)) return NULL;
 	
 	result = PyObject_New(DBFFileObject, &DBFFileType);
 	if (!result)
@@ -493,8 +515,12 @@
 
 static struct PyMethodDef dbflib_methods[] = 
 {
-	{"open", (PyCFunction)dbflib_open, METH_VARARGS, "open a DBFFile" },
-	{"create", (PyCFunction)dbflib_create, METH_VARARGS, "create a DBFFile" },
+	{"open", (PyCFunction)dbflib_open, METH_VARARGS, 
+		"open(filename [, mode])\n"
+		"open a DBFFile" },
+	{"create", (PyCFunction)dbflib_create, METH_VARARGS, 
+		"create(filename)\n"
+		"create a DBFFile" },
 	{NULL}
 };
 

Modified: branches/WIP-pyshapelib-bramz/libraries/pyshapelib/shapelibmodule.c
===================================================================
--- branches/WIP-pyshapelib-bramz/libraries/pyshapelib/shapelibmodule.c	2007-03-14 20:53:53 UTC (rev 2743)
+++ branches/WIP-pyshapelib-bramz/libraries/pyshapelib/shapelibmodule.c	2007-03-15 13:48:58 UTC (rev 2744)
@@ -105,7 +105,7 @@
 		PyErr_Format(PyExc_TypeError, "shapelib.SHPObject.__init__ takes no keyword arguments");
 		return -1;
 	}
-	if (!PyArg_ParseTuple(args, "iiO|O", &type, &id, &parts, &part_type_list)) return -1;
+	if (!PyArg_ParseTuple(args, "iiO|O:__init__", &type, &id, &parts, &part_type_list)) return -1;
 
 	/* check parts */
 	if (!PySequence_Check(parts))
@@ -205,14 +205,14 @@
 			switch (vertex_type)
 			{
 			case vtXY:
-				ok = PyArg_ParseTuple(vertex, "dd", xs + part_start + j, ys + part_start + j);
+				ok = PyArg_ParseTuple(vertex, "dd:__init__", xs + part_start + j, ys + part_start + j);
 				break;
 			case vtXYM:
-				ok = PyArg_ParseTuple(vertex, "ddd", xs + part_start + j, ys + part_start + j, ms + part_start + j);
+				ok = PyArg_ParseTuple(vertex, "ddd:__init__", xs + part_start + j, ys + part_start + j, ms + part_start + j);
 				break;
 			case vtXYZM:
 				ms[part_start + j] = 0.;
-				ok = PyArg_ParseTuple(vertex, "ddd|d", xs + part_start + j, ys + part_start + j, zs + part_start + j,
+				ok = PyArg_ParseTuple(vertex, "ddd|d:__init__", xs + part_start + j, ys + part_start + j, zs + part_start + j,
 					ms + part_start + j);
 				break;
 			}
@@ -436,16 +436,23 @@
 
 static struct PyMethodDef shpobject_methods[] = 
 {
-	{"extents", (PyCFunction)shpobject_extents, METH_NOARGS, NULL},
-	{"vertices", (PyCFunction)shpobject_vertices, METH_NOARGS, NULL},
-	{"part_types", (PyCFunction)shpobject_part_types, METH_NOARGS, NULL},
+	{"extents", (PyCFunction)shpobject_extents, METH_NOARGS, 
+		"extents()\n"
+		"returns ((x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max)), the 4D bounding box of the SHPObject"},
+	{"vertices", (PyCFunction)shpobject_vertices, METH_NOARGS, 
+		"vertices()\n"
+		"returns [[(x, y, ...), ...], ...], a list of object parts, where each part is again a list of vertices.\n"
+		"each vertex is a tuple of two to four doubles, depending on the object type."},
+	{"part_types", (PyCFunction)shpobject_part_types, METH_NOARGS, 
+		"part_types()\n"
+		"returns a tuple of integers, each integer indicating the type of the corresponding part in vertices()"},
 	{NULL}
 };
 
 static struct PyGetSetDef shpobject_getsetters[] = 
 {
-	{"type", (getter)shpobject_type, NULL, NULL },
-	{"id", (getter)shpobject_id, NULL, NULL },
+	{"type", (getter)shpobject_type, NULL, "type of the object (read-only)" },
+	{"id", (getter)shpobject_id, NULL, "id of the object (read-only)" },
 	{NULL}
 };
 
@@ -490,7 +497,7 @@
 		PyErr_Format(PyExc_TypeError, "shapelib.ShapeFile.__init__ takes no keyword arguments");
 		return -1;
 	}
-	if (!PyArg_ParseTuple(args, "s|s", &file, &mode)) return -1;
+	if (!PyArg_ParseTuple(args, "et|s:__init__", Py_FileSystemDefaultEncoding, &file, &mode)) return -1;
 	
 	self->handle = SHPOpen(file, mode);
 	return self->handle ? 0 : -1;
@@ -518,7 +525,7 @@
 	SHPObject* object;
 	SHPObjectObject* result;
 	
-	if (!PyArg_ParseTuple(args, "i", &index)) return NULL;
+	if (!PyArg_ParseTuple(args, "i:read_object", &index)) return NULL;
 	
 	object = SHPReadObject(self->handle, index);	
 	if (!object)
@@ -542,7 +549,7 @@
 	int index, result;
 	PyObject* object;
 	
-	if (!PyArg_ParseTuple(args, "iO", &index, &object)) return NULL;
+	if (!PyArg_ParseTuple(args, "iO:write_object", &index, &object)) return NULL;
 	
 	if (!PyObject_IsInstance(object, (PyObject*)&SHPObjectType))
 	{
@@ -572,13 +579,25 @@
 
 static struct PyMethodDef shapefile_methods[] = 
 {
-	{"close", (PyCFunction)shapefile_close, METH_NOARGS, "close the shape file" },
-	{"info", (PyCFunction)shapefile_info, METH_NOARGS, 
-		"Return a tuple (NUM_SHAPES, TYPE, MIN, MAX) where NUM_SHAPES is the number of shapes in the file, TYPE is the "
-		"shape type and MIN and MAX are 4-element tuples with the min. and max. values of the data." },
-	{"read_object", (PyCFunction)shapefile_read_object, METH_VARARGS, "Return object number i" },
-	{"write_object", (PyCFunction)shapefile_write_object, METH_VARARGS, "Write an object"},
-	{"cobject", (PyCFunction)shapefile_cobject, METH_NOARGS, "Return the shapelib SHPHandle as a Python CObject"},
+	{"close", (PyCFunction)shapefile_close, METH_NOARGS, 
+		"close()\n"
+		"close the shape file" },
+	{"info", (PyCFunction)shapefile_info, METH_NOARGS,
+		"info()\n"
+		"returns (num_shapes, type, (x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max)) with:\n"
+		"-num_shapes: the number of the objects in the file\n"
+		"-type: the type of the shape file (SHPT_POINT, SHPT_POLYGON, ...)\n"
+		"-(x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max): 4D bounding box of the data in the shape file" },
+	{"read_object", (PyCFunction)shapefile_read_object, METH_VARARGS, 
+		"read_object(id)\n"
+		"Return object indexed by id" },
+	{"write_object", (PyCFunction)shapefile_write_object, METH_VARARGS, 
+		"write_object(id, object)\n"
+		"Write an object at index id.\n"
+		"If id == -1, the object is appended at the end of the shape file"},
+	{"cobject", (PyCFunction)shapefile_cobject, METH_NOARGS, 
+		"cobject()\n"
+		"Return the shapelib SHPHandle as a Python CObject"},
 	{NULL}
 };
 
@@ -602,7 +621,7 @@
 	int type;
 	ShapeFileObject* result;
 	
-	if (!PyArg_ParseTuple(args, "si", &file, &type)) return NULL;
+	if (!PyArg_ParseTuple(args, "eti:create", Py_FileSystemDefaultEncoding, &file, &type)) return NULL;
 	
 	result = PyObject_New(ShapeFileObject, &ShapeFileType);
 	if (!result)
@@ -638,24 +657,34 @@
 static PyObject* shapelib_type_name(PyObject* module, PyObject* args)
 {
 	int type;
-	if (!PyArg_ParseTuple(args, "i", &type)) return NULL;
+	if (!PyArg_ParseTuple(args, "i:type_name", &type)) return NULL;
 	return PyString_FromString(SHPTypeName(type));
 }
 
 static PyObject* shapelib_part_type_name(PyObject* module, PyObject* args)
 {
 	int type;
-	if (!PyArg_ParseTuple(args, "i", &type)) return NULL;
+	if (!PyArg_ParseTuple(args, "i:part_type_name", &type)) return NULL;
 	return PyString_FromString(SHPPartTypeName(type));
 }
 
 static struct PyMethodDef shapelib_methods[] = 
 {
-	{"open", (PyCFunction)shapelib_open, METH_VARARGS, "open a ShapeFile" },
-	{"create", (PyCFunction)shapelib_create, METH_VARARGS, "create a ShapeFile" },
-	{"c_api", (PyCFunction)shapelib_c_api, METH_NOARGS, "get C API of shapelib" },
-	{"type_name", (PyCFunction)shapelib_type_name, METH_VARARGS, "return type as string" },
-	{"part_type_name", (PyCFunction)shapelib_part_type_name, METH_VARARGS, "return part type as string" },
+	{"open", (PyCFunction)shapelib_open, METH_VARARGS, 
+		"open(filename [, mode='rb'])\n"
+		"open a ShapeFile" },
+	{"create", (PyCFunction)shapelib_create, METH_VARARGS, 
+		"create(filename, type)\n"
+		"create a ShapeFile of a certain type (one of SHPT_POINT, SHPT_POLYGON)" },
+	{"c_api", (PyCFunction)shapelib_c_api, METH_NOARGS, 
+		"c_api()\n"
+		"get C API of shapelib" },
+	{"type_name", (PyCFunction)shapelib_type_name, METH_VARARGS, 
+		"type_name(type)\n"
+		"return type as string" },
+	{"part_type_name", (PyCFunction)shapelib_part_type_name, METH_VARARGS, 
+		"part_type_name(part_type)\n"
+		"return part type as string" },
 	{NULL}
 };
 



More information about the Thuban-commits mailing list