import os
import shutil
import tempfile

from chainer.serializers import npz
from chainer.training import extension


def snapshot_object(target, filename, savefun=npz.save_npz,
                    trigger=(1, 'epoch')):
    """Returns a trainer extension to take snapshots of a given object.

    This extension serializes the given object and saves it to the output
    directory.

    This extension is called once for each epoch by default. The default
    priority is -100, which is lower than that of most built-in extensions.

    Args:
        target: Object to serialize.
        filename (str): Name of the file into which the object is serialized.
            It can be a format string, where the trainer object is passed to
            the :meth:`str.format` method. For example,
            ``'snapshot_{.updater.iteration}'`` is converted to
            ``'snapshot_10000'`` at the 10,000th iteration.
        savefun: Function to save the object. It takes two arguments: the
            output file path and the object to serialize.
        trigger: Trigger that decides when to take snapshot. If it is a tuple
            in the form ``<int>, 'epoch'`` or ``<int>, 'iteration'``, it is
            passed to :class:`IntervalTrigger`.

    Returns:
        An extension function.

    """
    @extension.make_extension(trigger=trigger, priority=-100)
    def snapshot_object(trainer):
        _snapshot_object(trainer, target, filename.format(trainer), savefun)

    return snapshot_object


def snapshot(savefun=npz.save_npz,
             filename='snapshot_iter_{.updater.iteration}',
             trigger=(1, 'epoch')):
    """Returns a trainer extension to take snapshots of the trainer.

    This extension serializes the trainer object and saves it to the output
    directory. It is used to support resuming the training loop from the saved
    state.

    This extension is called once for each epoch by default. The default
    priority is -100, which is lower than that of most built-in extensions.

    .. note::
       This extension first writes the serialized object to a temporary file
       and then rename it to the target file name. Thus, if the program stops
       right before the renaming, the temporary file might be left in the
       output directory.

    Args:
        savefun: Function to save the trainer. It takes two arguments: the
            output file path and the trainer object.
        filename (str): Name of the file into which the trainer is serialized.
            It can be a format string, where the trainer object is passed to
            the :meth:`str.format` method.
        trigger: Trigger that decides when to take snapshot. If it is a tuple
            in the form ``<int>, 'epoch'`` or ``<int>, 'iteration'``, it is
            passed to :class:`IntervalTrigger`.

    """
    @extension.make_extension(trigger=trigger, priority=-100)
    def snapshot(trainer):
        _snapshot_object(trainer, trainer, filename.format(trainer), savefun)

    return snapshot


def _snapshot_object(trainer, target, filename, savefun):
    fn = filename.format(trainer)
    prefix = 'tmp' + fn
    fd, tmppath = tempfile.mkstemp(prefix=prefix, dir=trainer.out)
    try:
        savefun(tmppath, target)
    except Exception:
        os.close(fd)
        os.remove(tmppath)
        raise
    os.close(fd)
    shutil.move(tmppath, os.path.join(trainer.out, fn))

@@ -173,50 +173,8 @@
 _npz
-,%0A                    trigger=(1, 'epoch')
 ):%0A 
@@ -1002,210 +1002,8 @@
 ize.
-%0A        trigger: Trigger that decides when to take snapshot. If it is a tuple%0A            in the form %60%60%3Cint%3E, 'epoch'%60%60 or %60%60%3Cint%3E, 'iteration'%60%60, it is%0A            passed to :class:%60IntervalTrigger%60.
 %0A%0A  
@@ -1083,39 +1083,44 @@
 tension(trigger=
-trigger
+(1, 'epoch')
 , priority=-100)
@@ -1358,43 +1358,8 @@
 on%7D'
-,%0A             trigger=(1, 'epoch')
 ):%0A 
@@ -2353,210 +2353,8 @@
 hod.
-%0A        trigger: Trigger that decides when to take snapshot. If it is a tuple%0A            in the form %60%60%3Cint%3E, 'epoch'%60%60 or %60%60%3Cint%3E, 'iteration'%60%60, it is%0A            passed to :class:%60IntervalTrigger%60.
 %0A%0A  
@@ -2397,23 +2397,28 @@
 trigger=
-trigger
+(1, 'epoch')
 , priori

Revert "Initialize trigger of snapshot and snapshot_object."