Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

283 explicit error on missing xlsx field #284

Merged
merged 2 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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__)