Skip to content

config

CLI-wide config variables.

get_config_folder_path(tmp=False)

Gets the config folder path on current machine.

Parameters:

Name Type Description Default
tmp bool

A true/false flag that determines whether function should return temporary folder or base folder.

False

Raises:

Type Description
Exception

If the platform is not linux, darwin (macos) or windows.

Returns:

Type Description
str

The config folder path, or temporary folder path inside config folder.

Source code in dapla_team_cli/config.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def get_config_folder_path(tmp: bool = False) -> str:
    """Gets the config folder path on current machine.

    Args:
        tmp: A true/false flag that determines whether function should return temporary folder or base folder.

    Raises:
        Exception: If the platform is not linux, darwin (macos) or windows.

    Returns:
        The config folder path, or temporary folder path inside config folder.
    """
    if platform in ("linux", "darwin"):
        config_folder_path = Path.home() / ".config/dapla-team-cli"
    elif platform == "win32":
        username = os.getlogin()
        config_folder_path = Path(rf"C:\Users\{username}\AppData\dapla-team-cli")
    else:
        raise RuntimeError("Unknown platform. The CLI only supports Unix and Windows based platforms.")

    if not os.path.exists(config_folder_path):
        os.makedirs(config_folder_path)

    if tmp:
        tmp_path = config_folder_path / "tmp"

        if not os.path.exists(tmp_path):
            os.makedirs(tmp_path)

        return str(tmp_path)

    return str(config_folder_path)

get_version_file_content()

Reads a txt-file, fetching a stored timestamp.

:return The content in the txt file.

Source code in dapla_team_cli/config.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def get_version_file_content() -> str:
    """Reads a txt-file, fetching a stored timestamp.

    :return The content in the txt file.
    """
    txt_path: Path = Path(get_config_folder_path()) / "last_update_check.txt"
    content: str = ""

    if os.path.exists(txt_path) is False:
        f = open(txt_path, "w")
        f.close()

    with open(txt_path) as f:
        content = f.read()

    return content

has_recently_checked_version()

Checks if the current version should be controlled by accessing a text file.

Inside the file a timestamp for the last performed version check is stored.

The function takes use of the constant TIMESTAMP_DIFFERENCE. This is the time threshold in milliseconds before a new check should be performed.

:return: A boolean variable. False if a check should be performed, or True if not.

Source code in dapla_team_cli/config.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def has_recently_checked_version() -> bool:
    """Checks if the current version should be controlled by accessing a text file.

    Inside the file a timestamp for the last performed version check is stored.

    The function takes use of the constant TIMESTAMP_DIFFERENCE. This is the time threshold in milliseconds
    before a new check should be performed.

    :return: A boolean variable. False if a check should be performed, or True if not.
    """
    txt_path: Path = Path(get_config_folder_path()) / "last_update_check.txt"
    content: str = get_version_file_content()
    epoch_time: int = 0

    if content != "":
        epoch_time = int(content)

    curr_epoch_time: int = math.floor(time.time())

    if (curr_epoch_time - epoch_time) > TIMESTAMP_DIFFERENCE:
        with open(txt_path, "w") as f:
            f.write(str(curr_epoch_time))

        return False

    return True

version_cb(value)

Prints the current version of dapla-team-cli.

Source code in dapla_team_cli/config.py
65
66
67
68
69
def version_cb(value: bool) -> None:
    """Prints the current version of dapla-team-cli."""
    if value:
        print(f"dapla-team-cli {__version__}")
        raise typer.Exit()

warn_if_newer_version()

Prints a warning if there exists a newer version on PyPi.

Source code in dapla_team_cli/config.py
118
119
120
121
122
123
124
125
126
127
128
129
130
def warn_if_newer_version() -> None:
    """Prints a warning if there exists a newer version on PyPi."""
    if not has_recently_checked_version():
        installed_version = LooseVersion(__version__)
        pypi_url = "https://pypi.org/pypi/dapla-team-cli/json"
        response = requests.get(pypi_url, timeout=10)
        latest_version = max(LooseVersion(ver) for ver in response.json()["releases"].keys())

        if latest_version > installed_version:
            console.print(
                f"""⚠️[yellow]  WARNING: There is a newer version available.
                Current version={installed_version}, latest version={latest_version}"""
            )