itqan-cli 0.1.0rc1__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.
- itqan_cli-0.1.0rc1/.gitignore +9 -0
- itqan_cli-0.1.0rc1/LICENSE +21 -0
- itqan_cli-0.1.0rc1/PKG-INFO +108 -0
- itqan_cli-0.1.0rc1/README.md +78 -0
- itqan_cli-0.1.0rc1/RELEASING.md +37 -0
- itqan_cli-0.1.0rc1/pyproject.toml +73 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/__init__.py +1 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/__main__.py +5 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/client.py +233 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/downloader.py +220 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/exceptions.py +69 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/init_template.py +109 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/lockfile.py +285 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/main.py +293 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/manifest.py +274 -0
- itqan_cli-0.1.0rc1/src/itqan_cli/semver.py +205 -0
- itqan_cli-0.1.0rc1/tests/__init__.py +1 -0
- itqan_cli-0.1.0rc1/tests/test_assets_dir.py +144 -0
- itqan_cli-0.1.0rc1/tests/test_cli_integration.py +154 -0
- itqan_cli-0.1.0rc1/tests/test_client.py +133 -0
- itqan_cli-0.1.0rc1/tests/test_downloader.py +76 -0
- itqan_cli-0.1.0rc1/tests/test_init.py +199 -0
- itqan_cli-0.1.0rc1/tests/test_languages.py +207 -0
- itqan_cli-0.1.0rc1/tests/test_lockfile.py +89 -0
- itqan_cli-0.1.0rc1/tests/test_manifest.py +94 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Itqan Community
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: itqan-cli
|
|
3
|
+
Version: 0.1.0rc1
|
|
4
|
+
Summary: Itqan Quranic Asset Manager: install and pin Itqan Quranic assets in your project.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Itqan-community/itqan-cli
|
|
6
|
+
Project-URL: Repository, https://github.com/Itqan-community/itqan-cli
|
|
7
|
+
Project-URL: Issues, https://github.com/Itqan-community/itqan-cli/issues
|
|
8
|
+
Project-URL: Manifest format, https://github.com/Itqan-community/cms-backend/blob/main/docs/ASSET_MANIFEST.md
|
|
9
|
+
Author: Itqan
|
|
10
|
+
Author-email: Hassaan Alansary <hassaanalansary@yahoo.com>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: assets,cli,itqan,mushaf,package-manager,quran,quranic,tafsir
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Religion
|
|
24
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
25
|
+
Requires-Python: >=3.11
|
|
26
|
+
Requires-Dist: click>=8.1
|
|
27
|
+
Requires-Dist: pyyaml>=6.0
|
|
28
|
+
Requires-Dist: requests>=2.31
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# itqan-cli
|
|
32
|
+
|
|
33
|
+
**Itqan Quranic Asset Manager**
|
|
34
|
+
|
|
35
|
+
Install and pin [Itqan](https://cms.itqan.dev) Quranic assets (mushafs, tafsirs, translations,
|
|
36
|
+
recitations, fonts) in your project, the way a package manager does for code.
|
|
37
|
+
|
|
38
|
+
You declare the assets you use and the versions you accept in `itqan-assets.yaml`. The CLI
|
|
39
|
+
resolves them against the Itqan registry, downloads them, and records the exact versions in
|
|
40
|
+
`itqan-assets.lock`, so teammates and CI get the same files.
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
Requires Python 3.11+.
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pipx install itqan-cli # or: uv tool install itqan-cli
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Quick start
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
itqan init # writes itqan-assets.yaml with a few real assets as examples
|
|
54
|
+
itqan install # downloads them and writes itqan-assets.lock
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Commit both `itqan-assets.yaml` and `itqan-assets.lock`. The downloaded files are reproducible
|
|
58
|
+
from the lockfile, so add the assets folder to `.gitignore` if you like.
|
|
59
|
+
|
|
60
|
+
## The manifest
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
schema_version: 1
|
|
64
|
+
|
|
65
|
+
# Folder the assets are downloaded into, relative to this file (default: assets).
|
|
66
|
+
assets_dir: "public/quran"
|
|
67
|
+
|
|
68
|
+
assets:
|
|
69
|
+
# Source language of the asset.
|
|
70
|
+
tafsir-jalalayn:
|
|
71
|
+
version: "^2.0.0" # any 2.x from 2.0.0 on
|
|
72
|
+
|
|
73
|
+
# The same asset in English: its own entry, pointing at the asset.
|
|
74
|
+
tafsir-jalalayn-en:
|
|
75
|
+
asset: tafsir-jalalayn
|
|
76
|
+
language: en
|
|
77
|
+
version: "~1.3" # any 1.3.x
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Each language of an asset has its own version history, so each gets its own entry, constraint,
|
|
81
|
+
lock entry and folder (`public/quran/tafsir-jalalayn-en/`). The full format is specified in
|
|
82
|
+
[ASSET_MANIFEST.md](https://github.com/Itqan-community/cms-backend/blob/main/docs/ASSET_MANIFEST.md).
|
|
83
|
+
|
|
84
|
+
## Commands
|
|
85
|
+
|
|
86
|
+
| Command | What it does |
|
|
87
|
+
|---|---|
|
|
88
|
+
| `itqan init` | Create `itqan-assets.yaml` from the registry's catalog (commented examples when offline). `--assets-dir`, `--force`. |
|
|
89
|
+
| `itqan install` | Resolve, download and lock. Uses the lockfile's exact versions when it is up to date; `--force` re-resolves. |
|
|
90
|
+
| `itqan sync` | Same as `install`. |
|
|
91
|
+
| `itqan --version` | Print the CLI version. |
|
|
92
|
+
|
|
93
|
+
## Configuration
|
|
94
|
+
|
|
95
|
+
| Option | Environment variable | Default |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| `--registry-url` | `ITQAN_REGISTRY_URL` | `https://api.cms.itqan.dev` |
|
|
98
|
+
| `--api-key` | `ITQAN_API_KEY` | none; needed only for assets that require access approval |
|
|
99
|
+
|
|
100
|
+
## Development
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
uv sync
|
|
104
|
+
uv run pytest
|
|
105
|
+
uv build
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Releases are published from version tags; see [RELEASING.md](RELEASING.md).
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# itqan-cli
|
|
2
|
+
|
|
3
|
+
**Itqan Quranic Asset Manager**
|
|
4
|
+
|
|
5
|
+
Install and pin [Itqan](https://cms.itqan.dev) Quranic assets (mushafs, tafsirs, translations,
|
|
6
|
+
recitations, fonts) in your project, the way a package manager does for code.
|
|
7
|
+
|
|
8
|
+
You declare the assets you use and the versions you accept in `itqan-assets.yaml`. The CLI
|
|
9
|
+
resolves them against the Itqan registry, downloads them, and records the exact versions in
|
|
10
|
+
`itqan-assets.lock`, so teammates and CI get the same files.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
Requires Python 3.11+.
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pipx install itqan-cli # or: uv tool install itqan-cli
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick start
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
itqan init # writes itqan-assets.yaml with a few real assets as examples
|
|
24
|
+
itqan install # downloads them and writes itqan-assets.lock
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Commit both `itqan-assets.yaml` and `itqan-assets.lock`. The downloaded files are reproducible
|
|
28
|
+
from the lockfile, so add the assets folder to `.gitignore` if you like.
|
|
29
|
+
|
|
30
|
+
## The manifest
|
|
31
|
+
|
|
32
|
+
```yaml
|
|
33
|
+
schema_version: 1
|
|
34
|
+
|
|
35
|
+
# Folder the assets are downloaded into, relative to this file (default: assets).
|
|
36
|
+
assets_dir: "public/quran"
|
|
37
|
+
|
|
38
|
+
assets:
|
|
39
|
+
# Source language of the asset.
|
|
40
|
+
tafsir-jalalayn:
|
|
41
|
+
version: "^2.0.0" # any 2.x from 2.0.0 on
|
|
42
|
+
|
|
43
|
+
# The same asset in English: its own entry, pointing at the asset.
|
|
44
|
+
tafsir-jalalayn-en:
|
|
45
|
+
asset: tafsir-jalalayn
|
|
46
|
+
language: en
|
|
47
|
+
version: "~1.3" # any 1.3.x
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Each language of an asset has its own version history, so each gets its own entry, constraint,
|
|
51
|
+
lock entry and folder (`public/quran/tafsir-jalalayn-en/`). The full format is specified in
|
|
52
|
+
[ASSET_MANIFEST.md](https://github.com/Itqan-community/cms-backend/blob/main/docs/ASSET_MANIFEST.md).
|
|
53
|
+
|
|
54
|
+
## Commands
|
|
55
|
+
|
|
56
|
+
| Command | What it does |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `itqan init` | Create `itqan-assets.yaml` from the registry's catalog (commented examples when offline). `--assets-dir`, `--force`. |
|
|
59
|
+
| `itqan install` | Resolve, download and lock. Uses the lockfile's exact versions when it is up to date; `--force` re-resolves. |
|
|
60
|
+
| `itqan sync` | Same as `install`. |
|
|
61
|
+
| `itqan --version` | Print the CLI version. |
|
|
62
|
+
|
|
63
|
+
## Configuration
|
|
64
|
+
|
|
65
|
+
| Option | Environment variable | Default |
|
|
66
|
+
|---|---|---|
|
|
67
|
+
| `--registry-url` | `ITQAN_REGISTRY_URL` | `https://api.cms.itqan.dev` |
|
|
68
|
+
| `--api-key` | `ITQAN_API_KEY` | none; needed only for assets that require access approval |
|
|
69
|
+
|
|
70
|
+
## Development
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
uv sync
|
|
74
|
+
uv run pytest
|
|
75
|
+
uv build
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Releases are published from version tags; see [RELEASING.md](RELEASING.md).
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Releasing
|
|
2
|
+
|
|
3
|
+
Releases are published by [`.github/workflows/release.yml`](.github/workflows/release.yml) when a
|
|
4
|
+
version tag is pushed. It runs the tests, builds the package, publishes it to
|
|
5
|
+
[PyPI](https://pypi.org/project/itqan-cli/) and creates the GitHub Release.
|
|
6
|
+
|
|
7
|
+
## Steps
|
|
8
|
+
|
|
9
|
+
1. Bump `version` in `pyproject.toml` (e.g. `0.2.0`, or `0.2.0rc1` for a pre-release) and run
|
|
10
|
+
`uv lock`, on a branch with a PR.
|
|
11
|
+
2. Once it's merged, tag `main` with the same version and push the tag:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
git switch main && git pull
|
|
15
|
+
git tag v0.2.0
|
|
16
|
+
git push origin v0.2.0
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
3. Approve the `pypi` deployment in the workflow run if the environment requires approval.
|
|
20
|
+
|
|
21
|
+
If the tag doesn't match `pyproject.toml`, the workflow stops before publishing anything. Delete
|
|
22
|
+
the tag (`git push --delete origin v0.2.0 && git tag -d v0.2.0`), fix, and tag again.
|
|
23
|
+
|
|
24
|
+
Versions with `a`, `b` or `rc` (e.g. `0.2.0rc1`) are marked as pre-releases. `pipx install
|
|
25
|
+
itqan-cli` skips them unless asked for explicitly: `pipx install itqan-cli==0.2.0rc1`.
|
|
26
|
+
|
|
27
|
+
## One-time setup
|
|
28
|
+
|
|
29
|
+
- **PyPI:** under the project (or, before the first release, under *Your account →
|
|
30
|
+
Publishing → Add a pending publisher*), add a GitHub trusted publisher:
|
|
31
|
+
- Project name: `itqan-cli`
|
|
32
|
+
- Owner: `Itqan-community`
|
|
33
|
+
- Repository: `itqan-cli`
|
|
34
|
+
- Workflow: `release.yml`
|
|
35
|
+
- Environment: `pypi`
|
|
36
|
+
- **GitHub:** create an environment named `pypi` under *Settings → Environments*. Adding required
|
|
37
|
+
reviewers makes every release wait for a maintainer's approval.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "itqan-cli"
|
|
3
|
+
version = "0.1.0rc1"
|
|
4
|
+
description = "Itqan Quranic Asset Manager: install and pin Itqan Quranic assets in your project."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "Hassaan Alansary", email = "hassaanalansary@yahoo.com" },
|
|
11
|
+
{ name = "Itqan" },
|
|
12
|
+
]
|
|
13
|
+
keywords = ["itqan", "quran", "quranic", "tafsir", "mushaf", "assets", "package-manager", "cli"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Environment :: Console",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: Religion",
|
|
25
|
+
"Topic :: Software Development :: Build Tools",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
"click>=8.1",
|
|
29
|
+
"pyyaml>=6.0",
|
|
30
|
+
"requests>=2.31",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
itqan = "itqan_cli.main:cli"
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/Itqan-community/itqan-cli"
|
|
38
|
+
Repository = "https://github.com/Itqan-community/itqan-cli"
|
|
39
|
+
Issues = "https://github.com/Itqan-community/itqan-cli/issues"
|
|
40
|
+
"Manifest format" = "https://github.com/Itqan-community/cms-backend/blob/main/docs/ASSET_MANIFEST.md"
|
|
41
|
+
|
|
42
|
+
[dependency-groups]
|
|
43
|
+
dev = [
|
|
44
|
+
"pytest>=8",
|
|
45
|
+
"responses>=0.25",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[build-system]
|
|
49
|
+
requires = ["hatchling"]
|
|
50
|
+
build-backend = "hatchling.build"
|
|
51
|
+
|
|
52
|
+
[tool.hatch.build.targets.sdist]
|
|
53
|
+
# The source archive is for building and testing the package, not for
|
|
54
|
+
# developing it: leave out CI, editor and lock files.
|
|
55
|
+
exclude = [".github", ".pre-commit-config.yaml", "uv.lock", ".idea"]
|
|
56
|
+
|
|
57
|
+
[tool.pytest.ini_options]
|
|
58
|
+
testpaths = ["tests"]
|
|
59
|
+
|
|
60
|
+
[tool.black]
|
|
61
|
+
line-length = 120
|
|
62
|
+
target-version = ["py311"]
|
|
63
|
+
|
|
64
|
+
[tool.ruff]
|
|
65
|
+
line-length = 120
|
|
66
|
+
target-version = "py311"
|
|
67
|
+
|
|
68
|
+
[tool.isort]
|
|
69
|
+
profile = "black"
|
|
70
|
+
line_length = 120
|
|
71
|
+
known_first_party = ["itqan_cli"]
|
|
72
|
+
force_sort_within_sections = true
|
|
73
|
+
combine_as_imports = true
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Itqan Package Manager CLI."""
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"""Package Registry HTTP client for resolving manifests and obtaining download URLs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
7
|
+
|
|
8
|
+
import requests
|
|
9
|
+
|
|
10
|
+
from itqan_cli.exceptions import RegistryApiError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True, slots=True)
|
|
14
|
+
class ResolvedAssetPayload:
|
|
15
|
+
slug: str
|
|
16
|
+
asset_version_id: int
|
|
17
|
+
resolved_version: str
|
|
18
|
+
asset_name: str
|
|
19
|
+
download_url: str | None
|
|
20
|
+
publisher_id: int | None = None
|
|
21
|
+
publisher_name: str | None = None
|
|
22
|
+
name: str = ""
|
|
23
|
+
language: str | None = None
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def entry_name(self) -> str:
|
|
27
|
+
"""The manifest entry this result answers (the slug unless the entry set `asset`)."""
|
|
28
|
+
return self.name or self.slug
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True, slots=True)
|
|
32
|
+
class CatalogLanguage:
|
|
33
|
+
language: str
|
|
34
|
+
is_source: bool
|
|
35
|
+
latest_version: str
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True, slots=True)
|
|
39
|
+
class CatalogPackage:
|
|
40
|
+
"""One installable asset from GET /packages/."""
|
|
41
|
+
|
|
42
|
+
slug: str
|
|
43
|
+
name: str
|
|
44
|
+
category: str
|
|
45
|
+
is_open_access: bool
|
|
46
|
+
languages: tuple[CatalogLanguage, ...]
|
|
47
|
+
publisher_name: str | None = None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# The public API (not the cms.itqan.dev website, which serves the web app).
|
|
51
|
+
DEFAULT_REGISTRY_URL = "https://api.cms.itqan.dev"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _cli_version() -> str:
|
|
55
|
+
try:
|
|
56
|
+
return version("itqan-cli")
|
|
57
|
+
except PackageNotFoundError: # running from a source checkout that isn't installed
|
|
58
|
+
return "unknown"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class RegistryClient:
|
|
62
|
+
"""HTTP Client for Itqan Package Registry API."""
|
|
63
|
+
|
|
64
|
+
def __init__(
|
|
65
|
+
self,
|
|
66
|
+
base_url: str = DEFAULT_REGISTRY_URL,
|
|
67
|
+
api_key: str | None = None,
|
|
68
|
+
timeout: float = 30.0,
|
|
69
|
+
session: requests.Session | None = None,
|
|
70
|
+
) -> None:
|
|
71
|
+
self.base_url = base_url.rstrip("/")
|
|
72
|
+
self.api_key = api_key
|
|
73
|
+
self.timeout = timeout
|
|
74
|
+
self.session = session or requests.Session()
|
|
75
|
+
|
|
76
|
+
# Security: reject HTTP for non-loopback URLs when an API key is provided.
|
|
77
|
+
# Sending an API key over plain HTTP exposes it to network eavesdroppers.
|
|
78
|
+
if api_key:
|
|
79
|
+
parsed = __import__("urllib.parse", fromlist=["urlparse"]).urlparse(self.base_url)
|
|
80
|
+
is_loopback = parsed.hostname in ("localhost", "127.0.0.1", "::1")
|
|
81
|
+
if parsed.scheme == "http" and not is_loopback:
|
|
82
|
+
raise RegistryApiError(
|
|
83
|
+
"Refusing to send API key over an insecure HTTP connection. "
|
|
84
|
+
"Use HTTPS or a loopback address for development."
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def _headers(self) -> dict[str, str]:
|
|
88
|
+
headers = {
|
|
89
|
+
"Accept": "application/json",
|
|
90
|
+
"Content-Type": "application/json",
|
|
91
|
+
"User-Agent": f"itqan-cli/{_cli_version()}",
|
|
92
|
+
}
|
|
93
|
+
if self.api_key:
|
|
94
|
+
headers["X-API-Key"] = self.api_key
|
|
95
|
+
return headers
|
|
96
|
+
|
|
97
|
+
def list_packages(self, *, open_access: bool | None = None, page_size: int = 50) -> list[CatalogPackage]:
|
|
98
|
+
"""Call GET /packages/ for the first page of installable assets.
|
|
99
|
+
|
|
100
|
+
Raises:
|
|
101
|
+
RegistryApiError when the registry can't be reached or answers badly.
|
|
102
|
+
"""
|
|
103
|
+
params: dict[str, str] = {"page_size": str(page_size)}
|
|
104
|
+
if open_access is not None:
|
|
105
|
+
params["open_access"] = "true" if open_access else "false"
|
|
106
|
+
try:
|
|
107
|
+
response = self.session.get(
|
|
108
|
+
f"{self.base_url}/packages/", params=params, headers=self._headers(), timeout=self.timeout
|
|
109
|
+
)
|
|
110
|
+
except requests.RequestException as exc:
|
|
111
|
+
raise RegistryApiError(f"Failed to connect to package registry at {self.base_url}: {exc}") from exc
|
|
112
|
+
if response.status_code != 200:
|
|
113
|
+
raise RegistryApiError(
|
|
114
|
+
f"Registry catalog request failed with status {response.status_code}",
|
|
115
|
+
status_code=response.status_code,
|
|
116
|
+
)
|
|
117
|
+
try:
|
|
118
|
+
return [
|
|
119
|
+
CatalogPackage(
|
|
120
|
+
slug=item["slug"],
|
|
121
|
+
name=item["name"],
|
|
122
|
+
category=item["category"],
|
|
123
|
+
is_open_access=item["is_open_access"],
|
|
124
|
+
publisher_name=item.get("publisher_name"),
|
|
125
|
+
languages=tuple(
|
|
126
|
+
CatalogLanguage(
|
|
127
|
+
language=lang["language"],
|
|
128
|
+
is_source=lang["is_source"],
|
|
129
|
+
latest_version=lang["latest_version"],
|
|
130
|
+
)
|
|
131
|
+
for lang in item["languages"]
|
|
132
|
+
),
|
|
133
|
+
)
|
|
134
|
+
for item in response.json()["results"]
|
|
135
|
+
]
|
|
136
|
+
except (ValueError, KeyError, TypeError) as exc:
|
|
137
|
+
raise RegistryApiError(f"Failed to parse registry catalog response: {exc}") from exc
|
|
138
|
+
|
|
139
|
+
def resolve_manifest(self, assets: dict[str, str | dict[str, str]]) -> list[ResolvedAssetPayload]:
|
|
140
|
+
"""Call POST /packages/resolve/manifest/ to resolve manifest constraints.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
assets: dict of entry name -> constraint string (e.g. {"quran": "^2.1.0"}),
|
|
144
|
+
or -> request object ({"version": ..., "asset": ..., "language": ...})
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
list of ResolvedAssetPayload
|
|
148
|
+
|
|
149
|
+
Raises:
|
|
150
|
+
RegistryApiError on any failure.
|
|
151
|
+
"""
|
|
152
|
+
url = f"{self.base_url}/packages/resolve/manifest/"
|
|
153
|
+
payload = {"assets": assets}
|
|
154
|
+
|
|
155
|
+
try:
|
|
156
|
+
response = self.session.post(
|
|
157
|
+
url,
|
|
158
|
+
json=payload,
|
|
159
|
+
headers=self._headers(),
|
|
160
|
+
timeout=self.timeout,
|
|
161
|
+
)
|
|
162
|
+
except requests.RequestException as exc:
|
|
163
|
+
raise RegistryApiError(f"Failed to connect to package registry at {self.base_url}: {exc}") from exc
|
|
164
|
+
|
|
165
|
+
if response.status_code == 200:
|
|
166
|
+
try:
|
|
167
|
+
data = response.json()
|
|
168
|
+
# Require "results" to be explicitly present as a list.
|
|
169
|
+
# A missing key means the response is malformed — do not silently
|
|
170
|
+
# continue with zero assets, as that would allow _run_install to
|
|
171
|
+
# report success while writing an empty lockfile.
|
|
172
|
+
if "results" not in data or not isinstance(data["results"], list):
|
|
173
|
+
raise RegistryApiError(
|
|
174
|
+
"Registry returned a 200 response but 'results' field is missing or not a list. "
|
|
175
|
+
"Cannot proceed with installation."
|
|
176
|
+
)
|
|
177
|
+
results_raw = data["results"]
|
|
178
|
+
requested_slugs = set(assets.keys())
|
|
179
|
+
returned_slugs = {item.get("name") or item["slug"] for item in results_raw if isinstance(item, dict)}
|
|
180
|
+
missing = requested_slugs - returned_slugs
|
|
181
|
+
if missing:
|
|
182
|
+
raise RegistryApiError(
|
|
183
|
+
f"Registry resolved {len(returned_slugs)} of {len(requested_slugs)} requested assets. "
|
|
184
|
+
f"Missing: {', '.join(sorted(missing))}"
|
|
185
|
+
)
|
|
186
|
+
return [
|
|
187
|
+
ResolvedAssetPayload(
|
|
188
|
+
slug=item["slug"],
|
|
189
|
+
asset_version_id=item["asset_version_id"],
|
|
190
|
+
resolved_version=item["resolved_version"],
|
|
191
|
+
asset_name=item["asset_name"],
|
|
192
|
+
download_url=item.get("download_url"),
|
|
193
|
+
publisher_id=item.get("publisher_id"),
|
|
194
|
+
publisher_name=item.get("publisher_name"),
|
|
195
|
+
name=item.get("name") or item["slug"],
|
|
196
|
+
language=item.get("language"),
|
|
197
|
+
)
|
|
198
|
+
for item in results_raw
|
|
199
|
+
]
|
|
200
|
+
except RegistryApiError:
|
|
201
|
+
raise
|
|
202
|
+
except Exception as exc:
|
|
203
|
+
raise RegistryApiError(f"Failed to parse registry API response: {exc}") from exc
|
|
204
|
+
|
|
205
|
+
# Handle API Error responses
|
|
206
|
+
status_code = response.status_code
|
|
207
|
+
error_name: str | None = None
|
|
208
|
+
message = f"Registry request failed with status {status_code}"
|
|
209
|
+
|
|
210
|
+
try:
|
|
211
|
+
err_data = response.json()
|
|
212
|
+
if isinstance(err_data, dict):
|
|
213
|
+
error_name = err_data.get("error_name") or err_data.get("error")
|
|
214
|
+
message = err_data.get("message") or err_data.get("detail") or message
|
|
215
|
+
except Exception:
|
|
216
|
+
message = response.text or message
|
|
217
|
+
|
|
218
|
+
if status_code == 401:
|
|
219
|
+
friendly_msg = f"Authentication required (API key missing or invalid): {message}"
|
|
220
|
+
elif status_code == 403:
|
|
221
|
+
friendly_msg = f"Access denied for requested assets (license required): {message}"
|
|
222
|
+
elif status_code == 404:
|
|
223
|
+
friendly_msg = f"Asset or version not found in registry: {message}"
|
|
224
|
+
elif status_code == 422:
|
|
225
|
+
friendly_msg = f"Cannot satisfy version constraints: {message}"
|
|
226
|
+
else:
|
|
227
|
+
friendly_msg = f"Registry API error [{status_code}]: {message}"
|
|
228
|
+
|
|
229
|
+
raise RegistryApiError(
|
|
230
|
+
friendly_msg,
|
|
231
|
+
status_code=status_code,
|
|
232
|
+
error_name=error_name,
|
|
233
|
+
)
|