Skip to content

Commit

Permalink
Merge pull request #284 from datosgobar/283-explicit-error-on-missing…
Browse files Browse the repository at this point in the history
…-xlsx-field

283 explicit error on missing xlsx field
  • Loading branch information
lrromero committed Oct 4, 2019
2 parents c284204 + d14dba0 commit 879da46
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pydatajson/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def read_catalog(catalog, default_values=None, catalog_format=None,
catalog_dict = read_xlsx_catalog(catalog,
verify=verify,
timeout=timeout)
except openpyxl_exceptions + \
(ValueError, AssertionError, IOError, BadZipfile) as e:
except openpyxl_exceptions + (KeyError, ValueError, AssertionError,
IOError, BadZipfile) as e:
raise ce.NonParseableCatalog(catalog, str(e))
elif catalog_format == "json":
try:
Expand Down Expand Up @@ -459,15 +459,15 @@ def read_local_xlsx_catalog(xlsx_path, logger=None):
'{}' (fila #{} de la hoja "Field"). Este campo no figurara en
el data.json de salida.""".format(
unidecode(field["dataset_title"]),
unidecode(field["field_title"]), idx + 2))
unidecode(field.get("field_title", "sin title")), idx + 2))

elif distribution_index is None:
logger.warning(
"""No se encontro la distribucion '{}' especificada para el
campo'{}' (fila #{} de la hoja "Field"). Este campo no figurara
en el data.json de salida.""".format(
unidecode(field["distribution_title"]),
unidecode(field["field_title"]), idx + 2))
unidecode(field.get("field_title", )), idx + 2))

else:
dataset = catalog["catalog_dataset"][dataset_index]
Expand Down Expand Up @@ -539,10 +539,11 @@ def read_suffixless_catalog(catalog):
try:
catalog_dict = read_xlsx_catalog(catalog)
return catalog_dict
except openpyxl_exceptions + (ValueError, AssertionError,
IOError, BadZipfile):
raise ce.NonParseableCatalog(
catalog, 'No es posible discernir el formato del catalogo')
except openpyxl_exceptions + (KeyError, ValueError, AssertionError,
IOError, BadZipfile) as e:
msg = 'No es posible discernir el formato del catalogo: {}'\
.format(str(e))
raise ce.NonParseableCatalog(catalog, msg)


def read_table(path):
Expand Down
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/test_readers_and_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ def test_xlsx_write_missing_optional_fields_and_themes(self):
self.assertTrue('theme' not in written_dataset)
self.assertTrue('field' not in written_distribution)

@nose.tools.raises(NonParseableCatalog)
def test_missing_mandatory_field_on_xlsx_catalog_raises(self):
sample = self.get_sample(
'catalogo-justicia-missing-distribution-identifier.xlsx')
pydatajson.readers.read_catalog(sample)


if __name__ == '__main__':
nose.run(defaultTest=__name__)

0 comments on commit 879da46

Please sign in to comment.