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, "et|s:__init__", Py_FileSystemDefaultEncoding, &file, &mode)) return -1; |
|
|
|
|
|
self->handle = DBFOpen(file, mode); |
|
51 |
PyMem_Free(file); |
PyMem_Free(file); |
|
|
|
52 |
return self->handle ? 0 : -1; |
return self->handle ? 0 : -1; |
53 |
} |
} |
54 |
|
|
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 |
|
|
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 |
|
|
418 |
static struct PyMethodDef dbffile_methods[] = |
static struct PyMethodDef dbffile_methods[] = |
419 |
{ |
{ |
420 |
{"close", (PyCFunction)dbffile_close, METH_NOARGS, |
{"close", (PyCFunction)dbffile_close, METH_NOARGS, |
421 |
"close()\n" |
"close() -> None\n\n" |
422 |
"close DBFFile"}, |
"closes DBFFile"}, |
423 |
{"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS, |
{"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS, |
424 |
"field_count()\n" |
"field_count() -> integer\n\n" |
425 |
"returns number of fields currently defined"}, |
"returns number of fields currently defined"}, |
426 |
{"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS, |
{"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS, |
427 |
"record_count()\n" |
"record_count() -> integer\n\n" |
428 |
"returns number of records that currently exist"}, |
"returns number of records that currently exist"}, |
429 |
{"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS, |
{"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS, |
430 |
"field_info(field_index)\n" |
"field_info(field_index) -> (type, name, width, decimals)\n\n" |
431 |
"returns info of a field as a tuple (type, name, width, decimals) with:\n" |
"returns info of a field as a tuple with:\n" |
432 |
"-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 " |
433 |
"-name: the name of the field as a string\n" |
" of the constants FTString, FTInteger, ...\n" |
434 |
"-width: the width of the field as a number of characters\n" |
"- name: the name of the field as a string\n" |
435 |
"-decimals: the number of decimal digits" }, |
"- 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)\n" |
"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 |
|
"- decimals: the number of decimal digits" }, |
445 |
{"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS, |
{"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS, |
446 |
"read_attribute(record_index, field_index)\n" |
"read_attribute(record_index, field_index) -> value\n\n" |
447 |
"return the value of one field of a record"}, |
"returns the value of one field of a record"}, |
448 |
{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, |
{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, |
449 |
"read_record(record_index)\n" |
"read_record(record_index) -> dict\n\n" |
450 |
"return an entire record as a dict of field names and values"}, |
"returns an entire record as a dictionary of field names and values"}, |
451 |
{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, |
{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, |
452 |
"write_field(record_index, field_index, new_value)\n" |
"write_field(record_index, field_index, new_value)\n" |
453 |
"write a single field of a record"}, |
"writes a single field of a record"}, |
454 |
{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, |
{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, |
455 |
"write_record(record_index, record)\n" |
"write_record(record_index, record) -> record_index\n\n" |
456 |
"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" |
457 |
"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, " |
458 |
"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())"}, |
459 |
#if HAVE_UPDATE_HEADER |
#if HAVE_UPDATE_HEADER |
460 |
{"commit", (PyCFunction)dbffile_read_record, METH_NOARGS, |
{"commit", (PyCFunction)dbffile_read_record, METH_NOARGS, |
461 |
"commit()"}, |
"commit() -> None"}, |
462 |
#endif |
#endif |
463 |
{NULL} |
{NULL} |
464 |
}; |
}; |
514 |
static struct PyMethodDef dbflib_methods[] = |
static struct PyMethodDef dbflib_methods[] = |
515 |
{ |
{ |
516 |
{"open", (PyCFunction)dbflib_open, METH_VARARGS, |
{"open", (PyCFunction)dbflib_open, METH_VARARGS, |
517 |
"open(filename [, mode])\n" |
"open(name [, mode]) -> DBFFile\n\n" |
518 |
"open a DBFFile" }, |
"opens a DBFFile" }, |
519 |
{"create", (PyCFunction)dbflib_create, METH_VARARGS, |
{"create", (PyCFunction)dbflib_create, METH_VARARGS, |
520 |
"create(filename)\n" |
"create(name) -> DBFFile\n\n" |
521 |
"create a DBFFile" }, |
"create a DBFFile" }, |
522 |
{NULL} |
{NULL} |
523 |
}; |
}; |
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 |
} |
} |