yulqen / bcompiler-engine

MOVED: A Python library to alleviate the pain of using Excel spreadsheets to collect data from your stakeholders.
https://git.sr.ht/~yulqen/bcompiler-engine
MIT License
4 stars 1 forks source link

KeyError raised when datamap find empty cell #7

Closed yulqen closed 5 years ago

yulqen commented 5 years ago

The KeyError raised here in engine.use_cases.parsing:

        try:
            return self._comb_with_datamap(
                filename, self._template_data, self._datamap_data, key, sheet
            )
        except KeyError as e:
            logger.critical(
                "Unable to process datamapline due to problem with sheet/cellref referred to by datamap"
            )
            raise KeyError(e.args[0])

is a result of an empty cell. Empty cells should need to be handled according to the user requirement - either a None or empty string.

yulqen commented 5 years ago

Removing raise from last line allows the parsing to pass and returns a None value at present. Need to sort out the logic behind the KeyErrors at the end of _comb_with_datamap():

    def _comb_with_datamap(self, filename, template_data, datamap_data, key, sheet):
        """Given a filename, a template_data json str, a datamap_data dict, key and sheet, returns
        the value in the spreadsheet at given datamap key.

        Throws KeyError if the datamap refers to a sheet/cellref combo in the target file that does not exist.
        """
        _data_lst = json.loads(datamap_data)
        if key not in [x["key"] for x in _data_lst]:
            raise KeyError('No key "{}" in datamap'.format(key))
        if sheet not in [x["sheet"] for x in _data_lst]:
            raise KeyError('No sheet "{}" in datamap'.format(sheet))
        _target_cellref = [
            x["cellref"] for x in _data_lst if x["key"] == key and x["sheet"] == sheet
        ]
        _cellref = _target_cellref[0]
        try:
            output = json.loads(template_data)[filename]["data"][sheet][_cellref][
                "value"
            ]
            return output
        except KeyError:
            logger.critical(
                "Unable to handle value {} or {} when processing {} with datamap.".format(
                    sheet, _cellref, filename
                )
            )
            raise KeyError(
                "Unable to handle value {} or {} when processing {} with datamap.".format(
                    sheet, _cellref, filename
                )
            )
yulqen commented 5 years ago

Empty cells return None for now, and fixed exception to properly flag a file being processed which contains a sheet that is not referenced in the datamap.