Skip to content

Dir

Dir

Bases: BaseModel

Utility class for a directory.

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

    path: str

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

        Args:
            dir_path (click.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.
        """
        if dir_path is None:
            path = os.getcwd()
        else:
            path = click.format_filename(str(dir_path))

        if not os.path.exists(path):
            if create:
                os.makedirs(path)
            else:
                raise FileNotFoundError(f"Directory {path} not found!")

        super().__init__(path=path)

__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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(self, dir_path: click.Path | None = None, create: bool = False):
    """Constructor.

    Args:
        dir_path (click.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.
    """
    if dir_path is None:
        path = os.getcwd()
    else:
        path = click.format_filename(str(dir_path))

    if not os.path.exists(path):
        if create:
            os.makedirs(path)
        else:
            raise FileNotFoundError(f"Directory {path} not found!")

    super().__init__(path=path)