1 |
bh |
257 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
bh |
6 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Classes for handling tables of data. |
10 |
|
|
""" |
11 |
|
|
|
12 |
|
|
__version__ = "$Revision$" |
13 |
|
|
|
14 |
|
|
import dbflib |
15 |
|
|
|
16 |
|
|
# the field types supported by a Table instance. |
17 |
|
|
FIELDTYPE_INT = "int" |
18 |
|
|
FIELDTYPE_STRING = "string" |
19 |
|
|
FIELDTYPE_DOUBLE = "double" |
20 |
|
|
|
21 |
|
|
|
22 |
|
|
# map the dbflib constants for the field types to our constants |
23 |
|
|
dbflib_fieldtypes = {dbflib.FTString: FIELDTYPE_STRING, |
24 |
|
|
dbflib.FTInteger: FIELDTYPE_INT, |
25 |
|
|
dbflib.FTDouble: FIELDTYPE_DOUBLE} |
26 |
|
|
|
27 |
|
|
class Table: |
28 |
|
|
|
29 |
|
|
""" |
30 |
|
|
Represent a table of data. |
31 |
|
|
|
32 |
|
|
Currently this is basically just a wrapper around dbflib. |
33 |
|
|
""" |
34 |
|
|
|
35 |
bh |
286 |
# Implementation strategy regarding writing to a DBF file: |
36 |
|
|
# |
37 |
|
|
# Most of the time Thuban only needs to read from a table and it is |
38 |
|
|
# important that Thuban can work with read-only files. Therefore the |
39 |
|
|
# DBF file is opened only for reading initially. Only when |
40 |
|
|
# write_record is called we try to open the DBF file for writing as |
41 |
|
|
# well. If that succeeds the dbf read/write DBF file will be used |
42 |
|
|
# for all IO afterwards. |
43 |
|
|
# |
44 |
|
|
# It's important to use the same DBF file object for both reading |
45 |
|
|
# and writing to make sure that reading a records after writing |
46 |
|
|
# returns the new values. With two separate objects this wouldn't |
47 |
|
|
# work because a DBF file object buffers some data |
48 |
|
|
|
49 |
bh |
6 |
def __init__(self, filename): |
50 |
|
|
self.filename = filename |
51 |
bh |
284 |
self.dbf = dbflib.DBFFile(filename) |
52 |
bh |
6 |
|
53 |
bh |
286 |
# If true, self.dbf is open for writing. |
54 |
|
|
self._writable = 0 |
55 |
|
|
|
56 |
bh |
257 |
def Destroy(self): |
57 |
|
|
self.dbf.close() |
58 |
|
|
self.dbf = None |
59 |
|
|
|
60 |
bh |
6 |
def record_count(self): |
61 |
|
|
"""Return the number of records""" |
62 |
|
|
return self.dbf.record_count() |
63 |
|
|
|
64 |
|
|
def field_count(self): |
65 |
|
|
"""Return the number of fields in a record""" |
66 |
|
|
return self.dbf.field_count() |
67 |
|
|
|
68 |
|
|
def field_info(self, field): |
69 |
|
|
"""Return a tuple (type, name, width, prec) for the field no. field |
70 |
|
|
|
71 |
|
|
type is the data type of the field, name the name, width the |
72 |
|
|
field width in characters and prec the decimal precision. |
73 |
|
|
""" |
74 |
|
|
type, name, width, prec = self.dbf.field_info(field) |
75 |
|
|
type = dbflib_fieldtypes[type] |
76 |
|
|
return type, name, width, prec |
77 |
|
|
|
78 |
|
|
def read_record(self, record): |
79 |
|
|
"""Return the record no. record as a dict mapping field names to values |
80 |
|
|
""" |
81 |
|
|
return self.dbf.read_record(record) |
82 |
|
|
|
83 |
bh |
274 |
def write_record(self, record, values): |
84 |
|
|
"""Write the values into the record |
85 |
|
|
|
86 |
|
|
The values parameter may either be a dictionary or a sequence. |
87 |
|
|
|
88 |
|
|
If it's a dictionary the keys must be the names of the fields |
89 |
|
|
and their value must have a suitable type. Only the fields |
90 |
|
|
actually contained in the dictionary are written. Fields for |
91 |
|
|
which there's no item in the dict are not modified. |
92 |
|
|
|
93 |
|
|
If it's a sequence, all fields must be present in the right |
94 |
|
|
order. |
95 |
|
|
""" |
96 |
bh |
286 |
if not self._writable: |
97 |
|
|
new_dbf = dbflib.DBFFile(self.filename, "r+b") |
98 |
|
|
self.dbf.close() |
99 |
|
|
self.dbf = new_dbf |
100 |
|
|
self._writable = 1 |
101 |
|
|
self.dbf.write_record(record, values) |
102 |
|
|
self.dbf.commit() |