mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 23:10:17 +00:00
a21f8187f8
Squashing two squashes. Adding DBF support Adding the DBFpy python package The DBFpy package provides basic dbf support for python. Still need to write an interface format file for tablib. Adding DBF format and imports in compat.py Adding DBF format to formats.__init__ DBF format had not been committed to formats.__init__, so I’m adding it. Adding a dbf import test Adding at test to check whether a DBF can be created properly and compare it against a regression binary string. Adding an import_set test (and renaming another) Adding an import_set test that conforms with the other import_set tests for other formats. I’m also adding an export_set function. Fixing system site-packages import Importing dbfpy from tab lib.packages instead of system site packages. Fixing a syntaxError in dbfpy/dbfnew.py Fixing an issue with ending field definitions DBFPY, when writing a DBF, terminates the field definitions with a newline character. When importing a DBF from a stream, however, DBFPY was looking only for the \x0D character rather than the newline. Now we consider both cases. Adding a test for dbf format detection Adding DBF filetype detection tests Adding tests for YAML, JSON, TSV, CSV using the DBF detection function. Handling extra exceptions in dbf detection Adding exception handling for struct.error, an exception that DBFPY raises when trying to unpack a TSV table. Since it’s not a DBF file, we know it’s not a DBF and return False. Fixing an issue with the DBF set exporting test The DBF set export test needed a bit enabled (probably the writeable bit?) before the test would match the regression output. Updating dbf interface Updating the int/float class/type checking in the dbf format file. This allows for python2 and python3 compatibility. Tweaking dbfpy to work with python3 Altering a couple of imports. Updating dbf tests for binary data compatibility Making regression strings binary and improving debug messages for dbf assertion errors. Improving file handling for python 2 and 3 Updating DBF file handling for both python 2 and 3 in the _dbf interface. Adding a (seemingly) functional dbfpy for python3 I’ve made dbfpy python3 compatible! Tests appear to pass. A significant change was made to the format detection test whereby I made the input string a binary (bytes) string. If the string is not a bytes string by the time we try to detect the format, we try to decode the string as utf-8 (which admittedly might not be the safest thing to do) and try to decode anyways. Updating imports for tablib dbf interface Now importing python2 or python3 versions as appropriate. Updating dbf package references in compat.py Cleaning up debugging print statements Updating stream handling in dbf interface Factoring the open() call out of the py3 conditional and removing the temp file before returning the stream value. Adding dbfpy3 init.py I had apparently missed the dbfpy3 init file when committing dbfpy3. Adding dbfpy and dbfpy3 to setup.py's package list Switching test order of formats Putting dbf format testing ahead of TSV. In some of my tests with numeric DBF files, I encountered an issue where the ASCII horizontal tab character (0x09) would appear in a numeric DBF. Because of the order of tabular format imports, though, format detection would recognize it as a TSV and not as a DBF. Adding my name to AUTHORS. Adding a DBF property to tab lib core Documentation includes examples on how to explicitly load a DBF straight from a file and how to load a DBF from a binary string. Also, how to write the binary data to a file. Adding DBF format notes to README Adding exclamation point to DBF section title Matching formatting of XLS section Updating setup.py to match current dev state Setup.py had been updated since I forked the tablib repo, so I’m updating setup.py to match its current structure while still maintaining DBF compatibility. Fixed callable collumn test the test was sending a list instead of a function CORE CONTRIBUTORS 🍰 @iurisilvio v0.10.0 WHEELS 3.3, 3.4 makefile for WHEELS v0.10.0 history ALL Separate py2 and py3 packages to avoid installation errors. Fix #151 Running travis and tox with python 3.4. Adding DBF support Adding the DBFpy python package The DBFpy package provides basic dbf support for python. Still need to write an interface format file for tablib. Adding DBF format and imports in compat.py Adding DBF format to formats.__init__ DBF format had not been committed to formats.__init__, so I’m adding it. Adding a dbf import test Adding at test to check whether a DBF can be created properly and compare it against a regression binary string. Adding an import_set test (and renaming another) Adding an import_set test that conforms with the other import_set tests for other formats. I’m also adding an export_set function. Fixing system site-packages import Importing dbfpy from tab lib.packages instead of system site packages. Fixing a syntaxError in dbfpy/dbfnew.py Fixing an issue with ending field definitions DBFPY, when writing a DBF, terminates the field definitions with a newline character. When importing a DBF from a stream, however, DBFPY was looking only for the \x0D character rather than the newline. Now we consider both cases. Adding a test for dbf format detection Adding DBF filetype detection tests Adding tests for YAML, JSON, TSV, CSV using the DBF detection function. Handling extra exceptions in dbf detection Adding exception handling for struct.error, an exception that DBFPY raises when trying to unpack a TSV table. Since it’s not a DBF file, we know it’s not a DBF and return False. Fixing an issue with the DBF set exporting test The DBF set export test needed a bit enabled (probably the writeable bit?) before the test would match the regression output. Updating dbf interface Updating the int/float class/type checking in the dbf format file. This allows for python2 and python3 compatibility. Tweaking dbfpy to work with python3 Altering a couple of imports. Updating dbf tests for binary data compatibility Making regression strings binary and improving debug messages for dbf assertion errors. Improving file handling for python 2 and 3 Updating DBF file handling for both python 2 and 3 in the _dbf interface. Adding a (seemingly) functional dbfpy for python3 I’ve made dbfpy python3 compatible! Tests appear to pass. A significant change was made to the format detection test whereby I made the input string a binary (bytes) string. If the string is not a bytes string by the time we try to detect the format, we try to decode the string as utf-8 (which admittedly might not be the safest thing to do) and try to decode anyways. Updating imports for tablib dbf interface Now importing python2 or python3 versions as appropriate. Updating dbf package references in compat.py Cleaning up debugging print statements Updating stream handling in dbf interface Factoring the open() call out of the py3 conditional and removing the temp file before returning the stream value. Adding dbfpy3 init.py I had apparently missed the dbfpy3 init file when committing dbfpy3. Adding dbfpy and dbfpy3 to setup.py's package list Switching test order of formats Putting dbf format testing ahead of TSV. In some of my tests with numeric DBF files, I encountered an issue where the ASCII horizontal tab character (0x09) would appear in a numeric DBF. Because of the order of tabular format imports, though, format detection would recognize it as a TSV and not as a DBF. Adding my name to AUTHORS. Adding a DBF property to tab lib core Documentation includes examples on how to explicitly load a DBF straight from a file and how to load a DBF from a binary string. Also, how to write the binary data to a file. Adding DBF format notes to README Adding exclamation point to DBF section title Matching formatting of XLS section Updating setup.py to match current dev state Setup.py had been updated since I forked the tablib repo, so I’m updating setup.py to match its current structure while still maintaining DBF compatibility. Fixed callable collumn test the test was sending a list instead of a function CORE CONTRIBUTORS 🍰 @iurisilvio v0.10.0 WHEELS 3.3, 3.4 makefile for WHEELS v0.10.0 history ALL Separate py2 and py3 packages to avoid installation errors. Fix #151 Running travis and tox with python 3.4.
294 lines
9.0 KiB
Python
294 lines
9.0 KiB
Python
#! /usr/bin/env python
|
|
"""DBF accessing helpers.
|
|
|
|
FIXME: more documentation needed
|
|
|
|
Examples:
|
|
|
|
Create new table, setup structure, add records:
|
|
|
|
dbf = Dbf(filename, new=True)
|
|
dbf.addField(
|
|
("NAME", "C", 15),
|
|
("SURNAME", "C", 25),
|
|
("INITIALS", "C", 10),
|
|
("BIRTHDATE", "D"),
|
|
)
|
|
for (n, s, i, b) in (
|
|
("John", "Miller", "YC", (1980, 10, 11)),
|
|
("Andy", "Larkin", "", (1980, 4, 11)),
|
|
):
|
|
rec = dbf.newRecord()
|
|
rec["NAME"] = n
|
|
rec["SURNAME"] = s
|
|
rec["INITIALS"] = i
|
|
rec["BIRTHDATE"] = b
|
|
rec.store()
|
|
dbf.close()
|
|
|
|
Open existed dbf, read some data:
|
|
|
|
dbf = Dbf(filename, True)
|
|
for rec in dbf:
|
|
for fldName in dbf.fieldNames:
|
|
print '%s:\t %s (%s)' % (fldName, rec[fldName],
|
|
type(rec[fldName]))
|
|
print
|
|
dbf.close()
|
|
|
|
"""
|
|
"""History (most recent first):
|
|
11-feb-2007 [als] export INVALID_VALUE;
|
|
Dbf: added .ignoreErrors, .INVALID_VALUE
|
|
04-jul-2006 [als] added export declaration
|
|
20-dec-2005 [yc] removed fromStream and newDbf methods:
|
|
use argument of __init__ call must be used instead;
|
|
added class fields pointing to the header and
|
|
record classes.
|
|
17-dec-2005 [yc] split to several modules; reimplemented
|
|
13-dec-2005 [yc] adapted to the changes of the `strutil` module.
|
|
13-sep-2002 [als] support FoxPro Timestamp datatype
|
|
15-nov-1999 [jjk] documentation updates, add demo
|
|
24-aug-1998 [jjk] add some encodeValue methods (not tested), other tweaks
|
|
08-jun-1998 [jjk] fix problems, add more features
|
|
20-feb-1998 [jjk] fix problems, add more features
|
|
19-feb-1998 [jjk] add create/write capabilities
|
|
18-feb-1998 [jjk] from dbfload.py
|
|
"""
|
|
|
|
__version__ = "$Revision: 1.7 $"[11:-2]
|
|
__date__ = "$Date: 2007/02/11 09:23:13 $"[7:-2]
|
|
__author__ = "Jeff Kunce <kuncej@mail.conservation.state.mo.us>"
|
|
|
|
__all__ = ["Dbf"]
|
|
|
|
from . import header
|
|
from . import record
|
|
from .utils import INVALID_VALUE
|
|
|
|
class Dbf(object):
|
|
"""DBF accessor.
|
|
|
|
FIXME:
|
|
docs and examples needed (dont' forget to tell
|
|
about problems adding new fields on the fly)
|
|
|
|
Implementation notes:
|
|
``_new`` field is used to indicate whether this is
|
|
a new data table. `addField` could be used only for
|
|
the new tables! If at least one record was appended
|
|
to the table it's structure couldn't be changed.
|
|
|
|
"""
|
|
|
|
__slots__ = ("name", "header", "stream",
|
|
"_changed", "_new", "_ignore_errors")
|
|
|
|
HeaderClass = header.DbfHeader
|
|
RecordClass = record.DbfRecord
|
|
INVALID_VALUE = INVALID_VALUE
|
|
|
|
## initialization and creation helpers
|
|
|
|
def __init__(self, f, readOnly=False, new=False, ignoreErrors=False):
|
|
"""Initialize instance.
|
|
|
|
Arguments:
|
|
f:
|
|
Filename or file-like object.
|
|
new:
|
|
True if new data table must be created. Assume
|
|
data table exists if this argument is False.
|
|
readOnly:
|
|
if ``f`` argument is a string file will
|
|
be opend in read-only mode; in other cases
|
|
this argument is ignored. This argument is ignored
|
|
even if ``new`` argument is True.
|
|
headerObj:
|
|
`header.DbfHeader` instance or None. If this argument
|
|
is None, new empty header will be used with the
|
|
all fields set by default.
|
|
ignoreErrors:
|
|
if set, failing field value conversion will return
|
|
``INVALID_VALUE`` instead of raising conversion error.
|
|
|
|
"""
|
|
if isinstance(f, str):
|
|
# a filename
|
|
self.name = f
|
|
if new:
|
|
# new table (table file must be
|
|
# created or opened and truncated)
|
|
self.stream = open(f, "w+b")
|
|
else:
|
|
# tabe file must exist
|
|
self.stream = open(f, ("r+b", "rb")[bool(readOnly)])
|
|
else:
|
|
# a stream
|
|
self.name = getattr(f, "name", "")
|
|
self.stream = f
|
|
if new:
|
|
# if this is a new table, header will be empty
|
|
self.header = self.HeaderClass()
|
|
else:
|
|
# or instantiated using stream
|
|
self.header = self.HeaderClass.fromStream(self.stream)
|
|
self.ignoreErrors = ignoreErrors
|
|
self._new = bool(new)
|
|
self._changed = False
|
|
|
|
## properties
|
|
|
|
closed = property(lambda self: self.stream.closed)
|
|
recordCount = property(lambda self: self.header.recordCount)
|
|
fieldNames = property(
|
|
lambda self: [_fld.name for _fld in self.header.fields])
|
|
fieldDefs = property(lambda self: self.header.fields)
|
|
changed = property(lambda self: self._changed or self.header.changed)
|
|
|
|
def ignoreErrors(self, value):
|
|
"""Update `ignoreErrors` flag on the header object and self"""
|
|
self.header.ignoreErrors = self._ignore_errors = bool(value)
|
|
ignoreErrors = property(
|
|
lambda self: self._ignore_errors,
|
|
ignoreErrors,
|
|
doc="""Error processing mode for DBF field value conversion
|
|
|
|
if set, failing field value conversion will return
|
|
``INVALID_VALUE`` instead of raising conversion error.
|
|
|
|
""")
|
|
|
|
## protected methods
|
|
|
|
def _fixIndex(self, index):
|
|
"""Return fixed index.
|
|
|
|
This method fails if index isn't a numeric object
|
|
(long or int). Or index isn't in a valid range
|
|
(less or equal to the number of records in the db).
|
|
|
|
If ``index`` is a negative number, it will be
|
|
treated as a negative indexes for list objects.
|
|
|
|
Return:
|
|
Return value is numeric object maning valid index.
|
|
|
|
"""
|
|
if not isinstance(index, int):
|
|
raise TypeError("Index must be a numeric object")
|
|
if index < 0:
|
|
# index from the right side
|
|
# fix it to the left-side index
|
|
index += len(self) + 1
|
|
if index >= len(self):
|
|
raise IndexError("Record index out of range")
|
|
return index
|
|
|
|
## iterface methods
|
|
|
|
def close(self):
|
|
self.flush()
|
|
self.stream.close()
|
|
|
|
def flush(self):
|
|
"""Flush data to the associated stream."""
|
|
if self.changed:
|
|
self.header.setCurrentDate()
|
|
self.header.write(self.stream)
|
|
self.stream.flush()
|
|
self._changed = False
|
|
|
|
def indexOfFieldName(self, name):
|
|
"""Index of field named ``name``."""
|
|
# FIXME: move this to header class
|
|
names = [f.name for f in self.header.fields]
|
|
return names.index(name.upper())
|
|
|
|
def newRecord(self):
|
|
"""Return new record, which belong to this table."""
|
|
return self.RecordClass(self)
|
|
|
|
def append(self, record):
|
|
"""Append ``record`` to the database."""
|
|
record.index = self.header.recordCount
|
|
record._write()
|
|
self.header.recordCount += 1
|
|
self._changed = True
|
|
self._new = False
|
|
|
|
def addField(self, *defs):
|
|
"""Add field definitions.
|
|
|
|
For more information see `header.DbfHeader.addField`.
|
|
|
|
"""
|
|
if self._new:
|
|
self.header.addField(*defs)
|
|
else:
|
|
raise TypeError("At least one record was added, "
|
|
"structure can't be changed")
|
|
|
|
## 'magic' methods (representation and sequence interface)
|
|
|
|
def __repr__(self):
|
|
return "Dbf stream '%s'\n" % self.stream + repr(self.header)
|
|
|
|
def __len__(self):
|
|
"""Return number of records."""
|
|
return self.recordCount
|
|
|
|
def __getitem__(self, index):
|
|
"""Return `DbfRecord` instance."""
|
|
return self.RecordClass.fromStream(self, self._fixIndex(index))
|
|
|
|
def __setitem__(self, index, record):
|
|
"""Write `DbfRecord` instance to the stream."""
|
|
record.index = self._fixIndex(index)
|
|
record._write()
|
|
self._changed = True
|
|
self._new = False
|
|
|
|
#def __del__(self):
|
|
# """Flush stream upon deletion of the object."""
|
|
# self.flush()
|
|
|
|
|
|
def demoRead(filename):
|
|
_dbf = Dbf(filename, True)
|
|
for _rec in _dbf:
|
|
print()
|
|
print(repr(_rec))
|
|
_dbf.close()
|
|
|
|
def demoCreate(filename):
|
|
_dbf = Dbf(filename, new=True)
|
|
_dbf.addField(
|
|
("NAME", "C", 15),
|
|
("SURNAME", "C", 25),
|
|
("INITIALS", "C", 10),
|
|
("BIRTHDATE", "D"),
|
|
)
|
|
for (_n, _s, _i, _b) in (
|
|
("John", "Miller", "YC", (1981, 1, 2)),
|
|
("Andy", "Larkin", "AL", (1982, 3, 4)),
|
|
("Bill", "Clinth", "", (1983, 5, 6)),
|
|
("Bobb", "McNail", "", (1984, 7, 8)),
|
|
):
|
|
_rec = _dbf.newRecord()
|
|
_rec["NAME"] = _n
|
|
_rec["SURNAME"] = _s
|
|
_rec["INITIALS"] = _i
|
|
_rec["BIRTHDATE"] = _b
|
|
_rec.store()
|
|
print(repr(_dbf))
|
|
_dbf.close()
|
|
|
|
if (__name__=='__main__'):
|
|
import sys
|
|
_name = len(sys.argv) > 1 and sys.argv[1] or "county.dbf"
|
|
demoCreate(_name)
|
|
demoRead(_name)
|
|
|
|
# vim: set et sw=4 sts=4 :
|