Skip to content

Dir

Dir

Bases: BaseModel

Utility class for a directory.

Source code in src/task2md/util/dir.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Dir(BaseModel):
    """Utility class for a directory."""

    path: str

    def __init__(self, dir_path: Path | None = None, create: bool = False):
        """Constructor.

        Args:
            dir_path (Path | None): Directory file path. \
                Current working directory if None.
            create (bool, optional): Create directory if doesn't exist. \
                Defaults to False.

        Raises:
            FileNotFoundError: When create is False and directory not found.
        """
        directory = Path.cwd() if dir_path is None else dir_path

        if not directory.exists():
            if not create:
                raise FileNotFoundError(f"Directory {directory} not found!")
            directory.mkdir(parents=True)

        super().__init__(path=str(directory))

__init__(dir_path=None, create=False)

Constructor.

Parameters:

Name Type Description Default
dir_path Path | None

Directory file path. Current working directory if None.

None
create bool

Create directory if doesn't exist. Defaults to False.

False

Raises:

Type Description
FileNotFoundError

When create is False and directory not found.

Source code in src/task2md/util/dir.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def __init__(self, dir_path: Path | None = None, create: bool = False):
    """Constructor.

    Args:
        dir_path (Path | None): Directory file path. \
            Current working directory if None.
        create (bool, optional): Create directory if doesn't exist. \
            Defaults to False.

    Raises:
        FileNotFoundError: When create is False and directory not found.
    """
    directory = Path.cwd() if dir_path is None else dir_path

    if not directory.exists():
        if not create:
            raise FileNotFoundError(f"Directory {directory} not found!")
        directory.mkdir(parents=True)

    super().__init__(path=str(directory))