Remove prev_snapshot attribute and update docstrings

This commit is contained in:
Eric Torres 2019-03-15 11:40:09 -07:00
parent 16adea1a58
commit 71f725981c

View File

@ -8,28 +8,28 @@ import os.path
import datetime
import glob
from rbackup.hierarchy.hierarchy import Hierarchy
from rbackup.hierarchy.snapshot import Snapshot
class Repository(Hierarchy):
"""A class for interacting with a backup repository.
At the time of creation, the following is true about the class:
===============================================================
The current snapshot points to:
-------------------------------
* None if the repository is empty
* The most recent snapshot before running create_snapshot()
* A new, empty snapshot after running create_snapshot()
The previous snapshot points to:
--------------------------------
* None if the repository is empty
* The snapshot before the current snapshot if there is more than one
* The
Attributes
----------
* curr_snapshot - return either the most recent snapshot or
the new snapshot created after running create_snapshot()
* prev_snapshot - return the Snapshot of the previous backup
* path (inherited from Hierarchy)
* curr_snapshot - return either the most recent snapshot
before running create_snapshot() or the new snapshot
created after running create_snapshot()
* snapshots - return a list of snapshots stored in this repository
Methods
@ -52,10 +52,13 @@ class Repository(Hierarchy):
"""Default constructor for the Repository class."""
super().__init__(dest)
self._snapshot_dir = os.path.join(self.base_path, "data")
self._snapshot_dir = os.path.join(self.path, "data")
self.update_snapshots()
self._prev_snapshot = None
self._curr_snapshot = self._prev_snapshot
if self._snapshots == []:
self._curr_snapshot = None
else:
self._curr_snapshot = self._snapshots[-1]
@property
def snapshots(self):
@ -73,41 +76,8 @@ class Repository(Hierarchy):
:returns: the names of all snapshots in this repository sorted by
date
:rtype: list
:rtype: None if there are no snapshots in this Repository
"""
if self._snapshots == []:
return None
else:
return self._snapshots
def create_snapshot(self, name=datetime.datetime.utcnow().isoformat().replace(":", "-")):
"""
:return: a new Snapshot object
:rtype: Snapshot object
"""
path = os.path.join(self._snapshot_dir, f"snapshot-{name}")
self._curr_snapshot = Snapshot(path)
self._snapshots.append(_curr_snapshot)
def update_snapshots(self):
"""Update the list of snapshots in this repository."""
self._snapshots = [
Snapshot(s)
for s in glob.glob(f"{self._snapshot_dir}/*")
if os.path.isdir(s)
]
@property
def prev_snapshot(self):
"""Return the instance of the previous Snapshot.
:rtype: Snapshot object
"""
if self.snapshots is None:
return None
else:
return self.snapshots[-1]
return self._snapshots
@property
def curr_snapshot(self):
@ -116,9 +86,43 @@ class Repository(Hierarchy):
Example
-------
>>> repo = Repository('backup')
>>> repo.snapshots
>>> ['snapshot-one', 'snapshot-two']
>>> repo.curr_snapshot
>>> Snapshot('backup/data/snapshot-{utcnow}')
>>> Snapshot('backup/data/snapshot-two')
:rtype: Snapshot object
"""
return self._curr_snapshot
def create_snapshot(
self, name=datetime.datetime.utcnow().isoformat().replace(":", "-")
):
"""Create a new snapshot in this repository.
Example
-------
>>> repo = Repository('backup')
>>> repo.snapshots
>>> ['backup/data/snapshot-one', 'backup/data/snapshot-two']
>>> repo.curr_snapshot
>>> Snapshot('backup/data/snapshot-two')
>>> repo.create_snapshot('three')
>>> repo.curr_snapshot
>>> Snapshot('backup/data/snapshot-three')
:return: a new Snapshot object
:rtype: Snapshot object
"""
path = os.path.join(self._snapshot_dir, f"snapshot-{name}")
self._curr_snapshot = Snapshot(path)
self._snapshots.append(self._curr_snapshot)
def update_snapshots(self):
"""Update the list of snapshots in this repository."""
self._snapshots = [
Snapshot(s)
for s in glob.glob(f"{self._snapshot_dir}/*")
if os.path.isdir(s)
]