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 |
PyErr_Format(PyExc_TypeError, "dbflib.DBFFile.__init__ takes no keyword arguments"); |
Py_FileSystemDefaultEncoding, &file, &mode)) return -1; |
|
return -1; |
|
|
} |
|
|
|
|
|
if (!PyArg_ParseTuple(args, "et|s:__init__", Py_FileSystemDefaultEncoding, &file, &mode)) return -1; |
|
44 |
|
|
45 |
self->handle = DBFOpen(file, mode); |
self->handle = DBFOpen(file, mode); |
46 |
PyMem_Free(file); |
PyMem_Free(file); |
138 |
{ |
{ |
139 |
case FTString: |
case FTString: |
140 |
temp = DBFReadStringAttribute(handle, record, field); |
temp = DBFReadStringAttribute(handle, record, field); |
141 |
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); |
|
142 |
|
|
143 |
case FTInteger: |
case FTInteger: |
144 |
return PyInt_FromLong((long)DBFReadIntegerAttribute(handle, record, field)); |
return PyInt_FromLong((long)DBFReadIntegerAttribute(handle, record, field)); |
145 |
|
|
146 |
case FTDouble: |
case FTDouble: |
147 |
return PyFloat_FromDouble(DBFReadDoubleAttribute(handle, record, field)); |
return PyFloat_FromDouble(DBFReadDoubleAttribute(handle, record, field)); |
148 |
|
|
149 |
|
case FTLogical: |
150 |
|
temp = DBFReadLogicalAttribute(handle, record, field); |
151 |
|
if (temp) |
152 |
|
{ |
153 |
|
switch (temp[0]) |
154 |
|
{ |
155 |
|
case 'F': |
156 |
|
case 'N': |
157 |
|
Py_RETURN_FALSE; |
158 |
|
case 'T': |
159 |
|
case 'Y': |
160 |
|
Py_RETURN_TRUE; |
161 |
|
} |
162 |
|
} |
163 |
|
break; |
164 |
|
|
165 |
default: |
default: |
166 |
PyErr_Format(PyExc_TypeError, "Invalid field data type %d", type); |
PyErr_Format(PyExc_TypeError, "Invalid field data type %d", type); |
167 |
return NULL; |
return NULL; |
168 |
} |
} |
169 |
} |
} |
170 |
|
|
171 |
|
PyErr_Format(PyExc_IOError, "Can't read value for row %d column %d", record, field); |
172 |
|
return NULL; |
173 |
} |
} |
174 |
|
|
175 |
|
|
255 |
char * string_value; |
char * string_value; |
256 |
int int_value; |
int int_value; |
257 |
double double_value; |
double double_value; |
258 |
|
int logical_value; |
259 |
|
|
260 |
if (value == Py_None) |
if (value == Py_None) |
261 |
{ |
{ |
262 |
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; |
|
|
} |
|
263 |
} |
} |
264 |
else |
else |
265 |
{ |
{ |
268 |
case FTString: |
case FTString: |
269 |
string_value = PyString_AsString(value); |
string_value = PyString_AsString(value); |
270 |
if (!string_value) return 0; |
if (!string_value) return 0; |
271 |
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; |
|
|
} |
|
272 |
break; |
break; |
273 |
|
|
274 |
case FTInteger: |
case FTInteger: |
275 |
int_value = PyInt_AsLong(value); |
int_value = PyInt_AsLong(value); |
276 |
if (int_value == -1 && PyErr_Occurred()) return 0; |
if (int_value == -1 && PyErr_Occurred()) return 0; |
277 |
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; |
|
|
} |
|
278 |
break; |
break; |
279 |
|
|
280 |
case FTDouble: |
case FTDouble: |
281 |
double_value = PyFloat_AsDouble(value); |
double_value = PyFloat_AsDouble(value); |
282 |
if (double_value == -1 && PyErr_Occurred()) return 0; |
if (double_value == -1 && PyErr_Occurred()) return 0; |
283 |
if (!DBFWriteDoubleAttribute(handle, record, field, double_value)) |
if (DBFWriteDoubleAttribute(handle, record, field, double_value)) return 1; |
284 |
{ |
break; |
285 |
PyErr_Format(PyExc_IOError, |
|
286 |
"can't write field %d of record %d", |
case FTLogical: |
287 |
field, record); |
logical_value = PyObject_IsTrue(value); |
288 |
return 0; |
if (logical_value == -1) return 0; |
289 |
} |
if (DBFWriteLogicalAttribute(handle, record, field, logical_value ? 'T' : 'F')) return 1; |
290 |
break; |
break; |
291 |
|
|
292 |
default: |
default: |
295 |
} |
} |
296 |
} |
} |
297 |
|
|
298 |
return 1; |
PyErr_Format(PyExc_IOError, "can't write field %d of record %d", field, record); |
299 |
|
return 0; |
300 |
} |
} |
301 |
|
|
302 |
|
|
414 |
static struct PyMethodDef dbffile_methods[] = |
static struct PyMethodDef dbffile_methods[] = |
415 |
{ |
{ |
416 |
{"close", (PyCFunction)dbffile_close, METH_NOARGS, |
{"close", (PyCFunction)dbffile_close, METH_NOARGS, |
417 |
"close()\n" |
"close() -> None\n\n" |
418 |
"close DBFFile"}, |
"closes DBFFile"}, |
419 |
{"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS, |
{"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS, |
420 |
"field_count()\n" |
"field_count() -> integer\n\n" |
421 |
"returns number of fields currently defined"}, |
"returns number of fields currently defined"}, |
422 |
{"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS, |
{"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS, |
423 |
"record_count()\n" |
"record_count() -> integer\n\n" |
424 |
"returns number of records that currently exist"}, |
"returns number of records that currently exist"}, |
425 |
{"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS, |
{"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS, |
426 |
"field_info(field_index)\n" |
"field_info(field_index) -> (type, name, width, decimals)\n\n" |
427 |
"returns info of a field as a tuple (type, name, width, decimals) with:\n" |
"returns info of a field as a tuple with:\n" |
428 |
"-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 " |
429 |
"-name: the name of the field as a string\n" |
" of the constants FTString, FTInteger, ...\n" |
430 |
"-width: the width of the field as a number of characters\n" |
"- name: the name of the field as a string\n" |
431 |
"-decimals: the number of decimal digits" }, |
"- width: the width of the field as a number of characters\n" |
432 |
|
"- decimals: the number of decimal digits" }, |
433 |
{"add_field", (PyCFunction)dbffile_add_field, METH_VARARGS, |
{"add_field", (PyCFunction)dbffile_add_field, METH_VARARGS, |
434 |
"add_field(type, name, width, decimals)\n" |
"add_field(type, name, width, decimals) -> field_index\n\n" |
435 |
"adds a new field and returns field index if successful\n" |
"adds a new field and returns field index if successful\n" |
436 |
"-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 " |
437 |
"-name: the name of the field as a string\n" |
" of the constants FTString, FTInteger, ...\n" |
438 |
"-width: the width of the field as a number of characters\n" |
"- name: the name of the field as a string\n" |
439 |
"-decimals: the number of decimal digits" }, |
"- width: the width of the field as a number of characters\n" |
440 |
|
"- decimals: the number of decimal digits" }, |
441 |
{"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS, |
{"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS, |
442 |
"read_attribute(record_index, field_index)\n" |
"read_attribute(record_index, field_index) -> value\n\n" |
443 |
"return the value of one field of a record"}, |
"returns the value of one field of a record"}, |
444 |
{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, |
{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, |
445 |
"read_record(record_index)\n" |
"read_record(record_index) -> dict\n\n" |
446 |
"return an entire record as a dict of field names and values"}, |
"returns an entire record as a dictionary of field names and values"}, |
447 |
{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, |
{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, |
448 |
"write_field(record_index, field_index, new_value)\n" |
"write_field(record_index, field_index, new_value)\n" |
449 |
"write a single field of a record"}, |
"writes a single field of a record"}, |
450 |
{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, |
{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, |
451 |
"write_record(record_index, record)\n" |
"write_record(record_index, record) -> record_index\n\n" |
452 |
"write an entire record as a dict or a sequence\n" |
"Writes an entire record as a dict or a sequence, and return index of record\n" |
453 |
"record can either be a dictionary in which case the keys are used as field names, " |
"Record can either be a dictionary in which case the keys are used as field names, " |
454 |
"or a sequence that must have an item for every field (length = field_count())"}, |
"or a sequence that must have an item for every field (length = field_count())"}, |
455 |
#if HAVE_UPDATE_HEADER |
#if HAVE_UPDATE_HEADER |
456 |
{"commit", (PyCFunction)dbffile_read_record, METH_NOARGS, |
{"commit", (PyCFunction)dbffile_read_record, METH_NOARGS, |
457 |
"commit()"}, |
"commit() -> None"}, |
458 |
#endif |
#endif |
459 |
{NULL} |
{NULL} |
460 |
}; |
}; |
510 |
static struct PyMethodDef dbflib_methods[] = |
static struct PyMethodDef dbflib_methods[] = |
511 |
{ |
{ |
512 |
{"open", (PyCFunction)dbflib_open, METH_VARARGS, |
{"open", (PyCFunction)dbflib_open, METH_VARARGS, |
513 |
"open(filename [, mode])\n" |
"open(name [, mode]) -> DBFFile\n\n" |
514 |
"open a DBFFile" }, |
"opens a DBFFile" }, |
515 |
{"create", (PyCFunction)dbflib_create, METH_VARARGS, |
{"create", (PyCFunction)dbflib_create, METH_VARARGS, |
516 |
"create(filename)\n" |
"create(name) -> DBFFile\n\n" |
517 |
"create a DBFFile" }, |
"create a DBFFile" }, |
518 |
{NULL} |
{NULL} |
519 |
}; |
}; |
530 |
PYSHAPELIB_ADD_CONSTANT(FTString); |
PYSHAPELIB_ADD_CONSTANT(FTString); |
531 |
PYSHAPELIB_ADD_CONSTANT(FTInteger); |
PYSHAPELIB_ADD_CONSTANT(FTInteger); |
532 |
PYSHAPELIB_ADD_CONSTANT(FTDouble); |
PYSHAPELIB_ADD_CONSTANT(FTDouble); |
533 |
|
PYSHAPELIB_ADD_CONSTANT(FTLogical); |
534 |
PYSHAPELIB_ADD_CONSTANT(FTInvalid); |
PYSHAPELIB_ADD_CONSTANT(FTInvalid); |
535 |
PyModule_AddIntConstant(module, "_have_commit", HAVE_UPDATE_HEADER); |
PyModule_AddIntConstant(module, "_have_commit", HAVE_UPDATE_HEADER); |
536 |
} |
} |