# Path: siptools/utils.py
# def generate_digest(etree):
#     """Generate MD5 digest from etree.
# 
#     Identical metadata must
#     generate same digest even if attributes of any given element are
#     ordered differently. Also some metadata sections contain unique
#     identifiers that have to be removed if digest comparison is to work.
# 
#     This function creates a copy of the etree. All the attributes of the
#     copy are removed and collected to a separete list with path
#     information to the XML element the attributes belong to. This list
#     is sorted and appended to the end of the serialized XML string
#     without the attributes. Thus creating a string with all the original
#     information except the information about attribute ordering inside
#     any given XML element. This string is hashed and the digest
#     returned. For some PREMIS metadata identifiers are also removed.
# 
#     :etree: XML element for which the MD5 hash is generated
#     :returns: MD5 hash
#     """
#     # Creating copy of the original etree to avoid editing it
#     root = copy_etree(etree)
#     elem_tree = lxml.etree.ElementTree(root)
#     attrib_list = []
# 
#     # Remove premis identifiers and linking elements before metadata
#     # comparison
#     elem_tree = _remove_elements(elem_tree, 'eventIdentifierValue')
#     elem_tree = _remove_elements(elem_tree, 'agentIdentifierValue')
#     elem_tree = _remove_elements(elem_tree, 'linkingAgentIdentifier')
# 
#     # pop all attributes
#     for element in root.iter():
#         attributes = element.attrib
#         path = elem_tree.getpath(element)
#         _pop_attributes(attributes, attrib_list, path)
# 
#     attrib_list.sort()
#     xml_data = xml_helpers.utils.serialize(root)
# 
#     # Add the sorted attributes at the end of the serialized XML
#     attr_data = b"".join([attr.encode("utf-8") for attr in attrib_list])
#     xml_data = b"".join([xml_data, attr_data])
#     return hashlib.md5(xml_data).hexdigest()
# 
# def encode_path(path, suffix='', prefix='', safe=""):
#     """
#     Encode given path to URL encoding with given perfix and suffix.
# 
#     :path: Path to encode
#     :suffix: Suffix to add
#     :prefix: Prefix to add
#     :safe: Characters safe from URL quoting
#     :returns: Encoded string with given prefix and suffix
#     """
#     if isinstance(path, six.text_type):
#         path = path.encode("utf-8")
# 
#     if isinstance(safe, six.text_type):
#         safe = safe.encode("utf-8")
# 
#     quoted = quote_plus(path, safe=safe)
#     return "{}{}{}".format(prefix, quoted, suffix)


import os
import sys
import json
import six
import mets
import xml_helpers
from siptools.utils import generate_digest, encode_path
            mdwrap.append(xmldata)
            if section == 'digiprovmd':
                amd = mets.digiprovmd(md_id)
            else:
                amd = mets.techmd(md_id)
            amd.append(mdwrap)
            amdsec = mets.amdsec()
            amdsec.append(amd)
            mets_ = mets.mets()
            mets_.append(amdsec)

            with open(filename, 'wb+') as outfile:
                outfile.write(xml_helpers.utils.serialize(mets_))
                if stdout:
                    print(xml_helpers.utils.serialize(mets_).decode("utf-8"))
                print(
                    "Wrote lxml.etree %s administrative metadata to file "
                    "%s" % (mdtype, outfile.name)
                )

        return md_id, filename

    def write_dict(self, file_metadata_dict, premis_amd_id):
        """
        Write streams to a file for further scripts.

        :file_metadata_dict: File metadata dict
        :premis_amd_id: The AMDID of corresponding premis FILE object
        """
        digest = premis_amd_id[1:]

# Path: siptools/utils.py
# def generate_digest(etree):
#     """Generate MD5 digest from etree.
# 
#     Identical metadata must
#     generate same digest even if attributes of any given element are
#     ordered differently. Also some metadata sections contain unique
#     identifiers that have to be removed if digest comparison is to work.
# 
#     This function creates a copy of the etree. All the attributes of the
#     copy are removed and collected to a separete list with path
#     information to the XML element the attributes belong to. This list
#     is sorted and appended to the end of the serialized XML string
#     without the attributes. Thus creating a string with all the original
#     information except the information about attribute ordering inside
#     any given XML element. This string is hashed and the digest
#     returned. For some PREMIS metadata identifiers are also removed.
# 
#     :etree: XML element for which the MD5 hash is generated
#     :returns: MD5 hash
#     """
#     # Creating copy of the original etree to avoid editing it
#     root = copy_etree(etree)
#     elem_tree = lxml.etree.ElementTree(root)
#     attrib_list = []
# 
#     # Remove premis identifiers and linking elements before metadata
#     # comparison
#     elem_tree = _remove_elements(elem_tree, 'eventIdentifierValue')
#     elem_tree = _remove_elements(elem_tree, 'agentIdentifierValue')
#     elem_tree = _remove_elements(elem_tree, 'linkingAgentIdentifier')
# 
#     # pop all attributes
#     for element in root.iter():
#         attributes = element.attrib
#         path = elem_tree.getpath(element)
#         _pop_attributes(attributes, attrib_list, path)
# 
#     attrib_list.sort()
#     xml_data = xml_helpers.utils.serialize(root)
# 
#     # Add the sorted attributes at the end of the serialized XML
#     attr_data = b"".join([attr.encode("utf-8") for attr in attrib_list])
#     xml_data = b"".join([xml_data, attr_data])
#     return hashlib.md5(xml_data).hexdigest()
# 
# def encode_path(path, suffix='', prefix='', safe=""):
#     """
#     Encode given path to URL encoding with given perfix and suffix.
# 
#     :path: Path to encode
#     :suffix: Suffix to add
#     :prefix: Prefix to add
#     :safe: Characters safe from URL quoting
#     :returns: Encoded string with given prefix and suffix
#     """
#     if isinstance(path, six.text_type):
#         path = path.encode("utf-8")
# 
#     if isinstance(safe, six.text_type):
#         safe = safe.encode("utf-8")
# 
#     quoted = quote_plus(path, safe=safe)
#     return "{}{}{}".format(prefix, quoted, suffix)


# Path: siptools/mdcreator.py
import os
import sys
import json
import six
import mets
import xml_helpers
from siptools.utils import generate_digest, encode_path

            mdwrap.append(xmldata)
            if section == 'digiprovmd':
                amd = mets.digiprovmd(md_id)
            else:
                amd = mets.techmd(md_id)
            amd.append(mdwrap)
            amdsec = mets.amdsec()
            amdsec.append(amd)
            mets_ = mets.mets()
            mets_.append(amdsec)

            with open(filename, 'wb+') as outfile:
                outfile.write(xml_helpers.utils.serialize(mets_))
                if stdout:
                    print(xml_helpers.utils.serialize(mets_).decode("utf-8"))
                print(
                    "Wrote lxml.etree %s administrative metadata to file "
                    "%s" % (mdtype, outfile.name)
                )

        return md_id, filename

    def write_dict(self, file_metadata_dict, premis_amd_id):
        """
        Write streams to a file for further scripts.

        :file_metadata_dict: File metadata dict
        :premis_amd_id: The AMDID of corresponding premis FILE object
        """
        digest = premis_amd_id[1:]

        filename = encode_path("%s-scraper.json" % digest)