def runTest(self):
        for filename in ["select_one_external.xlsx"]:
            path_to_excel_file = os.path.join(DIR, "example_xls", filename)
            #Get the xform output path:
            root_filename, ext = os.path.splitext(filename)
            output_path = os.path.join(DIR, "test_output",
                                       root_filename + ".xml")
            expected_output_path = os.path.join(DIR, "test_expected_output",
                                                root_filename + ".xml")
            output_csv = os.path.join(DIR, "test_output",
                                      root_filename + ".csv")
            #Do the conversion:
            json_survey = pyxform.xls2json.parse_file_to_json(
                path_to_excel_file)

            self.assertTrue(
                sheet_to_csv(path_to_excel_file, output_csv,
                             "external_choices"))
            self.assertFalse(
                sheet_to_csv(path_to_excel_file, output_csv,
                             "non-existant sheet"))

            survey = pyxform.create_survey_element_from_dict(json_survey)

            survey.print_xform_to_file(output_path)

            #Compare with the expected output:
            with codecs.open(expected_output_path, 'rb', encoding="utf-8") as\
                    expected_file:
                with codecs.open(output_path, 'rb', encoding="utf-8") as \
                        actual_file:
                    self.assertXFormEqual(expected_file.read(),
                                          actual_file.read())
    def runTest(self):
        for filename in ["select_one_external.xlsx"]:
            path_to_excel_file = os.path.join(DIR, "example_xls", filename)
            #Get the xform output path:
            root_filename, ext = os.path.splitext(filename)
            output_path = os.path.join(
                DIR, "test_output", root_filename + ".xml")
            expected_output_path = os.path.join(
                DIR, "test_expected_output", root_filename + ".xml")
            output_csv = os.path.join(
                DIR, "test_output", root_filename + ".csv")
            #Do the conversion:
            json_survey = pyxform.xls2json.parse_file_to_json(
                path_to_excel_file)

            self.assertTrue(sheet_to_csv(path_to_excel_file, output_csv, "external_choices"))
            self.assertFalse(sheet_to_csv(path_to_excel_file, output_csv, "non-existant sheet"))

            survey = pyxform.create_survey_element_from_dict(json_survey)

            survey.print_xform_to_file(output_path)

            #Compare with the expected output:
            with codecs.open(expected_output_path, 'rb', encoding="utf-8") as\
                    expected_file:
                with codecs.open(output_path, 'rb', encoding="utf-8") as \
                        actual_file:
                    self.assertMultiLineEqual(
                        expected_file.read(), actual_file.read())
Exemple #3
0
    def runTest(self):
        for filename in ["select_one_external.xlsx"]:
            self.get_file_path(filename)
            expected_output_path = os.path.join(test_expected_output.PATH,
                                                self.root_filename + ".xml")

            output_csv = os.path.join(test_output.PATH,
                                      self.root_filename + ".csv")
            # Do the conversion:
            json_survey = pyxform.xls2json.parse_file_to_json(
                self.path_to_excel_file, default_name="select_one_external")

            self.assertTrue(
                sheet_to_csv(self.path_to_excel_file, output_csv,
                             "external_choices"))
            self.assertFalse(
                sheet_to_csv(self.path_to_excel_file, output_csv,
                             "non-existant sheet"))

            survey = pyxform.create_survey_element_from_dict(json_survey)

            survey.print_xform_to_file(self.output_path)

            # Compare with the expected output:
            with codecs.open(expected_output_path, "rb",
                             encoding="utf-8") as expected_file:
                with codecs.open(self.output_path, "rb",
                                 encoding="utf-8") as actual_file:
                    self.assertXFormEqual(expected_file.read(),
                                          actual_file.read())
    def runTest(self):
        for filename in ["select_one_external.xlsx"]:
            self.get_file_path(filename)
            expected_output_path = os.path.join(
                DIR, "test_expected_output", self.root_filename + ".xml"
            )

            output_csv = os.path.join(DIR, "test_output", self.root_filename + ".csv")
            # Do the conversion:
            json_survey = pyxform.xls2json.parse_file_to_json(self.path_to_excel_file)

            self.assertTrue(
                sheet_to_csv(self.path_to_excel_file, output_csv, "external_choices")
            )
            self.assertFalse(
                sheet_to_csv(self.path_to_excel_file, output_csv, "non-existant sheet")
            )

            survey = pyxform.create_survey_element_from_dict(json_survey)

            survey.print_xform_to_file(self.output_path)

            # Compare with the expected output:
            with codecs.open(
                expected_output_path, "rb", encoding="utf-8"
            ) as expected_file:
                with codecs.open(
                    self.output_path, "rb", encoding="utf-8"
                ) as actual_file:
                    self.assertXFormEqual(expected_file.read(), actual_file.read())
Exemple #5
0
def xls2xform_convert(xlsform_path,
                      xform_path,
                      validate=True,
                      pretty_print=True,
                      enketo=False):
    warnings = []

    json_survey = xls2json.parse_file_to_json(xlsform_path, warnings=warnings)
    survey = builder.create_survey_element_from_dict(json_survey)
    # Setting validate to false will cause the form not to be processed by
    # ODK Validate.
    # This may be desirable since ODK Validate requires launching a subprocess
    # that runs some java code.
    survey.print_xform_to_file(
        xform_path,
        validate=validate,
        pretty_print=pretty_print,
        warnings=warnings,
        enketo=enketo,
    )
    output_dir = os.path.split(xform_path)[0]
    if has_external_choices(json_survey):
        itemsets_csv = os.path.join(output_dir, "itemsets.csv")
        choices_exported = sheet_to_csv(xlsform_path, itemsets_csv,
                                        "external_choices")
        if not choices_exported:
            warnings.append("Could not export itemsets.csv, perhaps the "
                            "external choices sheet is missing.")
        else:
            logger.info("External choices csv is located at: %s", itemsets_csv)
    return warnings
Exemple #6
0
def _xls2itemset(xls_path, items_path):
    warnings = []
    if not sheet_to_csv(xls_path, items_path, "external_choices"):
        return warnings.append(
            "Could not export itemsets.csv, perhaps the external choices"
            " sheet is missing.")
    return warnings
    def test_csv_data(self):
        """
        Test that the data on the external choices sheet is cleaned and written
        appropriately to the csv file.
        """

        filename = "select_one_external.xlsx"
        self.get_file_path(filename)

        output_csv = os.path.join(DIR, "test_output",
                                  self.root_filename + ".csv")

        self.assertTrue(
            sheet_to_csv(self.path_to_excel_file, output_csv,
                         "external_choices"))

        # Compare with the expected output
        with codecs.open(output_csv, "rb", encoding="utf-8") as expected_file:
            rows = expected_file.read()
            # Evaluate the last line obtained from the csv file
            self.assertEqual(
                rows.splitlines()[-1],
                '"cities","puyallup","Puyallup","washington","pierce"',
            )
            # Evaluate the first line obtained from the csv file
            self.assertEqual(rows.splitlines()[0],
                             '"list_name","name","label","state","county"')
Exemple #8
0
def xls2xform_convert(
    xlsform_path, xform_path, validate=True, pretty_print=True, enketo=False
):
    warnings = []

    json_survey = xls2json.parse_file_to_json(xlsform_path, warnings=warnings)
    survey = builder.create_survey_element_from_dict(json_survey)
    # Setting validate to false will cause the form not to be processed by
    # ODK Validate.
    # This may be desirable since ODK Validate requires launching a subprocess
    # that runs some java code.
    survey.print_xform_to_file(
        xform_path,
        validate=validate,
        pretty_print=pretty_print,
        warnings=warnings,
        enketo=enketo,
    )
    output_dir = os.path.split(xform_path)[0]
    if has_external_choices(json_survey):
        itemsets_csv = os.path.join(output_dir, "itemsets.csv")
        choices_exported = sheet_to_csv(xlsform_path, itemsets_csv, "external_choices")
        if not choices_exported:
            warnings.append(
                "Could not export itemsets.csv, perhaps the "
                "external choices sheet is missing."
            )
        else:
            logger.info("External choices csv is located at: %s", itemsets_csv)
    return warnings
Exemple #9
0
def index(request):
    if request.method == 'POST':  # If the form has been submitted...
        form = UploadFileForm(request.POST,
                              request.FILES)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass

            error = None
            warnings = None

            filename, ext = os.path.splitext(request.FILES['file'].name)

            if not (os.access(SERVER_TMP_DIR, os.F_OK)):
                os.mkdir(SERVER_TMP_DIR)

            #Make a randomly generated directory to prevent name collisions
            temp_dir = tempfile.mkdtemp(dir=SERVER_TMP_DIR)
            xml_path = os.path.join(temp_dir, filename + '.xml')
            itemsets_url = None

            relpath = os.path.relpath(xml_path, SERVER_TMP_DIR)

            #Init the output xml file.
            fo = open(xml_path, "wb+")
            fo.close()

            try:
                #TODO: use the file object directly
                xls_path = handle_uploaded_file(request.FILES['file'],
                                                temp_dir)
                warnings = []
                json_survey = xls2json.parse_file_to_json(xls_path,
                                                          warnings=warnings)
                survey = pyxform.create_survey_element_from_dict(json_survey)
                survey.print_xform_to_file(xml_path, warnings=warnings)

                if has_external_choices(json_survey):
                    # Create a csv for the external choices
                    itemsets_csv = os.path.join(
                        os.path.split(xls_path)[0], "itemsets.csv")
                    relpath_itemsets_csv = os.path.relpath(
                        itemsets_csv, SERVER_TMP_DIR)
                    choices_exported = sheet_to_csv(xls_path, itemsets_csv,
                                                    "external_choices")
                    if not choices_exported:
                        warnings += [
                            "Could not export itemsets.csv, "
                            "perhaps the external choices sheet is missing."
                        ]
                    else:
                        itemsets_url = request.build_absolute_uri(
                            './downloads/' + relpath_itemsets_csv)
            except Exception as e:
                error = 'Error: ' + str(e)

            return render_to_response(
                'upload.html', {
                    'form':
                    UploadFileForm(),
                    'xml_path':
                    request.build_absolute_uri('./downloads/' + relpath),
                    'xml_url':
                    request.build_absolute_uri('./downloads/' + relpath),
                    'itemsets_url':
                    itemsets_url,
                    'success':
                    not error,
                    'error':
                    error,
                    'warnings':
                    warnings,
                    'result':
                    True,
                })
    else:
        form = UploadFileForm()  # An unbound form

    return render(request, 'upload.html', context={
        'form': form,
    })
Exemple #10
0
def index(request):
    if request.method == "POST":  # If the form has been submitted...
        form = UploadFileForm(request.POST, request.FILES)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass

            error = None
            warnings = None

            filename, ext = os.path.splitext(request.FILES["file"].name)

            if not (os.access(SERVER_TMP_DIR, os.F_OK)):
                os.mkdir(SERVER_TMP_DIR)

            # Make a randomly generated directory to prevent name collisions
            temp_dir = tempfile.mkdtemp(dir=SERVER_TMP_DIR)
            xml_path = os.path.join(temp_dir, filename + ".xml")
            itemsets_url = None

            relpath = os.path.relpath(xml_path, SERVER_TMP_DIR)

            # Init the output xml file.
            fo = open(xml_path, "wb+")
            fo.close()

            try:
                # TODO: use the file object directly
                xls_path = handle_uploaded_file(request.FILES["file"], temp_dir)
                warnings = []
                json_survey = xls2json.parse_file_to_json(xls_path, warnings=warnings)
                survey = pyxform.create_survey_element_from_dict(json_survey)
                survey.print_xform_to_file(xml_path, warnings=warnings)

                if has_external_choices(json_survey):
                    # Create a csv for the external choices
                    itemsets_csv = os.path.join(os.path.split(xls_path)[0], "itemsets.csv")
                    relpath_itemsets_csv = os.path.relpath(itemsets_csv, SERVER_TMP_DIR)
                    choices_exported = sheet_to_csv(xls_path, itemsets_csv, "external_choices")
                    if not choices_exported:
                        warnings += ["Could not export itemsets.csv, " "perhaps the external choices sheet is missing."]
                    else:
                        itemsets_url = request.build_absolute_uri("./downloads/" + relpath_itemsets_csv)
            except Exception as e:
                error = "Error: " + str(e)

            return render_to_response(
                "upload.html",
                {
                    "form": UploadFileForm(),
                    "xml_path": request.build_absolute_uri("./downloads/" + relpath),
                    "xml_url": request.build_absolute_uri("./downloads/" + relpath),
                    "itemsets_url": itemsets_url,
                    "success": not error,
                    "error": error,
                    "warnings": warnings,
                    "result": True,
                },
            )
    else:
        form = UploadFileForm()  # An unbound form

    return render_to_response("upload.html", {"form": form})
Exemple #11
0
        if len(argv) < 2:
            print __doc__
            print "Usage:"
            print argv[0] + " path_to_XLSForm"
        else:
            warnings = []
            name, ext = os.path.splitext(os.path.basename(argv[1]))
            output_dir = os.path.join(os.path.split(argv[0])[0], "output")
            out_file = os.path.join(output_dir, name + ".xml")
            if not os.path.exists(output_dir):
                os.makedirs(output_dir)
            json_survey = pyxform.xls2json.parse_file_to_json(argv[1], warnings=warnings)
            survey = pyxform.builder.create_survey_element_from_dict(json_survey)
            survey.print_xform_to_file(out_file, validate=False)
            if has_external_choices(json_survey):
                itemsets_csv = os.path.join(output_dir, "itemsets.csv")
                choices_exported = sheet_to_csv(argv[1], itemsets_csv, "external_choices")
                if not choices_exported:
                    print "Could not export itemsets.csv, perhaps the external choices sheet is missing."
                else:
                    print "external choices csv is located at:", itemsets_csv
            if len(warnings) > 0:
                print "Warnings:"
            for w in warnings:
                print w
            print "xform is located at:", out_file
            print "Conversion complete!"
    except Exception as e:
        print e
    raw_input("Press Enter to continue...")
Exemple #12
0
    def run(self):

        single_file = True
        start_message = 'Input file: ' + self.input_file_path + '\n\n'
        wx.PostEvent(self._notify_window, WorkEvent(start_message))

        try:

            warnings = []

            json_survey = xls2json.parse_file_to_json(self.input_file_path, warnings=warnings)
            survey = builder.create_survey_element_from_dict(json_survey)

            # need a temp file because print_xform_to_file automatically creates the file
            temp_dir = tempfile.mkdtemp()
            survey.print_xform_to_file(temp_dir + str(os.path.sep) + self.file_name + '.xml', validate=self.validate,
                                       warnings=warnings)

            if has_external_choices(json_survey):
                single_file = False
                choices_exported = sheet_to_csv(self.input_file_path, temp_dir + str(os.path.sep) + 'itemsets.csv',
                                                'external_choices')
                if not choices_exported:
                    warnings.append('Could not export itemsets.csv, perhaps the external choices sheet is missing.')

            if warnings:
                wx.PostEvent(self._notify_window, WorkEvent('ODK XLSForm Offline Warnings:\n'))
                # need to add whitespace to beginning to prevent truncation of forms with many warnings.
                for warning in warnings:
                    # warning = warning.replace('XForm Parse Warning: Warning: ', '').replace('    ', '')
                    wx.PostEvent(self._notify_window, WorkEvent(' ' + warning.strip() + '\n'))
                wx.PostEvent(self._notify_window, WorkEvent('\n'))

            if single_file:
                output_path_test = self.output_path + '.xml'
                output_path_template = self.output_path + ' ({0}).xml'
            else:
                output_path_test = self.output_path
                output_path_template = self.output_path + ' ({0})'

            if not self.overwrite and os.path.exists(output_path_test):
                # find an unused name
                i = 1
                while os.path.exists(output_path_template.format(i)):
                    i += 1
                output_path_test = output_path_template.format(i)

            if single_file:
                shutil.copyfile(temp_dir + str(os.path.sep) + self.file_name + '.xml', output_path_test)
            else:
                if self.overwrite:
                    shutil.rmtree(self.output_path, True)
                shutil.copytree(temp_dir, output_path_test)

            finish_message = 'Output file(s): ' + output_path_test + '\n\n'
            wx.PostEvent(self._notify_window, WorkEvent(finish_message))

        except Exception as e:

            exception_text = str(e)
            exception_text = exception_text.replace('>> Something broke the parser. See above for a hint.', '')
            exception_text = exception_text.replace('Result: Invalid', '')
            exception_text = exception_text.replace('\n\n', '\n')

            validate_regex = re.compile('ODK Validate')
            if not (validate_regex.match(exception_text)):
                wx.PostEvent(self._notify_window, WorkEvent('ODK XLSForm Offline Errors:\n'))
                wx.PostEvent(self._notify_window, WorkEvent(exception_text.strip() + '\n'))
                wx.PostEvent(self._notify_window, WorkEvent('\n'))
            else:
                wx.PostEvent(self._notify_window, WorkEvent(exception_text.strip() + '\n\n'))

        # special message to main thread
        wx.PostEvent(self._notify_window, WorkEvent(main.WORKER_FINISH))
        return
Exemple #13
0
            print argv[0] + ' path_to_XLSForm'
        else:
            warnings = []
            name, ext = os.path.splitext(os.path.basename(argv[1]))
            output_dir = os.path.join(os.path.split(argv[0])[0], 'output')
            out_file = os.path.join(output_dir, name + '.xml')
            if not os.path.exists(output_dir):
                os.makedirs(output_dir)
            json_survey = pyxform.xls2json.parse_file_to_json(
                argv[1], warnings=warnings)
            survey = pyxform.builder.create_survey_element_from_dict(
                json_survey)
            survey.print_xform_to_file(out_file, validate=False)
            if has_external_choices(json_survey):
                itemsets_csv = os.path.join(output_dir, "itemsets.csv")
                choices_exported = sheet_to_csv(argv[1], itemsets_csv,
                                                "external_choices")
                if not choices_exported:
                    print "Could not export itemsets.csv, perhaps the external choices sheet is missing."
                else:
                    print 'external choices csv is located at:', itemsets_csv
            if len(warnings) > 0:
                print "Warnings:"
            for w in warnings:
                print w
            print 'xform is located at:', out_file
            print 'Conversion complete!'
    except Exception as e:
        print e
    raw_input("Press Enter to continue...")