itqan-cli 0.5.0__tar.gz → 0.6.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.
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/CHANGELOG.md +24 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/PKG-INFO +31 -2
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/README.md +30 -1
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/pyproject.toml +1 -1
- itqan_cli-0.6.0/src/itqan_cli/autoupdate.py +93 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/browse.py +24 -5
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/client.py +42 -0
- itqan_cli-0.6.0/src/itqan_cli/columns_comment.py +118 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/main.py +136 -2
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/manifest.py +5 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_add.py +97 -0
- itqan_cli-0.6.0/tests/test_autoupdate.py +177 -0
- itqan_cli-0.6.0/tests/test_cli_integration.py +437 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_client.py +110 -0
- itqan_cli-0.6.0/tests/test_columns_comment.py +137 -0
- itqan_cli-0.5.0/tests/test_cli_integration.py +0 -234
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/.gitignore +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/LICENSE +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/RELEASING.md +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/.gitignore +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/README.md +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/analysis_options.yaml +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/bin/itqan.dart +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/lib/itqan_cli.dart +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/lib/src/launcher.dart +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/lib/src/version.dart +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/pubspec.yaml +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/dart/test/launcher_test.dart +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/npm/README.md +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/npm/bin/itqan.js +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/scripts/dart_package.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/scripts/homebrew_formula.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/scripts/npm_packages.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/__init__.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/__main__.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/add.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/credentials.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/downloader.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/exceptions.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/flutter.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/init_template.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/lockfile.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/progress.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/semver.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/updates.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/src/itqan_cli/usage.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/__init__.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/conftest.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_access.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_assets_dir.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_browse.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_dart_package.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_downloader.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_entry_points.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_flutter.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_homebrew_formula.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_init.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_install_progress.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_languages.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_lockfile.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_login.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_manifest.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_npm_packages.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_progress.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_updates.py +0 -0
- {itqan_cli-0.5.0 → itqan_cli-0.6.0}/tests/test_usage.py +0 -0
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
## v0.6.0 (2026-11-03)
|
|
2
|
+
|
|
3
|
+
### Feat
|
|
4
|
+
|
|
5
|
+
- `itqan autoupdate` writes `.github/workflows/update-itqan-assets.yml`, a GitHub Actions workflow
|
|
6
|
+
that runs `itqan install --force` on a schedule and opens a pull request (or updates the open
|
|
7
|
+
one) when that picks newer versions allowed by the manifest. It runs from the folder you ran it
|
|
8
|
+
in with the same `--manifest` and `--lockfile`, and adds `--flutter` in a Flutter project.
|
|
9
|
+
`--schedule` takes a cron expression (default every Monday at 06:00 UTC), `--output` writes the
|
|
10
|
+
file elsewhere and `--force` overwrites an existing one.
|
|
11
|
+
- for translations and tafsirs with several text columns, `itqan add` writes a
|
|
12
|
+
`# itqan:columns` note above the entry saying what the downloaded CSV holds: what one row is,
|
|
13
|
+
the header, and each column's English and Arabic label. `itqan install` keeps the note in line
|
|
14
|
+
with the version it installs. Only lines starting with `# itqan:columns` are ever changed.
|
|
15
|
+
|
|
16
|
+
### Fix
|
|
17
|
+
|
|
18
|
+
- the column notes keep the manifest's line endings, and a label with line breaks is written on
|
|
19
|
+
one line.
|
|
20
|
+
- `itqan add name@range --no-install` no longer writes a note for the newest version when the
|
|
21
|
+
range accepts only older ones; `itqan install` adds the right one.
|
|
22
|
+
- an asset whose column layout the registry sends malformed no longer fails the command; it just
|
|
23
|
+
gets no note.
|
|
24
|
+
|
|
1
25
|
## v0.5.0 (2026-10-21)
|
|
2
26
|
|
|
3
27
|
### Feat
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: itqan-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Itqan Quranic Asset Manager: install and pin Itqan Quranic assets in your project.
|
|
5
5
|
Project-URL: Homepage, https://github.com/Itqan-community/itqan-cli
|
|
6
6
|
Project-URL: Repository, https://github.com/Itqan-community/itqan-cli
|
|
@@ -301,6 +301,25 @@ Each language of an asset has its own version history. That's why `tafsir-jalala
|
|
|
301
301
|
2.x while its English translation is on 1.3.x. Leave out `language` to get the asset's original
|
|
302
302
|
language.
|
|
303
303
|
|
|
304
|
+
Some translations and tafsirs have several text columns, such as a question and an answer for
|
|
305
|
+
each ayah. For those, `itqan add` writes a note above the entry saying what the downloaded CSV
|
|
306
|
+
holds:
|
|
307
|
+
|
|
308
|
+
```yaml
|
|
309
|
+
assets:
|
|
310
|
+
# itqan:columns CSV, one row per ayah: surah, ayah, question, answer
|
|
311
|
+
# itqan:columns question: Question / السؤال
|
|
312
|
+
# itqan:columns answer: Answer / الجواب
|
|
313
|
+
ayah-qa:
|
|
314
|
+
version: "^1.2.0"
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
`itqan install` keeps the note in line with the version it installs, for example when a newer
|
|
318
|
+
version adds a column. It only ever changes lines starting with `# itqan:columns`, and keeps your
|
|
319
|
+
own comments and the file's line endings. Assets with a single text column get no note, and
|
|
320
|
+
`itqan add name@range --no-install` leaves it to `itqan install` when the range doesn't accept the
|
|
321
|
+
newest version (the note would describe a version the entry won't install).
|
|
322
|
+
|
|
304
323
|
The full format, including every rule and error, is specified in
|
|
305
324
|
[ASSET_MANIFEST.md](https://github.com/Itqan-community/cms-backend/blob/main/docs/ASSET_MANIFEST.md).
|
|
306
325
|
|
|
@@ -371,7 +390,16 @@ Pass `install: true` to have the action run `itqan install` itself. The
|
|
|
371
390
|
Dependabot and similar tools don't read `itqan-assets.yaml`. To get the same kind of pull request,
|
|
372
391
|
add a workflow that runs `itqan install --force` on a schedule. It picks the newest versions your
|
|
373
392
|
manifest allows, and when that changes the lockfile, it opens a pull request (or updates the open
|
|
374
|
-
one)
|
|
393
|
+
one). Run `itqan autoupdate` to write it to `.github/workflows/update-itqan-assets.yml`:
|
|
394
|
+
|
|
395
|
+
```bash
|
|
396
|
+
itqan autoupdate # every Monday at 06:00 UTC
|
|
397
|
+
itqan autoupdate --schedule "0 3 * * *" # or every day at 03:00 UTC
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
It runs `itqan install` from the folder you ran `itqan autoupdate` in, with the same `--manifest`
|
|
401
|
+
and `--lockfile`, and adds `--flutter` in a [Flutter](#flutter) project. `--output` writes the file
|
|
402
|
+
somewhere else and `--force` overwrites an existing one. The workflow it writes looks like this:
|
|
375
403
|
|
|
376
404
|
```yaml
|
|
377
405
|
# .github/workflows/update-itqan-assets.yml
|
|
@@ -432,6 +460,7 @@ example, `npm ci` for a project with `itqan-cli` in its `devDependencies`, or
|
|
|
432
460
|
| `itqan add ASSET[@RANGE]...` | Add assets to `itqan-assets.yaml` (created if missing) and install them. Without `@RANGE` an entry accepts `^` the newest version; an asset already in the manifest gets the new range. `--language` picks a language other than the source; `--no-install` only edits the manifest. |
|
|
433
461
|
| `itqan install` | Download the assets and write `itqan-assets.lock`. Uses the lockfile's exact versions while it matches the manifest. `--force` picks the newest allowed versions and downloads again. `--flutter` keeps `pubspec.yaml` up to date (see [Flutter](#flutter)). `--assets-dir` overrides the manifest's folder for one run. In a terminal it shows each download's progress; `--no-progress` turns that off. |
|
|
434
462
|
| `itqan sync` | Same as `itqan install`. |
|
|
463
|
+
| `itqan autoupdate` | Write a GitHub Actions workflow that opens a pull request when `itqan install --force` finds newer allowed versions (see [Get a pull request when assets have new versions](#get-a-pull-request-when-assets-have-new-versions)). `--schedule` sets when it runs; `--force` overwrites an existing workflow. |
|
|
435
464
|
| `itqan outdated` | List the locked assets that have newer versions: the newest your range allows and the newest overall. `--json` prints JSON. |
|
|
436
465
|
| `itqan login` | Save your API key for the registry, after checking it. `--api-key` passes the key without the prompt. |
|
|
437
466
|
| `itqan logout` | Remove the saved key. |
|
|
@@ -270,6 +270,25 @@ Each language of an asset has its own version history. That's why `tafsir-jalala
|
|
|
270
270
|
2.x while its English translation is on 1.3.x. Leave out `language` to get the asset's original
|
|
271
271
|
language.
|
|
272
272
|
|
|
273
|
+
Some translations and tafsirs have several text columns, such as a question and an answer for
|
|
274
|
+
each ayah. For those, `itqan add` writes a note above the entry saying what the downloaded CSV
|
|
275
|
+
holds:
|
|
276
|
+
|
|
277
|
+
```yaml
|
|
278
|
+
assets:
|
|
279
|
+
# itqan:columns CSV, one row per ayah: surah, ayah, question, answer
|
|
280
|
+
# itqan:columns question: Question / السؤال
|
|
281
|
+
# itqan:columns answer: Answer / الجواب
|
|
282
|
+
ayah-qa:
|
|
283
|
+
version: "^1.2.0"
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
`itqan install` keeps the note in line with the version it installs, for example when a newer
|
|
287
|
+
version adds a column. It only ever changes lines starting with `# itqan:columns`, and keeps your
|
|
288
|
+
own comments and the file's line endings. Assets with a single text column get no note, and
|
|
289
|
+
`itqan add name@range --no-install` leaves it to `itqan install` when the range doesn't accept the
|
|
290
|
+
newest version (the note would describe a version the entry won't install).
|
|
291
|
+
|
|
273
292
|
The full format, including every rule and error, is specified in
|
|
274
293
|
[ASSET_MANIFEST.md](https://github.com/Itqan-community/cms-backend/blob/main/docs/ASSET_MANIFEST.md).
|
|
275
294
|
|
|
@@ -340,7 +359,16 @@ Pass `install: true` to have the action run `itqan install` itself. The
|
|
|
340
359
|
Dependabot and similar tools don't read `itqan-assets.yaml`. To get the same kind of pull request,
|
|
341
360
|
add a workflow that runs `itqan install --force` on a schedule. It picks the newest versions your
|
|
342
361
|
manifest allows, and when that changes the lockfile, it opens a pull request (or updates the open
|
|
343
|
-
one)
|
|
362
|
+
one). Run `itqan autoupdate` to write it to `.github/workflows/update-itqan-assets.yml`:
|
|
363
|
+
|
|
364
|
+
```bash
|
|
365
|
+
itqan autoupdate # every Monday at 06:00 UTC
|
|
366
|
+
itqan autoupdate --schedule "0 3 * * *" # or every day at 03:00 UTC
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
It runs `itqan install` from the folder you ran `itqan autoupdate` in, with the same `--manifest`
|
|
370
|
+
and `--lockfile`, and adds `--flutter` in a [Flutter](#flutter) project. `--output` writes the file
|
|
371
|
+
somewhere else and `--force` overwrites an existing one. The workflow it writes looks like this:
|
|
344
372
|
|
|
345
373
|
```yaml
|
|
346
374
|
# .github/workflows/update-itqan-assets.yml
|
|
@@ -401,6 +429,7 @@ example, `npm ci` for a project with `itqan-cli` in its `devDependencies`, or
|
|
|
401
429
|
| `itqan add ASSET[@RANGE]...` | Add assets to `itqan-assets.yaml` (created if missing) and install them. Without `@RANGE` an entry accepts `^` the newest version; an asset already in the manifest gets the new range. `--language` picks a language other than the source; `--no-install` only edits the manifest. |
|
|
402
430
|
| `itqan install` | Download the assets and write `itqan-assets.lock`. Uses the lockfile's exact versions while it matches the manifest. `--force` picks the newest allowed versions and downloads again. `--flutter` keeps `pubspec.yaml` up to date (see [Flutter](#flutter)). `--assets-dir` overrides the manifest's folder for one run. In a terminal it shows each download's progress; `--no-progress` turns that off. |
|
|
403
431
|
| `itqan sync` | Same as `itqan install`. |
|
|
432
|
+
| `itqan autoupdate` | Write a GitHub Actions workflow that opens a pull request when `itqan install --force` finds newer allowed versions (see [Get a pull request when assets have new versions](#get-a-pull-request-when-assets-have-new-versions)). `--schedule` sets when it runs; `--force` overwrites an existing workflow. |
|
|
404
433
|
| `itqan outdated` | List the locked assets that have newer versions: the newest your range allows and the newest overall. `--json` prints JSON. |
|
|
405
434
|
| `itqan login` | Save your API key for the registry, after checking it. `--api-key` passes the key without the prompt. |
|
|
406
435
|
| `itqan logout` | Remove the saved key. |
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""`itqan autoupdate`: write a GitHub Actions workflow that opens a pull request for asset updates.
|
|
2
|
+
|
|
3
|
+
The workflow runs `itqan install --force` on a schedule, from the same folder and with the same
|
|
4
|
+
manifest and lockfile options the user would use locally, and hands any lockfile change to
|
|
5
|
+
peter-evans/create-pull-request, which opens the pull request or updates the open one.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from pathlib import Path, PurePosixPath
|
|
11
|
+
import re
|
|
12
|
+
import shlex
|
|
13
|
+
|
|
14
|
+
from itqan_cli.exceptions import ItqanCliError
|
|
15
|
+
|
|
16
|
+
DEFAULT_SCHEDULE = "0 6 * * 1" # every Monday at 06:00 UTC
|
|
17
|
+
WORKFLOW_FILE = "update-itqan-assets.yml"
|
|
18
|
+
PR_BRANCH = "itqan/update-assets"
|
|
19
|
+
|
|
20
|
+
_CRON_FIELD_RE = re.compile(r"^[0-9A-Za-z*/,\-]+$")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def find_repo_root(start: Path) -> Path | None:
|
|
24
|
+
"""The nearest folder at or above ``start`` with a `.git` (a folder, or a file in a worktree)."""
|
|
25
|
+
start = start.resolve()
|
|
26
|
+
for folder in (start, *start.parents):
|
|
27
|
+
if (folder / ".git").exists():
|
|
28
|
+
return folder
|
|
29
|
+
return None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def validate_schedule(schedule: str) -> str:
|
|
33
|
+
"""A five-field cron expression, as GitHub's `on.schedule` takes it."""
|
|
34
|
+
fields = schedule.split()
|
|
35
|
+
if len(fields) != 5 or not all(_CRON_FIELD_RE.match(f) for f in fields):
|
|
36
|
+
raise ItqanCliError(
|
|
37
|
+
f"--schedule must be a cron expression with five fields, such as {DEFAULT_SCHEDULE!r}; got {schedule!r}."
|
|
38
|
+
)
|
|
39
|
+
return " ".join(fields)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def relative_to_root(path: Path, root: Path, what: str) -> PurePosixPath:
|
|
43
|
+
"""``path`` as a forward-slash path relative to the repository root, which is what a workflow sees."""
|
|
44
|
+
try:
|
|
45
|
+
return PurePosixPath(path.resolve().relative_to(root).as_posix())
|
|
46
|
+
except ValueError:
|
|
47
|
+
raise ItqanCliError(f"The {what} {path} is outside the repository at {root}.") from None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _quote(value: str) -> str:
|
|
51
|
+
"""A double-quoted YAML scalar."""
|
|
52
|
+
return '"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"'
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def render_workflow(*, install_args: list[str], working_directory: str, schedule: str) -> str:
|
|
56
|
+
"""The workflow file's text.
|
|
57
|
+
|
|
58
|
+
``install_args`` follow `itqan install`, ``working_directory`` is the folder (relative to the
|
|
59
|
+
repository root) the command runs in, "." for the root.
|
|
60
|
+
"""
|
|
61
|
+
command = shlex.join(["itqan", "install", *install_args])
|
|
62
|
+
workdir = "" if working_directory == "." else f" working-directory: {_quote(working_directory)}\n"
|
|
63
|
+
body = f"`{command}` found newer versions allowed by the manifest."
|
|
64
|
+
return f"""\
|
|
65
|
+
# Written by `itqan autoupdate`. Runs `itqan install --force` on a schedule and opens a pull
|
|
66
|
+
# request (or updates the open one) when that picks newer versions allowed by the manifest.
|
|
67
|
+
name: Update Itqan assets
|
|
68
|
+
|
|
69
|
+
on:
|
|
70
|
+
schedule:
|
|
71
|
+
- cron: {_quote(schedule)}
|
|
72
|
+
workflow_dispatch: # and whenever you run it by hand
|
|
73
|
+
|
|
74
|
+
permissions:
|
|
75
|
+
contents: write
|
|
76
|
+
pull-requests: write
|
|
77
|
+
|
|
78
|
+
jobs:
|
|
79
|
+
update:
|
|
80
|
+
runs-on: ubuntu-latest
|
|
81
|
+
steps:
|
|
82
|
+
- uses: actions/checkout@v7
|
|
83
|
+
- uses: itqan-community/setup-itqan@v1
|
|
84
|
+
- run: {_quote(command)}
|
|
85
|
+
{workdir} env:
|
|
86
|
+
ITQAN_API_KEY: ${{{{ secrets.ITQAN_API_KEY }}}} # only needed for assets that need access approval
|
|
87
|
+
- uses: peter-evans/create-pull-request@v8
|
|
88
|
+
with:
|
|
89
|
+
branch: {PR_BRANCH}
|
|
90
|
+
commit-message: "chore: update Itqan assets"
|
|
91
|
+
title: "Update Itqan assets"
|
|
92
|
+
body: {_quote(body)}
|
|
93
|
+
"""
|
|
@@ -12,9 +12,11 @@ import shutil
|
|
|
12
12
|
from typing import TYPE_CHECKING, Any
|
|
13
13
|
|
|
14
14
|
from itqan_cli.client import CatalogLanguage, CatalogPackage
|
|
15
|
+
from itqan_cli.columns_comment import render_block
|
|
15
16
|
from itqan_cli.exceptions import ManifestError
|
|
16
17
|
from itqan_cli.init_template import entry_lines
|
|
17
|
-
from itqan_cli.manifest import AssetManifest, parse_manifest_content
|
|
18
|
+
from itqan_cli.manifest import ASSETS_KEY_LINE, AssetManifest, parse_manifest_content
|
|
19
|
+
from itqan_cli.semver import matches_constraint, parse_constraint, parse_semver
|
|
18
20
|
|
|
19
21
|
if TYPE_CHECKING:
|
|
20
22
|
from questionary import Question
|
|
@@ -33,9 +35,13 @@ class NewEntry:
|
|
|
33
35
|
constraint: str | None = None # None: ^ the language's latest version
|
|
34
36
|
|
|
35
37
|
def lines(self, indent: str) -> list[str]:
|
|
38
|
+
# A multi-column asset gets its `# itqan:columns` notes right above the entry.
|
|
39
|
+
notes = render_block(self.language.structure, indent) if self._describes_accepted_version() else []
|
|
36
40
|
if self.language.is_source:
|
|
37
|
-
return entry_lines(
|
|
38
|
-
|
|
41
|
+
return notes + entry_lines(
|
|
42
|
+
self.name, self.language.latest_version, indent=indent, constraint=self.constraint
|
|
43
|
+
)
|
|
44
|
+
return notes + entry_lines(
|
|
39
45
|
self.name,
|
|
40
46
|
self.language.latest_version,
|
|
41
47
|
asset=self.package.slug,
|
|
@@ -44,6 +50,20 @@ class NewEntry:
|
|
|
44
50
|
constraint=self.constraint,
|
|
45
51
|
)
|
|
46
52
|
|
|
53
|
+
def _describes_accepted_version(self) -> bool:
|
|
54
|
+
"""Whether the catalog's structure (its latest version's) fits this entry: with a
|
|
55
|
+
range that rejects the latest version, `itqan install` writes the notes instead,
|
|
56
|
+
for the version it resolves."""
|
|
57
|
+
if self.language.structure is None:
|
|
58
|
+
return False
|
|
59
|
+
if self.constraint is None:
|
|
60
|
+
return True
|
|
61
|
+
latest = parse_semver(self.language.latest_version)
|
|
62
|
+
try:
|
|
63
|
+
return latest is not None and matches_constraint(latest, parse_constraint(self.constraint))
|
|
64
|
+
except ValueError:
|
|
65
|
+
return False
|
|
66
|
+
|
|
47
67
|
|
|
48
68
|
def manifest_targets(manifest: AssetManifest | None) -> set[Target]:
|
|
49
69
|
if manifest is None:
|
|
@@ -86,7 +106,6 @@ def plan_entries(
|
|
|
86
106
|
return entries, warnings
|
|
87
107
|
|
|
88
108
|
|
|
89
|
-
_ASSETS_KEY = re.compile(r"^assets[ \t]*:[ \t]*(#.*)?$")
|
|
90
109
|
_ENTRY_INDENT = re.compile(r"^([ \t]+)[^ \t#]")
|
|
91
110
|
|
|
92
111
|
|
|
@@ -101,7 +120,7 @@ def add_entries(content: str, entries: list[NewEntry]) -> str:
|
|
|
101
120
|
result would not be a valid manifest.
|
|
102
121
|
"""
|
|
103
122
|
lines = content.splitlines()
|
|
104
|
-
start = next((i for i, line in enumerate(lines) if
|
|
123
|
+
start = next((i for i, line in enumerate(lines) if ASSETS_KEY_LINE.match(line)), None)
|
|
105
124
|
if start is None:
|
|
106
125
|
raise ManifestError("Could not find a block-style `assets:` key to add entries under.")
|
|
107
126
|
|
|
@@ -6,6 +6,7 @@ from dataclasses import dataclass
|
|
|
6
6
|
|
|
7
7
|
import requests
|
|
8
8
|
|
|
9
|
+
from itqan_cli.columns_comment import PackageColumn, PackageStructure
|
|
9
10
|
from itqan_cli.exceptions import RegistryApiError
|
|
10
11
|
from itqan_cli.usage import usage_headers
|
|
11
12
|
|
|
@@ -29,6 +30,9 @@ class ResolvedAssetPayload:
|
|
|
29
30
|
# manifest's range allows. None from a registry too old to report them.
|
|
30
31
|
latest_version: str | None = None
|
|
31
32
|
latest_in_range: str | None = None
|
|
33
|
+
# Translations / tafsirs: the resolved version's CSV layout. None from other
|
|
34
|
+
# categories and from registries that predate it.
|
|
35
|
+
structure: PackageStructure | None = None
|
|
32
36
|
|
|
33
37
|
@property
|
|
34
38
|
def entry_name(self) -> str:
|
|
@@ -41,6 +45,8 @@ class CatalogLanguage:
|
|
|
41
45
|
language: str
|
|
42
46
|
is_source: bool
|
|
43
47
|
latest_version: str
|
|
48
|
+
# The latest version's CSV layout (translations / tafsirs); None when unknown.
|
|
49
|
+
structure: PackageStructure | None = None
|
|
44
50
|
|
|
45
51
|
|
|
46
52
|
@dataclass(frozen=True, slots=True)
|
|
@@ -67,6 +73,40 @@ class Account:
|
|
|
67
73
|
email: str
|
|
68
74
|
|
|
69
75
|
|
|
76
|
+
def parse_structure(raw: object) -> PackageStructure | None:
|
|
77
|
+
"""A registry ``structure`` object, or None when it is absent, null or malformed.
|
|
78
|
+
|
|
79
|
+
The structure only feeds the column notes written above a manifest entry, so a
|
|
80
|
+
malformed one leaves the notes out rather than failing the whole registry call.
|
|
81
|
+
"""
|
|
82
|
+
if not isinstance(raw, dict):
|
|
83
|
+
return None
|
|
84
|
+
template, header, columns = raw.get("template"), raw.get("header"), raw.get("columns")
|
|
85
|
+
if not isinstance(template, str) or not isinstance(header, list) or not isinstance(columns, list):
|
|
86
|
+
return None
|
|
87
|
+
if not all(isinstance(name, str) for name in header):
|
|
88
|
+
return None
|
|
89
|
+
if not all(isinstance(column, dict) and isinstance(column.get("key"), str) for column in columns):
|
|
90
|
+
return None
|
|
91
|
+
return PackageStructure(
|
|
92
|
+
template=template,
|
|
93
|
+
header=tuple(header),
|
|
94
|
+
columns=tuple(
|
|
95
|
+
PackageColumn(
|
|
96
|
+
key=column["key"],
|
|
97
|
+
label_en=_label(column.get("label_en")),
|
|
98
|
+
label_ar=_label(column.get("label_ar")),
|
|
99
|
+
)
|
|
100
|
+
for column in columns
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _label(raw: object) -> str:
|
|
106
|
+
"""A column label, or "" when it is missing or not text."""
|
|
107
|
+
return raw if isinstance(raw, str) else ""
|
|
108
|
+
|
|
109
|
+
|
|
70
110
|
# The public API (not the cms.itqan.dev website, which serves the web app).
|
|
71
111
|
DEFAULT_REGISTRY_URL = "https://api.cms.itqan.dev"
|
|
72
112
|
|
|
@@ -206,6 +246,7 @@ class RegistryClient:
|
|
|
206
246
|
language=lang["language"],
|
|
207
247
|
is_source=lang["is_source"],
|
|
208
248
|
latest_version=lang["latest_version"],
|
|
249
|
+
structure=parse_structure(lang.get("structure")),
|
|
209
250
|
)
|
|
210
251
|
for lang in item["languages"]
|
|
211
252
|
),
|
|
@@ -300,6 +341,7 @@ class RegistryClient:
|
|
|
300
341
|
withdrawal_reason=item.get("withdrawal_reason"),
|
|
301
342
|
latest_version=item.get("latest_version"),
|
|
302
343
|
latest_in_range=item.get("latest_in_range"),
|
|
344
|
+
structure=parse_structure(item.get("structure")),
|
|
303
345
|
)
|
|
304
346
|
for item in results_raw
|
|
305
347
|
]
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""The `# itqan:columns` comment block above a manifest entry.
|
|
2
|
+
|
|
3
|
+
A translation or tafsir can have several text columns; the registry describes
|
|
4
|
+
the file (template, CSV header, columns with labels) as `structure`. The block
|
|
5
|
+
tells whoever reads the manifest what the downloaded CSV holds. Its format is
|
|
6
|
+
shared byte for byte with the registry's Dependabot (see the backend's
|
|
7
|
+
docs/ASSET_MANIFEST.md, "Column comment block"), so only lines carrying the
|
|
8
|
+
marker are ever replaced.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
import re
|
|
15
|
+
|
|
16
|
+
from itqan_cli.exceptions import ManifestError
|
|
17
|
+
from itqan_cli.manifest import ASSETS_KEY_LINE, parse_manifest_content
|
|
18
|
+
|
|
19
|
+
MARKER = "# itqan:columns"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True, slots=True)
|
|
23
|
+
class PackageColumn:
|
|
24
|
+
key: str
|
|
25
|
+
label_en: str = ""
|
|
26
|
+
label_ar: str = ""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True, slots=True)
|
|
30
|
+
class PackageStructure:
|
|
31
|
+
"""A version's CSV layout: what one row is, its header and its text columns."""
|
|
32
|
+
|
|
33
|
+
template: str
|
|
34
|
+
header: tuple[str, ...]
|
|
35
|
+
columns: tuple[PackageColumn, ...]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _one_line(value: str) -> str:
|
|
39
|
+
"""``value`` with every run of whitespace (newlines included) as one space: a
|
|
40
|
+
publisher-typed label must never become a line of YAML of its own."""
|
|
41
|
+
return " ".join(value.split())
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def render_block(structure: PackageStructure, indent: str) -> list[str]:
|
|
45
|
+
"""The block's lines at ``indent``; none for a single-column asset."""
|
|
46
|
+
if len(structure.columns) <= 1:
|
|
47
|
+
return []
|
|
48
|
+
header = ", ".join(_one_line(name) for name in structure.header)
|
|
49
|
+
lines = [f"{indent}{MARKER} CSV, one row per {_one_line(structure.template)}: {header}"]
|
|
50
|
+
for column in structure.columns:
|
|
51
|
+
labels = " / ".join(_one_line(label) for label in (column.label_en, column.label_ar) if _one_line(label))
|
|
52
|
+
if labels:
|
|
53
|
+
lines.append(f"{indent}{MARKER} {_one_line(column.key)}: {labels}")
|
|
54
|
+
return lines
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _entry_key(name: str) -> re.Pattern[str]:
|
|
58
|
+
"""A block-style key line for ``name`` (bare or quoted); flow entries don't match."""
|
|
59
|
+
quoted = re.escape(name)
|
|
60
|
+
return re.compile(rf"^(?P<indent>[ \t]+)(\"{quoted}\"|'{quoted}'|{quoted})[ \t]*:[ \t]*(#.*)?$")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _find_entry(lines: list[str], name: str) -> int | None:
|
|
64
|
+
start = next((i for i, line in enumerate(lines) if ASSETS_KEY_LINE.match(line)), None)
|
|
65
|
+
if start is None:
|
|
66
|
+
return None
|
|
67
|
+
key = _entry_key(name)
|
|
68
|
+
return next((i for i in range(start + 1, len(lines)) if key.match(lines[i])), None)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def entry_indent(content: str, name: str) -> str | None:
|
|
72
|
+
"""The indentation of entry ``name``'s key line, or None when it isn't a block entry."""
|
|
73
|
+
lines = content.split("\n")
|
|
74
|
+
index = _find_entry(lines, name)
|
|
75
|
+
if index is None:
|
|
76
|
+
return None
|
|
77
|
+
return _entry_key(name).match(lines[index]).group("indent")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def replace_block(content: str, name: str, block: list[str]) -> str:
|
|
81
|
+
"""``content`` with the marker lines directly above entry ``name`` replaced by
|
|
82
|
+
``block`` (``[]`` removes them). Every other line is kept as it is; a flow-style
|
|
83
|
+
or missing entry leaves ``content`` unchanged.
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
ManifestError when the result would not be a valid manifest.
|
|
87
|
+
"""
|
|
88
|
+
lines = content.split("\n")
|
|
89
|
+
index = _find_entry(lines, name)
|
|
90
|
+
if index is None:
|
|
91
|
+
return content
|
|
92
|
+
start = index
|
|
93
|
+
while start > 0 and lines[start - 1].lstrip().startswith(MARKER):
|
|
94
|
+
start -= 1
|
|
95
|
+
updated = "\n".join(lines[:start] + block + lines[index:])
|
|
96
|
+
if updated == content:
|
|
97
|
+
return content
|
|
98
|
+
try:
|
|
99
|
+
parse_manifest_content(updated.encode("utf-8"))
|
|
100
|
+
except ManifestError as exc:
|
|
101
|
+
raise ManifestError(f"Could not update the column notes of {name!r}: {exc}") from exc
|
|
102
|
+
return updated
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def refresh_blocks(content: str, payloads: list) -> str:
|
|
106
|
+
"""``content`` with each resolved entry's block matching its resolved version.
|
|
107
|
+
|
|
108
|
+
Entries whose registry reported no ``structure`` (older registries, other
|
|
109
|
+
categories) are left exactly as they are.
|
|
110
|
+
"""
|
|
111
|
+
for payload in payloads:
|
|
112
|
+
if payload.structure is None:
|
|
113
|
+
continue
|
|
114
|
+
indent = entry_indent(content, payload.entry_name)
|
|
115
|
+
if indent is None:
|
|
116
|
+
continue
|
|
117
|
+
content = replace_block(content, payload.entry_name, render_block(payload.structure, indent))
|
|
118
|
+
return content
|