XLSForm / pyxform

A Python package to create XForms for ODK Collect.
BSD 2-Clause "Simplified" License
77 stars 134 forks source link

Choice items without labels crash in pyxform v1.5 instead of leading to Validate error #532

Closed qlands closed 3 years ago

qlands commented 3 years ago

pyxform v1.5.0 FROM PyPI , Python 3.8

Calling this code fails:

from pyxform import xls2xform
xls2xform.xls2xform_convert("/home/cquiros/B.xlsx", "/home/cquiros/prueba.xml")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cquiros/data/projects2017/personal/software/env_formshare/lib/python3.8/site-packages/pyxform/xls2xform.py", line 44, in xls2xform_convert
    survey.print_xform_to_file(
  File "/home/cquiros/data/projects2017/personal/software/env_formshare/lib/python3.8/site-packages/pyxform/survey.py", line 1098, in print_xform_to_file
    raise error
  File "/home/cquiros/data/projects2017/personal/software/env_formshare/lib/python3.8/site-packages/pyxform/survey.py", line 1092, in print_xform_to_file
    file_obj.write(self._to_pretty_xml())
  File "/home/cquiros/data/projects2017/personal/software/env_formshare/lib/python3.8/site-packages/pyxform/survey.py", line 881, in _to_pretty_xml
    xml_with_linebreaks = self.xml().toprettyxml(indent="  ")
  File "/home/cquiros/data/projects2017/personal/software/env_formshare/lib/python3.8/site-packages/pyxform/survey.py", line 262, in xml
    node("h:head", node("h:title", self.title), self.xml_model()),
  File "/home/cquiros/data/projects2017/personal/software/env_formshare/lib/python3.8/site-packages/pyxform/survey.py", line 562, in xml_model
    self._setup_translations()
  File "/home/cquiros/data/projects2017/personal/software/env_formshare/lib/python3.8/site-packages/pyxform/survey.py", line 689, in _setup_translations
    and not has_dynamic_label(choice_list, multi_language)
  File "/home/cquiros/data/projects2017/personal/software/env_formshare/lib/python3.8/site-packages/pyxform/utils.py", line 280, in has_dynamic_label
    if re.search(BRACKETED_TAG_REGEX, choice_list[i].get("label")) is not None:
  File "/usr/lib/python3.8/re.py", line 201, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

The value for choice_list at crach point is: [{'name': '1'}]

You can access the XLSX form here

The same call does not fail with pyxform 1.4.0

lognaturel commented 3 years ago

In your form, it looks like the following line is a mistake that can be removed:

HHID 1

If this list were used, you would have gotten a Validate failure in v1.4.0. Because it happens to be unused, it's not part of the XML that is validated so it used to pass.

Really, I think pyxform should require choice labels and I'll file a separate issue for that.

qlands commented 3 years ago

Yep. That happens because the label is empty.

Instead of a plain crash, there should be an Exception with a message. Otherwise, a user might not even know what is wrong.