Package dbf
[hide private]

Source Code for Package dbf

  1  """ 
  2  Copyright 
  3  ========= 
  4      - Copyright: 2008-2009 Ad-Mail, Inc -- All rights reserved. 
  5      - Author: Ethan Furman 
  6      - Contact: ethanf@admailinc.com 
  7      - Organization: Ad-Mail, Inc. 
  8      - Version: 0.85.005 as of 29 Oct 2009 
  9   
 10  Redistribution and use in source and binary forms, with or without 
 11  modification, are permitted provided that the following conditions are met: 
 12      - Redistributions of source code must retain the above copyright 
 13        notice, this list of conditions and the following disclaimer. 
 14      - Redistributions in binary form must reproduce the above copyright 
 15        notice, this list of conditions and the following disclaimer in the 
 16        documentation and/or other materials provided with the distribution. 
 17      - Neither the name of Ad-Mail, Inc nor the 
 18        names of its contributors may be used to endorse or promote products 
 19        derived from this software without specific prior written permission. 
 20   
 21  THIS SOFTWARE IS PROVIDED BY Ad-Mail, Inc ''AS IS'' AND ANY 
 22  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 24  DISCLAIMED. IN NO EVENT SHALL Ad-Mail, Inc BE LIABLE FOR ANY 
 25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 26  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 28  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 29  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 30  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 31   
 32  B{I{Summary}} 
 33   
 34  Python package for reading/writing dBase III and VFP 6 tables and memos 
 35   
 36  The entire table is read into memory, and all operations occur on the in-memory 
 37  table, with data changes being written to disk as they occur. 
 38   
 39  Goals:  programming style with databases 
 40      - C{table = dbf.table('table name' [, fielddesc[, fielddesc[, ....]]])} 
 41          - fielddesc examples:  C{name C(30); age N(3,0); wisdom M; marriage D} 
 42      - C{record = [ table.current() | table[int] | table.append() | table.[next|prev|top|bottom|goto]() ]} 
 43      - C{record.field | record['field']} accesses the field 
 44   
 45  NOTE:  Of the VFP data types, auto-increment and null settings are not implemented. 
 46  """ 
 47  import os 
 48   
 49  from dbf.dates import Date, DateTime, Time 
 50  from dbf.exceptions import DbfWarning, Bof, Eof, DbfError, DataOverflow, FieldMissing 
 51  from dbf.tables import DbfTable, Db3Table, VfpTable, FpTable, DbfList, DbfCsv 
 52  from dbf.tables import ascii, codepage, encoding, version_map 
 53   
 54  version = (0, 87, 1) 
 55   
 56  __docformat__ = 'epytext' 
 57   
58 -def Table(filename, field_specs='', memo_size=128, ignore_memos=False, \ 59 read_only=False, keep_memos=False, meta_only=False, dbf_type=None, codepage=None):
60 "returns an open table of the correct dbf_type, or creates it if field_specs is given" 61 if dbf_type is not None: 62 dbf_type = dbf_type.lower() 63 if dbf_type == 'db3': 64 return Db3Table(filename, field_specs, memo_size, ignore_memos, read_only, keep_memos, meta_only, codepage) 65 elif dbf_type == 'fp': 66 return FpTable(filename, field_specs, memo_size, ignore_memos, read_only, keep_memos, meta_only, codepage) 67 elif dbf_type == 'vfp': 68 return VfpTable(filename, field_specs, memo_size, ignore_memos, read_only, keep_memos, meta_only, codepage) 69 elif dbf_type == 'dbf': 70 return DbfTable(filename, field_specs, memo_size, ignore_memos, read_only, keep_memos, meta_only, codepage) 71 else: 72 raise TypeError("Unknown table type: %s" % dbf_type) 73 else: 74 possibles = guess_table_type(filename) 75 if len(possibles) == 1: 76 return possibles[0][2](filename, field_specs, memo_size, ignore_memos, \ 77 read_only, keep_memos, meta_only) 78 elif len(possibles) > 1: 79 types = ', '.join(["%s" % item[1] for item in possibles]) 80 abbrs = '[' + ' | '.join(["%s" % item[0] for item in possibles]) + ']' 81 raise DbfError("Table could be any of %s. Please specify %s when opening" % (types, abbrs)) 82 else: 83 raise DbfError("Shouldn't have gotten here -- yell at programmer!")
84 -def guess_table_type(filename):
85 reported = table_type(filename) 86 possibles = [] 87 version = reported[0] 88 for tabletype in (Db3Table, FpTable, VfpTable): 89 if version in tabletype._supported_tables: 90 possibles.append((tabletype._versionabbv, tabletype._version, tabletype)) 91 if not possibles: 92 raise DbfError("Tables of type %s not supported" % str(reported)) 93 return possibles
94 -def table_type(filename):
95 "returns text representation of a table's dbf version" 96 base, ext = os.path.splitext(filename) 97 if ext == '': 98 filename = base + '.dbf' 99 if not os.path.exists(filename): 100 raise DbfError('File %s not found' % filename) 101 fd = open(filename) 102 version = fd.read(1) 103 fd.close() 104 fd = None 105 if not version in version_map: 106 raise TypeError("Unknown dbf type: %s (%x)" % (version, ord(version))) 107 return version, version_map[version]
108
109 -def add_fields(table, field_specs):
110 "adds fields to an existing table" 111 table = Table(table) 112 try: 113 table.add_fields(field_specs) 114 finally: 115 table.close()
116 -def delete_fields(table, field_names):
117 "deletes fields from an existing table" 118 table = Table(table) 119 try: 120 table.delete_fields(field_names) 121 finally: 122 table.close()
123 -def export(table, filename='', fields='', format='csv', header=True):
124 "creates a csv or tab-delimited file from an existing table" 125 table = Table(table) 126 try: 127 table.export(filename, fields, format, header) 128 finally: 129 table.close()
130 -def first_record(table):
131 "prints the first record of a table" 132 table = Table(table) 133 try: 134 print str(table[0]) 135 finally: 136 table.close()
137 -def from_csv(csvfile, to_disk=False, filename=None, field_names=None):
138 "creates a Character table from a csv file" 139 reader = csv.reader(open(csvfile)) 140 if field_names is None: 141 field_names = ['f0'] 142 else: 143 field_names = field_names.replace(', ',',').split(',') 144 mtable = Table(':memory:', '%s M' % field_names[0]) 145 field_count = 1 146 for row in reader: 147 while field_count < len(row): 148 if field_count == len(field_names): 149 field_names.append('f%d' % field_count) 150 mtable.add_fields('%s M' % field_names[field_count]) 151 field_count += 1 152 mtable.append(tuple(row)) 153 if to_disk: 154 if filename is None: 155 filename = os.path.splitext(csvfile)[0] 156 length = [1] * field_count 157 for record in mtable: 158 for i in range(field_count): 159 length[i] = max(length[i], len(record[i])) 160 fields = mtable.field_names 161 fielddef = [] 162 for i in range(field_count): 163 if length[i] < 255: 164 fielddef.append('%s C(%d)' % (fields[i], length[i])) 165 else: 166 fielddef.append('%s M' % (fields[i])) 167 csvtable = Table(filename, ','.join(fielddef)) 168 for record in mtable: 169 csvtable.append(record.scatter_fields()) 170 return mtable
171 -def get_fields(table):
172 "returns the list of field names of a table" 173 table = Table(table) 174 return table.field_names
175 -def info(table):
176 "prints table info" 177 table = Table(table) 178 print str(table)
179 -def rename_field(table, oldfield, newfield):
180 "renames a field in a table" 181 table = Table(table) 182 try: 183 table.rename_field(oldfield, newfield) 184 finally: 185 table.close()
186 -def structure(table, field=None):
187 "returns the definition of a field (or all fields)" 188 table = Table(table) 189 return table.structure(field)
190 -def hex_dump(records):
191 "just what it says ;)" 192 for index,dummy in enumerate(records): 193 chars = dummy._data 194 print "%2d: " % index, 195 for char in chars[1:]: 196 print " %2x " % ord(char), 197 print
198