Add snapshot_dir attribute to Repository

This commit is contained in:
Eric Torres 2019-03-20 00:22:48 -07:00
parent ccccdb6b77
commit 6fbf6a34de

View File

@ -59,15 +59,16 @@ class Repository(Hierarchy):
"""Default constructor for the Repository class.""" """Default constructor for the Repository class."""
super().__init__(dest) super().__init__(dest)
self._snapshot_dir = self.path / "data" self._snapshot_index = 0
self._snapshots = [ self._snapshots = [
Snapshot(s) for s in self._snapshot_dir.glob("*") if s.is_dir() Snapshot(s) for s in self.snapshot_dir.glob("*") if s.is_dir()
] ]
self._snapshots.sort()
if self._snapshots == []: try:
self._curr_snapshot = None
else:
self._curr_snapshot = self._snapshots[-1] self._curr_snapshot = self._snapshots[-1]
except IndexError:
self._curr_snapshot = None
def __len__(self): def __len__(self):
"""Return the number of snapshots in this Repository.""" """Return the number of snapshots in this Repository."""
@ -78,18 +79,32 @@ class Repository(Hierarchy):
return self.snapshots[position] return self.snapshots[position]
def __iter__(self): def __iter__(self):
self.snapshot_index = 0
return self return self
def __next__(self): def __next__(self):
"""Return the next Snapshot in this Repository.""" """Return the next Snapshot in this Repository."""
try: try:
result = self.snapshots[self.snapshot_index] result = self.snapshots[self._snapshot_index]
self.snapshot_index += 1 self._snapshot_index += 1
return result return result
except IndexError: except IndexError:
raise StopIteration raise StopIteration
@property
def snapshot_dir(self):
"""Return the directory in this Repository in which snapshots
are stored.
Example
-------
>>> repo = Repository('/tmp')
>>> repo.snapshot_dir # doctest: +ELLIPSIS
PosixPath('/tmp/data')
:rtype: path-like object
"""
return self.path / "data"
@property @property
def snapshots(self): def snapshots(self):
"""Return a list of snapshots stored in this Repository. """Return a list of snapshots stored in this Repository.
@ -144,10 +159,7 @@ class Repository(Hierarchy):
:rtype: Snapshot object :rtype: Snapshot object
""" """
try: return self._curr_snapshot
return self.snapshots[-1]
except IndexError:
return None
def create_snapshot( def create_snapshot(
self, name=datetime.datetime.utcnow().isoformat().replace(":", "-") self, name=datetime.datetime.utcnow().isoformat().replace(":", "-")
@ -170,7 +182,7 @@ class Repository(Hierarchy):
:return: a new Snapshot object :return: a new Snapshot object
""" """
syslog.debug("Creating snapshot") syslog.debug("Creating snapshot")
path = self._snapshot_dir / f"snapshot-{name}" path = self.snapshot_dir / f"snapshot-{name}"
self._curr_snapshot = Snapshot(path) self._curr_snapshot = Snapshot(path)
self.snapshots.append(self._curr_snapshot) self.snapshots.append(self._curr_snapshot)