12 |
|
|
13 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
14 |
|
|
15 |
|
import os |
16 |
|
import inspect |
17 |
|
import warnings |
18 |
|
|
19 |
import dbflib |
import dbflib |
20 |
|
|
21 |
# the field types supported by a Table instance. |
# the field types supported by a Table instance. |
34 |
|
|
35 |
"""Mixin to implement the old table interface using the new one""" |
"""Mixin to implement the old table interface using the new one""" |
36 |
|
|
37 |
|
def __deprecation_warning(self): |
38 |
|
"""Issue a DeprecationWarning for code hat uses the old interface""" |
39 |
|
callername = inspect.currentframe().f_back.f_code.co_name |
40 |
|
warnings.warn("The %s method of the old table interface" |
41 |
|
" is deprecated" % callername, |
42 |
|
DeprecationWarning, stacklevel = 3) |
43 |
|
|
44 |
def record_count(self): |
def record_count(self): |
45 |
|
self.__deprecation_warning() |
46 |
return self.NumRows() |
return self.NumRows() |
47 |
|
|
48 |
def field_count(self): |
def field_count(self): |
49 |
|
self.__deprecation_warning() |
50 |
return self.NumColumns() |
return self.NumColumns() |
51 |
|
|
52 |
def field_info(self, field): |
def field_info(self, field): |
57 |
and prec will be zero if the information returned by the Column |
and prec will be zero if the information returned by the Column |
58 |
method doesn't provide values for them. |
method doesn't provide values for them. |
59 |
""" |
""" |
60 |
|
self.__deprecation_warning() |
61 |
col = self.Column(field) |
col = self.Column(field) |
62 |
return (col.type, col.name, |
return (col.type, col.name, |
63 |
getattr(col, "width", 0), getattr(col, "prec", 0)) |
getattr(col, "width", 0), getattr(col, "prec", 0)) |
64 |
|
|
65 |
def field_info_by_name(self, col): |
def field_info_by_name(self, col): |
66 |
|
self.__deprecation_warning() |
67 |
try: |
try: |
68 |
return self.field_info(col) |
return self.field_info(col) |
69 |
except KeyError: |
except KeyError: |
72 |
return None |
return None |
73 |
|
|
74 |
def field_range(self, fieldName): |
def field_range(self, fieldName): |
75 |
|
self.__deprecation_warning() |
76 |
min, max = self.ValueRange(fieldName) |
min, max = self.ValueRange(fieldName) |
77 |
return ((min, None), (max, None)) |
return ((min, None), (max, None)) |
78 |
|
|
79 |
def GetUniqueValues(self, field): |
def GetUniqueValues(self, field): |
80 |
|
self.__deprecation_warning() |
81 |
return self.UniqueValues(field) |
return self.UniqueValues(field) |
82 |
|
|
83 |
def read_record(self, r): |
def read_record(self, r): |
84 |
|
self.__deprecation_warning() |
85 |
return self.ReadRowAsDict(r) |
return self.ReadRowAsDict(r) |
86 |
|
|
87 |
|
|
147 |
self.column_map[name] = col |
self.column_map[name] = col |
148 |
self.column_map[index] = col |
self.column_map[index] = col |
149 |
|
|
150 |
|
def Title(self): |
151 |
|
"""Return the title of the table. |
152 |
|
|
153 |
|
The title is simply the basename of the filename |
154 |
|
""" |
155 |
|
return os.path.basename(self.filename) |
156 |
|
|
157 |
def NumRows(self): |
def NumRows(self): |
158 |
"""Return the number of rows in the table""" |
"""Return the number of rows in the table""" |
159 |
return self.dbf.record_count() |
return self.dbf.record_count() |
177 |
""" |
""" |
178 |
return self.column_map[col] |
return self.column_map[col] |
179 |
|
|
180 |
|
def HasColumn(self, col): |
181 |
|
"""Return whether the table has a column with the given name or index |
182 |
|
""" |
183 |
|
return self.column_map.has_key(col) |
184 |
|
|
185 |
def ReadRowAsDict(self, row): |
def ReadRowAsDict(self, row): |
186 |
"""Return the entire row as a dictionary with column names as keys""" |
"""Return the entire row as a dictionary with column names as keys""" |
187 |
return self.dbf.read_record(row) |
return self.dbf.read_record(row) |
226 |
values.sort() |
values.sort() |
227 |
return values |
return values |
228 |
|
|
229 |
|
def Dependencies(self): |
230 |
|
"""Return an empty sequence. The DBFTable doesn't depend on anything""" |
231 |
|
return () |
232 |
|
|
233 |
# DBF specific interface parts. |
# DBF specific interface parts. |
234 |
|
|
257 |
self.dbf.write_record(record, values) |
self.dbf.write_record(record, values) |
258 |
self.dbf.commit() |
self.dbf.commit() |
259 |
|
|
260 |
|
def FileName(self): |
261 |
|
"""Return the filename the DBFTable was instantiated with""" |
262 |
# Temporary backwards compatibility |
return self.filename |
|
Table = DBFTable |
|
|
|
|
263 |
|
|
264 |
|
|
265 |
class MemoryColumn: |
class MemoryColumn: |
292 |
self.column_map[name] = col |
self.column_map[name] = col |
293 |
self.column_map[index] = col |
self.column_map[index] = col |
294 |
|
|
295 |
|
def Title(self): |
296 |
|
"""Return 'MemoryTable' |
297 |
|
|
298 |
|
Override in derived classes to have a more meaningful title. |
299 |
|
""" |
300 |
|
return "MemoryTable" |
301 |
|
|
302 |
def NumColumns(self): |
def NumColumns(self): |
303 |
"""Return the number of columns in the table""" |
"""Return the number of columns in the table""" |
304 |
return len(self.columns) |
return len(self.columns) |
318 |
""" |
""" |
319 |
return self.columns |
return self.columns |
320 |
|
|
321 |
|
def HasColumn(self, col): |
322 |
|
"""Return whether the table has a column with the given name or index |
323 |
|
""" |
324 |
|
return self.column_map.has_key(col) |
325 |
|
|
326 |
def NumRows(self): |
def NumRows(self): |
327 |
"""Return the number of rows in the table""" |
"""Return the number of rows in the table""" |
328 |
return len(self.data) |
return len(self.data) |
365 |
values.sort() |
values.sort() |
366 |
return values |
return values |
367 |
|
|
368 |
|
def Dependencies(self): |
369 |
|
"""Return an empty sequence. The MemoryTable doesn't depend on anything |
370 |
|
""" |
371 |
|
return () |
372 |
|
|
373 |
def write_record(self, record, values): |
def write_record(self, record, values): |
374 |
# TODO: Check for correct lenght and perhaps also |
# TODO: Check for correct lenght and perhaps also |