/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/table.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/table.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 257 by bh, Thu Aug 15 12:47:59 2002 UTC revision 474 by jonathan, Wed Mar 5 18:40:03 2003 UTC
# Line 12  Classes for handling tables of data. Line 12  Classes for handling tables of data.
12  __version__ = "$Revision$"  __version__ = "$Revision$"
13    
14  import dbflib  import dbflib
15    from Thuban.common import *
16    
17  # the field types supported by a Table instance.  # the field types supported by a Table instance.
18  FIELDTYPE_INT = "int"  FIELDTYPE_INT = "int"
# Line 32  class Table: Line 33  class Table:
33      Currently this is basically just a wrapper around dbflib.      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):      def __init__(self, filename):
51          self.filename = filename          self.filename = filename
52          self.dbf = dbflib.DBFFile(filename)          self.dbf = dbflib.DBFFile(filename)
53    
54            # If true, self.dbf is open for writing.
55            self._writable = 0
56    
57      def Destroy(self):      def Destroy(self):
58          self.dbf.close()          self.dbf.close()
59          self.dbf = None          self.dbf = None
# Line 58  class Table: Line 76  class Table:
76          type = dbflib_fieldtypes[type]          type = dbflib_fieldtypes[type]
77          return type, name, width, prec          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):      def read_record(self, record):
90          """Return the record no. record as a dict mapping field names to values          """Return the record no. record as a dict mapping field names to values
91          """          """
92          return self.dbf.read_record(record)          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    

Legend:
Removed from v.257  
changed lines
  Added in v.474

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26