sciop-cli 0.1.5__tar.gz → 0.2.0__tar.gz
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.
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/PKG-INFO +8 -2
- sciop_cli-0.2.0/README.md +5 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/pyproject.toml +7 -1
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/api.py +27 -14
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/cli/api.py +1 -8
- sciop_cli-0.2.0/src/sciop_cli/cli/client.py +121 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/cli/main.py +2 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/cli/torrent.py +130 -10
- sciop_cli-0.2.0/src/sciop_cli/clients/__init__.py +16 -0
- sciop_cli-0.2.0/src/sciop_cli/clients/base.py +76 -0
- sciop_cli-0.2.0/src/sciop_cli/clients/qbt.py +70 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/config.py +12 -42
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/models/sciop.py +7 -1
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/torrent.py +34 -11
- sciop_cli-0.2.0/src/sciop_cli/types.py +183 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/conftest.py +29 -1
- sciop_cli-0.2.0/tests/data/distributions/dlc_chrysler_ver02.json +1 -0
- sciop_cli-0.2.0/tests/data/distributions/nbu_arwen_ver01.json +1 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/test_api.py +80 -1
- sciop_cli-0.2.0/tests/test_client/__init__.py +0 -0
- sciop_cli-0.2.0/tests/test_client/test_client_config.py +52 -0
- sciop_cli-0.2.0/tests/test_config.py +98 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/test_torrent.py +35 -13
- sciop_cli-0.1.5/README.md +0 -3
- sciop_cli-0.1.5/src/sciop_cli/types.py +0 -29
- sciop_cli-0.1.5/tests/test_config.py +0 -55
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/LICENSE +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/__init__.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/__main__.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/cli/__init__.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/cli/manifest.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/const.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/data.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/exceptions.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/models/__init__.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/src/sciop_cli/schema.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/__init__.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/data/basic/part_1/subdir/part_1 +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/data/basic/part_2/subdir/part_2 +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/data/basic_hybrid.torrent +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/data/basic_v2.torrent +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/fixtures/__init__.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/fixtures/base.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/fixtures/fabricators.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/test_cli/__init__.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/test_cli/test_api.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/test_cli/test_torrent.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/test_data.py +0 -0
- {sciop_cli-0.1.5 → sciop_cli-0.2.0}/tests/test_temp.py +0 -0
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sciop-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A CLI for sciop
|
|
5
5
|
Author-Email: cosmicexplorer <dmcC2@hypnicjerk.ai>, sneakers-the-rat <sneakers-the-rat@protonmail.com>
|
|
6
6
|
License: EUPL-1.2
|
|
7
|
+
Project-URL: repository, https://codeberg.org/safeguarding/sciop-cli
|
|
8
|
+
Project-URL: homepage, https://sciop.net
|
|
7
9
|
Requires-Python: >=3.11
|
|
8
10
|
Requires-Dist: libtorrent>=2.0.11
|
|
9
11
|
Requires-Dist: click>=8.1.8
|
|
@@ -18,6 +20,8 @@ Requires-Dist: humanize>=4.12.2
|
|
|
18
20
|
Requires-Dist: pydantic-settings>=2.9.1
|
|
19
21
|
Requires-Dist: keyring>=25.6.0
|
|
20
22
|
Requires-Dist: pyyaml>=6.0.2
|
|
23
|
+
Requires-Dist: qbittorrent-api>=2025.5.0
|
|
24
|
+
Requires-Dist: typing-extensions>=4.10.0; python_version < "3.12"
|
|
21
25
|
Provides-Extra: mypy
|
|
22
26
|
Requires-Dist: mypy>=1.15; extra == "mypy"
|
|
23
27
|
Requires-Dist: types-tqdm>=4.67.0.20250401; extra == "mypy"
|
|
@@ -31,4 +35,6 @@ Description-Content-Type: text/markdown
|
|
|
31
35
|
|
|
32
36
|
# sciop-cli
|
|
33
37
|
|
|
34
|
-
a CLI for sciop!
|
|
38
|
+
a CLI for [sciop](https://sciop.net)!
|
|
39
|
+
|
|
40
|
+
(docs forthcoming sry)
|
|
@@ -20,14 +20,20 @@ dependencies = [
|
|
|
20
20
|
"pydantic-settings>=2.9.1",
|
|
21
21
|
"keyring>=25.6.0",
|
|
22
22
|
"pyyaml>=6.0.2",
|
|
23
|
+
"qbittorrent-api>=2025.5.0",
|
|
24
|
+
"typing-extensions>=4.10.0;python_version<\"3.12\"",
|
|
23
25
|
]
|
|
24
26
|
requires-python = ">=3.11"
|
|
25
27
|
readme = "README.md"
|
|
26
|
-
version = "0.
|
|
28
|
+
version = "0.2.0"
|
|
27
29
|
|
|
28
30
|
[project.license]
|
|
29
31
|
text = "EUPL-1.2"
|
|
30
32
|
|
|
33
|
+
[project.urls]
|
|
34
|
+
repository = "https://codeberg.org/safeguarding/sciop-cli"
|
|
35
|
+
homepage = "https://sciop.net"
|
|
36
|
+
|
|
31
37
|
[project.optional-dependencies]
|
|
32
38
|
mypy = [
|
|
33
39
|
"mypy>=1.15",
|
|
@@ -46,6 +46,7 @@ def api_call(
|
|
|
46
46
|
instance_url: str | None = None,
|
|
47
47
|
api_prefix: Literal["/api/v1", False] = "/api/v1",
|
|
48
48
|
autorefresh_token: bool = True,
|
|
49
|
+
timeout: float | None = None,
|
|
49
50
|
do_raise: bool = True,
|
|
50
51
|
) -> requests.Response:
|
|
51
52
|
"""
|
|
@@ -70,6 +71,7 @@ def api_call(
|
|
|
70
71
|
autorefresh_token (bool): If `True` (default), try and get a token if we have credentials.
|
|
71
72
|
Made `False` e.g. to avoid infinite recursion while logging in,
|
|
72
73
|
or if it is otherwise desirable to use only the preconfigured token.
|
|
74
|
+
timeout (float): Explicit request timeout. If `None`, use config.request_timeout
|
|
73
75
|
do_raise (bool): If `True` (default), raise on status codes >=400
|
|
74
76
|
"""
|
|
75
77
|
config = get_config()
|
|
@@ -84,10 +86,11 @@ def api_call(
|
|
|
84
86
|
instance_url=instance_url,
|
|
85
87
|
api_prefix=api_prefix,
|
|
86
88
|
autorefresh_token=autorefresh_token,
|
|
89
|
+
timeout=timeout,
|
|
87
90
|
)
|
|
88
91
|
|
|
89
92
|
res = requests.request(**request_kwargs)
|
|
90
|
-
if res.status_code
|
|
93
|
+
if res.status_code in (401, 403) and config.token and autorefresh_token:
|
|
91
94
|
# try once to get a new token if it just expired
|
|
92
95
|
token = get_token(force=True)
|
|
93
96
|
request_kwargs["headers"]["Authorization"] = f"Bearer {token}"
|
|
@@ -109,6 +112,7 @@ def _api_call_kwargs(
|
|
|
109
112
|
instance_url: str | None = None,
|
|
110
113
|
api_prefix: Literal["/api/v1", False] = "/api/v1",
|
|
111
114
|
autorefresh_token: bool = True,
|
|
115
|
+
timeout: float | None = None,
|
|
112
116
|
) -> dict:
|
|
113
117
|
default = default_headers(
|
|
114
118
|
instance_url=instance_url, headers=headers, autorefresh_token=autorefresh_token
|
|
@@ -126,7 +130,7 @@ def _api_call_kwargs(
|
|
|
126
130
|
"method": method,
|
|
127
131
|
"url": url,
|
|
128
132
|
"headers": merged_headers,
|
|
129
|
-
"timeout": config.request_timeout,
|
|
133
|
+
"timeout": timeout if timeout else config.request_timeout,
|
|
130
134
|
"files": files,
|
|
131
135
|
}
|
|
132
136
|
if data:
|
|
@@ -147,18 +151,27 @@ def default_headers(autorefresh_token: bool = True, **kwargs: Any) -> DefaultHea
|
|
|
147
151
|
return headers
|
|
148
152
|
|
|
149
153
|
|
|
150
|
-
def login(username: str, password: str, **kwargs: Any) -> str:
|
|
154
|
+
def login(username: str, password: str, save: bool = True, **kwargs: Any) -> str:
|
|
151
155
|
"""
|
|
152
|
-
Get a token from the API with a username and password
|
|
156
|
+
Get a token from the API with a username and password and saves all three to the local config.
|
|
153
157
|
|
|
154
158
|
Args:
|
|
159
|
+
save (bool): If ``True``, save username, password, and token to the local yaml config.
|
|
160
|
+
``False`` to just get the token without persisting anything
|
|
155
161
|
**kwargs: passed to [api_call][sciop_cli.api.api_call]
|
|
156
162
|
"""
|
|
157
163
|
res = api_call(
|
|
158
164
|
"login", data={"username": username, "password": password}, data_encoding="form", **kwargs
|
|
159
165
|
)
|
|
160
|
-
|
|
161
|
-
|
|
166
|
+
token_dict: Token = res.json()
|
|
167
|
+
token = token_dict["access_token"]
|
|
168
|
+
if save:
|
|
169
|
+
cfg = get_config()
|
|
170
|
+
cfg.username = username
|
|
171
|
+
cfg.password = SecretStr(password)
|
|
172
|
+
cfg.token = SecretStr(token)
|
|
173
|
+
set_config(cfg)
|
|
174
|
+
return token
|
|
162
175
|
|
|
163
176
|
|
|
164
177
|
def get_token(
|
|
@@ -199,10 +212,6 @@ def get_token(
|
|
|
199
212
|
|
|
200
213
|
if username and password and autorefresh_token:
|
|
201
214
|
token = login(username=username, password=password, autorefresh_token=False, **kwargs)
|
|
202
|
-
config.token = SecretStr(token)
|
|
203
|
-
config.username = username
|
|
204
|
-
config.password = SecretStr(password)
|
|
205
|
-
set_config(config)
|
|
206
215
|
return token
|
|
207
216
|
else:
|
|
208
217
|
return None
|
|
@@ -217,9 +226,9 @@ def claim_next(dataset: str) -> DatasetClaim | None:
|
|
|
217
226
|
return DatasetClaim(**claim)
|
|
218
227
|
|
|
219
228
|
|
|
220
|
-
def upload_torrent(path: Path) -> TorrentFile:
|
|
229
|
+
def upload_torrent(path: Path, timeout: float | None = 60.0) -> TorrentFile:
|
|
221
230
|
with open(path, "rb") as f:
|
|
222
|
-
response = api_call("torrents", files={"file": f})
|
|
231
|
+
response = api_call("torrents", files={"file": f}, timeout=timeout)
|
|
223
232
|
tf = cast(TorrentFile, response.json())
|
|
224
233
|
return TorrentFile(**tf)
|
|
225
234
|
|
|
@@ -231,6 +240,7 @@ def create_upload(
|
|
|
231
240
|
infohash: str | None = None,
|
|
232
241
|
method: str | Path | None = None,
|
|
233
242
|
description: str | Path | None = None,
|
|
243
|
+
timeout: float | None = 60.0,
|
|
234
244
|
) -> Upload:
|
|
235
245
|
"""
|
|
236
246
|
Create an upload for a dataset or dataset parts.
|
|
@@ -248,11 +258,14 @@ def create_upload(
|
|
|
248
258
|
description (str | Path | None): Any description of the contents of the upload
|
|
249
259
|
that are not contained within the dataset description, e.g. additional structure,
|
|
250
260
|
if this is some sub-component of a dataset, and so on
|
|
261
|
+
timeout (float | None): Timeout in seconds to wait for the upload to complete.
|
|
262
|
+
Defaults to 60 seconds since processing uploads can take some time,
|
|
263
|
+
explicitly pass `None` to use `config.request_timeout`
|
|
251
264
|
"""
|
|
252
265
|
if torrent_path and infohash or not (torrent_path or infohash):
|
|
253
266
|
raise ValueError("Must pass torrent path OR infohash")
|
|
254
267
|
elif torrent_path:
|
|
255
|
-
tf = upload_torrent(path=torrent_path)
|
|
268
|
+
tf = upload_torrent(path=torrent_path, timeout=timeout)
|
|
256
269
|
infohash = tf["v1_infohash"] if tf["version"] == "v1" else tf["v2_infohash"]
|
|
257
270
|
|
|
258
271
|
kwargs: dict[str, str | list[str] | None] = {
|
|
@@ -267,6 +280,6 @@ def create_upload(
|
|
|
267
280
|
if dataset_parts:
|
|
268
281
|
kwargs["part_slugs"] = dataset_parts
|
|
269
282
|
|
|
270
|
-
res = api_call("uploads", data=kwargs)
|
|
283
|
+
res = api_call("uploads", data=kwargs, timeout=timeout)
|
|
271
284
|
ul = cast(Upload, res.json())
|
|
272
285
|
return Upload(**ul)
|
|
@@ -2,14 +2,12 @@ import json as json_
|
|
|
2
2
|
from pathlib import Path
|
|
3
3
|
|
|
4
4
|
import click
|
|
5
|
-
from pydantic import SecretStr
|
|
6
5
|
from rich import print as pprint
|
|
7
6
|
from rich.progress import Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
|
|
8
7
|
|
|
9
8
|
from sciop_cli.api import claim_next as claim_next_api
|
|
10
9
|
from sciop_cli.api import create_upload
|
|
11
10
|
from sciop_cli.api import login as api_login
|
|
12
|
-
from sciop_cli.config import get_config, set_config
|
|
13
11
|
|
|
14
12
|
|
|
15
13
|
@click.command("login")
|
|
@@ -21,12 +19,7 @@ def login(username: str, password: str, instance_url: str | None = None) -> None
|
|
|
21
19
|
Login to sciop.net (or another instance), storing the password in a keyring
|
|
22
20
|
and getting a token
|
|
23
21
|
"""
|
|
24
|
-
|
|
25
|
-
cfg = get_config()
|
|
26
|
-
cfg.username = username
|
|
27
|
-
cfg.password = SecretStr(password)
|
|
28
|
-
cfg.token = SecretStr(token)
|
|
29
|
-
set_config(cfg)
|
|
22
|
+
api_login(username, password, instance_url=instance_url)
|
|
30
23
|
click.echo(f"Logged in as {username} ;)")
|
|
31
24
|
|
|
32
25
|
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Any, Callable
|
|
3
|
+
|
|
4
|
+
import click
|
|
5
|
+
from pydantic import TypeAdapter
|
|
6
|
+
from rich import print as rprint
|
|
7
|
+
|
|
8
|
+
from sciop_cli.clients import BittorrentClientAdapter, ClientConfigs
|
|
9
|
+
from sciop_cli.config import get_config, set_config
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def client_idx(f: Callable) -> Callable:
|
|
13
|
+
f = click.option(
|
|
14
|
+
"-c",
|
|
15
|
+
"--client",
|
|
16
|
+
type=int,
|
|
17
|
+
help="Index of client config to use.",
|
|
18
|
+
default=0,
|
|
19
|
+
show_default=True,
|
|
20
|
+
)(f)
|
|
21
|
+
return f
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@click.group("client")
|
|
25
|
+
def client() -> None:
|
|
26
|
+
"""
|
|
27
|
+
Interact with a bittorrent client
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
_available_clients = list(BittorrentClientAdapter.get_adapters().keys())
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@client.command("login")
|
|
35
|
+
@click.option(
|
|
36
|
+
"--client",
|
|
37
|
+
type=click.Choice(_available_clients),
|
|
38
|
+
prompt="Which type of client?",
|
|
39
|
+
required=True,
|
|
40
|
+
)
|
|
41
|
+
@click.option("-u", "--username", prompt=True, help="Username", required=True)
|
|
42
|
+
@click.option("-p", "--password", prompt=True, hide_input=True, help="Password", required=True)
|
|
43
|
+
@click.option(
|
|
44
|
+
"-h", "--host", prompt=True, help="Host (without protocol, like `localhost`)", required=True
|
|
45
|
+
)
|
|
46
|
+
@click.option("--port", prompt=True, help="Port", type=int, required=True)
|
|
47
|
+
def login(**kwargs: Any) -> None:
|
|
48
|
+
adapter = TypeAdapter(ClientConfigs)
|
|
49
|
+
client_config = adapter.validate_python(kwargs)
|
|
50
|
+
cfg = get_config()
|
|
51
|
+
cfg.clients.append(client_config)
|
|
52
|
+
set_config(cfg)
|
|
53
|
+
click.echo("Added new client configuration")
|
|
54
|
+
rprint(client_config.model_dump())
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@client.command("remove")
|
|
58
|
+
@click.option(
|
|
59
|
+
"-c",
|
|
60
|
+
"--client",
|
|
61
|
+
type=int,
|
|
62
|
+
help="Index of client config to remove.",
|
|
63
|
+
default=0,
|
|
64
|
+
show_default=True,
|
|
65
|
+
confirmation_prompt=True,
|
|
66
|
+
)
|
|
67
|
+
def remove(client: int = 0) -> None:
|
|
68
|
+
"""Remove a client configuration by index"""
|
|
69
|
+
cfg = get_config()
|
|
70
|
+
client_cfg = cfg.clients.pop(client)
|
|
71
|
+
set_config(cfg)
|
|
72
|
+
click.echo("Removed client configuration")
|
|
73
|
+
rprint(client_cfg.model_dump())
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@client.command("list")
|
|
77
|
+
def list_configs() -> None:
|
|
78
|
+
"""List client configurations"""
|
|
79
|
+
cfg = get_config()
|
|
80
|
+
if len(cfg.clients) == 0:
|
|
81
|
+
click.echo("No clients configured - use sciop-cli client login")
|
|
82
|
+
else:
|
|
83
|
+
rprint({i: client for i, client in enumerate(cfg.clients)})
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@client.command("list-clients")
|
|
87
|
+
def list_clients() -> None:
|
|
88
|
+
"""List available bittorrent client adapters"""
|
|
89
|
+
rprint(BittorrentClientAdapter.get_adapters())
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@client.command("add")
|
|
93
|
+
@client_idx
|
|
94
|
+
@click.option(
|
|
95
|
+
"-t",
|
|
96
|
+
"--torrent",
|
|
97
|
+
type=click.Path(exists=True),
|
|
98
|
+
required=True,
|
|
99
|
+
help="Path of a local torrent file to upload",
|
|
100
|
+
)
|
|
101
|
+
@click.option(
|
|
102
|
+
"-p",
|
|
103
|
+
"--path",
|
|
104
|
+
type=click.Path(),
|
|
105
|
+
required=True,
|
|
106
|
+
help="Path to save downloaded data/check existing data",
|
|
107
|
+
)
|
|
108
|
+
def add_torrent(torrent: Path, path: Path, client: int = 0) -> None:
|
|
109
|
+
"""Add a new torrent to the client"""
|
|
110
|
+
adapter = _get_adapter(client)
|
|
111
|
+
torrent = Path(torrent)
|
|
112
|
+
data_path = Path(path)
|
|
113
|
+
adapter.add_torrent_file(torrent_path=torrent, data_path=data_path)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _get_adapter(idx: int = 0) -> BittorrentClientAdapter:
|
|
117
|
+
cfg = get_config()
|
|
118
|
+
client_config = cfg.clients[idx]
|
|
119
|
+
adapter = client_config.make_adapter()
|
|
120
|
+
adapter.login()
|
|
121
|
+
return adapter
|
|
@@ -3,6 +3,7 @@ import logging
|
|
|
3
3
|
import click
|
|
4
4
|
|
|
5
5
|
from sciop_cli.cli.api import claims, login
|
|
6
|
+
from sciop_cli.cli.client import client
|
|
6
7
|
from sciop_cli.cli.manifest import manifest
|
|
7
8
|
from sciop_cli.cli.torrent import torrent
|
|
8
9
|
|
|
@@ -25,5 +26,6 @@ cli.add_command(login)
|
|
|
25
26
|
cli.add_command(claims)
|
|
26
27
|
|
|
27
28
|
# command groups
|
|
29
|
+
cli.add_command(client)
|
|
28
30
|
cli.add_command(torrent)
|
|
29
31
|
cli.add_command(manifest)
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
+
import json as json_
|
|
2
|
+
from collections import defaultdict
|
|
1
3
|
from pathlib import Path
|
|
2
4
|
from typing import Literal as L
|
|
3
5
|
from typing import cast
|
|
4
6
|
|
|
5
7
|
import click
|
|
6
8
|
import humanize
|
|
9
|
+
from pydantic import TypeAdapter, ValidationError
|
|
7
10
|
from rich import print
|
|
8
11
|
from rich.console import Group
|
|
9
12
|
from rich.panel import Panel
|
|
10
13
|
from rich.table import Table
|
|
14
|
+
from rich.text import Text
|
|
11
15
|
|
|
12
|
-
from sciop_cli.const import DEFAULT_TORRENT_CREATOR
|
|
16
|
+
from sciop_cli.const import DEFAULT_TORRENT_CREATOR, PIECE_SIZES
|
|
13
17
|
from sciop_cli.data import get_default_trackers
|
|
14
18
|
from sciop_cli.torrent import (
|
|
15
19
|
calculate_overhead,
|
|
@@ -194,25 +198,109 @@ def create(
|
|
|
194
198
|
help="Path to a directory or file to create .torrent from",
|
|
195
199
|
type=click.Path(exists=True),
|
|
196
200
|
)
|
|
197
|
-
|
|
201
|
+
@click.option(
|
|
202
|
+
"--json",
|
|
203
|
+
is_flag=True,
|
|
204
|
+
default=False,
|
|
205
|
+
help="Rather than evaluating a directory, "
|
|
206
|
+
"path is a json array of integers representing a theoretical distribution of file sizes",
|
|
207
|
+
)
|
|
208
|
+
@click.option(
|
|
209
|
+
"--view",
|
|
210
|
+
type=click.Choice(["optimal", "comparison"]),
|
|
211
|
+
default="optimal",
|
|
212
|
+
show_default=True,
|
|
213
|
+
help="What type of piece size table to show.\n"
|
|
214
|
+
"- optimal (default): the piece size that the optimizer would pick with default params "
|
|
215
|
+
"for each torrent version + associated stats\n"
|
|
216
|
+
"- comparison: the number of pieces (and stats) for each choice of piece size."
|
|
217
|
+
"usually used with --version to just show one at a time",
|
|
218
|
+
)
|
|
219
|
+
@click.option(
|
|
220
|
+
"-v",
|
|
221
|
+
"--version",
|
|
222
|
+
type=click.Choice(["v1", "v2", "hybrid", "all"]),
|
|
223
|
+
default="hybrid",
|
|
224
|
+
show_default=True,
|
|
225
|
+
help='When view == "comparison", which version to show',
|
|
226
|
+
)
|
|
227
|
+
@click.option(
|
|
228
|
+
"-w",
|
|
229
|
+
"--weight",
|
|
230
|
+
multiple=True,
|
|
231
|
+
help="version=key=val pairs of weight parameters, "
|
|
232
|
+
"can be passed multiple times for multiple kwargs. "
|
|
233
|
+
"E.g. --weight hybrid=overhead_weight=0.5",
|
|
234
|
+
)
|
|
235
|
+
def piece_size(
|
|
236
|
+
path: Path,
|
|
237
|
+
json: bool = False,
|
|
238
|
+
view: L["optimal", "comparison"] = "optimal",
|
|
239
|
+
version: L["v1", "v2", "hybrid", "all"] = "hybrid",
|
|
240
|
+
weight: list[str] | None = None,
|
|
241
|
+
) -> None:
|
|
198
242
|
"""
|
|
199
243
|
Show the optimal piece sizes calculated for each torrent version
|
|
200
244
|
for a given file or directory.
|
|
245
|
+
|
|
246
|
+
Any additional kwargs passed from the CLI are collected and passed on to the
|
|
247
|
+
"optimal piece size" calculation when view == optimal.
|
|
248
|
+
This allows you to see what different weights on the params might do.
|
|
201
249
|
"""
|
|
202
250
|
path = Path(path)
|
|
203
|
-
|
|
251
|
+
if json:
|
|
252
|
+
with open(path) as f:
|
|
253
|
+
sizes = json_.load(f)
|
|
254
|
+
adapter = TypeAdapter(list[int])
|
|
255
|
+
try:
|
|
256
|
+
adapter.validate_python(sizes)
|
|
257
|
+
except ValidationError as e:
|
|
258
|
+
raise ValueError("JSON sizes must be given as an array of integers") from e
|
|
259
|
+
# fake paths are fine, we are passing the sizes explicitly
|
|
260
|
+
paths = [Path(f"__{i}.notafile") for i in range(len(sizes))]
|
|
261
|
+
else:
|
|
262
|
+
paths = [path] if path.is_file() else list(iter_files(path))
|
|
263
|
+
sizes = [p.stat().st_size for p in paths]
|
|
264
|
+
|
|
265
|
+
if view == "optimal":
|
|
266
|
+
if weight:
|
|
267
|
+
weight_kwargs: dict[str, dict] = defaultdict(dict)
|
|
268
|
+
for arg in weight:
|
|
269
|
+
ver, key, val = arg.split("=")
|
|
270
|
+
weight_kwargs[ver][key] = float(val)
|
|
271
|
+
# weight_kwargs = cast(V1PieceParams | V2PieceParams | HybridPieceParams, weight_kwargs)
|
|
272
|
+
else:
|
|
273
|
+
weight_kwargs = {}
|
|
274
|
+
panel = _optional_piece_size(path, paths, sizes, weight_kwargs=weight_kwargs)
|
|
275
|
+
elif view == "comparison":
|
|
276
|
+
panel = _comparison_piece_size(path, paths, sizes, version)
|
|
277
|
+
else:
|
|
278
|
+
raise ValueError("unknown view type")
|
|
279
|
+
print(panel)
|
|
204
280
|
|
|
205
|
-
sizes = [p.stat().st_size for p in paths]
|
|
206
|
-
versions = ("v1", "v2", "hybrid")
|
|
207
|
-
piece_sizes = {v: find_optimal_piece_size(path=paths, sizes=sizes, version=v) for v in versions}
|
|
208
|
-
n_pieces = {v: calculate_total_pieces(sizes, piece_sizes[v], v) for v in versions}
|
|
209
|
-
hybrid_overhead = sum(calculate_overhead(sizes, piece_sizes["hybrid"]))
|
|
210
281
|
|
|
282
|
+
def _summary_table(paths: list[Path], sizes: list[int]) -> Table:
|
|
211
283
|
summary = Table(show_header=False)
|
|
212
284
|
summary.add_column("", style="bold magenta")
|
|
213
285
|
summary.add_column("")
|
|
214
286
|
summary.add_row("N Files", humanize.number.intcomma(len(paths)))
|
|
215
287
|
summary.add_row("Total size", humanize.naturalsize(sum(sizes), binary=True))
|
|
288
|
+
return summary
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _optional_piece_size(
|
|
292
|
+
path: Path, paths: list[Path], sizes: list[int], weight_kwargs: dict[str, dict] | None = None
|
|
293
|
+
) -> Panel:
|
|
294
|
+
versions = ("v1", "v2", "hybrid")
|
|
295
|
+
params = {} if weight_kwargs is None else weight_kwargs
|
|
296
|
+
piece_sizes = {
|
|
297
|
+
v: find_optimal_piece_size(path=paths, version=v, sizes=sizes, params=params.get(v))
|
|
298
|
+
for v in versions
|
|
299
|
+
}
|
|
300
|
+
n_pieces = {v: calculate_total_pieces(sizes, piece_sizes[v], v) for v in versions}
|
|
301
|
+
hybrid_overhead = sum(calculate_overhead(sizes, piece_sizes["hybrid"]))
|
|
302
|
+
|
|
303
|
+
summary = _summary_table(paths, sizes)
|
|
216
304
|
|
|
217
305
|
pieces = Table(title="Piece sizes")
|
|
218
306
|
pieces.add_column("Version")
|
|
@@ -226,5 +314,37 @@ def piece_size(path: Path) -> None:
|
|
|
226
314
|
row.append(humanize.naturalsize(hybrid_overhead, binary=True))
|
|
227
315
|
pieces.add_row(*row)
|
|
228
316
|
|
|
229
|
-
|
|
230
|
-
|
|
317
|
+
header = Text("Optimal piece sizes")
|
|
318
|
+
|
|
319
|
+
panel = Panel(Group(header, summary, pieces), title=str(path))
|
|
320
|
+
return panel
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def _comparison_piece_size(
|
|
324
|
+
path: Path, paths: list[Path], sizes: list[int], version: L["v1", "v2", "hybrid", "all"]
|
|
325
|
+
) -> Panel:
|
|
326
|
+
|
|
327
|
+
header = Text("Piece size comparison")
|
|
328
|
+
summary = _summary_table(paths, sizes)
|
|
329
|
+
if version == "all":
|
|
330
|
+
panels = [_comparison_table(paths, sizes, v) for v in ("v1", "v2", "hybrid")]
|
|
331
|
+
else:
|
|
332
|
+
panels = [_comparison_table(paths, sizes, version)]
|
|
333
|
+
|
|
334
|
+
panel = Panel(Group(header, summary, *panels), title=str(path))
|
|
335
|
+
return panel
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def _comparison_table(paths: list[Path], sizes: list[int], version: str) -> Table:
|
|
339
|
+
pieces = Table(title=version)
|
|
340
|
+
pieces.add_column("Piece Size")
|
|
341
|
+
pieces.add_column("N Pieces")
|
|
342
|
+
if version == "hybrid":
|
|
343
|
+
pieces.add_column("Padding Overhead")
|
|
344
|
+
|
|
345
|
+
for ps in PIECE_SIZES:
|
|
346
|
+
row = [str(ps), humanize.number.intcomma(calculate_total_pieces(sizes, ps, version))]
|
|
347
|
+
if version == "hybrid":
|
|
348
|
+
row.append(humanize.naturalsize(sum(calculate_overhead(sizes, ps)), binary=True))
|
|
349
|
+
pieces.add_row(*row)
|
|
350
|
+
return pieces
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from typing import Annotated, TypeAlias, Union
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from sciop_cli.clients.base import BittorrentClientAdapter, ClientConfig
|
|
6
|
+
from sciop_cli.clients.qbt import QBittorrentAdapter, QBittorrentConfig
|
|
7
|
+
|
|
8
|
+
ClientConfigs: TypeAlias = Annotated[Union[QBittorrentConfig,], Field(discriminator="client")]
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"BittorrentClientAdapter",
|
|
12
|
+
"ClientConfig",
|
|
13
|
+
"ClientConfigs",
|
|
14
|
+
"QBittorrentAdapter",
|
|
15
|
+
"QBittorrentConfig",
|
|
16
|
+
]
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from abc import abstractmethod
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any, ClassVar, Sequence
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ClientConfig(BaseModel):
|
|
10
|
+
"""Base class for client configurations"""
|
|
11
|
+
|
|
12
|
+
client: str
|
|
13
|
+
"""Set as a `Literal` in subclasses for union discrimination"""
|
|
14
|
+
host: str
|
|
15
|
+
port: int
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def make_adapter(self) -> "BittorrentClientAdapter": ...
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class BittorrentClientAdapter:
|
|
23
|
+
"""
|
|
24
|
+
ABC for adapters
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
name: ClassVar[str]
|
|
28
|
+
config: ClientConfig
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def get_adapters(cls) -> dict[str, type["BittorrentClientAdapter"]]:
|
|
32
|
+
"""Get available client adapters"""
|
|
33
|
+
return {subc.name: subc for subc in cls.__subclasses__()}
|
|
34
|
+
|
|
35
|
+
def login(self) -> Any:
|
|
36
|
+
"""
|
|
37
|
+
Login to the API, storing any token in the config
|
|
38
|
+
"""
|
|
39
|
+
raise NotImplementedError()
|
|
40
|
+
|
|
41
|
+
def add_torrent_file(self, torrent_path: Path, data_path: Path, **kwargs: Any) -> Any:
|
|
42
|
+
"""
|
|
43
|
+
Add a torrent file to the client
|
|
44
|
+
"""
|
|
45
|
+
raise NotImplementedError()
|
|
46
|
+
|
|
47
|
+
def get_config(self) -> dict:
|
|
48
|
+
"""
|
|
49
|
+
Get the client configuration
|
|
50
|
+
"""
|
|
51
|
+
raise NotImplementedError()
|
|
52
|
+
|
|
53
|
+
def set_config(self, config: dict) -> Any:
|
|
54
|
+
"""
|
|
55
|
+
Set some new configuration
|
|
56
|
+
"""
|
|
57
|
+
raise NotImplementedError()
|
|
58
|
+
|
|
59
|
+
def get_torrents(self, **kwargs: Any) -> Sequence[dict]:
|
|
60
|
+
"""Get information about all torrents"""
|
|
61
|
+
raise NotImplementedError()
|
|
62
|
+
|
|
63
|
+
def get_torrent_info(self, infohash: str, **kwargs: Any) -> dict:
|
|
64
|
+
"""Get information about a single torrent"""
|
|
65
|
+
raise NotImplementedError()
|
|
66
|
+
|
|
67
|
+
def start_torrent(self, infohash: str, **kwargs: Any) -> Any:
|
|
68
|
+
"""Start downloading/seeding a torrent"""
|
|
69
|
+
raise NotImplementedError()
|
|
70
|
+
|
|
71
|
+
def stop_torrent(self, infohash: str, **kwargs: Any) -> Any:
|
|
72
|
+
"""Stop downloading/seeding a torrent"""
|
|
73
|
+
raise NotImplementedError()
|
|
74
|
+
|
|
75
|
+
def add_webseeds(self, infohash: str, urls: list[str], **kwargs: Any) -> Any:
|
|
76
|
+
raise NotImplementedError()
|