Пример #1
0
    def _generate_pulldata_instances(element):
        if "calculate" in element["bind"]:
            calculate = element["bind"]["calculate"]
            if calculate.startswith("pulldata("):
                pieces = (calculate.split('"')
                          if '"' in calculate else calculate.split("'"))
                if len(pieces) > 1 and pieces[1]:
                    file_id = pieces[1]
                    uri = "jr://file-csv/{}.csv".format(file_id)
                    return InstanceInfo(
                        type="pulldata",
                        context="[type: {t}, name: {n}]".format(
                            t=element["parent"]["type"],
                            n=element["parent"]["name"]),
                        name=file_id,
                        src=uri,
                        instance=node(
                            "instance",
                            Survey._get_dummy_instance(),
                            id=file_id,
                            src=uri,
                        ),
                    )

        return None
Пример #2
0
    def _generate_static_instances(list_name, choice_list):
        """
        Generates <instance> elements for static data
        (e.g. choices for select type questions)

        Note that per commit message 0578242 and in xls2json.py R539, an
        instance is only output for select items defined in the choices sheet
        when the item has a choice_filter, and it is that way for backwards
        compatibility.
        """
        instance_element_list = []
        for idx, choice in enumerate(choice_list):
            choice_element_list = []
            # Add a unique id to the choice element in case there is itext
            # it references
            itext_id = '-'.join(['static_instance', list_name, str(idx)])
            choice_element_list.append(node("itextId", itext_id))

            for choicePropertyName, choicePropertyValue in choice.items():
                if isinstance(choicePropertyValue, basestring) \
                        and choicePropertyName != 'label':
                    choice_element_list.append(
                        node(choicePropertyName, unicode(choicePropertyValue)))
            instance_element_list.append(node("item", *choice_element_list))
        return InstanceInfo(type=u"choice",
                            context=u"survey",
                            name=list_name,
                            src=None,
                            instance=node("instance",
                                          node("root", *instance_element_list),
                                          id=list_name))
Пример #3
0
    def _get_last_saved_instance():
        name = "__last-saved"  # double underscore used to minimize risk of name conflicts
        uri = "jr://instance/last-saved"

        return InstanceInfo(
            type="instance",
            context=None,
            name=name,
            src=uri,
            instance=node("instance", id=name, src=uri),
        )
Пример #4
0
        def get_instance_info(element, file_id):
            uri = "jr://file-csv/{}.csv".format(file_id)

            return InstanceInfo(
                type=u"pulldata",
                context="[type: {t}, name: {n}]".format(
                    t=element[u"parent"][u"type"],
                    n=element[u"parent"][u"name"]),
                name=file_id,
                src=uri,
                instance=node("instance", id=file_id, src=uri),
            )
Пример #5
0
    def _generate_external_instances(element):
        if isinstance(element, ExternalInstance):
            name = element["name"]
            src = "jr://file/{}.xml".format(name)
            return InstanceInfo(
                type="external",
                context="[type: {t}, name: {n}]".format(
                    t=element["parent"]["type"], n=element["parent"]["name"]),
                name=name,
                src=src,
                instance=node("instance", id=name, src=src),
            )

        return None
Пример #6
0
    def _generate_static_instances(list_name, choice_list):
        """
        Generates <instance> elements for static data
        (e.g. choices for select type questions)

        Note that per commit message 0578242 and in xls2json.py R539, an
        instance is only output for select items defined in the choices sheet
        when the item has a choice_filter, and it is that way for backwards
        compatibility.
        """
        instance_element_list = []
        multi_language = isinstance(choice_list[0].get("label"), dict)
        has_media = bool(choice_list[0].get("media"))

        for idx, choice in enumerate(choice_list):
            choice_element_list = []
            # Add a unique id to the choice element in case there is itext
            # it references
            if (
                multi_language
                or has_media
                or has_dynamic_label(choice_list, multi_language)
            ):
                itext_id = "-".join([list_name, str(idx)])
                choice_element_list.append(node("itextId", itext_id))

            for name, value in sorted(choice.items()):
                if isinstance(value, basestring) and name != "label":
                    choice_element_list.append(node(name, unicode(value)))
                if (
                    not multi_language
                    and not has_media
                    and not has_dynamic_label(choice_list, multi_language)
                    and isinstance(value, basestring)
                    and name == "label"
                ):
                    choice_element_list.append(node(name, unicode(value)))

            instance_element_list.append(node("item", *choice_element_list))

        return InstanceInfo(
            type="choice",
            context="survey",
            name=list_name,
            src=None,
            instance=node(
                "instance", node("root", *instance_element_list), id=list_name
            ),
        )
Пример #7
0
 def _generate_from_file_instances(element):
     itemset = element.get('itemset')
     if itemset and (itemset.endswith('.csv') or itemset.endswith('.xml')):
         file_id, ext = os.path.splitext(itemset)
         uri = 'jr://%s/%s' % ('file' if ext == '.xml' else "file-%s" %
                               ext[1:], itemset)
         return InstanceInfo(type=u"file",
                             context="[type: {t}, name: {n}]".format(
                                 t=element[u"parent"][u"type"],
                                 n=element[u"parent"][u"name"]),
                             name=file_id,
                             src=uri,
                             instance=node("instance",
                                           Survey._get_dummy_instance(),
                                           id=file_id,
                                           src=uri))
Пример #8
0
    def _generate_external_instances(element):
        if isinstance(element, ExternalInstance):
            name = element["name"]
            extension = element["type"].split("-")[0]
            prefix = "file-csv" if extension == "csv" else "file"
            src = "jr://{}/{}.{}".format(prefix, name, extension)
            return InstanceInfo(
                type="external",
                context="[type: {t}, name: {n}]".format(
                    t=element["parent"]["type"], n=element["parent"]["name"]),
                name=name,
                src=src,
                instance=node("instance", id=name, src=src),
            )

        return None
Пример #9
0
    def _generate_from_file_instances(element):
        itemset = element.get("itemset")
        if itemset and (itemset.endswith(".csv") or itemset.endswith(".xml")):
            file_id, ext = os.path.splitext(itemset)
            uri = "jr://%s/%s" % (
                "file" if ext == ".xml" else "file-%s" % ext[1:],
                itemset,
            )
            return InstanceInfo(
                type="file",
                context="[type: {t}, name: {n}]".format(
                    t=element["parent"]["type"], n=element["parent"]["name"]),
                name=file_id,
                src=uri,
                instance=node("instance", id=file_id, src=uri),
            )

        return None
Пример #10
0
    def _generate_pulldata_instances(element):
        def get_pulldata_functions(element):
            """
            Returns a list of different pulldata(... function strings if
            pulldata function is defined at least once for any of:
            calculate, constraint, readonly, required, relevant

            :param: element (pyxform.survey.Survey):
            """
            functions_present = []
            for formula_name in constants.EXTERNAL_INSTANCES:
                if unicode(element["bind"].get(formula_name)).startswith(
                        "pulldata("):
                    functions_present.append(element["bind"][formula_name])
            return functions_present

        formulas = get_pulldata_functions(element)
        if len(formulas) > 0:
            formula_instances = []
            for formula in formulas:
                pieces = formula.split(
                    '"') if '"' in formula else formula.split("'")
                if len(pieces) > 1 and pieces[1]:
                    file_id = pieces[1]
                    uri = "jr://file-csv/{}.csv".format(file_id)
                    formula_instances.append(
                        InstanceInfo(
                            type=u"pulldata",
                            context="[type: {t}, name: {n}]".format(
                                t=element[u"parent"][u"type"],
                                n=element[u"parent"][u"name"],
                            ),
                            name=file_id,
                            src=uri,
                            instance=node(
                                "instance",
                                Survey._get_dummy_instance(),
                                id=file_id,
                                src=uri,
                            ),
                        ))
            return formula_instances
        return None
Пример #11
0
    def _generate_from_file_instances(element):
        itemset = element.get("itemset")
        file_id, ext = os.path.splitext(itemset)
        if itemset and ext in EXTERNAL_INSTANCE_EXTENSIONS:
            uri = "jr://%s/%s" % (
                "file" if ext == ".xml" or ext == ".geojson" else "file-%s" %
                ext[1:],
                itemset,
            )
            return InstanceInfo(
                type="file",
                context="[type: {t}, name: {n}]".format(
                    t=element["parent"]["type"], n=element["parent"]["name"]),
                name=file_id,
                src=uri,
                instance=node("instance", id=file_id, src=uri),
            )

        return None
Пример #12
0
 def _generate_pulldata_instances(element):
     if 'calculate' in element['bind']:
         calculate = element['bind']['calculate']
         if calculate.startswith('pulldata('):
             pieces = calculate.split('"') \
                 if '"' in calculate else calculate.split("'")
             if len(pieces) > 1 and pieces[1]:
                 file_id = pieces[1]
                 uri = "jr://file-csv/{}.csv".format(file_id)
                 return InstanceInfo(
                     type=u"pulldata",
                     context="[type: {t}, name: {n}]".format(
                         t=element[u"parent"][u"type"],
                         n=element[u"parent"][u"name"]),
                     name=file_id,
                     src=uri,
                     instance=node("instance",
                                   Survey._get_dummy_instance(),
                                   id=file_id,
                                   src=uri))