"""
Collection management and migration helpers

See also: documents/notes/schema-evolution-notes:
"""

from __future__ import unicode_literals
from __future__ import absolute_import, division, print_function

__author__      = "Graham Klyne (GK@ACM.ORG)"
__copyright__   = "Copyright 2016, G. Klyne"
__license__     = "MIT (http://opensource.org/licenses/MIT)"

import os
import sys
import logging
import subprocess
import importlib
import shutil
import datetime

log = logging.getLogger(__name__)

from annalist                       import layout
from annalist.identifiers           import ANNAL, RDFS
from annalist.util                  import valid_id, extract_entity_id, make_type_entity_id
from annalist.collections_data      import installable_collections

from annalist.models.site           import Site
from annalist.models.collection     import Collection
from annalist.models.recordtype     import RecordType
from annalist.models.recordview     import RecordView
from annalist.models.recordlist     import RecordList
from annalist.models.recordfield    import RecordField
from annalist.models.recordgroup    import RecordGroup
from annalist.models.collectiondata import initialize_coll_data, copy_coll_data, migrate_coll_data

from .                              import am_errors
from .am_settings                   import (
    am_get_settings, am_get_site_settings, am_get_site
    )
from .am_getargvalue                import getarg, getargvalue

# Collection access helpers

def get_settings_site(annroot, userhome, options):
    """
    Get settings and site data based on command line options provided

    returns:

        (status, settings, site)

    where 'settings' and/or 'site' are None if not found.
    """
    status   = am_errors.AM_SUCCESS
    settings = am_get_settings(annroot, userhome, options)
    site     = None
    if not settings:
        print("Settings not found (%s)"%(options.configuration), file=sys.stderr)
        status = am_errors.AM_NOSETTINGS
    if status == am_errors.AM_SUCCESS:
        sitesettings = am_get_site_settings(annroot, userhome, options)
        if not sitesettings:
            print("Site settings not found (%s)"%(options.configuration), file=sys.stderr)