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