doctable.util.tempfolder

View Source
import os
import shutil
import pathlib
#import dataclasses

#@dataclasses.dataclass
class TempFolder:
    def __init__(self, folder: str, make_folder:bool=True):
        self.path = pathlib.Path(folder)

        if make_folder:
            self.mkdir()

    def mkdir(self):
        ''' Make the directory if it does not exist.
        '''
        if not os.path.exists(self.path):
            os.mkdir(self.path)

    def rmtree(self, ignore_errors=True, **kwargs):
        ''' Remove all files recursively in the folder.
        '''
        if os.path.exists(self.path):
            shutil.rmtree(self.path, ignore_errors=ignore_errors, **kwargs)
    
    def rglob(self, p='*', **kwargs):
        ''' Get list of filenames in the directory.
        '''
        return list(self.path.rglob(p, **kwargs))

    def joinpath(self, fname):
        ''' Join path with provided fname.
        '''
        return self.path.joinpath(fname)
    
    def __del__(self):
        ''' Delete the directory.
        '''
        self.rmtree()

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.rmtree()
#   class TempFolder:
View Source
class TempFolder:
    def __init__(self, folder: str, make_folder:bool=True):
        self.path = pathlib.Path(folder)

        if make_folder:
            self.mkdir()

    def mkdir(self):
        ''' Make the directory if it does not exist.
        '''
        if not os.path.exists(self.path):
            os.mkdir(self.path)

    def rmtree(self, ignore_errors=True, **kwargs):
        ''' Remove all files recursively in the folder.
        '''
        if os.path.exists(self.path):
            shutil.rmtree(self.path, ignore_errors=ignore_errors, **kwargs)
    
    def rglob(self, p='*', **kwargs):
        ''' Get list of filenames in the directory.
        '''
        return list(self.path.rglob(p, **kwargs))

    def joinpath(self, fname):
        ''' Join path with provided fname.
        '''
        return self.path.joinpath(fname)
    
    def __del__(self):
        ''' Delete the directory.
        '''
        self.rmtree()

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.rmtree()
#   TempFolder(folder: str, make_folder: bool = True)
View Source
    def __init__(self, folder: str, make_folder:bool=True):
        self.path = pathlib.Path(folder)

        if make_folder:
            self.mkdir()
#   def mkdir(self):
View Source
    def mkdir(self):
        ''' Make the directory if it does not exist.
        '''
        if not os.path.exists(self.path):
            os.mkdir(self.path)

Make the directory if it does not exist.

#   def rmtree(self, ignore_errors=True, **kwargs):
View Source
    def rmtree(self, ignore_errors=True, **kwargs):
        ''' Remove all files recursively in the folder.
        '''
        if os.path.exists(self.path):
            shutil.rmtree(self.path, ignore_errors=ignore_errors, **kwargs)

Remove all files recursively in the folder.

#   def rglob(self, p='*', **kwargs):
View Source
    def rglob(self, p='*', **kwargs):
        ''' Get list of filenames in the directory.
        '''
        return list(self.path.rglob(p, **kwargs))

Get list of filenames in the directory.

#   def joinpath(self, fname):
View Source
    def joinpath(self, fname):
        ''' Join path with provided fname.
        '''
        return self.path.joinpath(fname)

Join path with provided fname.