# Path: pyxform/utils.py
# SEP = "_"
# TAG_START_CHAR = r"[a-zA-Z:_]"
# TAG_CHAR = r"[a-zA-Z:_0-9\-.]"
# XFORM_TAG_REGEXP = "%(start)s%(char)s*" % {"start": TAG_START_CHAR, "char": TAG_CHAR}
# INVALID_XFORM_TAG_REGEXP = r"[^a-zA-Z:_][^a-zA-Z:_0-9\-.]*"
# LAST_SAVED_INSTANCE_NAME = "__last-saved"
# BRACKETED_TAG_REGEX = re.compile(r"\${(last-saved#)?(.*?)}")
# LAST_SAVED_REGEX = re.compile(r"\${last-saved#(.*?)}")
# 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",
# }
# class DetachableElement(Element):
# class PatchedText(Text):
#     def __init__(self, *args, **kwargs):
#     def writexml(self, writer, indent="", addindent="", newl=""):
# def is_valid_xml_tag(tag):
# def node(*args, **kwargs):
# def get_pyobj_from_json(str_or_path):
# def flatten(li):
# def sheet_to_csv(workbook_path, csv_path, sheet_name):
# def xls_sheet_to_csv(workbook_path, csv_path, sheet_name):
# def xlsx_sheet_to_csv(workbook_path, csv_path, sheet_name):
# def has_external_choices(json_struct):
# def get_languages_with_bad_tags(languages):
# def default_is_dynamic(element_default, element_type=None):
# def has_dynamic_label(choice_list, multi_language):
# def levenshtein_distance(a: str, b: str) -> int:
# 
# Path: pyxform/xls2json.py
# class SurveyReader(SpreadsheetReader):
#     """
#     SurveyReader is a wrapper for the parse_file_to_json function.
#     It allows us to use the old interface where a SpreadsheetReader
#     based object is created then a to_json_dict function is called on it.
#     """
# 
#     def __init__(self, path_or_file, default_name=None):
#         if isinstance(path_or_file, str):
#             self._file_object = None
#             path = path_or_file
#         else:
#             self._file_object = path_or_file
#             path = path_or_file.name
# 
#         self._warnings = []
#         self._dict = parse_file_to_json(
#             path,
#             default_name=default_name,
#             warnings=self._warnings,
#             file_object=self._file_object,
#         )
#         self._path = path
# 
#     def print_warning_log(self, warn_out_file):
#         # Open file to print warning log to.
#         warn_out = open(warn_out_file, "w")
#         warn_out.write("\n".join(self._warnings))


import glob
import os
from pyxform import utils
from pyxform.xls2json import SurveyReader
# -*- coding: utf-8 -*-
"""
The pyxform file utility functions.
"""



def _section_name(path_or_file_name):
    directory, filename = os.path.split(path_or_file_name)
    section_name, extension = os.path.splitext(filename)
    return section_name


def load_file_to_dict(path):
    """
    Takes a file path and loads it into a nested json dict
    following the format in json_form_schema.json
    The file may be a xls file or json file.
    If it is xls it is converted using xls2json.
    """
    if path.endswith(".json"):
        name = _section_name(path)
        return name, utils.get_pyobj_from_json(path)
    else:
        name = _section_name(path)

# Path: pyxform/utils.py
# SEP = "_"
# TAG_START_CHAR = r"[a-zA-Z:_]"
# TAG_CHAR = r"[a-zA-Z:_0-9\-.]"
# XFORM_TAG_REGEXP = "%(start)s%(char)s*" % {"start": TAG_START_CHAR, "char": TAG_CHAR}
# INVALID_XFORM_TAG_REGEXP = r"[^a-zA-Z:_][^a-zA-Z:_0-9\-.]*"
# LAST_SAVED_INSTANCE_NAME = "__last-saved"
# BRACKETED_TAG_REGEX = re.compile(r"\${(last-saved#)?(.*?)}")
# LAST_SAVED_REGEX = re.compile(r"\${last-saved#(.*?)}")
# 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",
# }
# class DetachableElement(Element):
# class PatchedText(Text):
#     def __init__(self, *args, **kwargs):
#     def writexml(self, writer, indent="", addindent="", newl=""):
# def is_valid_xml_tag(tag):
# def node(*args, **kwargs):
# def get_pyobj_from_json(str_or_path):
# def flatten(li):
# def sheet_to_csv(workbook_path, csv_path, sheet_name):
# def xls_sheet_to_csv(workbook_path, csv_path, sheet_name):
# def xlsx_sheet_to_csv(workbook_path, csv_path, sheet_name):
# def has_external_choices(json_struct):
# def get_languages_with_bad_tags(languages):
# def default_is_dynamic(element_default, element_type=None):
# def has_dynamic_label(choice_list, multi_language):
# def levenshtein_distance(a: str, b: str) -> int:
# 
# Path: pyxform/xls2json.py
# class SurveyReader(SpreadsheetReader):
#     """
#     SurveyReader is a wrapper for the parse_file_to_json function.
#     It allows us to use the old interface where a SpreadsheetReader
#     based object is created then a to_json_dict function is called on it.
#     """
# 
#     def __init__(self, path_or_file, default_name=None):
#         if isinstance(path_or_file, str):
#             self._file_object = None
#             path = path_or_file
#         else:
#             self._file_object = path_or_file
#             path = path_or_file.name
# 
#         self._warnings = []
#         self._dict = parse_file_to_json(
#             path,
#             default_name=default_name,
#             warnings=self._warnings,
#             file_object=self._file_object,
#         )
#         self._path = path
# 
#     def print_warning_log(self, warn_out_file):
#         # Open file to print warning log to.
#         warn_out = open(warn_out_file, "w")
#         warn_out.write("\n".join(self._warnings))


# Path: pyxform/file_utils.py
import glob
import os
from pyxform import utils
from pyxform.xls2json import SurveyReader

# -*- coding: utf-8 -*-
"""
The pyxform file utility functions.
"""



def _section_name(path_or_file_name):
    directory, filename = os.path.split(path_or_file_name)
    section_name, extension = os.path.splitext(filename)
    return section_name


def load_file_to_dict(path):
    """
    Takes a file path and loads it into a nested json dict
    following the format in json_form_schema.json
    The file may be a xls file or json file.
    If it is xls it is converted using xls2json.
    """
    if path.endswith(".json"):
        name = _section_name(path)
        return name, utils.get_pyobj_from_json(path)
    else:
        name = _section_name(path)

        excel_reader = SurveyReader(path, name)