# Path: siptools/utils.py
# def get_objectlist(refs_dict, file_path=None):
#     """Get unique and sorted list of files or streams.
# 
#     Files or streasm are read from md-references.jsonl
# 
#     :refs_dict: Dictionary of objects
#     :file_path: If given, finds streams of the given file.
#                 If None, finds a sorted list all file paths.
#     :returns: Sorted list of files, or streams of a given file
#     """
#     objectset = set()
#     if file_path is not None:
#         for stream in refs_dict[file_path]['streams']:
#             objectset.add(stream)
#     elif refs_dict:
#         for key, value in six.iteritems(refs_dict):
#             if value['path_type'] == 'file':
#                 objectset.add(key)
# 
#     return sorted(objectset)
# 
# def read_md_references(workspace, ref_file):
#     """Read all the MD IDs as a dictionary.
# 
#     :workspace: path to workspace directory
#     :ref_file: Metadata reference file
#     :returns: A dict of references or None if reference file doesn't
#               exist
#     """
#     reference_file = os.path.join(workspace, ref_file)
# 
#     if os.path.isfile(reference_file):
#         references = {}
#         with open(reference_file) as in_file:
#             for line in in_file:
#                 references.update(json.loads(line))
#         return references
#     return None
# 
# Path: siptools/xml/mets.py
# METS_CATALOG = "1.7.3"
# 
# METS_PROFILE = {
#     'ch': 'http://digitalpreservation.fi/mets-profiles/cultural-heritage',
#     'tpas': 'http://digitalpreservation.fi/mets-profiles/research-data',
# }
# 
# METS_SPECIFICATION = "1.7.3"
# 
# NAMESPACES = {
#     'mets': 'http://www.loc.gov/METS/',
#     'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
#     'premis': 'info:lc/xmlns/premis-v2',
#     'fi': 'http://digitalpreservation.fi/schemas/mets/fi-extensions',
#     'xlink': 'http://www.w3.org/1999/xlink',
#     'mix': 'http://www.loc.gov/mix/v20',
#     'ead3': 'http://ead3.archivists.org/schema/',
#     'addml': 'http://www.arkivverket.no/standarder/addml',
#     'audiomd': 'http://www.loc.gov/audioMD/',
#     'videomd': 'http://www.loc.gov/videoMD/'
# }
# 
# RECORD_STATUS_TYPES = [
#     'submission',
#     'update',
#     'dissemination'
# ]
# 
# def mets_extend(mets_root, catalog=METS_CATALOG,
#                 specification=METS_SPECIFICATION, contentid=None,
#                 contractid=None):
#     """Create METS ElementTree"""
# 
#     del mets_root.attrib['{%s}schemaLocation' % XSI_NS]
#     mets_root.set('{%s}schemaLocation' % XSI_NS,
#                   NAMESPACES['mets'] + ' ' + METS_SCHEMA)
#     mets_root.set('{%s}CATALOG' % FI_NS, catalog)
#     mets_root.set('{%s}SPECIFICATION' % FI_NS, specification)
#     if contentid:
#         mets_root.set('{%s}CONTENTID' % FI_NS, contentid)
#     if contractid:
#         contractstr = six.text_type(contractid)
#         mets_root.set('{%s}CONTRACTID' % FI_NS, contractstr)
# 
#     return mets_root


import datetime
import os
import sys
import uuid
import six
import click
import lxml.etree
import mets
import xml_helpers.utils as xml_utils
from shutil import copyfile
from siptools.utils import get_objectlist, read_md_references
from siptools.xml.mets import (METS_CATALOG, METS_PROFILE, METS_SPECIFICATION,
                               NAMESPACES, RECORD_STATUS_TYPES, mets_extend)
    from scandir import scandir  # Python 2
    from os import scandir  # Python 3+
        agents.append(mets.agent(attributes["packagingservice"],
                                 agent_type='OTHER',
                                 agent_role='CREATOR',
                                 othertype='SOFTWARE'))
    else:
        agents = [mets.agent(attributes["organization_name"],
                             agent_role='CREATOR')]

    # Create mets header
    metshdr = mets.metshdr(attributes["create_date"],
                           attributes["last_moddate"],
                           attributes["record_status"],
                           agents)

    # Collect elements from workspace XML files
    elements = []
    for entry in scandir(attributes["workspace"]):
        if entry.name.endswith(('-amd.xml', 'dmdsec.xml',
                                'structmap.xml', 'filesec.xml',
                                'rightsmd.xml')) and entry.is_file():
            element = lxml.etree.parse(entry.path).getroot()[0]
            elements.append(element)

    elements = mets.merge_elements('{%s}amdSec' % NAMESPACES['mets'], elements)
    elements.sort(key=mets.order)

    # Create METS element
    mets_element = mets.mets(METS_PROFILE[attributes["mets_profile"]],
                             objid=attributes["objid"],
                             label=attributes["label"],

# Path: siptools/utils.py
# def get_objectlist(refs_dict, file_path=None):
#     """Get unique and sorted list of files or streams.
# 
#     Files or streasm are read from md-references.jsonl
# 
#     :refs_dict: Dictionary of objects
#     :file_path: If given, finds streams of the given file.
#                 If None, finds a sorted list all file paths.
#     :returns: Sorted list of files, or streams of a given file
#     """
#     objectset = set()
#     if file_path is not None:
#         for stream in refs_dict[file_path]['streams']:
#             objectset.add(stream)
#     elif refs_dict:
#         for key, value in six.iteritems(refs_dict):
#             if value['path_type'] == 'file':
#                 objectset.add(key)
# 
#     return sorted(objectset)
# 
# def read_md_references(workspace, ref_file):
#     """Read all the MD IDs as a dictionary.
# 
#     :workspace: path to workspace directory
#     :ref_file: Metadata reference file
#     :returns: A dict of references or None if reference file doesn't
#               exist
#     """
#     reference_file = os.path.join(workspace, ref_file)
# 
#     if os.path.isfile(reference_file):
#         references = {}
#         with open(reference_file) as in_file:
#             for line in in_file:
#                 references.update(json.loads(line))
#         return references
#     return None
# 
# Path: siptools/xml/mets.py
# METS_CATALOG = "1.7.3"
# 
# METS_PROFILE = {
#     'ch': 'http://digitalpreservation.fi/mets-profiles/cultural-heritage',
#     'tpas': 'http://digitalpreservation.fi/mets-profiles/research-data',
# }
# 
# METS_SPECIFICATION = "1.7.3"
# 
# NAMESPACES = {
#     'mets': 'http://www.loc.gov/METS/',
#     'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
#     'premis': 'info:lc/xmlns/premis-v2',
#     'fi': 'http://digitalpreservation.fi/schemas/mets/fi-extensions',
#     'xlink': 'http://www.w3.org/1999/xlink',
#     'mix': 'http://www.loc.gov/mix/v20',
#     'ead3': 'http://ead3.archivists.org/schema/',
#     'addml': 'http://www.arkivverket.no/standarder/addml',
#     'audiomd': 'http://www.loc.gov/audioMD/',
#     'videomd': 'http://www.loc.gov/videoMD/'
# }
# 
# RECORD_STATUS_TYPES = [
#     'submission',
#     'update',
#     'dissemination'
# ]
# 
# def mets_extend(mets_root, catalog=METS_CATALOG,
#                 specification=METS_SPECIFICATION, contentid=None,
#                 contractid=None):
#     """Create METS ElementTree"""
# 
#     del mets_root.attrib['{%s}schemaLocation' % XSI_NS]
#     mets_root.set('{%s}schemaLocation' % XSI_NS,
#                   NAMESPACES['mets'] + ' ' + METS_SCHEMA)
#     mets_root.set('{%s}CATALOG' % FI_NS, catalog)
#     mets_root.set('{%s}SPECIFICATION' % FI_NS, specification)
#     if contentid:
#         mets_root.set('{%s}CONTENTID' % FI_NS, contentid)
#     if contractid:
#         contractstr = six.text_type(contractid)
#         mets_root.set('{%s}CONTRACTID' % FI_NS, contractstr)
# 
#     return mets_root


# Path: siptools/scripts/compile_mets.py
import datetime
import os
import sys
import uuid
import six
import click
import lxml.etree
import mets
import xml_helpers.utils as xml_utils
from shutil import copyfile
from siptools.utils import get_objectlist, read_md_references
from siptools.xml.mets import (METS_CATALOG, METS_PROFILE, METS_SPECIFICATION,
                               NAMESPACES, RECORD_STATUS_TYPES, mets_extend)
    from scandir import scandir  # Python 2
    from os import scandir  # Python 3+

        agents.append(mets.agent(attributes["packagingservice"],
                                 agent_type='OTHER',
                                 agent_role='CREATOR',
                                 othertype='SOFTWARE'))
    else:
        agents = [mets.agent(attributes["organization_name"],
                             agent_role='CREATOR')]

    # Create mets header
    metshdr = mets.metshdr(attributes["create_date"],
                           attributes["last_moddate"],
                           attributes["record_status"],
                           agents)

    # Collect elements from workspace XML files
    elements = []
    for entry in scandir(attributes["workspace"]):
        if entry.name.endswith(('-amd.xml', 'dmdsec.xml',
                                'structmap.xml', 'filesec.xml',
                                'rightsmd.xml')) and entry.is_file():
            element = lxml.etree.parse(entry.path).getroot()[0]
            elements.append(element)

    elements = mets.merge_elements('{%s}amdSec' % NAMESPACES['mets'], elements)
    elements.sort(key=mets.order)

    # Create METS element
    mets_element = mets.mets(METS_PROFILE[attributes["mets_profile"]],
                             objid=attributes["objid"],
                             label=attributes["label"],

                             namespaces=NAMESPACES)