1 "table definitions"
2 import os
3 import sys
4 import csv
5 import codecs
6 import locale
7 import unicodedata
8 import weakref
9 from array import array
10 from decimal import Decimal
11 from shutil import copyfileobj
12 from dbf import _io as io
13 from dbf.dates import Date, DateTime, Time
14 from dbf.exceptions import Bof, Eof, DbfError, DataOverflow, FieldMissing, NonUnicode
15
16 input_decoding = locale.getdefaultlocale()[1]
17 default_codepage = 'cp1252'
18 return_ascii = False
19
20 version_map = {
21 '\x02' : 'FoxBASE',
22 '\x03' : 'dBase III Plus',
23 '\x04' : 'dBase IV',
24 '\x05' : 'dBase V',
25 '\x30' : 'Visual FoxPro',
26 '\x31' : 'Visual FoxPro (auto increment field)',
27 '\x43' : 'dBase IV SQL',
28 '\x7b' : 'dBase IV w/memos',
29 '\x83' : 'dBase III Plus w/memos',
30 '\x8b' : 'dBase IV w/memos',
31 '\x8e' : 'dBase IV w/SQL table',
32 '\xf5' : 'FoxPro w/memos'}
33
34 code_pages = {
35 '\x00' : ('ascii', "plain ol' ascii"),
36 '\x01' : ('cp437', 'U.S. MS-DOS'),
37 '\x02' : ('cp850', 'International MS-DOS'),
38 '\x03' : ('cp1252', 'Windows ANSI'),
39 '\x04' : ('mac_roman', 'Standard Macintosh'),
40 '\x08' : ('cp865', 'Danish OEM'),
41 '\x09' : ('cp437', 'Dutch OEM'),
42 '\x0A' : ('cp850', 'Dutch OEM (secondary)'),
43 '\x0B' : ('cp437', 'Finnish OEM'),
44 '\x0D' : ('cp437', 'French OEM'),
45 '\x0E' : ('cp850', 'French OEM (secondary)'),
46 '\x0F' : ('cp437', 'German OEM'),
47 '\x10' : ('cp850', 'German OEM (secondary)'),
48 '\x11' : ('cp437', 'Italian OEM'),
49 '\x12' : ('cp850', 'Italian OEM (secondary)'),
50 '\x13' : ('cp932', 'Japanese Shift-JIS'),
51 '\x14' : ('cp850', 'Spanish OEM (secondary)'),
52 '\x15' : ('cp437', 'Swedish OEM'),
53 '\x16' : ('cp850', 'Swedish OEM (secondary)'),
54 '\x17' : ('cp865', 'Norwegian OEM'),
55 '\x18' : ('cp437', 'Spanish OEM'),
56 '\x19' : ('cp437', 'English OEM (Britain)'),
57 '\x1A' : ('cp850', 'English OEM (Britain) (secondary)'),
58 '\x1B' : ('cp437', 'English OEM (U.S.)'),
59 '\x1C' : ('cp863', 'French OEM (Canada)'),
60 '\x1D' : ('cp850', 'French OEM (secondary)'),
61 '\x1F' : ('cp852', 'Czech OEM'),
62 '\x22' : ('cp852', 'Hungarian OEM'),
63 '\x23' : ('cp852', 'Polish OEM'),
64 '\x24' : ('cp860', 'Portugese OEM'),
65 '\x25' : ('cp850', 'Potugese OEM (secondary)'),
66 '\x26' : ('cp866', 'Russian OEM'),
67 '\x37' : ('cp850', 'English OEM (U.S.) (secondary)'),
68 '\x40' : ('cp852', 'Romanian OEM'),
69 '\x4D' : ('cp936', 'Chinese GBK (PRC)'),
70 '\x4E' : ('cp949', 'Korean (ANSI/OEM)'),
71 '\x4F' : ('cp950', 'Chinese Big 5 (Taiwan)'),
72 '\x50' : ('cp874', 'Thai (ANSI/OEM)'),
73 '\x57' : ('cp1252', 'ANSI'),
74 '\x58' : ('cp1252', 'Western European ANSI'),
75 '\x59' : ('cp1252', 'Spanish ANSI'),
76 '\x64' : ('cp852', 'Eastern European MS-DOS'),
77 '\x65' : ('cp866', 'Russian MS-DOS'),
78 '\x66' : ('cp865', 'Nordic MS-DOS'),
79 '\x67' : ('cp861', 'Icelandic MS-DOS'),
80 '\x68' : (None, 'Kamenicky (Czech) MS-DOS'),
81 '\x69' : (None, 'Mazovia (Polish) MS-DOS'),
82 '\x6a' : ('cp737', 'Greek MS-DOS (437G)'),
83 '\x6b' : ('cp857', 'Turkish MS-DOS'),
84 '\x78' : ('cp950', 'Traditional Chinese (Hong Kong SAR, Taiwan) Windows'),
85 '\x79' : ('cp949', 'Korean Windows'),
86 '\x7a' : ('cp936', 'Chinese Simplified (PRC, Singapore) Windows'),
87 '\x7b' : ('cp932', 'Japanese Windows'),
88 '\x7c' : ('cp874', 'Thai Windows'),
89 '\x7d' : ('cp1255', 'Hebrew Windows'),
90 '\x7e' : ('cp1256', 'Arabic Windows'),
91 '\xc8' : ('cp1250', 'Eastern European Windows'),
92 '\xc9' : ('cp1251', 'Russian Windows'),
93 '\xca' : ('cp1254', 'Turkish Windows'),
94 '\xcb' : ('cp1253', 'Greek Windows'),
95 '\x96' : ('mac_cyrillic', 'Russian Macintosh'),
96 '\x97' : ('mac_latin2', 'Macintosh EE'),
97 '\x98' : ('mac_greek', 'Greek Macintosh') }
98
99 if sys.version_info[:2] < (2, 6):
102 "Emulate PyProperty_Type() in Objects/descrobject.c"
103
104 - def __init__(self, fget=None, fset=None, fdel=None, doc=None):
105 self.fget = fget
106 self.fset = fset
107 self.fdel = fdel
108 self.__doc__ = doc or fget.__doc__
110 self.fget = func
111 if not self.__doc__:
112 self.__doc__ = fget.__doc__
113 - def __get__(self, obj, objtype=None):
114 if obj is None:
115 return self
116 if self.fget is None:
117 raise AttributeError, "unreadable attribute"
118 return self.fget(obj)
120 if self.fset is None:
121 raise AttributeError, "can't set attribute"
122 self.fset(obj, value)
124 if self.fdel is None:
125 raise AttributeError, "can't delete attribute"
126 self.fdel(obj)
128 self.fset = func
129 return self
131 self.fdel = func
132 return self
134 """Provides routines to extract and save data within the fields of a dbf record."""
135 __slots__ = ['_recnum', '_layout', '_data', '__weakref__']
137 """calls appropriate routine to fetch value stored in field from array
138 @param record_data: the data portion of the record
139 @type record_data: array of characters
140 @param fielddef: description of the field definition
141 @type fielddef: dictionary with keys 'type', 'start', 'length', 'end', 'decimals', and 'flags'
142 @returns: python data stored in field"""
143
144 field_type = fielddef['type']
145 retrieve = yo._layout.fieldtypes[field_type]['Retrieve']
146 datum = retrieve(record_data, fielddef, yo._layout.memo)
147 if field_type in yo._layout.character_fields:
148 datum = yo._layout.decoder(datum)[0]
149 if yo._layout.return_ascii:
150 try:
151 datum = yo._layout.output_encoder(datum)[0]
152 except UnicodeEncodeError:
153 datum = unicodedata.normalize('NFD', datum).encode('ascii','ignore')
154 return datum
156 "calls appropriate routine to convert value to ascii bytes, and save it in record"
157 field_type = fielddef['type']
158 update = yo._layout.fieldtypes[field_type]['Update']
159 if field_type in yo._layout.character_fields:
160 if not isinstance(value, unicode):
161 if yo._layout.input_decoder is None:
162 raise NonUnicode("String not in unicode format, no default encoding specified")
163 value = yo._layout.input_decoder(value)[0]
164 value = yo._layout.encoder(value)[0]
165 bytes = array('c', update(value, fielddef, yo._layout.memo))
166 size = fielddef['length']
167 if len(bytes) > size:
168 raise DataOverflow("tried to store %d bytes in %d byte field" % (len(bytes), size))
169 blank = array('c', ' ' * size)
170 start = fielddef['start']
171 end = start + size
172 blank[:len(bytes)] = bytes[:]
173 yo._data[start:end] = blank[:]
174 yo._update_disk(yo._recnum * yo._layout.header.record_length + yo._layout.header.start, yo._data.tostring())
186 results = []
187 if not specs:
188 specs = yo._layout.index
189 specs = _normalize_tuples(tuples=specs, length=2, filler=[_nop])
190 for field, func in specs:
191 results.append(func(yo[field]))
192 return tuple(results)
193
199 if name[0:2] == '__' and name[-2:] == '__':
200 raise AttributeError, 'Method %s is not implemented.' % name
201 elif not name in yo._layout.fields:
202 raise FieldMissing(name)
203 try:
204 fielddef = yo._layout[name]
205 value = yo._retrieveFieldValue(yo._data[fielddef['start']:fielddef['end']], fielddef)
206 return value
207 except DbfError, error:
208 error.message = "field --%s-- is %s -> %s" % (name, yo._layout.fieldtypes[fielddef['type']]['Type'], error.message)
209 raise
226 - def __new__(cls, recnum, layout, kamikaze='', _fromdisk=False):
272 result = []
273 for field in yo.field_names:
274 result.append("%-10s: %s" % (field, yo[field]))
275 return '\n'.join(result)
277 return yo._data.tostring()
279 "creates a blank record data chunk"
280 layout = yo._layout
281 ondisk = layout.ondisk
282 layout.ondisk = False
283 yo._data = array('c', ' ' * layout.header.record_length)
284 layout.memofields = []
285 for field in layout.fields:
286 yo._updateFieldValue(layout[field], layout.fieldtypes[layout[field]['type']]['Blank']())
287 if layout[field]['type'] in layout.memotypes:
288 layout.memofields.append(field)
289 layout.blankrecord = yo._data[:]
290 layout.ondisk = ondisk
295 @property
300 "saves a dictionary into a records fields\nkeys with no matching field will raise a FieldMissing exception unless drop = True"
301 for key in dict:
302 if not key in yo.field_names:
303 if drop:
304 continue
305 raise FieldMissing(key)
306 yo.__setattr__(key, dict[key])
307 @property
309 "marked for deletion?"
310 return yo._data[0] == '*'
311 @property
313 "physical record number"
314 return yo._recnum
315 @property
317 table = yo._layout.table()
318 if table is None:
319 raise DbfError("table is no longer available")
320 return table
335 "returns a dictionary of fieldnames and values which can be used with gather_fields(). if blank is True, values are empty."
336 keys = yo._layout.fields
337 if blank:
338 values = [yo._layout.fieldtypes[yo._layout[key]['type']]['Blank']() for key in keys]
339 else:
340 values = [yo[field] for field in keys]
341 return dict(zip(keys, values))
347 """Provides access to memo fields as dictionaries
348 must override _init, _get_memo, and _put_memo to
349 store memo contents to disk"""
351 "initialize disk file usage"
353 "retrieve memo contents from disk"
355 "store memo contents to disk"
357 ""
358 yo.meta = meta
359 yo.memory = {}
360 yo.nextmemo = 1
361 yo._init()
362 yo.meta.newmemofile = False
364 "gets the memo in block"
365 if yo.meta.ignorememos or not block:
366 return ''
367 if yo.meta.ondisk:
368 return yo._get_memo(block)
369 else:
370 return yo.memory[block]
372 "stores data in memo file, returns block number"
373 if yo.meta.ignorememos or data == '':
374 return 0
375 if yo.meta.inmemory:
376 thismemo = yo.nextmemo
377 yo.nextmemo += 1
378 yo.memory[thismemo] = data
379 else:
380 thismemo = yo._put_memo(data)
381 return thismemo
384 "dBase III specific"
385 yo.meta.memo_size= 512
386 yo.record_header_length = 2
387 if yo.meta.ondisk and not yo.meta.ignorememos:
388 if yo.meta.newmemofile:
389 yo.meta.mfd = open(yo.meta.memoname, 'w+b')
390 yo.meta.mfd.write(io.packLongInt(1) + '\x00' * 508)
391 else:
392 try:
393 yo.meta.mfd = open(yo.meta.memoname, 'r+b')
394 yo.meta.mfd.seek(0)
395 yo.nextmemo = io.unpackLongInt(yo.meta.mfd.read(4))
396 except:
397 raise DbfError("memo file appears to be corrupt")
399 block = int(block)
400 yo.meta.mfd.seek(block * yo.meta.memo_size)
401 eom = -1
402 data = ''
403 while eom == -1:
404 newdata = yo.meta.mfd.read(yo.meta.memo_size)
405 if not newdata:
406 return data
407 data += newdata
408 eom = data.find('\x1a\x1a')
409 return data[:eom].rstrip()
411 length = len(data) + yo.record_header_length
412 blocks = length // yo.meta.memo_size
413 if length % yo.meta.memo_size:
414 blocks += 1
415 thismemo = yo.nextmemo
416 yo.nextmemo = thismemo + blocks
417 yo.meta.mfd.seek(0)
418 yo.meta.mfd.write(io.packLongInt(yo.nextmemo))
419 yo.meta.mfd.seek(thismemo * yo.meta.memo_size)
420 yo.meta.mfd.write(data)
421 yo.meta.mfd.write('\x1a\x1a')
422 if len(yo._get_memo(thismemo)) != len(data):
423 raise DbfError("unknown error: memo not saved")
424 return thismemo
427 "Visual Foxpro 6 specific"
428 if yo.meta.ondisk and not yo.meta.ignorememos:
429 yo.record_header_length = 8
430 if yo.meta.newmemofile:
431 if yo.meta.memo_size == 0:
432 yo.meta.memo_size = 1
433 elif 1 < yo.meta.memo_size < 33:
434 yo.meta.memo_size *= 512
435 yo.meta.mfd = open(yo.meta.memoname, 'w+b')
436 nextmemo = 512 // yo.meta.memo_size
437 if nextmemo * yo.meta.memo_size < 512:
438 nextmemo += 1
439 yo.nextmemo = nextmemo
440 yo.meta.mfd.write(io.packLongInt(nextmemo, bigendian=True) + '\x00\x00' + \
441 io.packShortInt(yo.meta.memo_size, bigendian=True) + '\x00' * 504)
442 else:
443 try:
444 yo.meta.mfd = open(yo.meta.memoname, 'r+b')
445 yo.meta.mfd.seek(0)
446 header = yo.meta.mfd.read(512)
447 yo.nextmemo = io.unpackLongInt(header[:4], bigendian=True)
448 yo.meta.memo_size = io.unpackShortInt(header[6:8], bigendian=True)
449 except:
450 raise DbfError("memo file appears to be corrupt")
452 yo.meta.mfd.seek(block * yo.meta.memo_size)
453 header = yo.meta.mfd.read(8)
454 length = io.unpackLongInt(header[4:], bigendian=True)
455 return yo.meta.mfd.read(length)
457 yo.meta.mfd.seek(0)
458 thismemo = io.unpackLongInt(yo.meta.mfd.read(4), bigendian=True)
459 yo.meta.mfd.seek(0)
460 length = len(data) + yo.record_header_length
461 blocks = length // yo.meta.memo_size
462 if length % yo.meta.memo_size:
463 blocks += 1
464 yo.meta.mfd.write(io.packLongInt(thismemo+blocks, bigendian=True))
465 yo.meta.mfd.seek(thismemo*yo.meta.memo_size)
466 yo.meta.mfd.write('\x00\x00\x00\x01' + io.packLongInt(len(data), bigendian=True) + data)
467 return thismemo
469 """Provides a framework for dbf style tables."""
470 _version = 'basic memory table'
471 _versionabbv = 'dbf'
472 _fieldtypes = {
473 'D' : { 'Type':'Date', 'Init':io.addDate, 'Blank':Date.today, 'Retrieve':io.retrieveDate, 'Update':io.updateDate, },
474 'L' : { 'Type':'Logical', 'Init':io.addLogical, 'Blank':bool, 'Retrieve':io.retrieveLogical, 'Update':io.updateLogical, },
475 'M' : { 'Type':'Memo', 'Init':io.addMemo, 'Blank':str, 'Retrieve':io.retrieveMemo, 'Update':io.updateMemo, } }
476 _memoext = ''
477 _memotypes = tuple('M', )
478 _memoClass = _DbfMemo
479 _yesMemoMask = ''
480 _noMemoMask = ''
481 _fixed_fields = ('M','D','L')
482 _variable_fields = tuple()
483 _character_fields = tuple('M', )
484 _decimal_fields = tuple()
485 _numeric_fields = tuple()
486 _dbfTableHeader = array('c', '\x00' * 32)
487 _dbfTableHeader[0] = '\x00'
488 _dbfTableHeader[8:10] = array('c', io.packShortInt(33))
489 _dbfTableHeader[10] = '\x01'
490 _dbfTableHeader[29] = '\x00'
491 _dbfTableHeader = _dbfTableHeader.tostring()
492 _dbfTableHeaderExtra = ''
493 _supported_tables = []
494 _read_only = False
495 _meta_only = False
496 _use_deleted = True
497 _backed_up = False
515 if len(data) != 32:
516 raise DbfError('table header should be 32 bytes, but is %d bytes' % len(data))
517 yo._data = array('c', data + '\x0d')
519 "get/set code page of table"
520 if cp is None:
521 return yo._data[29]
522 else:
523 cp, sd, ld = _codepage_lookup(cp)
524 yo._data[29] = cp
525 return cp
526 @property
532 @data.setter
534 if len(bytes) < 32:
535 raise DbfError("length for data of %d is less than 32" % len(bytes))
536 yo._data[:] = array('c', bytes)
537 @property
539 "extra dbf info (located after headers, before data records)"
540 fieldblock = yo._data[32:]
541 for i in range(len(fieldblock)//32+1):
542 cr = i * 32
543 if fieldblock[cr] == '\x0d':
544 break
545 else:
546 raise DbfError("corrupt field structure")
547 cr += 33
548 return yo._data[cr:].tostring()
549 @extra.setter
551 fieldblock = yo._data[32:]
552 for i in range(len(fieldblock)//32+1):
553 cr = i * 32
554 if fieldblock[cr] == '\x0d':
555 break
556 else:
557 raise DbfError("corrupt field structure")
558 cr += 33
559 yo._data[cr:] = array('c', data)
560 yo._data[8:10] = array('c', io.packShortInt(len(yo._data)))
561 @property
563 "number of fields (read-only)"
564 fieldblock = yo._data[32:]
565 for i in range(len(fieldblock)//32+1):
566 cr = i * 32
567 if fieldblock[cr] == '\x0d':
568 break
569 else:
570 raise DbfError("corrupt field structure")
571 return len(fieldblock[:cr]) // 32
572 @property
574 "field block structure"
575 fieldblock = yo._data[32:]
576 for i in range(len(fieldblock)//32+1):
577 cr = i * 32
578 if fieldblock[cr] == '\x0d':
579 break
580 else:
581 raise DbfError("corrupt field structure")
582 return fieldblock[:cr].tostring()
583 @fields.setter
585 fieldblock = yo._data[32:]
586 for i in range(len(fieldblock)//32+1):
587 cr = i * 32
588 if fieldblock[cr] == '\x0d':
589 break
590 else:
591 raise DbfError("corrupt field structure")
592 cr += 32
593 fieldlen = len(block)
594 if fieldlen % 32 != 0:
595 raise DbfError("fields structure corrupt: %d is not a multiple of 32" % fieldlen)
596 yo._data[32:cr] = array('c', block)
597 yo._data[8:10] = array('c', io.packShortInt(len(yo._data)))
598 fieldlen = fieldlen // 32
599 recordlen = 1
600 for i in range(fieldlen):
601 recordlen += ord(block[i*32+16])
602 yo._data[10:12] = array('c', io.packShortInt(recordlen))
603 @property
605 "number of records (maximum 16,777,215)"
606 return io.unpackLongInt(yo._data[4:8].tostring())
607 @record_count.setter
610 @property
612 "length of a record (read_only) (max of 65,535)"
613 return io.unpackShortInt(yo._data[10:12].tostring())
614 @property
616 "starting position of first record in file (must be within first 64K)"
617 return io.unpackShortInt(yo._data[8:10].tostring())
618 @start.setter
621 @property
623 "date of last table modification (read-only)"
624 return io.unpackDate(yo._data[1:4].tostring())
625 @property
627 "dbf version"
628 return yo._data[0]
629 @version.setter
633 "implements the weakref table for records"
635 yo._meta = meta
636 yo._weakref_list = [weakref.ref(lambda x: None)] * count
650 yo._weakref_list.append(weakref.ref(record))
652 "returns records using current index"
654 yo._table = table
655 yo._index = -1
656 yo._more_records = True
660 while yo._more_records:
661 yo._index += 1
662 if yo._index >= len(yo._table):
663 yo._more_records = False
664 continue
665 record = yo._table[yo._index]
666 if not yo._table.use_deleted and record.has_been_deleted:
667 continue
668 return record
669 else:
670 raise StopIteration
672 "constructs fieldblock for disk table"
673 fieldblock = array('c', '')
674 memo = False
675 yo._meta.header.version = chr(ord(yo._meta.header.version) & ord(yo._noMemoMask))
676 for field in yo._meta.fields:
677 if yo._meta.fields.count(field) > 1:
678 raise DbfError("corrupted field structure (noticed in _buildHeaderFields)")
679 fielddef = array('c', '\x00' * 32)
680 fielddef[:11] = array('c', io.packStr(field))
681 fielddef[11] = yo._meta[field]['type']
682 fielddef[12:16] = array('c', io.packLongInt(yo._meta[field]['start']))
683 fielddef[16] = chr(yo._meta[field]['length'])
684 fielddef[17] = chr(yo._meta[field]['decimals'])
685 fielddef[18] = chr(yo._meta[field]['flags'])
686 fieldblock.extend(fielddef)
687 if yo._meta[field]['type'] in yo._meta.memotypes:
688 memo = True
689 yo._meta.header.fields = fieldblock.tostring()
690 if memo:
691 yo._meta.header.version = chr(ord(yo._meta.header.version) | ord(yo._yesMemoMask))
692 if yo._meta.memo is None:
693 yo._meta.memo = yo._memoClass(yo._meta)
695 "dBase III specific"
696 if yo._meta.header.version == '\x83':
697 try:
698 yo._meta.memo = yo._memoClass(yo._meta)
699 except:
700 yo._meta.dfd.close()
701 yo._meta.dfd = None
702 raise
703 if not yo._meta.ignorememos:
704 for field in yo._meta.fields:
705 if yo._meta[field]['type'] in yo._memotypes:
706 if yo._meta.header.version != '\x83':
707 yo._meta.dfd.close()
708 yo._meta.dfd = None
709 raise DbfError("Table structure corrupt: memo fields exist, header declares no memos")
710 elif not os.path.exists(yo._meta.memoname):
711 yo._meta.dfd.close()
712 yo._meta.dfd = None
713 raise DbfError("Table structure corrupt: memo fields exist without memo file")
714 break
716 "builds the FieldList of names, types, and descriptions from the disk file"
717 offset = 1
718 fieldsdef = yo._meta.header.fields
719 if len(fieldsdef) % 32 != 0:
720 raise DbfError("field definition block corrupt: %d bytes in size" % len(fieldsdef))
721 if len(fieldsdef) // 32 != yo.field_count:
722 raise DbfError("Header shows %d fields, but field definition block has %d fields" % (yo.field_count, len(fieldsdef)//32))
723 for i in range(yo.field_count):
724 fieldblock = fieldsdef[i*32:(i+1)*32]
725 name = io.unpackStr(fieldblock[:11])
726 type = fieldblock[11]
727 if not type in yo._meta.fieldtypes:
728 raise DbfError("Unknown field type: %s" % type)
729 start = offset
730 length = ord(fieldblock[16])
731 offset += length
732 end = start + length
733 decimals = ord(fieldblock[17])
734 flags = ord(fieldblock[18])
735 yo._meta.fields.append(name)
736 yo._meta[name] = {'type':type,'start':start,'length':length,'end':end,'decimals':decimals,'flags':flags}
738 "Returns field information Name Type(Length[,Decimals])"
739 name = yo._meta.fields[i]
740 type = yo._meta[name]['type']
741 length = yo._meta[name]['length']
742 decimals = yo._meta[name]['decimals']
743 if type in yo._decimal_fields:
744 description = "%s %s(%d,%d)" % (name, type, length, decimals)
745 elif type in yo._fixed_fields:
746 description = "%s %s" % (name, type)
747 else:
748 description = "%s %s(%d)" % (name, type, length)
749 return description
751 "loads the records from disk to memory"
752 if yo._meta_only:
753 raise DbfError("%s has been closed, records are unavailable" % yo.filename)
754 dfd = yo._meta.dfd
755 header = yo._meta.header
756 dfd.seek(header.start)
757 allrecords = dfd.read()
758 dfd.seek(0)
759 length = header.record_length
760 for i in range(header.record_count):
761 record_data = allrecords[length*i:length*i+length]
762 yo._table.append(_DbfRecord(i, yo._meta, allrecords[length*i:length*i+length], _fromdisk=True))
763 yo._index.append(i)
764 dfd.seek(0)
766 if specs is None:
767 specs = yo.field_names
768 elif isinstance(specs, str):
769 specs = specs.split(sep)
770 else:
771 specs = list(specs)
772 specs = [s.strip() for s in specs]
773 return specs
775 "synchronizes the disk file with current data"
776 if yo._meta.inmemory:
777 return
778 fd = yo._meta.dfd
779 fd.seek(0)
780 fd.write(yo._meta.header.data)
781 if not headeronly:
782 for record in yo._table:
783 record._update_disk()
784 fd.flush()
785 fd.truncate(yo._meta.header.start + yo._meta.header.record_count * yo._meta.header.record_length)
793 if name in ('_index','_table'):
794 if yo._meta.ondisk:
795 yo._table = yo._Table(len(yo), yo._meta)
796 yo._index = range(len(yo))
797 else:
798 yo._table = []
799 yo._index = []
800 yo._loadtable()
801 return object.__getattribute__(yo, name)
803 if type(value) == int:
804 if not -yo._meta.header.record_count <= value < yo._meta.header.record_count:
805 raise IndexError("Record %d is not in table." % value)
806 return yo._table[yo._index[value]]
807 elif type(value) == slice:
808 sequence = DbfList(desc='%s --> %s' % (yo.filename, value))
809 for index in yo._index[value]:
810 record = yo._table[index]
811 if yo.use_deleted is True or not record.has_been_deleted:
812 sequence.append(record)
813 return sequence
814 else:
815 raise TypeError('type <%s> not valid for indexing' % type(value))
816 - def __init__(yo, filename=':memory:', field_specs=None, memo_size=128, ignore_memos=False,
817 read_only=False, keep_memos=False, meta_only=False, codepage=None):
818 """open/create dbf file
819 filename should include path if needed
820 field_specs can be either a ;-delimited string or a list of strings
821 memo_size is always 512 for db3 memos
822 ignore_memos is useful if the memo file is missing or corrupt
823 read_only will load records into memory, then close the disk file
824 keep_memos will also load any memo fields into memory
825 meta_only will ignore all records, keeping only basic table information
826 codepage will override whatever is set in the table itself"""
827 if filename == ':memory:':
828 if field_specs is None:
829 raise DbfError("field list must be specified for in-memory tables")
830 elif type(yo) is DbfTable:
831 raise DbfError("only memory tables supported")
832 yo._meta = meta = yo._MetaData()
833 meta.table = weakref.ref(yo)
834 meta.filename = filename
835 meta.fields = []
836 meta.fieldtypes = yo._fieldtypes
837 meta.fixed_fields = yo._fixed_fields
838 meta.variable_fields = yo._variable_fields
839 meta.character_fields = yo._character_fields
840 meta.decimal_fields = yo._decimal_fields
841 meta.numeric_fields = yo._numeric_fields
842 meta.memotypes = yo._memotypes
843 meta.ignorememos = ignore_memos
844 meta.memo_size = memo_size
845 meta.input_decoder = codecs.getdecoder(input_decoding)
846 meta.output_encoder = codecs.getencoder(input_decoding)
847 meta.return_ascii = return_ascii
848 meta.header = header = yo._TableHeader(yo._dbfTableHeader)
849 header.extra = yo._dbfTableHeaderExtra
850 header.data
851 if filename == ':memory:':
852 yo._index = []
853 yo._table = []
854 meta.ondisk = False
855 meta.inmemory = True
856 meta.memoname = ':memory:'
857 else:
858 base, ext = os.path.splitext(filename)
859 if ext == '':
860 meta.filename = base + '.dbf'
861 meta.memoname = base + yo._memoext
862 meta.ondisk = True
863 meta.inmemory = False
864 if field_specs:
865 if meta.ondisk:
866 meta.dfd = open(meta.filename, 'w+b')
867 meta.newmemofile = True
868 yo.add_fields(field_specs)
869 header.codepage = codepage or default_codepage
870 meta.decoder = codecs.getdecoder(header.codepage)
871 meta.encoder = codecs.getencoder(header.codepage)
872 return
873 dfd = meta.dfd = open(meta.filename, 'r+b')
874 dfd.seek(0)
875 meta.header = header = yo._TableHeader(dfd.read(32))
876 if not header.version in yo._supported_tables:
877 dfd.close()
878 dfd = None
879 raise TypeError("Unsupported dbf type: %s [%x]" % (version_map.get(meta.header.version, 'Unknown: %s' % meta.header.version), ord(meta.header.version)))
880 cp, sd, ld = _codepage_lookup(meta.header.codepage())
881 yo._meta.decoder = codecs.getdecoder(sd)
882 yo._meta.encoder = codecs.getencoder(sd)
883 fieldblock = dfd.read(header.start - 32)
884 for i in range(len(fieldblock)//32+1):
885 fieldend = i * 32
886 if fieldblock[fieldend] == '\x0d':
887 break
888 else:
889 raise DbfError("corrupt field structure in header")
890 if len(fieldblock[:fieldend]) % 32 != 0:
891 raise DbfError("corrupt field structure in header")
892 header.fields = fieldblock[:fieldend]
893 header.extra = fieldblock[fieldend+1:]
894 yo._initializeFields()
895 yo._checkMemoIntegrity()
896 meta.current = -1
897 if len(yo) > 0:
898 meta.current = 0
899 dfd.seek(0)
900 if meta_only:
901 yo.close(keep_table=False, keep_memos=False)
902 elif read_only:
903 yo.close(keep_table=True, keep_memos=keep_memos)
904 if codepage is not None:
905 cp, sd, ld = _codepage_lookup(codepage)
906 yo._meta.decoder = codecs.getdecoder(sd)
907 yo._meta.encoder = codecs.getencoder(sd)
908
916 if yo._read_only:
917 return __name__ + ".Table('%s', read_only=True)" % yo._meta.filename
918 elif yo._meta_only:
919 return __name__ + ".Table('%s', meta_only=True)" % yo._meta.filename
920 else:
921 return __name__ + ".Table('%s')" % yo._meta.filename
923 if yo._read_only:
924 status = "read-only"
925 elif yo._meta_only:
926 status = "meta-only"
927 else:
928 status = "read/write"
929 str = """
930 Table: %s
931 Type: %s
932 Codepage: %s
933 Status: %s
934 Last updated: %s
935 Record count: %d
936 Field count: %d
937 Record length: %d
938 """ % (yo.filename, version_map.get(yo._meta.header.version, 'unknown - ' + hex(ord(yo._meta.header.version))),
939 yo.codepage, status, yo.last_update, len(yo), yo.field_count, yo.record_length)
940 str += "\n --Fields--\n"
941 for i in range(len(yo._meta.fields)):
942 str += " " + yo._fieldLayout(i) + "\n"
943 return str
944 @property
946 return "%s (%s)" % code_pages[yo._meta.header.codepage()]
947 @codepage.setter
948 - def codepage(yo, cp):
949 cp = code_pages[yo._meta.header.codepage(cp)][0]
950 yo._meta.decoder = codecs.getdecoder(cp)
951 yo._meta.encoder = codecs.getencoder(cp)
952 yo._update_disk(headeronly=True)
953 @property
955 "the number of fields in the table"
956 return yo._meta.header.field_count
957 @property
959 "a list of the fields in the table"
960 return yo._meta.fields[:]
961 @property
963 "table's file name, including path (if specified on open)"
964 return yo._meta.filename
965 @property
967 "date of last update"
968 return yo._meta.header.update
969 @property
971 "table's memo name (if path included in filename on open)"
972 return yo._meta.memoname
973 @property
975 "number of bytes in a record"
976 return yo._meta.header.record_length
977 @property
979 "index number of the current record"
980 return yo._meta.current
981 @property
985 @property
987 "process or ignore deleted records"
988 return yo._use_deleted
989 @use_deleted.setter
992 @property
994 "returns the dbf type of the table"
995 return yo._version
997 """adds field(s) to the table layout; format is Name Type(Length,Decimals)[; Name Type(Length,Decimals)[...]]
998 backup table is created with _backup appended to name
999 then modifies current structure"""
1000 all_records = [record for record in yo]
1001 if yo:
1002 yo.create_backup()
1003 yo._meta.blankrecord = None
1004 meta = yo._meta
1005 offset = meta.header.record_length
1006 fields = yo._list_fields(field_specs, sep=';')
1007 for field in fields:
1008 try:
1009 name, format = field.split()
1010 if name[0] == '_' or name[0].isdigit() or not name.replace('_','').isalnum():
1011 raise DbfError("Field names cannot start with _ or digits, and can only contain the _, letters, and digits")
1012 name = name.lower()
1013 if name in meta.fields:
1014 raise DbfError("Field '%s' already exists" % name)
1015 field_type = format[0].upper()
1016 if len(name) > 10:
1017 raise DbfError("Maximum field name length is 10. '%s' is %d characters long." % (name, len(name)))
1018 if not field_type in meta.fieldtypes.keys():
1019 raise DbfError("Unknown field type: %s" % field_type)
1020 length, decimals = yo._meta.fieldtypes[field_type]['Init'](format)
1021 except ValueError:
1022 raise DbfError("invalid field specifier: %s" % field)
1023 start = offset
1024 end = offset + length
1025 offset = end
1026 meta.fields.append(name)
1027 meta[name] = {'type':field_type, 'start':start, 'length':length, 'end':end, 'decimals':decimals, 'flags':0}
1028 if meta[name]['type'] in yo._memotypes and meta.memo is None:
1029 meta.memo = yo._memoClass(meta)
1030 for record in yo:
1031 record[name] = meta.fieldtypes[field_type]['Blank']()
1032 yo._buildHeaderFields()
1033 yo._update_disk()
1034 - def append(yo, kamikaze='', drop=False, multiple=1):
1035 "adds <multiple> blank records, and fills fields with dict/tuple values if present"
1036 if not yo.field_count:
1037 raise DbfError("No fields defined, cannot append")
1038 empty_table = len(yo) == 0
1039 dictdata = False
1040 tupledata = False
1041 if not isinstance(kamikaze, _DbfRecord):
1042 if isinstance(kamikaze, dict):
1043 dictdata = kamikaze
1044 kamikaze = ''
1045 elif isinstance(kamikaze, tuple):
1046 tupledata = kamikaze
1047 kamikaze = ''
1048 newrecord = _DbfRecord(recnum=yo._meta.header.record_count, layout=yo._meta, kamikaze=kamikaze)
1049 yo._table.append(newrecord)
1050 yo._index.append(yo._meta.header.record_count)
1051 yo._meta.header.record_count += 1
1052 if dictdata:
1053 newrecord.gather_fields(dictdata, drop)
1054 elif tupledata:
1055 for index, item in enumerate(tupledata):
1056 newrecord[index] = item
1057 elif kamikaze == str:
1058 for field in yo._meta.memofields:
1059 newrecord[field] = ''
1060 elif kamikaze:
1061 for field in yo._meta.memofields:
1062 newrecord[field] = kamikaze[field]
1063 multiple -= 1
1064 if multiple:
1065 data = newrecord._data
1066 single = yo._meta.header.record_count
1067 total = single + multiple
1068 while single < total:
1069 multi_record = _DbfRecord(single, yo._meta, kamikaze=data)
1070 yo._table.append(multi_record)
1071 yo._index.append(single)
1072 for field in yo._meta.memofields:
1073 multi_record[field] = newrecord[field]
1074 single += 1
1075 yo._meta.header.record_count = total
1076 yo._meta.current = yo._meta.header.record_count - 1
1077 newrecord = multi_record
1078 yo._update_disk(headeronly=True)
1079 if empty_table:
1080 yo._meta.current = 0
1081 return newrecord
1083 "moves record pointer to previous usable record; returns True if no more usable records"
1084 while yo._meta.current > 0:
1085 yo._meta.current -= 1
1086 if yo.use_deleted or not yo.current().has_been_deleted:
1087 break
1088 else:
1089 yo._meta.current = -1
1090 return True
1091 return False
1092 - def bottom(yo, get_record=False):
1093 """sets record pointer to bottom of table
1094 if get_record, seeks to and returns last (non-deleted) record
1095 DbfError if table is empty
1096 Bof if all records deleted and use_deleted is False"""
1097 yo._meta.current = yo._meta.header.record_count
1098 if get_record:
1099 try:
1100 return yo.prev()
1101 except Bof:
1102 yo._meta.current = yo._meta.header.record_count
1103 raise Eof()
1104 - def close(yo, keep_table=False, keep_memos=False):
1105 """closes disk files
1106 ensures table data is available if keep_table
1107 ensures memo data is available if keep_memos"""
1108 if keep_table:
1109 yo._table
1110 else:
1111 if '_index' in dir(yo):
1112 del yo._table
1113 del yo._index
1114 yo._meta.inmemory = True
1115 if yo._meta.ondisk:
1116 yo._meta.dfd.close()
1117 yo._meta.dfd = None
1118 if '_index' in dir(yo):
1119 yo._read_only = True
1120 else:
1121 yo._meta_only = True
1122 if yo._meta.mfd is not None:
1123 if not keep_memos:
1124 yo._meta.ignorememos = True
1125 else:
1126 memo_fields = []
1127 for field in yo.field_names:
1128 if yo.is_memotype(field):
1129 memo_fields.append(field)
1130 for record in yo:
1131 for field in memo_fields:
1132 record[field] = record[field]
1133 yo._meta.mfd.close()
1134 yo._meta.mfd = None
1135 yo._meta.ondisk = False
1137 "creates a backup table -- ignored if memory table"
1138 if yo.filename.startswith(':memory:'):
1139 return
1140 if new_name is None:
1141 new_name = os.path.splitext(yo.filename)[0] + '_backup.dbf'
1142 else:
1143 overwrite = True
1144 if overwrite or not yo._backed_up:
1145 bkup = open(new_name, 'wb')
1146 try:
1147 yo._meta.dfd.seek(0)
1148 copyfileobj(yo._meta.dfd, bkup)
1149 yo._backed_up = True
1150 finally:
1151 bkup.close()
1153 "returns current logical record, or its index"
1154 if yo._meta.current < 0:
1155 raise Bof()
1156 elif yo._meta.current >= yo._meta.header.record_count:
1157 raise Eof()
1158 if index:
1159 return yo._meta.current
1160 return yo._table[yo._index[yo._meta.current]]
1162 """removes field(s) from the table
1163 creates backup files with _backup appended to the file name,
1164 then modifies current structure"""
1165 doomed = yo._list_fields(doomed)
1166 for victim in doomed:
1167 if victim not in yo._meta.fields:
1168 raise DbfError("field %s not in table -- delete aborted" % victim)
1169 all_records = [record for record in yo]
1170 yo.create_backup()
1171 for victim in doomed:
1172 yo._meta.fields.pop(yo._meta.fields.index(victim))
1173 start = yo._meta[victim]['start']
1174 end = yo._meta[victim]['end']
1175 for record in yo:
1176 record._data = record._data[:start] + record._data[end:]
1177 for field in yo._meta.fields:
1178 if yo._meta[field]['start'] == end:
1179 end = yo._meta[field]['end']
1180 yo._meta[field]['start'] = start
1181 yo._meta[field]['end'] = start + yo._meta[field]['length']
1182 start = yo._meta[field]['end']
1183 yo._buildHeaderFields()
1184 yo._update_disk()
1195 - def export(yo, records=None, filename=None, field_specs=None, format='csv', header=True):
1196 """writes the table using CSV or tab-delimited format, using the filename
1197 given if specified, otherwise the table name"""
1198 if filename is None:
1199 filename = yo.filename
1200 field_specs = yo._list_fields(field_specs)
1201 if records is None:
1202 records = yo
1203 format = format.lower()
1204 if format not in ('csv', 'tab'):
1205 raise DbfError("export format: csv or tab, not %s" % format)
1206 base, ext = os.path.splitext(filename)
1207 if ext.lower() in ('', '.dbf'):
1208 filename = base + "." + format
1209 fd = open(filename, 'wb')
1210 try:
1211 if format == 'csv':
1212 csvfile = csv.writer(fd, dialect='dbf')
1213 if header:
1214 csvfile.writerow(field_specs)
1215 for record in records:
1216 fields = []
1217 for fieldname in field_specs:
1218 fields.append(record[fieldname])
1219 csvfile.writerow(fields)
1220 else:
1221 if header:
1222 fd.write('\t'.join(field_specs) + '\n')
1223 for record in records:
1224 fields = []
1225 for fieldname in field_specs:
1226 fields.append(str(record[fieldname]))
1227 fd.write('\t'.join(fields) + '\n')
1228 finally:
1229 fd.close()
1230 fd = None
1231 return len(records)
1233 "returns record at physical_index[recno]"
1234 return yo._table[recno]
1235 - def goto(yo, criteria):
1236 """changes the record pointer to the first matching (non-deleted) record
1237 criteria should be either a tuple of tuple(value, field, func) triples,
1238 or an integer to go to"""
1239 if isinstance(criteria, int):
1240 if not -yo._meta.header.record_count <= criteria < yo._meta.header.record_count:
1241 raise IndexError("Record %d does not exist" % criteria)
1242 if criteria < 0:
1243 criteria += yo._meta.header.record_count
1244 yo._meta.current = criteria
1245 return yo.current()
1246 criteria = _normalize_tuples(tuples=criteria, length=3, filler=[_nop])
1247 specs = tuple([(field, func) for value, field, func in criteria])
1248 match = tuple([value for value, field, func in criteria])
1249 current = yo.current(index=True)
1250 matchlen = len(match)
1251 while not yo.Eof():
1252 record = yo.current()
1253 results = record(*specs)
1254 if results == match:
1255 return record
1256 return yo.goto(current)
1257 - def index(yo, sort=None, reverse=False):
1278 "returns True if name is a memo type field"
1279 return yo._meta[name]['type'] in yo._memotypes
1280 - def new(yo, filename, _field_specs=None):
1281 "returns a new table of the same type"
1282 if _field_specs is None:
1283 _field_specs = yo.structure()
1284 if filename != ':memory:':
1285 path, name = os.path.split(filename)
1286 if path == "":
1287 filename = os.path.join(os.path.split(yo.filename)[0], filename)
1288 elif name == "":
1289 filename = os.path.join(path, os.path.split(yo.filename)[1])
1290 return yo.__class__(filename, _field_specs)
1292 "set record pointer to next (non-deleted) record, and return it"
1293 if yo.eof():
1294 raise Eof()
1295 return yo.current()
1296 - def pack(yo, _pack=True):
1297 "physically removes all deleted records"
1298 newtable = []
1299 newindex = []
1300 i = 0
1301 for record in yo._table:
1302 if record.has_been_deleted and _pack:
1303 record._recnum = -1
1304 else:
1305 record._recnum = i
1306 newtable.append(record)
1307 newindex.append(i)
1308 i += 1
1309 yo._table = newtable
1310 yo._index = newindex
1311 yo._meta.header.record_count = i
1312 yo._current = -1
1313 yo._meta.index = ''
1314 yo._update_disk()
1316 "set record pointer to previous (non-deleted) record, and return it"
1317 if yo.bof():
1318 raise Bof
1319 return yo.current()
1320 - def query(yo, sql=None, python=None):
1321 "uses exec to perform python queries on the table"
1322 if python is None:
1323 raise DbfError("query: python parameter must be specified")
1324 possible = DbfList(desc="%s --> %s" % (yo.filename, python))
1325 query_result = {}
1326 select = 'query_result["keep"] = %s' % python
1327 g = {}
1328 for record in yo:
1329 query_result['keep'] = False
1330 g['query_result'] = query_result
1331 exec select in g, record
1332 if query_result['keep']:
1333 possible.append(record)
1334 return possible
1336 "renames an existing field"
1337 if yo:
1338 yo.create_backup()
1339 if not oldname in yo._meta.fields:
1340 raise DbfError("field --%s-- does not exist -- cannot rename it." % oldname)
1341 if newname[0] == '_' or newname[0].isdigit() or not newname.replace('_','').isalnum():
1342 raise DbfError("field names cannot start with _ or digits, and can only contain the _, letters, and digits")
1343 newname = newname.lower()
1344 if newname in yo._meta.fields:
1345 raise DbfError("field --%s-- already exists" % newname)
1346 if len(newname) > 10:
1347 raise DbfError("maximum field name length is 10. '%s' is %d characters long." % (newname, len(newname)))
1348 yo._meta[newname] = yo._meta[oldname]
1349 yo._meta.fields[yo._meta.fields.index(oldname)] = newname
1350 yo._buildHeaderFields()
1351 yo._update_disk(headeronly=True)
1352 - def search(yo, match, fuzzy=None, indices=False):
1353 """searches using a binary algorythm
1354 looking for records that match the criteria in match, which is a tuple
1355 with a data item per ordered field. table must be sorted. if index,
1356 returns a list of records' indices from the current sort order.
1357 """
1358 if yo._meta.index is None:
1359 raise DbfError('table must be indexed to use Search')
1360 matchlen = len(match)
1361 if fuzzy:
1362 matchlen -= 1
1363 fuzzy_match = match[-1]
1364 fuzzy_field = yo._meta.index[matchlen][0]
1365 match = match[:-1]
1366 records = DbfList(desc="%s --> search: index=%s, match=%s, fuzzy=%s(%s))" % (yo.filename, yo.index(), match, fuzzy.__name__, fuzzy_match))
1367 else:
1368 records = DbfList(desc="%s --> search: index=%s, match=%s)" % (yo.filename, yo.index(), match))
1369 if indices:
1370 records = []
1371 if not isinstance(match, tuple):
1372 match = tuple(match)
1373 segment = len(yo)
1374 current = 0
1375 toosoon = True
1376 notFound = True
1377 while notFound:
1378 segment = segment // 2
1379 if toosoon:
1380 current += segment
1381 else:
1382 current -= segment
1383 if current % 2:
1384 segment += 1
1385 if current == len(yo) or segment == 0:
1386 break
1387 value = yo._meta.orderresults[yo[current].record_number][:matchlen]
1388 if value < match:
1389 toosoon = True
1390 elif value > match:
1391 toosoon = False
1392 else:
1393 notFound = False
1394 break
1395 if current == 0:
1396 break
1397 if notFound:
1398 return records
1399 while current > 0:
1400 current -= 1
1401 value = yo._meta.orderresults[yo[current].record_number][:matchlen]
1402 if value != match:
1403 current += 1
1404 break
1405 while True:
1406 value = yo._meta.orderresults[yo[current].record_number][:matchlen]
1407 if value != match:
1408 break
1409 if yo.use_deleted or not yo[current].has_been_deleted:
1410 if indices:
1411 records.append(current)
1412 else:
1413 records.append(yo[current])
1414 current += 1
1415 if current == len(yo):
1416 break
1417 if fuzzy:
1418 if indices:
1419 records = [rec for rec in records if fuzzy(yo[rec][fuzzy_field]) == fuzzy_match]
1420 else:
1421 final_records = [rec for rec in records if fuzzy(rec[fuzzy_field]) == fuzzy_match]
1422 records.clear()
1423 records.extend(final_records)
1424 return records
1425 - def size(yo, field):
1426 "returns size of field as a tuple of (length, decimals)"
1427 if field in yo:
1428 return (yo._meta[field]['length'], yo._meta[field]['decimals'])
1429 raise DbfError("%s is not a field in %s" % (field, yo.filename))
1431 """return list of fields suitable for creating same table layout
1432 @param fields: list of fields or None for all fields"""
1433 field_specs = []
1434 fields = yo._list_fields(fields)
1435 try:
1436 for name in fields:
1437 field_specs.append(yo._fieldLayout(yo.field_names.index(name)))
1438 except ValueError:
1439 raise DbfError("field --%s-- does not exist" % name)
1440 return field_specs
1441 - def top(yo, get_record=False):
1442 """sets record pointer to top of table; if get_record, seeks to and returns first (non-deleted) record
1443 DbfError if table is empty
1444 Eof if all records are deleted and use_deleted is False"""
1445 yo._meta.current = -1
1446 if get_record:
1447 try:
1448 return yo.next()
1449 except Eof:
1450 yo._meta.current = -1
1451 raise Bof()
1452 - def type(yo, field):
1453 "returns type of field"
1454 if field in yo:
1455 return yo._meta[field]['type']
1456 raise DbfError("%s is not a field in %s" % (field, yo.filename))
1457 - def zap(yo, areyousure=False):
1458 """removes all records from table -- this cannot be undone!
1459 areyousure must be True, else error is raised"""
1460 if areyousure:
1461 yo._table = []
1462 yo._index = []
1463 yo._meta.header.record_count = 0
1464 yo._current = -1
1465 yo._meta.index = ''
1466 yo._update_disk()
1467 else:
1468 raise DbfError("You must say you are sure to wipe the table")
1469
1471 """Provides an interface for working with dBase III tables."""
1472 _version = 'dBase III Plus'
1473 _versionabbv = 'db3'
1474 _fieldtypes = {
1475 'C' : {'Type':'Character', 'Retrieve':io.retrieveCharacter, 'Update':io.updateCharacter, 'Blank':str, 'Init':io.addCharacter},
1476 'D' : {'Type':'Date', 'Retrieve':io.retrieveDate, 'Update':io.updateDate, 'Blank':Date.today, 'Init':io.addDate},
1477 'L' : {'Type':'Logical', 'Retrieve':io.retrieveLogical, 'Update':io.updateLogical, 'Blank':bool, 'Init':io.addLogical},
1478 'M' : {'Type':'Memo', 'Retrieve':io.retrieveMemo, 'Update':io.updateMemo, 'Blank':str, 'Init':io.addMemo},
1479 'N' : {'Type':'Numeric', 'Retrieve':io.retrieveNumeric, 'Update':io.updateNumeric, 'Blank':int, 'Init':io.addNumeric} }
1480 _memoext = '.dbt'
1481 _memotypes = ('M',)
1482 _memoClass = _Db3Memo
1483 _yesMemoMask = '\x80'
1484 _noMemoMask = '\x7f'
1485 _fixed_fields = ('D','L','M')
1486 _variable_fields = ('C','N')
1487 _character_fields = ('C','M')
1488 _decimal_fields = ('N',)
1489 _numeric_fields = ('N',)
1490 _dbfTableHeader = array('c', '\x00' * 32)
1491 _dbfTableHeader[0] = '\x03'
1492 _dbfTableHeader[8:10] = array('c', io.packShortInt(33))
1493 _dbfTableHeader[10] = '\x01'
1494 _dbfTableHeader[29] = '\x03'
1495 _dbfTableHeader = _dbfTableHeader.tostring()
1496 _dbfTableHeaderExtra = ''
1497 _supported_tables = ['\x03', '\x83']
1498 _read_only = False
1499 _meta_only = False
1500 _use_deleted = True
1502 "dBase III specific"
1503 if yo._meta.header.version == '\x83':
1504 try:
1505 yo._meta.memo = yo._memoClass(yo._meta)
1506 except:
1507 yo._meta.dfd.close()
1508 yo._meta.dfd = None
1509 raise
1510 if not yo._meta.ignorememos:
1511 for field in yo._meta.fields:
1512 if yo._meta[field]['type'] in yo._memotypes:
1513 if yo._meta.header.version != '\x83':
1514 yo._meta.dfd.close()
1515 yo._meta.dfd = None
1516 raise DbfError("Table structure corrupt: memo fields exist, header declares no memos")
1517 elif not os.path.exists(yo._meta.memoname):
1518 yo._meta.dfd.close()
1519 yo._meta.dfd = None
1520 raise DbfError("Table structure corrupt: memo fields exist without memo file")
1521 break
1523 "builds the FieldList of names, types, and descriptions"
1524 offset = 1
1525 fieldsdef = yo._meta.header.fields
1526 if len(fieldsdef) % 32 != 0:
1527 raise DbfError("field definition block corrupt: %d bytes in size" % len(fieldsdef))
1528 if len(fieldsdef) // 32 != yo.field_count:
1529 raise DbfError("Header shows %d fields, but field definition block has %d fields" % (yo.field_count, len(fieldsdef)//32))
1530 for i in range(yo.field_count):
1531 fieldblock = fieldsdef[i*32:(i+1)*32]
1532 name = io.unpackStr(fieldblock[:11])
1533 type = fieldblock[11]
1534 if not type in yo._meta.fieldtypes:
1535 raise DbfError("Unknown field type: %s" % type)
1536 start = offset
1537 length = ord(fieldblock[16])
1538 offset += length
1539 end = start + length
1540 decimals = ord(fieldblock[17])
1541 flags = ord(fieldblock[18])
1542 yo._meta.fields.append(name)
1543 yo._meta[name] = {'type':type,'start':start,'length':length,'end':end,'decimals':decimals,'flags':flags}
1545 'Provides an interface for working with FoxPro 2 tables'
1546 _version = 'Foxpro'
1547 _versionabbv = 'fp'
1548 _fieldtypes = {
1549 'C' : {'Type':'Character', 'Retrieve':io.retrieveCharacter, 'Update':io.updateCharacter, 'Blank':str, 'Init':io.addCharacter},
1550 'F' : {'Type':'Float', 'Retrieve':io.retrieveNumeric, 'Update':io.updateNumeric, 'Blank':float, 'Init':io.addVfpNumeric},
1551 'N' : {'Type':'Numeric', 'Retrieve':io.retrieveNumeric, 'Update':io.updateNumeric, 'Blank':int, 'Init':io.addVfpNumeric},
1552 'L' : {'Type':'Logical', 'Retrieve':io.retrieveLogical, 'Update':io.updateLogical, 'Blank':bool, 'Init':io.addLogical},
1553 'D' : {'Type':'Date', 'Retrieve':io.retrieveDate, 'Update':io.updateDate, 'Blank':Date.today, 'Init':io.addDate},
1554 'M' : {'Type':'Memo', 'Retrieve':io.retrieveMemo, 'Update':io.updateMemo, 'Blank':str, 'Init':io.addVfpMemo},
1555 'G' : {'Type':'General', 'Retrieve':io.retrieveMemo, 'Update':io.updateMemo, 'Blank':str, 'Init':io.addMemo},
1556 'P' : {'Type':'Picture', 'Retrieve':io.retrieveMemo, 'Update':io.updateMemo, 'Blank':str, 'Init':io.addMemo},
1557 '0' : {'Type':'_NullFlags', 'Retrieve':io.unsupportedType, 'Update':io.unsupportedType, 'Blank':int, 'Init':None} }
1558 _memoext = '.fpt'
1559 _memotypes = ('G','M','P')
1560 _memoClass = _VfpMemo
1561 _yesMemoMask = '\xf5'
1562 _noMemoMask = '\x03'
1563 _fixed_fields = ('B','D','G','I','L','M','P','T','Y')
1564 _variable_fields = ('C','F','N')
1565 _character_fields = ('C','M')
1566 _decimal_fields = ('F','N')
1567 _numeric_fields = ('B','F','I','N','Y')
1568 _supported_tables = ('\x03', '\xf5')
1569 _dbfTableHeader = array('c', '\x00' * 32)
1570 _dbfTableHeader[0] = '\x30'
1571 _dbfTableHeader[8:10] = array('c', io.packShortInt(33+263))
1572 _dbfTableHeader[10] = '\x01'
1573 _dbfTableHeader[29] = '\x03'
1574 _dbfTableHeader = _dbfTableHeader.tostring()
1575 _dbfTableHeaderExtra = '\x00' * 263
1576 _use_deleted = True
1578 if os.path.exists(yo._meta.memoname):
1579 try:
1580 yo._meta.memo = yo._memoClass(yo._meta)
1581 except:
1582 yo._meta.dfd.close()
1583 yo._meta.dfd = None
1584 raise
1585 if not yo._meta.ignorememos:
1586 for field in yo._meta.fields:
1587 if yo._meta[field]['type'] in yo._memotypes:
1588 if not os.path.exists(yo._meta.memoname):
1589 yo._meta.dfd.close()
1590 yo._meta.dfd = None
1591 raise DbfError("Table structure corrupt: memo fields exist without memo file")
1592 break
1594 "builds the FieldList of names, types, and descriptions"
1595 offset = 1
1596 fieldsdef = yo._meta.header.fields
1597 if len(fieldsdef) % 32 != 0:
1598 raise DbfError("field definition block corrupt: %d bytes in size" % len(fieldsdef))
1599 if len(fieldsdef) // 32 != yo.field_count:
1600 raise DbfError("Header shows %d fields, but field definition block has %d fields" % (yo.field_count, len(fieldsdef)//32))
1601 for i in range(yo.field_count):
1602 fieldblock = fieldsdef[i*32:(i+1)*32]
1603 name = io.unpackStr(fieldblock[:11])
1604 type = fieldblock[11]
1605 if not type in yo._meta.fieldtypes:
1606 raise DbfError("Unknown field type: %s" % type)
1607 elif type == '0':
1608 return
1609 start = offset
1610 length = ord(fieldblock[16])
1611 offset += length
1612 end = start + length
1613 decimals = ord(fieldblock[17])
1614 flags = ord(fieldblock[18])
1615 yo._meta.fields.append(name)
1616 yo._meta[name] = {'type':type,'start':start,'length':length,'end':end,'decimals':decimals,'flags':flags}
1617
1619 'Provides an interface for working with Visual FoxPro 6 tables'
1620 _version = 'Visual Foxpro v6'
1621 _versionabbv = 'vfp'
1622 _fieldtypes = {
1623 'C' : {'Type':'Character', 'Retrieve':io.retrieveCharacter, 'Update':io.updateCharacter, 'Blank':str, 'Init':io.addCharacter},
1624 'Y' : {'Type':'Currency', 'Retrieve':io.retrieveCurrency, 'Update':io.updateCurrency, 'Blank':Decimal(), 'Init':io.addVfpCurrency},
1625 'B' : {'Type':'Double', 'Retrieve':io.retrieveDouble, 'Update':io.updateDouble, 'Blank':float, 'Init':io.addVfpDouble},
1626 'F' : {'Type':'Float', 'Retrieve':io.retrieveNumeric, 'Update':io.updateNumeric, 'Blank':float, 'Init':io.addVfpNumeric},
1627 'N' : {'Type':'Numeric', 'Retrieve':io.retrieveNumeric, 'Update':io.updateNumeric, 'Blank':int, 'Init':io.addVfpNumeric},
1628 'I' : {'Type':'Integer', 'Retrieve':io.retrieveInteger, 'Update':io.updateInteger, 'Blank':int, 'Init':io.addVfpInteger},
1629 'L' : {'Type':'Logical', 'Retrieve':io.retrieveLogical, 'Update':io.updateLogical, 'Blank':bool, 'Init':io.addLogical},
1630 'D' : {'Type':'Date', 'Retrieve':io.retrieveDate, 'Update':io.updateDate, 'Blank':Date.today, 'Init':io.addDate},
1631 'T' : {'Type':'DateTime', 'Retrieve':io.retrieveVfpDateTime, 'Update':io.updateVfpDateTime, 'Blank':DateTime.now, 'Init':io.addVfpDateTime},
1632 'M' : {'Type':'Memo', 'Retrieve':io.retrieveVfpMemo, 'Update':io.updateVfpMemo, 'Blank':str, 'Init':io.addVfpMemo},
1633 'G' : {'Type':'General', 'Retrieve':io.retrieveVfpMemo, 'Update':io.updateVfpMemo, 'Blank':str, 'Init':io.addVfpMemo},
1634 'P' : {'Type':'Picture', 'Retrieve':io.retrieveVfpMemo, 'Update':io.updateVfpMemo, 'Blank':str, 'Init':io.addVfpMemo},
1635 '0' : {'Type':'_NullFlags', 'Retrieve':io.unsupportedType, 'Update':io.unsupportedType, 'Blank':int, 'Init':None} }
1636 _memoext = '.fpt'
1637 _memotypes = ('G','M','P')
1638 _memoClass = _VfpMemo
1639 _yesMemoMask = '\x30'
1640 _noMemoMask = '\x30'
1641 _fixed_fields = ('B','D','G','I','L','M','P','T','Y')
1642 _variable_fields = ('C','F','N')
1643 _character_fields = ('C','M')
1644 _decimal_fields = ('F','N')
1645 _numeric_fields = ('B','F','I','N','Y')
1646 _supported_tables = ('\x30',)
1647 _dbfTableHeader = array('c', '\x00' * 32)
1648 _dbfTableHeader[0] = '\x30'
1649 _dbfTableHeader[8:10] = array('c', io.packShortInt(33+263))
1650 _dbfTableHeader[10] = '\x01'
1651 _dbfTableHeader[29] = '\x03'
1652 _dbfTableHeader = _dbfTableHeader.tostring()
1653 _dbfTableHeaderExtra = '\x00' * 263
1654 _use_deleted = True
1656 if os.path.exists(yo._meta.memoname):
1657 try:
1658 yo._meta.memo = yo._memoClass(yo._meta)
1659 except:
1660 yo._meta.dfd.close()
1661 yo._meta.dfd = None
1662 raise
1663 if not yo._meta.ignorememos:
1664 for field in yo._meta.fields:
1665 if yo._meta[field]['type'] in yo._memotypes:
1666 if not os.path.exists(yo._meta.memoname):
1667 yo._meta.dfd.close()
1668 yo._meta.dfd = None
1669 raise DbfError("Table structure corrupt: memo fields exist without memo file")
1670 break
1672 "builds the FieldList of names, types, and descriptions"
1673 offset = 1
1674 fieldsdef = yo._meta.header.fields
1675 for i in range(yo.field_count):
1676 fieldblock = fieldsdef[i*32:(i+1)*32]
1677 name = io.unpackStr(fieldblock[:11])
1678 type = fieldblock[11]
1679 if not type in yo._meta.fieldtypes:
1680 raise DbfError("Unknown field type: %s" % type)
1681 elif type == '0':
1682 return
1683 start = io.unpackLongInt(fieldblock[12:16])
1684 length = ord(fieldblock[16])
1685 offset += length
1686 end = start + length
1687 decimals = ord(fieldblock[17])
1688 flags = ord(fieldblock[18])
1689 yo._meta.fields.append(name)
1690 yo._meta[name] = {'type':type,'start':start,'length':length,'end':end,'decimals':decimals,'flags':flags}
1692 "list of Dbf records, with set-like behavior"
1693 _desc = ''
1694 - def __init__(yo, new_records=None, desc=None):
1695 yo._list = []
1696 yo._set = set()
1697 yo._current = -1
1698 if isinstance(new_records, DbfList):
1699 yo._list = new_records._list[:]
1700 yo._set = new_records._set.copy()
1701 yo._current = 0
1702 elif new_records is not None:
1703 for record in new_records:
1704 item = (record.record_table, record.record_number)
1705 if item not in yo._set:
1706 yo._set.add(item)
1707 yo._list.append(item)
1708 yo._current = 0
1709 if desc is not None:
1710 yo._desc = desc
1712 if isinstance(other, DbfList):
1713 result = DbfList()
1714 result._set = yo._set.copy()
1715 result._list[:] = yo._list[:]
1716 for item in other._list:
1717 if item not in result._set:
1718 result._set.add(item)
1719 result._list.append(item)
1720 result._current = 0 if result else -1
1721 return result
1722 return NotImplemented
1724 if isinstance(key, int):
1725 item = yo._list.pop[key]
1726 yo._set.remove(item)
1727 elif isinstance(key, slice):
1728 yo._set.difference_update(yo._list[key])
1729 yo._list.__delitem__(key)
1730 else:
1731 raise TypeError
1733 if isinstance(key, int):
1734 count = len(yo._list)
1735 if not -count <= key < count:
1736 raise IndexError("Record %d is not in list." % key)
1737 return yo._get_record(*yo._list[key])
1738 elif isinstance(key, slice):
1739 result = DbfList()
1740 result._list[:] = yo._list[key]
1741 result._set = set(result._list)
1742 result._current = 0 if result else -1
1743 return result
1744 else:
1745 raise TypeError
1747 return (table.get_record(recno) for table, recno in yo._list)
1749 return len(yo._list)
1755 if yo._desc:
1756 return "DbfList(%s - %d records)" % (yo._desc, len(yo._list))
1757 else:
1758 return "DbfList(%d records)" % len(yo._list)
1760 if isinstance(other, DbfList):
1761 result = DbfList()
1762 result._list[:] = other._list[:]
1763 result._set = other._set.copy()
1764 lost = set()
1765 for item in yo._list:
1766 if item in result._list:
1767 result._set.remove(item)
1768 lost.add(item)
1769 result._list = [item for item in result._list if item not in lost]
1770 result._current = 0 if result else -1
1771 return result
1772 return NotImplemented
1774 if isinstance(other, DbfList):
1775 result = DbfList()
1776 result._list[:] = yo._list[:]
1777 result._set = yo._set.copy()
1778 lost = set()
1779 for item in other._list:
1780 if item in result._set:
1781 result._set.remove(item)
1782 lost.add(item)
1783 result._list = [item for item in result._list if item not in lost]
1784 result._current = 0 if result else -1
1785 return result
1786 return NotImplemented
1788 if item not in yo._set:
1789 yo._set.add(item)
1790 yo._list.append(item)
1792 if table is rec_no is None:
1793 table, rec_no = yo._list[yo._current]
1794 return table.get_record(rec_no)
1800 if yo._list:
1801 yo._current = len(yo._list) - 1
1802 return yo._get_record()
1803 raise DbfError("DbfList is empty")
1805 yo._list = []
1806 yo._set = set()
1807 yo._current = -1
1809 if yo._current < 0:
1810 raise Bof()
1811 elif yo._current == len(yo._list):
1812 raise Eof()
1813 return yo._get_record()
1814 - def extend(yo, new_records):
1815 if isinstance(new_records, DbfList):
1816 for item in new_records._list:
1817 yo._maybe_add(item)
1818 else:
1819 for record in new_records:
1820 yo.append(record)
1821 if yo._current == -1 and yo._list:
1822 yo._current = 0
1823 - def goto(yo, index_number):
1824 if yo._list:
1825 if 0 <= index_number <= len(yo._list):
1826 yo._current = index_number
1827 return yo._get_record()
1828 raise DbfError("index %d not in DbfList of %d records" % (index_number, len(yo._list)))
1829 raise DbfError("DbfList is empty")
1830 - def insert(yo, i, table, record):
1831 item = table, record.record_number
1832 if item not in yo._set:
1833 yo._set.add(item)
1834 yo._list.insert(i, item)
1836 if yo._current < len(yo._list):
1837 yo._current += 1
1838 if yo._current < len(yo._list):
1839 return yo._get_record()
1840 raise Eof()
1841 - def pop(yo, index=None):
1842 if index is None:
1843 table, recno = yo._list.pop()
1844 yo._set.remove((table, recno))
1845 else:
1846 table, recno = yo._list.pop(index)
1847 yo._set.remove((table, recno))
1848 return _get_record(table, recno)
1850 if yo._current >= 0:
1851 yo._current -= 1
1852 if yo._current > -1:
1853 return yo._get_record()
1854 raise Bof()
1862 if yo._list:
1863 yo._current = 0
1864 return yo._get_record()
1865 raise DbfError("DbfList is empty")
1866 - def sort(yo, key=None, reverse=None):
1877 csv.register_dialect('dbf', DbfCsv)
1878
1879 -def _nop(value):
1880 "returns parameter unchanged"
1881 return value
1883 "ensures each tuple is the same length, using filler[-missing] for the gaps"
1884 final = []
1885 for t in tuples:
1886 if len(t) < length:
1887 final.append( tuple([item for item in t] + filler[len(t)-length:]) )
1888 else:
1889 final.append(t)
1890 return tuple(final)
1892 if cp not in code_pages:
1893 for code_page in sorted(code_pages.keys()):
1894 sd, ld = code_pages[code_page]
1895 if cp == sd or cp == ld:
1896 if sd is None:
1897 raise DbfError("Unsupported codepage: %s" % ld)
1898 cp = code_page
1899 break
1900 else:
1901 raise DbfError("Unsupported codepage: %s" % cp)
1902 sd, ld = code_pages[cp]
1903 return cp, sd, ld
1904 -def ascii(new_setting=None):
1911 -def codepage(cp=None):
1912 "get/set default codepage for any new tables"
1913 global default_codepage
1914 cp, sd, ld = _codepage_lookup(cp or default_codepage)
1915 default_codepage = sd
1916 return "%s (LDID: 0x%02x - %s)" % (sd, ord(cp), ld)
1924 version = 'dBase IV w/memos (non-functional)'
1925 _versionabbv = 'db4'
1926 _fieldtypes = {
1927 'C' : {'Type':'Character', 'Retrieve':io.retrieveCharacter, 'Update':io.updateCharacter, 'Blank':str, 'Init':io.addCharacter},
1928 'Y' : {'Type':'Currency', 'Retrieve':io.retrieveCurrency, 'Update':io.updateCurrency, 'Blank':Decimal(), 'Init':io.addVfpCurrency},
1929 'B' : {'Type':'Double', 'Retrieve':io.retrieveDouble, 'Update':io.updateDouble, 'Blank':float, 'Init':io.addVfpDouble},
1930 'F' : {'Type':'Float', 'Retrieve':io.retrieveNumeric, 'Update':io.updateNumeric, 'Blank':float, 'Init':io.addVfpNumeric},
1931 'N' : {'Type':'Numeric', 'Retrieve':io.retrieveNumeric, 'Update':io.updateNumeric, 'Blank':int, 'Init':io.addVfpNumeric},
1932 'I' : {'Type':'Integer', 'Retrieve':io.retrieveInteger, 'Update':io.updateInteger, 'Blank':int, 'Init':io.addVfpInteger},
1933 'L' : {'Type':'Logical', 'Retrieve':io.retrieveLogical, 'Update':io.updateLogical, 'Blank':bool, 'Init':io.addLogical},
1934 'D' : {'Type':'Date', 'Retrieve':io.retrieveDate, 'Update':io.updateDate, 'Blank':Date.today, 'Init':io.addDate},
1935 'T' : {'Type':'DateTime', 'Retrieve':io.retrieveVfpDateTime, 'Update':io.updateVfpDateTime, 'Blank':DateTime.now, 'Init':io.addVfpDateTime},
1936 'M' : {'Type':'Memo', 'Retrieve':io.retrieveMemo, 'Update':io.updateMemo, 'Blank':str, 'Init':io.addMemo},
1937 'G' : {'Type':'General', 'Retrieve':io.retrieveMemo, 'Update':io.updateMemo, 'Blank':str, 'Init':io.addMemo},
1938 'P' : {'Type':'Picture', 'Retrieve':io.retrieveMemo, 'Update':io.updateMemo, 'Blank':str, 'Init':io.addMemo},
1939 '0' : {'Type':'_NullFlags', 'Retrieve':io.unsupportedType, 'Update':io.unsupportedType, 'Blank':int, 'Init':None} }
1940 _memoext = '.dbt'
1941 _memotypes = ('G','M','P')
1942 _memoClass = _VfpMemo
1943 _yesMemoMask = '\x8b'
1944 _noMemoMask = '\x04'
1945 _fixed_fields = ('B','D','G','I','L','M','P','T','Y')
1946 _variable_fields = ('C','F','N')
1947 _character_fields = ('C','M')
1948 _decimal_fields = ('F','N')
1949 _numeric_fields = ('B','F','I','N','Y')
1950 _supported_tables = ('\x04', '\x8b')
1951 _dbfTableHeader = ['\x00'] * 32
1952 _dbfTableHeader[0] = '\x8b'
1953 _dbfTableHeader[10] = '\x01'
1954 _dbfTableHeader[29] = '\x03'
1955 _dbfTableHeader = ''.join(_dbfTableHeader)
1956 _dbfTableHeaderExtra = ''
1957 _use_deleted = True
1959 "dBase III specific"
1960 if yo._meta.header.version == '\x8b':
1961 try:
1962 yo._meta.memo = yo._memoClass(yo._meta)
1963 except:
1964 yo._meta.dfd.close()
1965 yo._meta.dfd = None
1966 raise
1967 if not yo._meta.ignorememos:
1968 for field in yo._meta.fields:
1969 if yo._meta[field]['type'] in yo._memotypes:
1970 if yo._meta.header.version != '\x8b':
1971 yo._meta.dfd.close()
1972 yo._meta.dfd = None
1973 raise DbfError("Table structure corrupt: memo fields exist, header declares no memos")
1974 elif not os.path.exists(yo._meta.memoname):
1975 yo._meta.dfd.close()
1976 yo._meta.dfd = None
1977 raise DbfError("Table structure corrupt: memo fields exist without memo file")
1978 break
1979