# Path: src/formpack/b64_attachment.py
# class B64Attachment(str):
#     """
#     * loaded from base64 string
#     * unloadable to file
#     """
# 
#     @classmethod
#     def _is_attachment(cls, data_uri):
#         return hasattr(data_uri, "startswith") and data_uri.startswith('data:')
# 
#     @classmethod
#     def _attachment_split(cls, data_uri):
#         """
#         accepts a data_uri,
#         returns a tuple (mediatype, extension, contents)
#         """
#         return re.match(DATA_URI_RE, data_uri).groups()
# 
#     @classmethod
#     def write_to_tempfile(cls, data_uri):
#         """
#         *placeholder method*
# 
#         this method will create a new TempFile (or cStringIO) with the
#         contents of the data_uri which can then be POST'ed to kobocat in
#         a mock submission.
#         """
#         (ftype, fext, fcont) = cls._attachment_split(data_uri)
#         fname = 'a%s.%s' % (randstr(10), fext)
#         return fname, '/tmp/path/to/%s' % fname
# 
# Path: src/formpack/utils/xform_tools.py
# def parse_xmljson_to_data(data, parent_tags=[], output=[]):
#     tag = data.get('tag')
#     new_parents = parent_tags + [tag]
#     if len(data.get('children', [])) > 0:
#         for child in data.get('children'):
#             parse_xmljson_to_data(child, new_parents, output)
#     else:
#         key = '/' + '/'.join(parent_tags) + '/' + tag
#         val = data.get('text')
#         output.append((key, val))
#     return output


import json
import re
from collections import OrderedDict
from io import StringIO
from lxml import etree
from pyquery import PyQuery
from .b64_attachment import B64Attachment
from .utils import parse_xmljson_to_data
            'tag': self._version._root_node_name or 'data',
            'attributes': {
                'id_string': self._version.id_string,
                'version': self._version._version_id,
            },
            'children': [
                _item_to_struct(item) for item in iter(self.data.items())
            ],
        }

    def to_xml(self, files=False):
        return xmljson_to_xml(self.to_xml_struct(files))

    def to_xml_export(self):
        files = []
        return self.to_xml(), files

    @classmethod
    def from_xml(cls, xml, version=None):
        xmljson = OrderedDict(parse_xmljson_to_data(xml, [], []))
        return cls(xmljson, version)


class NestedStruct(OrderedDict):
    def get(self, key):
        if key not in self:
            self[key] = NestedStruct()
        return self[key]

    def to_json(self):

# Path: src/formpack/b64_attachment.py
# class B64Attachment(str):
#     """
#     * loaded from base64 string
#     * unloadable to file
#     """
# 
#     @classmethod
#     def _is_attachment(cls, data_uri):
#         return hasattr(data_uri, "startswith") and data_uri.startswith('data:')
# 
#     @classmethod
#     def _attachment_split(cls, data_uri):
#         """
#         accepts a data_uri,
#         returns a tuple (mediatype, extension, contents)
#         """
#         return re.match(DATA_URI_RE, data_uri).groups()
# 
#     @classmethod
#     def write_to_tempfile(cls, data_uri):
#         """
#         *placeholder method*
# 
#         this method will create a new TempFile (or cStringIO) with the
#         contents of the data_uri which can then be POST'ed to kobocat in
#         a mock submission.
#         """
#         (ftype, fext, fcont) = cls._attachment_split(data_uri)
#         fname = 'a%s.%s' % (randstr(10), fext)
#         return fname, '/tmp/path/to/%s' % fname
# 
# Path: src/formpack/utils/xform_tools.py
# def parse_xmljson_to_data(data, parent_tags=[], output=[]):
#     tag = data.get('tag')
#     new_parents = parent_tags + [tag]
#     if len(data.get('children', [])) > 0:
#         for child in data.get('children'):
#             parse_xmljson_to_data(child, new_parents, output)
#     else:
#         key = '/' + '/'.join(parent_tags) + '/' + tag
#         val = data.get('text')
#         output.append((key, val))
#     return output


# Path: src/formpack/submission.py
import json
import re
from collections import OrderedDict
from io import StringIO
from lxml import etree
from pyquery import PyQuery
from .b64_attachment import B64Attachment
from .utils import parse_xmljson_to_data

            'tag': self._version._root_node_name or 'data',
            'attributes': {
                'id_string': self._version.id_string,
                'version': self._version._version_id,
            },
            'children': [
                _item_to_struct(item) for item in iter(self.data.items())
            ],
        }

    def to_xml(self, files=False):
        return xmljson_to_xml(self.to_xml_struct(files))

    def to_xml_export(self):
        files = []
        return self.to_xml(), files

    @classmethod
    def from_xml(cls, xml, version=None):
        xmljson = OrderedDict(parse_xmljson_to_data(xml, [], []))
        return cls(xmljson, version)


class NestedStruct(OrderedDict):
    def get(self, key):
        if key not in self:
            self[key] = NestedStruct()
        return self[key]

    def to_json(self):

        return json.dumps(self, indent=4)