roskakori / cutplace

validate data stored in CSV, PRN, ODS or Excel files
http://cutplace.readthedocs.org/
GNU Lesser General Public License v3.0
18 stars 20 forks source link

Add validated writing #84

Open roskakori opened 9 years ago

roskakori commented 9 years ago

Goals: format for which there are already standard modules to write can be written in a validated way.

Rough design:

In rowio:

class AbtractWriter():
    def __init__(self, target):
        assert target is not None
        self.target = target
        self.is_opened = False
    def write_row():
        raise NotImplementedError
    def write_rows(self, rows):
        assert rows is not None
        for row in rows:
            self.write_row(row)
    def close():
        if self.is_opened:
           self.target.close()
    def __enter__(self):
       # for context manager
    def __exit__(self):
        # call close() from context manager

From AbstractWriter we can derive format specific writers, e.g.

class DelimitedWriter():
    def __init__(self, target, ...): ...
    def write_row(): ...
class ExcelWriter(AbstractWriter):
    def __init__(self, target): ...
    def write_row(): ...
    def close():
       # TODO: Actually write workbook.
       super().close()
class FixedWriter(AbstractWriter):
    def __init___(self, target, field_names_and_lengths, has_line_delimiter): ...
    def write_row(): ...

There will be not OdsWriter() because at the moment there does not seem to exist a stable and easy to use Python package for that.

These writers can be collected in a single factory function:

def writer(data_format, target):
    if data_format.format == data.FORMAT_DELIMITED:
        result = DelimitedWriter(...)
    elif ...
    elif data_format.format == data.FORMAT_ODS:
        raise NotImplementedError('cannot write ODS, see <url to github issue for OdsWriter>')
    else:
        assert False

In validio:

class Writer(object):
    def __init__(self, cid, target):
        self._cid = cid
        self._delegated_writer = rowio.writer(cid.data_format, target)
    def write_row(self, row):
        # TODO: validate row against self._cid
        self._delegated_writer.write_row(row)    
    def write_rows(self, rows):
        assert rows is not None
        for row in rows:
            self.write_row(row)
    close():
        # Perform checks at end.
        # For Excel, actually write workbook
    def __enter__(self):
       # for context manager
    def __exit__(self):
        # call close() from context manager