# Path: invenio_pidstore/providers/recordid.py
# class RecordIdProvider(BaseProvider):
#     """Record identifier provider."""
# 
#     pid_type = 'recid'
#     """Type of persistent identifier."""
# 
#     pid_provider = None
#     """Provider name.
# 
#     The provider name is not recorded in the PID since the provider does not
#     provide any additional features besides creation of record ids.
#     """
# 
#     default_status = PIDStatus.RESERVED
#     """Record IDs are by default registered immediately.
# 
#     Default: :attr:`invenio_pidstore.models.PIDStatus.RESERVED`
#     """
# 
#     @classmethod
#     def create(cls, object_type=None, object_uuid=None, **kwargs):
#         """Create a new record identifier.
# 
#         Note: if the object_type and object_uuid values are passed, then the
#         PID status will be automatically setted to
#         :attr:`invenio_pidstore.models.PIDStatus.REGISTERED`.
# 
#         :param object_type: The object type. (Default: None.)
#         :param object_uuid: The object identifier. (Default: None).
#         :param kwargs: You specify the pid_value.
#         """
#         # Request next integer in recid sequence.
#         assert 'pid_value' not in kwargs
#         kwargs['pid_value'] = str(RecordIdentifier.next())
#         kwargs.setdefault('status', cls.default_status)
#         if object_type and object_uuid:
#             kwargs['status'] = PIDStatus.REGISTERED
#         return super(RecordIdProvider, cls).create(
#             object_type=object_type, object_uuid=object_uuid, **kwargs)
# 
# Path: invenio_pidstore/providers/recordid_v2.py
# class RecordIdProviderV2(BaseProvider):
#     """Record identifier provider V2.
# 
#     This is the recommended record id provider.
# 
#     It generates a random alphanumeric string as opposed to an increasing
#     integer (:class:`invenio_pidstore.providers.recordid.RecordIdProvider`).
#     """
# 
#     pid_type = 'recid'
#     """Type of persistent identifier."""
# 
#     pid_provider = None
#     """Provider name.
# 
#     The provider name is not recorded in the PID since the provider does not
#     provide any additional features besides creation of record ids.
#     """
# 
#     default_status_with_obj = PIDStatus.REGISTERED
#     """Record IDs are by default registered immediately.
# 
#     Default: :attr:`invenio_pidstore.models.PIDStatus.REGISTERED`
#     """
# 
#     default_status = PIDStatus.RESERVED
#     """Record IDs with an object are by default reserved.
# 
#     Default: :attr:`invenio_pidstore.models.PIDStatus.RESERVED`
#     """
# 
#     @classmethod
#     def generate_id(cls, options=None):
#         """Generate record id."""
#         passed_options = options or {}
#         # WHY: A new dict needs to be created to prevent side-effects
#         options = copy.deepcopy(current_app.config.get(
#             'PIDSTORE_RECORDID_OPTIONS', {}
#         ))
#         options.update(passed_options)
#         length = options.get('length', 10)
#         split_every = options.get('split_every', 0)
#         checksum = options.get('checksum', True)
# 
#         return base32.generate(
#             length=length,
#             split_every=split_every,
#             checksum=checksum
#         )
# 
#     @classmethod
#     def create(cls, object_type=None, object_uuid=None, options=None,
#                **kwargs):
#         """Create a new record identifier.
# 
#         Note: if the object_type and object_uuid values are passed, then the
#         PID status will be automatically setted to
#         :attr:`invenio_pidstore.models.PIDStatus.REGISTERED`.
# 
#         For more information about parameters,
#         see :meth:`invenio_pidstore.providers.base.BaseProvider.create`.
# 
#         :param object_type: The object type. (Default: None.)
#         :param object_uuid: The object identifier. (Default: None).
#         :param options: ``dict`` with optional keys:
#             ``"length"`` (integer), ``"split_every"`` (integer),
#             ``"checksum"`` (boolean). (Default: None).
#         :param kwargs: dict to hold generated pid_value and status. See
#             :meth:`invenio_pidstore.providers.base.BaseProvider.create` extra
#             parameters.
#         :returns: A :class:`RecordIdProviderV2` instance.
#         """
#         assert 'pid_value' not in kwargs
# 
#         kwargs['pid_value'] = cls.generate_id(options)
#         kwargs.setdefault('status', cls.default_status)
# 
#         if object_type and object_uuid:
#             kwargs['status'] = cls.default_status_with_obj
# 
#         return super(RecordIdProviderV2, cls).create(
#             object_type=object_type, object_uuid=object_uuid, **kwargs)


from collections import namedtuple
from flask import current_app
from .providers.recordid import RecordIdProvider
from .providers.recordid_v2 import RecordIdProviderV2

FetchedPID = namedtuple('FetchedPID', ['provider', 'pid_type', 'pid_value'])
"""A pid fetcher."""


def recid_fetcher_v2(record_uuid, data):
    """Fetch a record's identifiers.

    :param record_uuid: The record UUID.
    :param data: The record metadata.
    :returns: A :data:`invenio_pidstore.fetchers.FetchedPID` instance.
    """
    pid_field = current_app.config['PIDSTORE_RECID_FIELD']
    return FetchedPID(
        provider=RecordIdProviderV2,
        pid_type=RecordIdProviderV2.pid_type,
        pid_value=str(data[pid_field])
    )


def recid_fetcher(record_uuid, data):
    """Legacy way to fetch a record's identifiers.

    :param record_uuid: The record UUID.
    :param data: The record metadata.
    :returns: A :data:`invenio_pidstore.fetchers.FetchedPID` instance.
    """
    pid_field = current_app.config['PIDSTORE_RECID_FIELD']
    return FetchedPID(
        provider=RecordIdProvider,

# Path: invenio_pidstore/providers/recordid.py
# class RecordIdProvider(BaseProvider):
#     """Record identifier provider."""
# 
#     pid_type = 'recid'
#     """Type of persistent identifier."""
# 
#     pid_provider = None
#     """Provider name.
# 
#     The provider name is not recorded in the PID since the provider does not
#     provide any additional features besides creation of record ids.
#     """
# 
#     default_status = PIDStatus.RESERVED
#     """Record IDs are by default registered immediately.
# 
#     Default: :attr:`invenio_pidstore.models.PIDStatus.RESERVED`
#     """
# 
#     @classmethod
#     def create(cls, object_type=None, object_uuid=None, **kwargs):
#         """Create a new record identifier.
# 
#         Note: if the object_type and object_uuid values are passed, then the
#         PID status will be automatically setted to
#         :attr:`invenio_pidstore.models.PIDStatus.REGISTERED`.
# 
#         :param object_type: The object type. (Default: None.)
#         :param object_uuid: The object identifier. (Default: None).
#         :param kwargs: You specify the pid_value.
#         """
#         # Request next integer in recid sequence.
#         assert 'pid_value' not in kwargs
#         kwargs['pid_value'] = str(RecordIdentifier.next())
#         kwargs.setdefault('status', cls.default_status)
#         if object_type and object_uuid:
#             kwargs['status'] = PIDStatus.REGISTERED
#         return super(RecordIdProvider, cls).create(
#             object_type=object_type, object_uuid=object_uuid, **kwargs)
# 
# Path: invenio_pidstore/providers/recordid_v2.py
# class RecordIdProviderV2(BaseProvider):
#     """Record identifier provider V2.
# 
#     This is the recommended record id provider.
# 
#     It generates a random alphanumeric string as opposed to an increasing
#     integer (:class:`invenio_pidstore.providers.recordid.RecordIdProvider`).
#     """
# 
#     pid_type = 'recid'
#     """Type of persistent identifier."""
# 
#     pid_provider = None
#     """Provider name.
# 
#     The provider name is not recorded in the PID since the provider does not
#     provide any additional features besides creation of record ids.
#     """
# 
#     default_status_with_obj = PIDStatus.REGISTERED
#     """Record IDs are by default registered immediately.
# 
#     Default: :attr:`invenio_pidstore.models.PIDStatus.REGISTERED`
#     """
# 
#     default_status = PIDStatus.RESERVED
#     """Record IDs with an object are by default reserved.
# 
#     Default: :attr:`invenio_pidstore.models.PIDStatus.RESERVED`
#     """
# 
#     @classmethod
#     def generate_id(cls, options=None):
#         """Generate record id."""
#         passed_options = options or {}
#         # WHY: A new dict needs to be created to prevent side-effects
#         options = copy.deepcopy(current_app.config.get(
#             'PIDSTORE_RECORDID_OPTIONS', {}
#         ))
#         options.update(passed_options)
#         length = options.get('length', 10)
#         split_every = options.get('split_every', 0)
#         checksum = options.get('checksum', True)
# 
#         return base32.generate(
#             length=length,
#             split_every=split_every,
#             checksum=checksum
#         )
# 
#     @classmethod
#     def create(cls, object_type=None, object_uuid=None, options=None,
#                **kwargs):
#         """Create a new record identifier.
# 
#         Note: if the object_type and object_uuid values are passed, then the
#         PID status will be automatically setted to
#         :attr:`invenio_pidstore.models.PIDStatus.REGISTERED`.
# 
#         For more information about parameters,
#         see :meth:`invenio_pidstore.providers.base.BaseProvider.create`.
# 
#         :param object_type: The object type. (Default: None.)
#         :param object_uuid: The object identifier. (Default: None).
#         :param options: ``dict`` with optional keys:
#             ``"length"`` (integer), ``"split_every"`` (integer),
#             ``"checksum"`` (boolean). (Default: None).
#         :param kwargs: dict to hold generated pid_value and status. See
#             :meth:`invenio_pidstore.providers.base.BaseProvider.create` extra
#             parameters.
#         :returns: A :class:`RecordIdProviderV2` instance.
#         """
#         assert 'pid_value' not in kwargs
# 
#         kwargs['pid_value'] = cls.generate_id(options)
#         kwargs.setdefault('status', cls.default_status)
# 
#         if object_type and object_uuid:
#             kwargs['status'] = cls.default_status_with_obj
# 
#         return super(RecordIdProviderV2, cls).create(
#             object_type=object_type, object_uuid=object_uuid, **kwargs)


# Path: invenio_pidstore/fetchers.py
from collections import namedtuple
from flask import current_app
from .providers.recordid import RecordIdProvider
from .providers.recordid_v2 import RecordIdProviderV2


FetchedPID = namedtuple('FetchedPID', ['provider', 'pid_type', 'pid_value'])
"""A pid fetcher."""


def recid_fetcher_v2(record_uuid, data):
    """Fetch a record's identifiers.

    :param record_uuid: The record UUID.
    :param data: The record metadata.
    :returns: A :data:`invenio_pidstore.fetchers.FetchedPID` instance.
    """
    pid_field = current_app.config['PIDSTORE_RECID_FIELD']
    return FetchedPID(
        provider=RecordIdProviderV2,
        pid_type=RecordIdProviderV2.pid_type,
        pid_value=str(data[pid_field])
    )


def recid_fetcher(record_uuid, data):
    """Legacy way to fetch a record's identifiers.

    :param record_uuid: The record UUID.
    :param data: The record metadata.
    :returns: A :data:`invenio_pidstore.fetchers.FetchedPID` instance.
    """
    pid_field = current_app.config['PIDSTORE_RECID_FIELD']
    return FetchedPID(
        provider=RecordIdProvider,

        pid_type=RecordIdProvider.pid_type,