예제 #1
0
 def test_mkdir_and_rmdir_with_parents(self):
     abc = Path(self.d, "a", "b", "c")
     abc.mkdir(parents=True)
     assert abc.isdir()
     abc.rmdir(parents=True)
     assert not Path(self.d, "a").exists()
예제 #2
0
파일: tests.py 프로젝트: gkadillak/blog
 def tearDown(self):
     created_dir = Path(
         './portfolio/static/portfolio/media/2016_08_19_Test_headline')
     created_dir.rmdir()
예제 #3
0
class Base(object):
    """
    set and make directory
    
    Parameters
    ----------
    home : str
        set a directory as home
    """

    def __init__(self, home='.'):
        """
        set home directory

        Parameters
        ----------
        home : str
            set a directory as home

        Returns
        -------
        """
        self._home = Path(home).absolute()

    def __str__(self):
        return self.home

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self.home)

    # def __abs(self, string):
    #     return os.path.abspath(string)

    @property
    def home(self):
        return self._home.__str__()

    @home.setter
    def home(self, path):
        self._home = Path(path).absolute()

    def make_home(self, force=False):
        """
        make home directory

        Parameters
        ----------
        force : bool
            if True, if home exists and is a dir that
            containing contents, then delete contents
            in it, if exists and not a dir, remove it
            and make dir

        Returns
        -------

        """
        self.__mkdir(force)

    def __mkdir(self, force=False):
        if self._home.exists():
            if not self._home.isdir():
                if not force:
                    raise Exception('%s exists but is not a dir' % self.home)
                self._home.remove()
                self._home.mkdir()
            if force:
                self._home.rmtree()
                self._home.mkdir()
        else:
            self._home.mkdir(parents=True)

    def __rmdir(self, force=False):
        if self._home.exists():
            if not self._home.isdir():
                if not force:
                    raise Exception('%s exists but is not a dir' % self.home)
                self._home.remove()

            if force:
                self._home.rmtree()
            else:
                self._home.rmdir()

    def rm_home(self, force=False):
        """
        remove home directory

        Parameters
        ----------
        force : bool
            if True, if home exists and is a dir that
            containing contents, then delete it and
            it's contents, if exists and not a dir,
            remove then

        Returns
        -------

        """
        self.__rmdir(force)
예제 #4
0
 def test_mkdir_and_rmdir_with_parents(self):
     abc = Path(self.d, "a", "b", "c")
     abc.mkdir(parents=True)
     assert abc.isdir()
     abc.rmdir(parents=True)
     assert not Path(self.d, "a").exists()