src.fairreckitlib.core.io.io_delete

This module contains IO functions that delete something on the disk and dispatch an IO event.

Functions:

delete_dir: delete a directory, recursively, with IO event dispatching.
delete_file: delete a file from the disk with IO event dispatching.

This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)

 1"""This module contains IO functions that delete something on the disk and dispatch an IO event.
 2
 3Functions:
 4
 5    delete_dir: delete a directory, recursively, with IO event dispatching.
 6    delete_file: delete a file from the disk with IO event dispatching.
 7
 8This program has been developed by students from the bachelor Computer Science at
 9Utrecht University within the Software Project course.
10© Copyright Utrecht University (Department of Information and Computing Sciences)
11"""
12
13import os
14
15from ..events.event_dispatcher import EventDispatcher
16from .event_io import ON_REMOVE_DIR, ON_REMOVE_FILE, DirEventArgs, FileEventArgs
17
18
19def delete_dir(directory: str, event_dispatcher: EventDispatcher) -> None:
20    """Delete the specified directory.
21
22    This functions removes all the files and directories that are present
23    in the specified directory path (recursively).
24    The IO event is dispatched after the directory and/or files are deleted.
25
26    Args:
27        directory: the path to the directory to delete.
28        event_dispatcher: used to dispatch the IO event.
29    """
30    for file in os.listdir(directory):
31        file_name = os.fsdecode(file)
32        file_path = os.path.join(directory, file_name)
33        if os.path.isdir(file_path):
34            delete_dir(file_path, event_dispatcher)
35        else:
36            delete_file(file_path, event_dispatcher)
37
38    os.rmdir(directory)
39    event_dispatcher.dispatch(DirEventArgs(ON_REMOVE_DIR, directory))
40
41
42def delete_file(file_path: str, event_dispatcher: EventDispatcher) -> None:
43    """Delete the specified file.
44
45    The IO event is dispatched after the file is deleted.
46
47    Args:
48        file_path: the path to the file to delete.
49        event_dispatcher: used to dispatch the IO event.
50    """
51    os.remove(file_path)
52    event_dispatcher.dispatch(FileEventArgs(ON_REMOVE_FILE, file_path))
def delete_dir( directory: str, event_dispatcher: src.fairreckitlib.core.events.event_dispatcher.EventDispatcher) -> None:
20def delete_dir(directory: str, event_dispatcher: EventDispatcher) -> None:
21    """Delete the specified directory.
22
23    This functions removes all the files and directories that are present
24    in the specified directory path (recursively).
25    The IO event is dispatched after the directory and/or files are deleted.
26
27    Args:
28        directory: the path to the directory to delete.
29        event_dispatcher: used to dispatch the IO event.
30    """
31    for file in os.listdir(directory):
32        file_name = os.fsdecode(file)
33        file_path = os.path.join(directory, file_name)
34        if os.path.isdir(file_path):
35            delete_dir(file_path, event_dispatcher)
36        else:
37            delete_file(file_path, event_dispatcher)
38
39    os.rmdir(directory)
40    event_dispatcher.dispatch(DirEventArgs(ON_REMOVE_DIR, directory))

Delete the specified directory.

This functions removes all the files and directories that are present in the specified directory path (recursively). The IO event is dispatched after the directory and/or files are deleted.

Args: directory: the path to the directory to delete. event_dispatcher: used to dispatch the IO event.

def delete_file( file_path: str, event_dispatcher: src.fairreckitlib.core.events.event_dispatcher.EventDispatcher) -> None:
43def delete_file(file_path: str, event_dispatcher: EventDispatcher) -> None:
44    """Delete the specified file.
45
46    The IO event is dispatched after the file is deleted.
47
48    Args:
49        file_path: the path to the file to delete.
50        event_dispatcher: used to dispatch the IO event.
51    """
52    os.remove(file_path)
53    event_dispatcher.dispatch(FileEventArgs(ON_REMOVE_FILE, file_path))

Delete the specified file.

The IO event is dispatched after the file is deleted.

Args: file_path: the path to the file to delete. event_dispatcher: used to dispatch the IO event.