Exemplo n.º 1
0
    def __init__(self, filepath, **kwargs):
        """DBF2 constructor

        Args:
            filepath (str): The dbf file path or file like object.
            kwargs: Keyword arguments that are passed to the DBF reader.

        Kwargs:
            load (bool): Load all records into memory (default: false).
            encoding (bool): Character encoding (default: None, parsed from
                the `language_driver`).

            sanitize (bool): Convert field names to lower case
                (default: False).
            ignorecase (bool): Treat file name as case insensitive
                (default: true).
            ignore_missing_memofile (bool): Suppress `MissingMemoFile`
                exceptions (default: False).
        """
        try:
            kwargs["recfactory"] = dict
            return super(DBF2, self).__init__(filepath, **kwargs)
        except (AttributeError, TypeError):
            filename = filepath.name

        defaults = {
            "ignorecase": True,
            "parserclass": FieldParser,
            "recfactory": dict
        }

        [kwargs.setdefault(k1, v1) for k1, v1 in defaults.items()]
        [self.__setattr__(k2, v2) for k2, v2 in kwargs.items()]
        self.name = p.splitext(p.basename(filename))[0].lower()
        self.filename = ifind(filename) if self.ignorecase else filename

        if not self.filename:
            raise DBFNotFound("could not find file {!r}".format(filename))

        self.fields = []
        self.field_names = []
        self._read_headers(filepath, self.ignore_missing_memofile)
        self._check_headers()

        try:
            year = expand_year(self.header.year)
        except ValueError:
            self.date = None
        else:
            self.date = date(year, self.header.month, self.header.day)

        self.memofilename = self._get_memofilename()

        if self.load:
            self.load()
Exemplo n.º 2
0
    def __init__(self, filename, encoding=None, ignorecase=True,
                 lowernames=False, parserclass=FieldParser,
                 recfactory=collections.OrderedDict,
                 load=False, raw=False, ignore_missing_memofile=False,
                 char_decode_errors='strict'):

        if isinstance(filename, str):
            DBF.__init__(self, filename, encoding=encoding, ignorecase=ignorecase,
                         lowernames=lowernames, parserclass=parserclass,
                         recfactory=recfactory, load=load,
                         raw=raw, ignore_missing_memofile=ignore_missing_memofile,
                         char_decode_errors=char_decode_errors)
        else:
            self.encoding = encoding
            self.ignorecase = ignorecase
            self.lowernames = lowernames
            self.parserclass = parserclass
            self.raw = raw
            self.ignore_missing_memofile = ignore_missing_memofile
            self.char_decode_errors = char_decode_errors

            if recfactory is None:
                self.recfactory = lambda items: items
            else:
                self.recfactory = recfactory

            self.name = None
            self.filename = None
            self.content = filename

            self._records = None
            self._deleted = None

            # Filled in by self._read_headers()
            self.memofilename = None
            self.header = None
            self.fields = []       # namedtuples
            self.field_names = []  # strings

            obj = BytesIO(filename)
            self._read_header(obj)
            self._read_field_headers(obj)
            self._check_headers()

            try:
                self.date = datetime.date(expand_year(self.header.year),
                                          self.header.month, self.header.day)
            except ValueError:  # pragma: no cover
                # Invalid date or '\x00\x00\x00'.
                self.date = None

            self.memofilename = self._get_memofilename()

            if load:
                self.load()
Exemplo n.º 3
0
    def __init__(self, filepath, **kwargs):
        """DBF2 constructor

        Args:
            filepath (str): The dbf file path or file like object.
            kwargs: Keyword arguments that are passed to the DBF reader.

        Kwargs:
            load (bool): Load all records into memory (default: false).
            encoding (bool): Character encoding (default: None, parsed from
                the `language_driver`).

            sanitize (bool): Convert field names to lower case
                (default: False).
            ignorecase (bool): Treat file name as case insensitive
                (default: true).
            ignore_missing_memofile (bool): Suppress `MissingMemoFile`
                exceptions (default: False).
        """
        try:
            kwargs['recfactory'] = dict
            return super(DBF2, self).__init__(filepath, **kwargs)
        except AttributeError:
            filename = filepath.name

        defaults = {
            'ignorecase': True, 'parserclass': FieldParser, 'recfactory': dict}

        [kwargs.setdefault(k, v) for k, v in defaults.items()]
        [self.__setattr__(k, v) for k, v in kwargs.items()]
        self.name = p.splitext(p.basename(filename))[0].lower()
        self.filename = ifind(filename) if self.ignorecase else filename

        if not self.filename:
            raise DBFNotFound('could not find file {!r}'.format(filename))

        self.fields = []
        self.field_names = []
        self._read_headers(filepath, self.ignore_missing_memofile)
        self._check_headers()

        try:
            year = expand_year(self.header.year)
        except ValueError:
            self.date = None
        else:
            self.date = date(year, self.header.month, self.header.day)

        self.memofilename = self._get_memofilename()

        if self.load:
            self.load()