'Ensure that this is a valid directory.
Will check if a directory exists, can optionally also enforce that
it be writable. It can optionally create it if necessary. Creation
will still fail if the path is rooted in a non-writable directory, or
if a file already exists at the given location.
:param is_writable: A boolean indicating that the directory should
not only exist, but also be writable.
:type is_writable: bool
:raises: :class:`MissingDirectoryException` if ``is_writable=False``
and the directory does not exist.
:raises: :class:`UnwritableDirectoryException` if ``is_writable=True``
and the directory is not wrtiable.
:raises: :class:`UncreatableDirectoryException` if ``is_writable=True``
the directory does not exist and it cannot be created'

def _ensure_valid(self, is_writable):

    if is_writable:
        try:
            os.makedirs(self.path)
        except OSError as e:
            if ((e.errno != errno.EEXIST) or (not os.path.isdir(self.path))):
                raise self.UncreatableDirectoryException(self.path, e)
    elif (not os.path.isdir(self.path)):
        raise self.MissingDirectoryException(self.path)
    if (is_writable and (not os.access(self.path, os.W_OK))):
        raise self.UnwritableDirectoryException(self.path)
