Beispiel #1
0
    def _addField(self, *defs):
        """Internal variant of the `addField` method.

        This method doesn't set `self.changed` field to True.

        Return value is a length of the appended records.
        Note: this method doesn't modify ``recordLength`` and
        ``headerLength`` fields. Use `addField` instead of this
        method if you don't exactly know what you're doing.

        """
        # insure we have dbf.DbfFieldDef instances first (instantiation
        # from the tuple could raise an error, in such a case I don't
        # wanna add any of the definitions -- all will be ignored)
        _defs = []
        _recordLength = 0
        for _def in defs:
            if isinstance(_def, fields.DbfFieldDef):
                _obj = _def
            else:
                (_name, _type, _len, _dec) = (tuple(_def) + (None,) * 4)[:4]
                _cls = fields.lookupFor(_type)
                _obj = _cls(_name, _len, _dec,
                    ignoreErrors=self._ignore_errors)
            _recordLength += _obj.length
            _defs.append(_obj)
        # and now extend field definitions and
        # update record length
        self.fields += _defs
        return _recordLength
Beispiel #2
0
 def fromStream(cls, stream):
     """Return header object from the stream."""
     stream.seek(0)
     _data = stream.read(32)
     (_cnt, _hdrLen, _recLen) = struct.unpack("<I2H", _data[4:12])
     #reserved = _data[12:32]
     _year = ord(_data[1])
     if _year < 80:
         # dBase II started at 1980.  It is quite unlikely
         # that actual last update date is before that year.
         _year += 2000
     else:
         _year += 1900
     ## create header object
     _obj = cls(None, _hdrLen, _recLen, _cnt, ord(_data[0]),
         (_year, ord(_data[2]), ord(_data[3])))
     ## append field definitions
     # position 0 is for the deletion flag
     _pos = 1
     _data = stream.read(1)
     while _data[0] != "\x0D":
         _data += stream.read(31)
         _fld = fields.lookupFor(_data[11]).fromString(_data, _pos)
         _obj._addField(_fld)
         _pos = _fld.end
         _data = stream.read(1)
     return _obj
Beispiel #3
0
    def fromStream(cls, stream, encoding=None, enableFieldNameDups=False):
        """Return header object from the stream."""
        stream.seek(0)
        _data = stream.read(32)
        (_cnt, _hdrLen, _recLen) = struct.unpack("<I2H", _data[4:12])
        #reserved = _data[12:32]
        ## create header object
        _obj = cls(None,
                   _hdrLen,
                   _recLen,
                   _cnt,
                   ord(_data[0]),
                   (1900 + ord(_data[1]), ord(_data[2]), ord(_data[3])),
                   encoding=encoding,
                   enableFieldNameDups=enableFieldNameDups)

        ## append field definitions
        # position 0 is for the deletion flag
        _pos = 1
        _data = stream.read(1)
        while _data[0] != "\x0D":
            _data += stream.read(31)
            _fld = fields.lookupFor(_data[11]).fromString(_data,
                                                          _pos,
                                                          encoding=encoding)
            _obj._addField(_fld)
            _pos = _fld.end
            _data = stream.read(1)
        return _obj
Beispiel #4
0
 def fromStream(cls, stream):
     """Return header object from the stream."""
     stream.seek(0)
     _data = stream.read(32)
     (_cnt, _hdrLen, _recLen) = struct.unpack("<I2H", _data[4:12])
     # reserved = _data[12:32]
     ## create header object
     _obj = cls(None, _hdrLen, _recLen, _cnt, ord(_data[0]), (1900 + ord(_data[1]), ord(_data[2]), ord(_data[3])))
     ## append field definitions
     # position 0 is for the deletion flag
     _pos = 1
     _data = stream.read(1)
     while _data[0] != "\x0D":
         _data += stream.read(31)
         _fld = fields.lookupFor(_data[11]).fromString(_data, _pos)
         _obj._addField(_fld)
         _pos = _fld.end
         _data = stream.read(1)
     return _obj
Beispiel #5
0
    def _addField(self, *defs):
        """Internal variant of the `addField` method.

        This method doesn't set `self.changed` field to True.

        Return value is a length of the appended records.
        Note: this method doesn't modify ``recordLength`` and
        ``headerLength`` fields. Use `addField` instead of this
        method if you don't exactly know what you're doing.

        """
        _recordLength = 0
        for _def in defs:
            if isinstance(_def, fields.DbfFieldDef):
                _obj = _def
            else:
                (_name, _type, _len, _dec,
                 _encoding) = (tuple(_def) + (None, ) * 5)[:5]
                _cls = fields.lookupFor(_type)
                _obj = _cls(_name, _len, _dec, _encoding)
            _recordLength += _obj.length
            if _obj.encoding is None:
                _obj.encoding = self.encoding
            if isinstance(_obj, fields.DbfMemoFieldDef):
                self.signature |= 0x80
            if _obj.name in self.mapNameToIndex:
                if self.enableFieldNameDups:
                    index = self.mapNameToIndex[_obj.name]
                    if isinstance(index, int):
                        index = [index]
                    index.append(len(self.fields))
                else:
                    raise Exception, 'DBF field name "%s" duplicated' % _obj.name
            else:
                index = len(self.fields)
            self.mapNameToIndex[_obj.name] = index
            self.fields.append(_obj)
        return _recordLength