SPPAS 4.22

https://sppas.org/

Module sppas.config

Class sppasTrash

Description

Utility manager of the Trash of SPPAS.

Constructor

Create a sppasTrash instance.

Create the trash directory if not already existing.

View Source
def __init__(self):
    """Create a sppasTrash instance.

    Create the trash directory if not already existing.

    """
    self._trash_dir = paths.trash
    if os.path.exists(self._trash_dir) is False:
        os.mkdir(self._trash_dir)

Public functions

is_empty

Return True if the trash is empty.

View Source
def is_empty(self):
    """Return True if the trash is empty."""
    return len(os.listdir(self._trash_dir)) == 0

do_empty

Empty the trash, i.e. definitely delete all files.

View Source
def do_empty(self):
    """Empty the trash, i.e. definitely delete all files."""
    for f in os.listdir(self._trash_dir):
        full_name = os.path.join(self._trash_dir, f)
        if os.path.isdir(full_name):
            shutil.rmtree(full_name)
        if os.path.isfile(full_name):
            os.remove(full_name)

put_file_into

Put a file into the trash.

Parameters
  • filename: (str)
Returns
  • Full name of the file in the trash
Raises

PermissionError

View Source
def put_file_into(self, filename):
    """Put a file into the trash.

        :param filename: (str)
        :returns: Full name of the file in the trash
        :raises: PermissionError

        """
    fn, fe = os.path.splitext(os.path.basename(filename))
    now = time.strftime('-%a-%d-%b-%Y_%H%M%S_0000', time.localtime())
    if os.path.exists(filename):
        trashname = os.path.join(self._trash_dir, fn + now + fe)
        try:
            shutil.move(filename, trashname)
        except PermissionError as e:
            logging.error("Can't put {} into the trash: {}".format(filename, str(e)))
            raise
    else:
        return ''
    return trashname

put_folder_into

Put a folder into the trash.

Parameters
  • folder: (str)
View Source
def put_folder_into(self, folder):
    """Put a folder into the trash.

        :param folder: (str)

        """
    now = time.strftime('-%a-%d-%b-%Y_%H%M%S_0000', time.localtime())
    shutil.move(folder, os.path.join(self._trash_dir, now))