Update docstrings and add doctests

This commit is contained in:
Eric Torres 2019-03-16 07:44:28 -07:00
parent 71f725981c
commit 50f042b2b0
3 changed files with 89 additions and 80 deletions

View File

@ -1,38 +1,45 @@
""" """
.. author:: Eric Torres .. author:: Eric Torres
.. module:: rbackup.hierarchy .. module:: rbackup.hierarchy.hierarchy
:synopsis: Classes for creating the backup hierarchy. :synopsis: Classes for creating the backup hierarchy.
""" """
import os.path
import datetime
import glob
# ========== Classes ==========
class Hierarchy: class Hierarchy:
"""A class for organizing the backup root hierarchy. """A class for organizing the backup root hierarchy."""
See README.rst for more details."""
def __init__(self, dest): def __init__(self, dest):
"""Default constructor for the Hierarchy class. """Default constructor for the Hierarchy class.
Example
-------
>>> hier = Hierarchy('/tmp')
>>> hier.path
'/tmp'
:param dest: the root directory of the backup hierarchy :param dest: the root directory of the backup hierarchy
:type dest: str, bytes :type dest: str, bytes
:raises: NotADirectoryError if dest is not a directory
""" """
if not os.path.isdir(dest):
raise NotADirectoryError(f"{dest}: no such directory")
self.dest = dest self.dest = dest
@property @property
def base_path(self): def path(self):
"""Return the base directory of this hierarchy. """Return the base directory of this hierarchy.
>>> h = Hierarchy('backup') Example
>>> h.base_path -------
>>> 'backup' >>> hier = Hierarchy('/tmp')
>>> hier.path
'/tmp'
:rtype: str :rtype: str
""" """
return self.dest return self.dest
# ========== Functions ==========
if __name__ == "__main__":
import doctest
doctest.testmod()

View File

@ -66,16 +66,16 @@ class Repository(Hierarchy):
Example Example
------- -------
* Assuming that backup/data has the snapshot dirs: >>> repo = Repository('/tmp')
* snapshot-one
* snapshot-two
>>> repo = Repository('backup')
>>> repo.snapshots >>> repo.snapshots
>>> ['backup/data/snapshot-one', 'backup/data/snapshot-two'] []
>>> repo.create_snapshot()
>>> repo.snapshots # doctest: +ELLIPSIS
[<...Snapshot ... at 0x...>]
:returns: the names of all snapshots in this repository sorted by :returns: the names of all snapshots in this repository sorted by
date date
:rtype: list :rtype: list of Snapshot objects
""" """
return self._snapshots return self._snapshots
@ -85,11 +85,15 @@ class Repository(Hierarchy):
Example Example
------- -------
>>> repo = Repository('backup') >>> repo = Repository('/tmp')
>>> repo.snapshots
>>> ['snapshot-one', 'snapshot-two']
>>> repo.curr_snapshot >>> repo.curr_snapshot
>>> Snapshot('backup/data/snapshot-two') >>> repo.snapshots
[]
>>> repo.create_snapshot()
>>> repo.snapshots # doctest: +ELLIPSIS
[<...Snapshot ... at 0x...>]
>>> repo.curr_snapshot # doctest: +ELLIPSIS
<...Snapshot ... at 0x...>
:rtype: Snapshot object :rtype: Snapshot object
""" """
@ -100,16 +104,19 @@ class Repository(Hierarchy):
): ):
"""Create a new snapshot in this repository. """Create a new snapshot in this repository.
This method is non-intrusive in that it will not
make any changes in the filesystem when called.
Example Example
------- -------
>>> repo = Repository('backup') directive ::= "#" "doctest":" ELLIPSIS
>>> repo = Repository('/tmp')
>>> repo.snapshots >>> repo.snapshots
>>> ['backup/data/snapshot-one', 'backup/data/snapshot-two'] []
>>> repo.curr_snapshot >>> repo.curr_snapshot
>>> Snapshot('backup/data/snapshot-two') >>> repo.create_snapshot()
>>> repo.create_snapshot('three') >>> repo.curr_snapshot # doctest: +ELLIPSIS
>>> repo.curr_snapshot <...Snapshot ... at 0x...>
>>> Snapshot('backup/data/snapshot-three')
:return: a new Snapshot object :return: a new Snapshot object
:rtype: Snapshot object :rtype: Snapshot object
@ -126,3 +133,10 @@ class Repository(Hierarchy):
for s in glob.glob(f"{self._snapshot_dir}/*") for s in glob.glob(f"{self._snapshot_dir}/*")
if os.path.isdir(s) if os.path.isdir(s)
] ]
# ========== Functions ==========
if __name__ == "__main__":
import doctest
doctest.testmod()

View File

@ -1,103 +1,84 @@
""" """
.. author:: Eric Torres .. author:: Eric Torres
.. module:: rbackup.hierarchy .. module:: r/tmp.hierarchy.snapshot
:synopsis: Classes for creating the backup hierarchy. :synopsis: Classes for creating the /tmp hierarchy.
""" """
import os.path import os.path
from rbackup.hierarchy.hierarchy import Hierarchy
class Snapshot(Hierarchy): class Snapshot(Hierarchy):
"""Hierarchy for a single snapshot. """Hierarchy for a single snapshot.
Attributes
Example ----------
------- * path (inherited from Hierarchy)
>>> repo = Repository('backup') * name
>>> snapshots = repo.snapshots * boot_dir
>>> prev = snapshots[-1] * etc_dir
>>> prev.name * home_dir
>>> 'snapshot-{prevtime}' * root_home_dir
>>> prev.home_dir
>>> 'backup/data/snapshot-{prevtime}/home'
>>> curr = repo.curr_snapshot
>>> curr.name
>>> 'snapshot-{utcnow}'
>>> curr.home_dir
>>> 'backup/data/snapshot-{utcnow}/home'
""" """
def __init__(self, path): def __init__(self, path):
"""Default constructor for the Snapshot class.""" """Default constructor for the Snapshot class."""
super().__init__(path) super().__init__(path)
@property
def path(self):
"""Return the canonical path of this snapshot.
Example
-------
>>> s = Snapshot('backup/data/snapshot-{utcprev}')
>>> s.name
>>> 'backup/data/snapshot-{utcprev}'
:rtype: str
"""
return self.base_path
@property @property
def name(self): def name(self):
"""Return the name of this snapshot. """Return the name of this snapshot.
Example Example
------- -------
>>> s = Snapshot('backup/data/snapshot-{utcprev}') >>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.name >>> s.name
>>> 'snapshot-{utcprev}' 'snapshot-new'
:rtype: str :rtype: str
""" """
return os.path.basename(self.base_path) return os.path.basename(self.path)
@property @property
def boot_dir(self): def boot_dir(self):
"""Retrieve the /boot backup directory of this snapshot. """Retrieve the /boot /tmp directory of this snapshot.
Example Example
------- -------
>>> s = Snapshot('backup/data/snapshot-{utcnow}') >>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.boot_dir >>> s.boot_dir
>>> 'backup/data/snapshot-{utcnow}/boot' '/tmp/data/snapshot-new/boot'
:rtype: str :rtype: str
""" """
return os.path.join(self.base_path, "boot") return os.path.join(self.path, "boot")
@property @property
def etc_dir(self): def etc_dir(self):
"""Retrieve the /etc backup directory of this snapshot. """Retrieve the /etc /tmp directory of this snapshot.
Example Example
------- -------
>>> s = Snapshot('backup/data/snapshot-{utcnow}') >>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.etc_dir >>> s.etc_dir
>>> 'backup/data/snapshot-{utcnow}/etc' '/tmp/data/snapshot-new/etc'
:rtype: str :rtype: str
""" """
return os.path.join(self.base_path, "etc") return os.path.join(self.path, "etc")
@property @property
def home_dir(self): def home_dir(self):
"""Retrieve the /home backup directory of this snapshot. """Retrieve the /home /tmp directory of this snapshot.
Example Example
------- -------
>>> s = Snapshot('backup/data/snapshot-{utcnow}') >>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.home_dir >>> s.home_dir
>>> 'backup/data/snapshot-{utcnow}/home' '/tmp/data/snapshot-new/home'
:rtype: str :rtype: str
""" """
return os.path.join(self.base_path, "home") return os.path.join(self.path, "home")
@property @property
def root_home_dir(self): def root_home_dir(self):
@ -105,10 +86,17 @@ class Snapshot(Hierarchy):
Example Example
------- -------
>>> s = Snapshot('backup/data/snapshot-{utcnow}') >>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.root_home_dir >>> s.root_home_dir
>>> 'backup/data/snapshot-{utcnow}/root' '/tmp/data/snapshot-new/root'
:rtype: str :rtype: str
""" """
return os.path.join(self.base_path, "root") return os.path.join(self.path, "root")
# ========== Functions ==========
if __name__ == "__main__":
import doctest
doctest.testmod()