deweypy 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deweypy/__init__.py +0 -0
- deweypy/__main__.py +5 -0
- deweypy/app.py +5 -0
- deweypy/auth.py +77 -0
- deweypy/context.py +156 -0
- deweypy/display.py +5 -0
- deweypy/download/__init__.py +10 -0
- deweypy/download/asynchronous.py +1026 -0
- deweypy/download/settings.py +93 -0
- deweypy/download/speed_testing.py +97 -0
- deweypy/download/speedy.py +97 -0
- deweypy/download/synchronous.py +447 -0
- deweypy/download/types.py +113 -0
- deweypy/main.py +228 -0
- deweypy/py.typed +0 -0
- deweypy-0.0.1.dist-info/METADATA +60 -0
- deweypy-0.0.1.dist-info/RECORD +19 -0
- deweypy-0.0.1.dist-info/WHEEL +4 -0
- deweypy-0.0.1.dist-info/entry_points.txt +3 -0
deweypy/__init__.py
ADDED
|
File without changes
|
deweypy/__main__.py
ADDED
deweypy/app.py
ADDED
deweypy/auth.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from typing import Literal
|
|
6
|
+
|
|
7
|
+
from deweypy.context import main_context
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def set_api_key(
|
|
11
|
+
api_key: str,
|
|
12
|
+
*,
|
|
13
|
+
api_key_source: Literal[
|
|
14
|
+
"cli_args", "cli_fallback", "environment", "manually_set"
|
|
15
|
+
] = "manually_set",
|
|
16
|
+
) -> None:
|
|
17
|
+
if not api_key:
|
|
18
|
+
raise ValueError("API key cannot be empty.")
|
|
19
|
+
main_context.api_key = api_key.strip()
|
|
20
|
+
main_context.api_key_source = api_key_source
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def resolve_api_key(
|
|
24
|
+
*,
|
|
25
|
+
potentially_provided_value: str | None,
|
|
26
|
+
callback_if_missing: Callable[[], str],
|
|
27
|
+
invalid_exception_class: type[RuntimeError],
|
|
28
|
+
) -> tuple[str, Literal["provided", "environment", "callback"]]:
|
|
29
|
+
# If `potentially_provided_value` is non-empty, then use it.
|
|
30
|
+
if (cli_api_key := potentially_provided_value) is not None:
|
|
31
|
+
sanity_check_api_key_value(
|
|
32
|
+
cli_api_key,
|
|
33
|
+
invalid_exception_class=invalid_exception_class,
|
|
34
|
+
empty_message="The provided API Key cannot be empty.",
|
|
35
|
+
too_short_message="The provided API Key does not appear to be valid (sanity check, too short).",
|
|
36
|
+
)
|
|
37
|
+
return (cli_api_key, "provided")
|
|
38
|
+
|
|
39
|
+
# Otherwise, check the `DEWEY_API_KEY` environment variable and use that if
|
|
40
|
+
# it's present.
|
|
41
|
+
if env_apikey := os.environ.get("DEWEY_API_KEY"):
|
|
42
|
+
sanity_check_api_key_value(
|
|
43
|
+
env_apikey,
|
|
44
|
+
invalid_exception_class=invalid_exception_class,
|
|
45
|
+
empty_message=(
|
|
46
|
+
"The provided API Key from the environment variable DEWEY_API_KEY "
|
|
47
|
+
"cannot be empty."
|
|
48
|
+
),
|
|
49
|
+
too_short_message=(
|
|
50
|
+
"The provided API Key from the environment variable DEWEY_API_KEY does "
|
|
51
|
+
"not appear to be valid (sanity check, too short)."
|
|
52
|
+
),
|
|
53
|
+
)
|
|
54
|
+
return (env_apikey, "environment")
|
|
55
|
+
|
|
56
|
+
callback_api_key = callback_if_missing()
|
|
57
|
+
sanity_check_api_key_value(
|
|
58
|
+
callback_api_key, invalid_exception_class=invalid_exception_class
|
|
59
|
+
)
|
|
60
|
+
assert callback_api_key, "Post-condition"
|
|
61
|
+
return (callback_api_key, "callback")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def sanity_check_api_key_value(
|
|
65
|
+
api_key: str | None,
|
|
66
|
+
*,
|
|
67
|
+
invalid_exception_class: type[RuntimeError] | type[ValueError] = ValueError,
|
|
68
|
+
empty_message: str = "The API Key cannot be empty.",
|
|
69
|
+
too_short_message: str = "The API Key does not appear to be valid (sanity check, too short).",
|
|
70
|
+
) -> str:
|
|
71
|
+
if api_key in ("", None):
|
|
72
|
+
raise invalid_exception_class(empty_message)
|
|
73
|
+
assert api_key is not None, "Post-condition"
|
|
74
|
+
assert api_key != "", "Post-condition"
|
|
75
|
+
if len(api_key) <= 18:
|
|
76
|
+
raise invalid_exception_class(too_short_message)
|
|
77
|
+
return api_key
|
deweypy/context.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from functools import lru_cache
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from textwrap import dedent
|
|
7
|
+
from typing import Any, Literal, NoReturn
|
|
8
|
+
|
|
9
|
+
from deweypy.display import ELLIPSIS_CHAR
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(kw_only=True)
|
|
13
|
+
class MainContext:
|
|
14
|
+
entrypoint: Literal["cli", "other", "unknown"] = "unknown"
|
|
15
|
+
|
|
16
|
+
_api_key: str = field(repr=False, default="")
|
|
17
|
+
api_key_source: Literal[
|
|
18
|
+
"cli_args", "cli_fallback", "environment", "manually_set", "unknown"
|
|
19
|
+
] = "unknown"
|
|
20
|
+
api_key_repr_preview: str = field(init=False, repr=True)
|
|
21
|
+
|
|
22
|
+
_download_directory: Path | None = field(default=None)
|
|
23
|
+
download_directory_repr_preview: str = field(init=False, repr=True)
|
|
24
|
+
download_directory_source: Literal[
|
|
25
|
+
"cli_args", "cli_fallback", "environment", "manually_set", "unknown"
|
|
26
|
+
] = "unknown"
|
|
27
|
+
|
|
28
|
+
def __post_init__(self):
|
|
29
|
+
self._set_api_key_repr_preview()
|
|
30
|
+
self._set_download_directory_repr_preview()
|
|
31
|
+
|
|
32
|
+
def __setattr__(self, name: str, value: Any):
|
|
33
|
+
parent_return_value = super().__setattr__(name, value)
|
|
34
|
+
if name == "_api_key":
|
|
35
|
+
self._set_api_key_repr_preview()
|
|
36
|
+
elif name == "_download_directory":
|
|
37
|
+
self._set_download_directory_repr_preview()
|
|
38
|
+
return parent_return_value
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def api_key(self) -> str:
|
|
42
|
+
if self._api_key:
|
|
43
|
+
return self._api_key
|
|
44
|
+
auth_module = _get_auth_module()
|
|
45
|
+
resolved_api_key = auth_module.resolve_api_key(
|
|
46
|
+
potentially_provided_value=self._api_key,
|
|
47
|
+
callback_if_missing=self._missing_api_key_callback,
|
|
48
|
+
invalid_exception_class=RuntimeError,
|
|
49
|
+
)
|
|
50
|
+
self._api_key = resolved_api_key
|
|
51
|
+
auth_module.sanity_check_api_key_value(self._api_key)
|
|
52
|
+
return resolved_api_key
|
|
53
|
+
|
|
54
|
+
@api_key.setter
|
|
55
|
+
def api_key(self, value: str):
|
|
56
|
+
self._api_key = value
|
|
57
|
+
self._set_api_key_repr_preview()
|
|
58
|
+
|
|
59
|
+
def _set_api_key_repr_preview(self):
|
|
60
|
+
if self._api_key in ("", None):
|
|
61
|
+
self.api_key_repr_preview = "not_set"
|
|
62
|
+
return
|
|
63
|
+
auth_module = _get_auth_module()
|
|
64
|
+
auth_module.sanity_check_api_key_value(self._api_key)
|
|
65
|
+
preview_value = self._api_key[:4] + ELLIPSIS_CHAR + self.api_key[-3:]
|
|
66
|
+
self.api_key_repr_preview = preview_value
|
|
67
|
+
|
|
68
|
+
@staticmethod
|
|
69
|
+
def _missing_api_key_callback() -> NoReturn:
|
|
70
|
+
raise RuntimeError(
|
|
71
|
+
dedent(
|
|
72
|
+
"""
|
|
73
|
+
The API Key is not set. You can set it via one of three ways:
|
|
74
|
+
1. If using `deweypy` directly from the shell/command line, you can
|
|
75
|
+
provide the --api-key option to set it.
|
|
76
|
+
2. Set the `DEWEY_API_KEY` environment variable.
|
|
77
|
+
3. If using `deweypy` as a Python library/module, you can call
|
|
78
|
+
`deweypy.auth.set_api_key()` with the API key as the argument.
|
|
79
|
+
For example:
|
|
80
|
+
```
|
|
81
|
+
import deweypy.auth
|
|
82
|
+
deweypy.auth.set_api_key("your_api_key_value...")
|
|
83
|
+
```
|
|
84
|
+
"""
|
|
85
|
+
).strip()
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def download_directory(self) -> Path:
|
|
90
|
+
if self._download_directory:
|
|
91
|
+
return self._download_directory
|
|
92
|
+
download_module = _get_download_module()
|
|
93
|
+
resolved_download_directory = download_module.resolve_download_directory(
|
|
94
|
+
potentially_provided_value=self._download_directory,
|
|
95
|
+
callback_if_missing=self._missing_download_directory_callback,
|
|
96
|
+
invalid_exception_class=RuntimeError,
|
|
97
|
+
)
|
|
98
|
+
self._download_directory = resolved_download_directory
|
|
99
|
+
download_module.sanity_check_download_directory_value(self._download_directory)
|
|
100
|
+
return resolved_download_directory
|
|
101
|
+
|
|
102
|
+
@download_directory.setter
|
|
103
|
+
def download_directory(self, value: Path):
|
|
104
|
+
self._download_directory = value
|
|
105
|
+
self._set_download_directory_repr_preview()
|
|
106
|
+
|
|
107
|
+
def _set_download_directory_repr_preview(self):
|
|
108
|
+
if self._download_directory in ("", None):
|
|
109
|
+
self.download_directory_repr_preview = "not_set"
|
|
110
|
+
return
|
|
111
|
+
download_module = _get_download_module()
|
|
112
|
+
download_module.sanity_check_download_directory_value(self._download_directory)
|
|
113
|
+
assert self._download_directory is not None, "Post-condition"
|
|
114
|
+
preview_value = self._download_directory.as_posix()
|
|
115
|
+
self.download_directory_repr_preview = preview_value
|
|
116
|
+
|
|
117
|
+
@staticmethod
|
|
118
|
+
def _missing_download_directory_callback() -> NoReturn:
|
|
119
|
+
raise RuntimeError(
|
|
120
|
+
dedent(
|
|
121
|
+
"""
|
|
122
|
+
The Download Directory is not set. You can set it via one of three ways:
|
|
123
|
+
1. If using `deweypy` directly from the shell/command line, you
|
|
124
|
+
can provide the --download-directory option to set it.
|
|
125
|
+
2. Set the `DEWEY_DOWNLOAD_DIRECTORY` environment variable.
|
|
126
|
+
3. If using `deweypy` as a Python library/module, you can call
|
|
127
|
+
`deweypy.downloads.set_download_directory()` with the
|
|
128
|
+
download directory as the argument. For example:
|
|
129
|
+
```
|
|
130
|
+
import deweypy.downloads
|
|
131
|
+
deweypy.downloads.set_download_directory("your_download_directory_value...")
|
|
132
|
+
```
|
|
133
|
+
"""
|
|
134
|
+
).strip()
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
main_context = MainContext()
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def set_entrypoint(entrypoint: Literal["cli", "other"]):
|
|
142
|
+
main_context.entrypoint = entrypoint
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@lru_cache(1)
|
|
146
|
+
def _get_auth_module():
|
|
147
|
+
from deweypy import auth as auth_module
|
|
148
|
+
|
|
149
|
+
return auth_module
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@lru_cache(1)
|
|
153
|
+
def _get_download_module():
|
|
154
|
+
from deweypy import download as download_module
|
|
155
|
+
|
|
156
|
+
return download_module
|
deweypy/display.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from .asynchronous import AsyncDatasetDownloader, async_api_request, make_async_client
|
|
4
|
+
from .settings import (
|
|
5
|
+
resolve_download_directory,
|
|
6
|
+
sanity_check_download_directory_value,
|
|
7
|
+
set_download_directory,
|
|
8
|
+
)
|
|
9
|
+
from .speedy import run_speedy_download
|
|
10
|
+
from .synchronous import DatasetDownloader, api_request, make_client
|