38 |
{ |
{ |
39 |
char* file; |
char* file; |
40 |
char* mode = "rb"; |
char* mode = "rb"; |
41 |
if (kwds != NULL && PyDict_Size(kwds) > 0) |
static char *kwlist[] = {"name", "mode", NULL}; |
42 |
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:__init__", kwlist, |
43 |
|
Py_FileSystemDefaultEncoding, &file, &mode)) return -1; |
44 |
|
|
45 |
|
self->handle = DBFOpen(file, mode); |
46 |
|
if (!self->handle) |
47 |
{ |
{ |
48 |
PyErr_Format(PyExc_TypeError, "dbflib.DBFFile.__init__ takes no keyword arguments"); |
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
|
return -1; |
|
49 |
} |
} |
50 |
if (!PyArg_ParseTuple(args, "s|s", &file, &mode)) return -1; |
|
51 |
|
PyMem_Free(file); |
|
self->handle = DBFOpen(file, mode); |
|
52 |
return self->handle ? 0 : -1; |
return self->handle ? 0 : -1; |
53 |
} |
} |
54 |
|
|
82 |
char field_name[12]; |
char field_name[12]; |
83 |
int field, width = 0, decimals = 0, field_type; |
int field, width = 0, decimals = 0, field_type; |
84 |
|
|
85 |
if (!PyArg_ParseTuple(args, "i", &field)) return NULL; |
if (!PyArg_ParseTuple(args, "i:field_info", &field)) return NULL; |
86 |
|
|
87 |
field_name[0] = '\0'; |
field_name[0] = '\0'; |
88 |
field_type = DBFGetFieldInfo(self->handle, field, field_name, &width, &decimals); |
field_type = DBFGetFieldInfo(self->handle, field, field_name, &width, &decimals); |
98 |
int type, width, decimals; |
int type, width, decimals; |
99 |
int field; |
int field; |
100 |
|
|
101 |
if (!PyArg_ParseTuple(args, "siii", &name, &type, &width, &decimals)) return NULL; |
if (!PyArg_ParseTuple(args, "siii:add_field", &name, &type, &width, &decimals)) return NULL; |
102 |
|
|
103 |
field = DBFAddField(self->handle, name, (DBFFieldType)type, width, decimals); |
field = DBFAddField(self->handle, name, (DBFFieldType)type, width, decimals); |
104 |
|
|
142 |
{ |
{ |
143 |
case FTString: |
case FTString: |
144 |
temp = DBFReadStringAttribute(handle, record, field); |
temp = DBFReadStringAttribute(handle, record, field); |
145 |
if (!temp) |
if (temp) return PyString_FromString(temp); |
|
{ |
|
|
PyErr_Format(PyExc_IOError, |
|
|
"Can't read value for row %d column %d", |
|
|
record, field); |
|
|
return NULL; |
|
|
} |
|
|
return PyString_FromString(temp); |
|
146 |
|
|
147 |
case FTInteger: |
case FTInteger: |
148 |
return PyInt_FromLong((long)DBFReadIntegerAttribute(handle, record, field)); |
return PyInt_FromLong((long)DBFReadIntegerAttribute(handle, record, field)); |
149 |
|
|
150 |
case FTDouble: |
case FTDouble: |
151 |
return PyFloat_FromDouble(DBFReadDoubleAttribute(handle, record, field)); |
return PyFloat_FromDouble(DBFReadDoubleAttribute(handle, record, field)); |
152 |
|
|
153 |
|
case FTLogical: |
154 |
|
temp = DBFReadLogicalAttribute(handle, record, field); |
155 |
|
if (temp) |
156 |
|
{ |
157 |
|
switch (temp[0]) |
158 |
|
{ |
159 |
|
case 'F': |
160 |
|
case 'N': |
161 |
|
Py_RETURN_FALSE; |
162 |
|
case 'T': |
163 |
|
case 'Y': |
164 |
|
Py_RETURN_TRUE; |
165 |
|
} |
166 |
|
} |
167 |
|
break; |
168 |
|
|
169 |
default: |
default: |
170 |
PyErr_Format(PyExc_TypeError, "Invalid field data type %d", type); |
PyErr_Format(PyExc_TypeError, "Invalid field data type %d", type); |
171 |
return NULL; |
return NULL; |
172 |
} |
} |
173 |
} |
} |
174 |
|
|
175 |
|
PyErr_Format(PyExc_IOError, "Can't read value for row %d column %d", record, field); |
176 |
|
return NULL; |
177 |
} |
} |
178 |
|
|
179 |
|
|
185 |
{ |
{ |
186 |
int record, field; |
int record, field; |
187 |
|
|
188 |
if (!PyArg_ParseTuple(args, "ii", &record, &field)) return NULL; |
if (!PyArg_ParseTuple(args, "ii:read_field", &record, &field)) return NULL; |
189 |
|
|
190 |
if (record < 0 || record >= DBFGetRecordCount(self->handle)) |
if (record < 0 || record >= DBFGetRecordCount(self->handle)) |
191 |
{ |
{ |
221 |
PyObject *dict; |
PyObject *dict; |
222 |
PyObject *value = NULL; |
PyObject *value = NULL; |
223 |
|
|
224 |
if (!PyArg_ParseTuple(args, "i", &record)) return NULL; |
if (!PyArg_ParseTuple(args, "i:read_record", &record)) return NULL; |
225 |
|
|
226 |
if (record < 0 || record >= DBFGetRecordCount(self->handle)) |
if (record < 0 || record >= DBFGetRecordCount(self->handle)) |
227 |
{ |
{ |
259 |
char * string_value; |
char * string_value; |
260 |
int int_value; |
int int_value; |
261 |
double double_value; |
double double_value; |
262 |
|
int logical_value; |
263 |
|
|
264 |
if (value == Py_None) |
if (value == Py_None) |
265 |
{ |
{ |
266 |
if (!DBFWriteNULLAttribute(handle, record, field)) |
if (DBFWriteNULLAttribute(handle, record, field)) return 1; |
|
{ |
|
|
PyErr_Format(PyExc_IOError, |
|
|
"can't write NULL field %d of record %d", |
|
|
field, record); |
|
|
return 0; |
|
|
} |
|
267 |
} |
} |
268 |
else |
else |
269 |
{ |
{ |
272 |
case FTString: |
case FTString: |
273 |
string_value = PyString_AsString(value); |
string_value = PyString_AsString(value); |
274 |
if (!string_value) return 0; |
if (!string_value) return 0; |
275 |
if (!DBFWriteStringAttribute(handle, record, field, string_value)) |
if (DBFWriteStringAttribute(handle, record, field, string_value)) return 1; |
|
{ |
|
|
PyErr_Format(PyExc_IOError, |
|
|
"can't write field %d of record %d", |
|
|
field, record); |
|
|
return 0; |
|
|
} |
|
276 |
break; |
break; |
277 |
|
|
278 |
case FTInteger: |
case FTInteger: |
279 |
int_value = PyInt_AsLong(value); |
int_value = PyInt_AsLong(value); |
280 |
if (int_value == -1 && PyErr_Occurred()) return 0; |
if (int_value == -1 && PyErr_Occurred()) return 0; |
281 |
if (!DBFWriteIntegerAttribute(handle, record, field, int_value)) |
if (DBFWriteIntegerAttribute(handle, record, field, int_value)) return 1; |
|
{ |
|
|
PyErr_Format(PyExc_IOError, |
|
|
"can't write field %d of record %d", |
|
|
field, record); |
|
|
return 0; |
|
|
} |
|
282 |
break; |
break; |
283 |
|
|
284 |
case FTDouble: |
case FTDouble: |
285 |
double_value = PyFloat_AsDouble(value); |
double_value = PyFloat_AsDouble(value); |
286 |
if (double_value == -1 && PyErr_Occurred()) return 0; |
if (double_value == -1 && PyErr_Occurred()) return 0; |
287 |
if (!DBFWriteDoubleAttribute(handle, record, field, double_value)) |
if (DBFWriteDoubleAttribute(handle, record, field, double_value)) return 1; |
288 |
{ |
break; |
289 |
PyErr_Format(PyExc_IOError, |
|
290 |
"can't write field %d of record %d", |
case FTLogical: |
291 |
field, record); |
logical_value = PyObject_IsTrue(value); |
292 |
return 0; |
if (logical_value == -1) return 0; |
293 |
} |
if (DBFWriteLogicalAttribute(handle, record, field, logical_value ? 'T' : 'F')) return 1; |
294 |
break; |
break; |
295 |
|
|
296 |
default: |
default: |
299 |
} |
} |
300 |
} |
} |
301 |
|
|
302 |
return 1; |
PyErr_Format(PyExc_IOError, "can't write field %d of record %d", field, record); |
303 |
|
return 0; |
304 |
} |
} |
305 |
|
|
306 |
|
|
311 |
PyObject* value; |
PyObject* value; |
312 |
int type; |
int type; |
313 |
|
|
314 |
if (!PyArg_ParseTuple(args, "iiO", &record, &field, &value)) return NULL; |
if (!PyArg_ParseTuple(args, "iiO:write_field", &record, &field, &value)) return NULL; |
315 |
|
|
316 |
if (field < 0 || field >= DBFGetFieldCount(self->handle)) |
if (field < 0 || field >= DBFGetFieldCount(self->handle)) |
317 |
{ |
{ |
338 |
char name[12]; |
char name[12]; |
339 |
PyObject* value = NULL; |
PyObject* value = NULL; |
340 |
|
|
341 |
if (!PyArg_ParseTuple(args, "iO", &record, &record_object)) return NULL; |
if (!PyArg_ParseTuple(args, "iO:write_record", &record, &record_object)) return NULL; |
342 |
|
|
343 |
num_fields = DBFGetFieldCount(self->handle); |
num_fields = DBFGetFieldCount(self->handle); |
344 |
|
|
417 |
|
|
418 |
static struct PyMethodDef dbffile_methods[] = |
static struct PyMethodDef dbffile_methods[] = |
419 |
{ |
{ |
420 |
{"close", (PyCFunction)dbffile_close, METH_NOARGS, "close DBFFile"}, |
{"close", (PyCFunction)dbffile_close, METH_NOARGS, |
421 |
{"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS, "return number of fields currently defined"}, |
"close() -> None\n\n" |
422 |
{"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS, "return number of records that currently exist"}, |
"closes DBFFile"}, |
423 |
{"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS, |
{"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS, |
424 |
"returns info of a field as a tuple (type, name, width, decimals) with:\n" |
"field_count() -> integer\n\n" |
425 |
"-type: the type of the field corresponding to the integer value of one of the constants FTString, FTInteger, ...\n" |
"returns number of fields currently defined"}, |
426 |
"-name: the name of the field as a string\n" |
{"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS, |
427 |
"-width: the width of the field as a number of characters\n" |
"record_count() -> integer\n\n" |
428 |
"-decimals: the number of decimal digits" }, |
"returns number of records that currently exist"}, |
429 |
|
{"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS, |
430 |
|
"field_info(field_index) -> (type, name, width, decimals)\n\n" |
431 |
|
"returns info of a field as a tuple with:\n" |
432 |
|
"- type: the type of the field corresponding to the integer value of one " |
433 |
|
" of the constants FTString, FTInteger, ...\n" |
434 |
|
"- name: the name of the field as a string\n" |
435 |
|
"- width: the width of the field as a number of characters\n" |
436 |
|
"- decimals: the number of decimal digits" }, |
437 |
{"add_field", (PyCFunction)dbffile_add_field, METH_VARARGS, |
{"add_field", (PyCFunction)dbffile_add_field, METH_VARARGS, |
438 |
|
"add_field(type, name, width, decimals) -> field_index\n\n" |
439 |
"adds a new field and returns field index if successful\n" |
"adds a new field and returns field index if successful\n" |
440 |
"-type: the type of the field corresponding to the integer value of one of the constants FTString, FTInteger, ...\n" |
"- type: the type of the field corresponding to the integer value of one " |
441 |
"-name: the name of the field as a string\n" |
" of the constants FTString, FTInteger, ...\n" |
442 |
"-width: the width of the field as a number of characters\n" |
"- name: the name of the field as a string\n" |
443 |
"-decimals: the number of decimal digits" }, |
"- width: the width of the field as a number of characters\n" |
444 |
{"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS, "return the value of one field of a record"}, |
"- decimals: the number of decimal digits" }, |
445 |
{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, "return an entire record as a dict of field names and values"}, |
{"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS, |
446 |
{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, "write a single field of a record"}, |
"read_attribute(record_index, field_index) -> value\n\n" |
447 |
{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, "write an entire record as a dict or a sequence"}, |
"returns the value of one field of a record"}, |
448 |
|
{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, |
449 |
|
"read_record(record_index) -> dict\n\n" |
450 |
|
"returns an entire record as a dictionary of field names and values"}, |
451 |
|
{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, |
452 |
|
"write_field(record_index, field_index, new_value)\n" |
453 |
|
"writes a single field of a record"}, |
454 |
|
{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, |
455 |
|
"write_record(record_index, record) -> record_index\n\n" |
456 |
|
"Writes an entire record as a dict or a sequence, and return index of record\n" |
457 |
|
"Record can either be a dictionary in which case the keys are used as field names, " |
458 |
|
"or a sequence that must have an item for every field (length = field_count())"}, |
459 |
#if HAVE_UPDATE_HEADER |
#if HAVE_UPDATE_HEADER |
460 |
{"commit", (PyCFunction)dbffile_read_record, METH_NOARGS, NULL}, |
{"commit", (PyCFunction)dbffile_read_record, METH_NOARGS, |
461 |
|
"commit() -> None"}, |
462 |
#endif |
#endif |
463 |
{NULL} |
{NULL} |
464 |
}; |
}; |
490 |
char* file; |
char* file; |
491 |
DBFFileObject* result; |
DBFFileObject* result; |
492 |
|
|
493 |
if (!PyArg_ParseTuple(args, "s", &file)) return NULL; |
if (!PyArg_ParseTuple(args, "et:create", Py_FileSystemDefaultEncoding, &file)) return NULL; |
494 |
|
|
495 |
result = PyObject_New(DBFFileObject, &DBFFileType); |
result = PyObject_New(DBFFileObject, &DBFFileType); |
496 |
if (!result) |
if (!result) |
513 |
|
|
514 |
static struct PyMethodDef dbflib_methods[] = |
static struct PyMethodDef dbflib_methods[] = |
515 |
{ |
{ |
516 |
{"open", (PyCFunction)dbflib_open, METH_VARARGS, "open a DBFFile" }, |
{"open", (PyCFunction)dbflib_open, METH_VARARGS, |
517 |
{"create", (PyCFunction)dbflib_create, METH_VARARGS, "create a DBFFile" }, |
"open(name [, mode]) -> DBFFile\n\n" |
518 |
|
"opens a DBFFile" }, |
519 |
|
{"create", (PyCFunction)dbflib_create, METH_VARARGS, |
520 |
|
"create(name) -> DBFFile\n\n" |
521 |
|
"create a DBFFile" }, |
522 |
{NULL} |
{NULL} |
523 |
}; |
}; |
524 |
|
|
534 |
PYSHAPELIB_ADD_CONSTANT(FTString); |
PYSHAPELIB_ADD_CONSTANT(FTString); |
535 |
PYSHAPELIB_ADD_CONSTANT(FTInteger); |
PYSHAPELIB_ADD_CONSTANT(FTInteger); |
536 |
PYSHAPELIB_ADD_CONSTANT(FTDouble); |
PYSHAPELIB_ADD_CONSTANT(FTDouble); |
537 |
|
PYSHAPELIB_ADD_CONSTANT(FTLogical); |
538 |
PYSHAPELIB_ADD_CONSTANT(FTInvalid); |
PYSHAPELIB_ADD_CONSTANT(FTInvalid); |
539 |
PyModule_AddIntConstant(module, "_have_commit", HAVE_UPDATE_HEADER); |
PyModule_AddIntConstant(module, "_have_commit", HAVE_UPDATE_HEADER); |
540 |
} |
} |