12 |
|
|
13 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
14 |
|
|
15 |
|
import os |
16 |
|
import inspect |
17 |
|
import warnings |
18 |
|
|
19 |
|
from base import TitledObject |
20 |
|
|
21 |
import dbflib |
import dbflib |
22 |
|
|
23 |
# the field types supported by a Table instance. |
# the field types supported by a Table instance. |
36 |
|
|
37 |
"""Mixin to implement the old table interface using the new one""" |
"""Mixin to implement the old table interface using the new one""" |
38 |
|
|
39 |
|
def __deprecation_warning(self): |
40 |
|
"""Issue a DeprecationWarning for code hat uses the old interface""" |
41 |
|
callername = inspect.currentframe().f_back.f_code.co_name |
42 |
|
warnings.warn("The %s method of the old table interface" |
43 |
|
" is deprecated" % callername, |
44 |
|
DeprecationWarning, stacklevel = 3) |
45 |
|
|
46 |
def record_count(self): |
def record_count(self): |
47 |
|
self.__deprecation_warning() |
48 |
return self.NumRows() |
return self.NumRows() |
49 |
|
|
50 |
def field_count(self): |
def field_count(self): |
51 |
|
self.__deprecation_warning() |
52 |
return self.NumColumns() |
return self.NumColumns() |
53 |
|
|
54 |
def field_info(self, field): |
def field_info(self, field): |
59 |
and prec will be zero if the information returned by the Column |
and prec will be zero if the information returned by the Column |
60 |
method doesn't provide values for them. |
method doesn't provide values for them. |
61 |
""" |
""" |
62 |
|
self.__deprecation_warning() |
63 |
col = self.Column(field) |
col = self.Column(field) |
64 |
return (col.type, col.name, |
return (col.type, col.name, |
65 |
getattr(col, "width", 0), getattr(col, "prec", 0)) |
getattr(col, "width", 0), getattr(col, "prec", 0)) |
66 |
|
|
67 |
def field_info_by_name(self, col): |
def field_info_by_name(self, col): |
68 |
|
self.__deprecation_warning() |
69 |
try: |
try: |
70 |
return self.field_info(col) |
return self.field_info(col) |
71 |
except KeyError: |
except KeyError: |
74 |
return None |
return None |
75 |
|
|
76 |
def field_range(self, fieldName): |
def field_range(self, fieldName): |
77 |
|
self.__deprecation_warning() |
78 |
min, max = self.ValueRange(fieldName) |
min, max = self.ValueRange(fieldName) |
79 |
return ((min, None), (max, None)) |
return ((min, None), (max, None)) |
80 |
|
|
81 |
def GetUniqueValues(self, field): |
def GetUniqueValues(self, field): |
82 |
|
self.__deprecation_warning() |
83 |
return self.UniqueValues(field) |
return self.UniqueValues(field) |
84 |
|
|
85 |
def read_record(self, r): |
def read_record(self, r): |
86 |
|
self.__deprecation_warning() |
87 |
return self.ReadRowAsDict(r) |
return self.ReadRowAsDict(r) |
88 |
|
|
89 |
|
|
110 |
self.index = index |
self.index = index |
111 |
|
|
112 |
|
|
113 |
class DBFTable(OldTableInterfaceMixin): |
class DBFTable(TitledObject, OldTableInterfaceMixin): |
114 |
|
|
115 |
""" |
""" |
116 |
Table interface for the data in a DBF file |
Table interface for the data in a DBF file |
132 |
|
|
133 |
def __init__(self, filename): |
def __init__(self, filename): |
134 |
self.filename = filename |
self.filename = filename |
135 |
|
title = os.path.basename(self.filename) |
136 |
|
TitledObject.__init__(self, title) |
137 |
self.dbf = dbflib.DBFFile(filename) |
self.dbf = dbflib.DBFFile(filename) |
138 |
|
|
139 |
# If true, self.dbf is open for writing. |
# If true, self.dbf is open for writing. |
174 |
""" |
""" |
175 |
return self.column_map[col] |
return self.column_map[col] |
176 |
|
|
177 |
|
def HasColumn(self, col): |
178 |
|
"""Return whether the table has a column with the given name or index |
179 |
|
""" |
180 |
|
return self.column_map.has_key(col) |
181 |
|
|
182 |
def ReadRowAsDict(self, row): |
def ReadRowAsDict(self, row): |
183 |
"""Return the entire row as a dictionary with column names as keys""" |
"""Return the entire row as a dictionary with column names as keys""" |
184 |
return self.dbf.read_record(row) |
return self.dbf.read_record(row) |
223 |
values.sort() |
values.sort() |
224 |
return values |
return values |
225 |
|
|
226 |
|
def Dependencies(self): |
227 |
|
"""Return an empty sequence. The DBFTable doesn't depend on anything""" |
228 |
|
return () |
229 |
|
|
230 |
# DBF specific interface parts. |
# DBF specific interface parts. |
231 |
|
|
254 |
self.dbf.write_record(record, values) |
self.dbf.write_record(record, values) |
255 |
self.dbf.commit() |
self.dbf.commit() |
256 |
|
|
257 |
|
def FileName(self): |
258 |
|
"""Return the filename the DBFTable was instantiated with""" |
259 |
# Temporary backwards compatibility |
return self.filename |
|
Table = DBFTable |
|
|
|
|
260 |
|
|
261 |
|
|
262 |
class MemoryColumn: |
class MemoryColumn: |
266 |
self.type = type |
self.type = type |
267 |
self.index = index |
self.index = index |
268 |
|
|
269 |
class MemoryTable(OldTableInterfaceMixin): |
class MemoryTable(TitledObject, OldTableInterfaceMixin): |
270 |
|
|
271 |
"""Very simple table implementation that operates on a list of tuples""" |
"""Very simple table implementation that operates on a list of tuples""" |
272 |
|
|
278 |
data -- List of tuples, one for each row of data |
data -- List of tuples, one for each row of data |
279 |
""" |
""" |
280 |
self.data = data |
self.data = data |
281 |
|
title = 'MemoryTable' |
282 |
|
TitledObject.__init__(self, title) |
283 |
|
|
284 |
# Create the column information objects |
# Create the column information objects |
285 |
self.columns = [] |
self.columns = [] |
310 |
""" |
""" |
311 |
return self.columns |
return self.columns |
312 |
|
|
313 |
|
def HasColumn(self, col): |
314 |
|
"""Return whether the table has a column with the given name or index |
315 |
|
""" |
316 |
|
return self.column_map.has_key(col) |
317 |
|
|
318 |
def NumRows(self): |
def NumRows(self): |
319 |
"""Return the number of rows in the table""" |
"""Return the number of rows in the table""" |
320 |
return len(self.data) |
return len(self.data) |
357 |
values.sort() |
values.sort() |
358 |
return values |
return values |
359 |
|
|
360 |
|
def Dependencies(self): |
361 |
|
"""Return an empty sequence. The MemoryTable doesn't depend on anything |
362 |
|
""" |
363 |
|
return () |
364 |
|
|
365 |
def write_record(self, record, values): |
def write_record(self, record, values): |
366 |
# TODO: Check for correct lenght and perhaps also |
# TODO: Check for correct lenght and perhaps also |