Beispiel #1
0
 def _run_odk_validate(xml):
     # On Windows, NamedTemporaryFile must be opened exclusively.
     # So it must be explicitly created, opened, closed, and removed
     tmp = tempfile.NamedTemporaryFile(suffix=".xml", delete=False)
     tmp.close()
     try:
         with codecs.open(tmp.name, mode="w", encoding="utf-8") as fp:
             fp.write(xml)
             fp.close()
         check_xform(tmp.name)
     finally:
         # Clean up the temporary file
         os.remove(tmp.name)
         assert not os.path.isfile(tmp.name)
 def _run_odk_validate(xml):
     # On Windows, NamedTemporaryFile must be opened exclusively.
     # So it must be explicitly created, opened, closed, and removed
     tmp = tempfile.NamedTemporaryFile(suffix='.xml', delete=False)
     tmp.close()
     try:
         with codecs.open(tmp.name, mode="w", encoding="utf-8") as fp:
             fp.write(xml)
             fp.close()
         check_xform(tmp.name)
     finally:
         # Clean up the temporary file
         os.remove(tmp.name)
         assert not os.path.isfile(tmp.name)
Beispiel #3
0
 def print_xform_to_file(self,
                         path=None,
                         validate=True,
                         pretty_print=True,
                         warnings=None,
                         enketo=False):
     """
     Print the xForm to a file and optionally validate it as well by
     throwing exceptions and adding warnings to the warnings array.
     """
     if warnings is None:
         warnings = []
     if not path:
         path = self._print_name + ".xml"
     try:
         with codecs.open(path, mode="w", encoding="utf-8") as fp:
             if pretty_print:
                 fp.write(self._to_pretty_xml())
             else:
                 fp.write(self._to_ugly_xml())
     except Exception as e:
         if os.path.exists(path):
             os.unlink(path)
         raise e
     if validate:
         warnings.extend(odk_validate.check_xform(path))
     if enketo:
         warnings.extend(enketo_validate.check_xform(path))
Beispiel #4
0
    def print_xform_to_file(self, path=None, validate=True, pretty_print=True,
                            warnings=None, enketo=False):
        """
        Print the xForm to a file and optionally validate it as well by
        throwing exceptions and adding warnings to the warnings array.
        """
        if warnings is None:
            warnings = []
        if not path:
            path = self._print_name + ".xml"
        try:
            with codecs.open(path, mode="w", encoding="utf-8") as fp:
                if pretty_print:
                    fp.write(self._to_pretty_xml())
                else:
                    fp.write(self._to_ugly_xml())
        except Exception as e:
            if os.path.exists(path):
                os.unlink(path)
            raise e
        if validate:
            warnings.extend(odk_validate.check_xform(path))
        if enketo:
            warnings.extend(enketo_validate.check_xform(path))

        # Warn if one or more translation is missing a valid IANA subtag
        if len(self._translations.keys()) > 0:
            bad_languages = get_languages_with_bad_tags(self._translations.keys())
            if len(bad_languages) > 0:
                warnings.append("\tThe following language declarations do not contain valid machine-readable codes: " +
                    ", ".join(bad_languages) + ". Learn more: http://xlsform.org/#language")
Beispiel #5
0
    def print_xform_to_file(
        self, path=None, validate=True, pretty_print=True, warnings=None, enketo=False
    ):
        """
        Print the xForm to a file and optionally validate it as well by
        throwing exceptions and adding warnings to the warnings array.
        """
        if warnings is None:
            warnings = []
        if not path:
            path = self._print_name + ".xml"
        try:
            with codecs.open(path, mode="w", encoding="utf-8") as file_obj:
                if pretty_print:
                    file_obj.write(self._to_pretty_xml())
                else:
                    file_obj.write(self._to_ugly_xml())
        except Exception as error:
            if os.path.exists(path):
                os.unlink(path)
            raise error
        if validate:
            warnings.extend(odk_validate.check_xform(path))
        if enketo:
            warnings.extend(enketo_validate.check_xform(path))

        # Warn if one or more translation is missing a valid IANA subtag
        translations = self._translations.keys()
        if translations:
            bad_languages = get_languages_with_bad_tags(translations)
            if bad_languages:
                warnings.append(
                    "\tThe following language declarations do not contain "
                    "valid machine-readable codes: "
                    + ", ".join(bad_languages)
                    + ". "
                    + "Learn more: http://xlsform.org#multiple-language-support"
                )
Beispiel #6
0
 def print_xform_to_file(self, path=None, validate=True, pretty_print=True,
                         warnings=None, enketo=False):
     """
     Print the xForm to a file and optionally validate it as well by
     throwing exceptions and adding warnings to the warnings array.
     """
     if warnings is None:
         warnings = []
     if not path:
         path = self._print_name + ".xml"
     try:
         with codecs.open(path, mode="w", encoding="utf-8") as fp:
             if pretty_print:
                 fp.write(self._to_pretty_xml())
             else:
                 fp.write(self._to_ugly_xml())
     except Exception as e:
         if os.path.exists(path):
             os.unlink(path)
         raise e
     if validate:
         warnings.extend(odk_validate.check_xform(path))
     if enketo:
         warnings.extend(enketo_validate.check_xform(path))