# 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
# coding: utf-8




class FormSubmission:
    def __init__(self, submission_data=None, version=None):
        self.data = submission_data or {}
        self._version = version

        for key in submission_data:
            val = submission_data[key]
            if B64Attachment._is_attachment(val):
                submission_data[key] = B64Attachment(val)

    def to_dict(self):
        return self.data

    def to_xml_struct(self, files=False):
        def _item_to_struct(item):
            key, val = item
            if isinstance(val, list):
                val = list(map(_item_to_struct, val))
            elif isinstance(val, B64Attachment) and files is not False:

# 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

# coding: utf-8




class FormSubmission:
    def __init__(self, submission_data=None, version=None):
        self.data = submission_data or {}
        self._version = version

        for key in submission_data:
            val = submission_data[key]
            if B64Attachment._is_attachment(val):
                submission_data[key] = B64Attachment(val)

    def to_dict(self):
        return self.data

    def to_xml_struct(self, files=False):
        def _item_to_struct(item):
            key, val = item
            if isinstance(val, list):
                val = list(map(_item_to_struct, val))
            elif isinstance(val, B64Attachment) and files is not False:

                (fname, fpath) = B64Attachment.write_to_tempfile(val)