# Path: pyxform/builder.py
# def copy_json_dict(json_dict):
#     def __init__(self, **kwargs):
#     def set_sections(self, sections):
#     def create_survey_element_from_dict(self, d):
#     def _save_trigger_as_setvalue_and_remove_calculate(self, d):
#     def _create_question_from_dict(d, question_type_dictionary, add_none_option=False):
#     def _add_other_option_to_multiple_choice_question(d):
#     def _add_none_option_to_select_all_that_apply(d_copy):
#     def _get_question_class(question_type_str, question_type_dictionary):
#     def _create_specify_other_question_from_dict(d):
#     def _create_section_from_dict(self, d):
#     def _create_loop_from_dict(self, d):
#     def _name_and_label_substitutions(self, question_template, column_headers):
#     def create_survey_element_from_json(self, str_or_path):
# def create_survey_element_from_dict(d, sections=None):
# def create_survey_element_from_json(str_or_path):
# def create_survey_from_xls(path_or_file, default_name=None):
# def create_survey(
#     name_of_main_section=None,
#     sections=None,
#     main_section=None,
#     id_string=None,
#     title=None,
#     default_language=None,
# ):
# def create_survey_from_path(path, include_directory=False):
# class SurveyElementBuilder:
#     QUESTION_CLASSES = {
#         "": Question,
#         "action": Question,
#         "input": InputQuestion,
#         "odk:rank": MultipleChoiceQuestion,
#         "osm": OsmUploadQuestion,
#         "range": RangeQuestion,
#         "select": MultipleChoiceQuestion,
#         "select1": MultipleChoiceQuestion,
#         "trigger": TriggerQuestion,
#         "upload": UploadQuestion,
#     }
#     SECTION_CLASSES = {
#         "group": GroupedSection,
#         "repeat": RepeatingSection,
#         "survey": Survey,
#     }
# 
# Path: pyxform/utils.py
# NSMAP = {
#     "xmlns": "http://www.w3.org/2002/xforms",
#     "xmlns:h": "http://www.w3.org/1999/xhtml",
#     "xmlns:ev": "http://www.w3.org/2001/xml-events",
#     "xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
#     "xmlns:jr": "http://openrosa.org/javarosa",
#     "xmlns:orx": "http://openrosa.org/xforms",
#     "xmlns:odk": "http://www.opendatakit.org/xforms",
# }


import codecs
import copy
import json
import logging
import re
import xml.etree.ElementTree as ETree
from operator import itemgetter
from pyxform import builder
from pyxform.utils import NSMAP


# end of http://code.activestate.com/recipes/573463/ }}}


def _try_parse(root, parser=None):
    """
    Try to parse the root from a string or a file/file-like object.
    """
    root = root.encode("UTF-8")
    try:
        parsed_root = ETree.fromstring(root, parser)
    except ETree.ParseError:
        parsed_root = ETree.parse(root, parser=parser).getroot()
    return parsed_root


class XFormToDict:
    def __init__(self, root):
        if isinstance(root, str):
            parser = ETree.XMLParser(encoding="UTF-8")
            self._root = _try_parse(root, parser)
            self._dict = XmlDictObject(
                {self._root.tag: _convert_xml_to_dict_recurse(self._root, XmlDictObject)}
            )
        elif not isinstance(root, ETree.Element):
            raise TypeError("Expected ElementTree.Element or file path string")

    def get_dict(self):
        json_str = json.dumps(self._dict)

# Path: pyxform/builder.py
# def copy_json_dict(json_dict):
#     def __init__(self, **kwargs):
#     def set_sections(self, sections):
#     def create_survey_element_from_dict(self, d):
#     def _save_trigger_as_setvalue_and_remove_calculate(self, d):
#     def _create_question_from_dict(d, question_type_dictionary, add_none_option=False):
#     def _add_other_option_to_multiple_choice_question(d):
#     def _add_none_option_to_select_all_that_apply(d_copy):
#     def _get_question_class(question_type_str, question_type_dictionary):
#     def _create_specify_other_question_from_dict(d):
#     def _create_section_from_dict(self, d):
#     def _create_loop_from_dict(self, d):
#     def _name_and_label_substitutions(self, question_template, column_headers):
#     def create_survey_element_from_json(self, str_or_path):
# def create_survey_element_from_dict(d, sections=None):
# def create_survey_element_from_json(str_or_path):
# def create_survey_from_xls(path_or_file, default_name=None):
# def create_survey(
#     name_of_main_section=None,
#     sections=None,
#     main_section=None,
#     id_string=None,
#     title=None,
#     default_language=None,
# ):
# def create_survey_from_path(path, include_directory=False):
# class SurveyElementBuilder:
#     QUESTION_CLASSES = {
#         "": Question,
#         "action": Question,
#         "input": InputQuestion,
#         "odk:rank": MultipleChoiceQuestion,
#         "osm": OsmUploadQuestion,
#         "range": RangeQuestion,
#         "select": MultipleChoiceQuestion,
#         "select1": MultipleChoiceQuestion,
#         "trigger": TriggerQuestion,
#         "upload": UploadQuestion,
#     }
#     SECTION_CLASSES = {
#         "group": GroupedSection,
#         "repeat": RepeatingSection,
#         "survey": Survey,
#     }
# 
# Path: pyxform/utils.py
# NSMAP = {
#     "xmlns": "http://www.w3.org/2002/xforms",
#     "xmlns:h": "http://www.w3.org/1999/xhtml",
#     "xmlns:ev": "http://www.w3.org/2001/xml-events",
#     "xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
#     "xmlns:jr": "http://openrosa.org/javarosa",
#     "xmlns:orx": "http://openrosa.org/xforms",
#     "xmlns:odk": "http://www.opendatakit.org/xforms",
# }


# Path: pyxform/xform2json.py
import codecs
import copy
import json
import logging
import re
import xml.etree.ElementTree as ETree
from operator import itemgetter
from pyxform import builder
from pyxform.utils import NSMAP



# end of http://code.activestate.com/recipes/573463/ }}}


def _try_parse(root, parser=None):
    """
    Try to parse the root from a string or a file/file-like object.
    """
    root = root.encode("UTF-8")
    try:
        parsed_root = ETree.fromstring(root, parser)
    except ETree.ParseError:
        parsed_root = ETree.parse(root, parser=parser).getroot()
    return parsed_root


class XFormToDict:
    def __init__(self, root):
        if isinstance(root, str):
            parser = ETree.XMLParser(encoding="UTF-8")
            self._root = _try_parse(root, parser)
            self._dict = XmlDictObject(
                {self._root.tag: _convert_xml_to_dict_recurse(self._root, XmlDictObject)}
            )
        elif not isinstance(root, ETree.Element):
            raise TypeError("Expected ElementTree.Element or file path string")

    def get_dict(self):
        json_str = json.dumps(self._dict)

        for uri in NSMAP.values():