# Copyright 2019 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Python wrappers for building checkpointers."""

import abc
import tempfile
from typing import Optional

import numpy  # pylint: disable=unused-import
from reverb import pybind


class CheckpointerBase(metaclass=abc.ABCMeta):
  """Base class for Python wrappers of the Checkpointer."""

  @abc.abstractmethod
  def internal_checkpointer(self) -> pybind.Checkpointer:
    """Creates the actual Checkpointer-object used by the C++ layer."""


class DefaultCheckpointer(CheckpointerBase):
  """Base class for storing checkpoints to as recordIO files.."""

  def __init__(self,
               path: str,
               group: str = '',
               fallback_checkpoint_path: Optional[str] = None):
    """Constructor of DefaultCheckpointer.

    Args:
      path: Root directory to store checkpoints in.
      group: MDB group to set as "group" of checkpoint directory. If empty
        (default) then no group is set.
      fallback_checkpoint_path: (Optional) path to the actual checkpoint.
        The checkpointer first attempts to load from the root directory (path)
        and, then, iff the root directory is empty it loads the fallback
        checkpoint. This way we are effectively using the fallback checkpoint as
        a way to initialise the service with a checkpoint generated by another
        experiment. Note: unlike `path`, `fallback_checkpoint_path` has to be
        the path to the actual checkpoint and not a directory from which to
        reload a checkpoint.
    """
    self.path = path
    self.group = group
    self.fallback_checkpoint_path = fallback_checkpoint_path

  def internal_checkpointer(self) -> pybind.Checkpointer:
    """Creates the actual Checkpointer-object used by the C++ layer."""
    return pybind.create_default_checkpointer(self.path, self.group,
                                              self.fallback_checkpoint_path)


class TempDirCheckpointer(DefaultCheckpointer):
  """Stores and loads checkpoints from a temporary directory."""

  def __init__(self):
    super().__init__(tempfile.mkdtemp())
