itqan-cli 0.5.0__tar.gz → 0.7.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.
Files changed (66) hide show
  1. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/CHANGELOG.md +37 -0
  2. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/PKG-INFO +33 -3
  3. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/README.md +32 -2
  4. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/pyproject.toml +2 -1
  5. itqan_cli-0.7.0/src/itqan_cli/autoupdate.py +93 -0
  6. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/browse.py +32 -7
  7. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/client.py +52 -2
  8. itqan_cli-0.7.0/src/itqan_cli/columns_comment.py +118 -0
  9. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/main.py +143 -6
  10. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/manifest.py +5 -0
  11. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_add.py +97 -0
  12. itqan_cli-0.7.0/tests/test_autoupdate.py +177 -0
  13. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_browse.py +14 -1
  14. itqan_cli-0.7.0/tests/test_cli_integration.py +437 -0
  15. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_client.py +110 -0
  16. itqan_cli-0.7.0/tests/test_columns_comment.py +137 -0
  17. itqan_cli-0.5.0/tests/test_cli_integration.py +0 -234
  18. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/.gitignore +0 -0
  19. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/LICENSE +0 -0
  20. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/RELEASING.md +0 -0
  21. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/.gitignore +0 -0
  22. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/README.md +0 -0
  23. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/analysis_options.yaml +0 -0
  24. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/bin/itqan.dart +0 -0
  25. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/lib/itqan_cli.dart +0 -0
  26. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/lib/src/launcher.dart +0 -0
  27. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/lib/src/version.dart +0 -0
  28. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/pubspec.yaml +0 -0
  29. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/dart/test/launcher_test.dart +0 -0
  30. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/npm/README.md +0 -0
  31. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/npm/bin/itqan.js +0 -0
  32. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/scripts/dart_package.py +0 -0
  33. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/scripts/homebrew_formula.py +0 -0
  34. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/scripts/npm_packages.py +0 -0
  35. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/__init__.py +0 -0
  36. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/__main__.py +0 -0
  37. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/add.py +0 -0
  38. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/credentials.py +0 -0
  39. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/downloader.py +0 -0
  40. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/exceptions.py +0 -0
  41. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/flutter.py +0 -0
  42. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/init_template.py +0 -0
  43. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/lockfile.py +0 -0
  44. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/progress.py +0 -0
  45. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/semver.py +0 -0
  46. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/updates.py +0 -0
  47. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/src/itqan_cli/usage.py +0 -0
  48. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/__init__.py +0 -0
  49. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/conftest.py +0 -0
  50. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_access.py +0 -0
  51. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_assets_dir.py +0 -0
  52. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_dart_package.py +0 -0
  53. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_downloader.py +0 -0
  54. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_entry_points.py +0 -0
  55. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_flutter.py +0 -0
  56. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_homebrew_formula.py +0 -0
  57. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_init.py +0 -0
  58. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_install_progress.py +0 -0
  59. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_languages.py +0 -0
  60. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_lockfile.py +0 -0
  61. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_login.py +0 -0
  62. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_manifest.py +0 -0
  63. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_npm_packages.py +0 -0
  64. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_progress.py +0 -0
  65. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_updates.py +0 -0
  66. {itqan_cli-0.5.0 → itqan_cli-0.7.0}/tests/test_usage.py +0 -0
@@ -1,3 +1,40 @@
1
+ ## v0.7.0 (2026-11-14)
2
+
3
+ ### BREAKING CHANGE
4
+
5
+ - the registry now reports tafsirs and translations as category `text` with tags such as `tafsir`
6
+ or `translation`. `itqan browse --category tafsir` and `--category translation` now fail with
7
+ the registry's `category_renamed` error; use `--tag` instead.
8
+
9
+ ### Feat
10
+
11
+ - `itqan browse` shows each package's tags, in the table and in `--json` output.
12
+ - `itqan browse --tag/-t <tag>` filters the catalog by tag, for example `itqan browse --tag tafsir`.
13
+
14
+ ## v0.6.0 (2026-11-03)
15
+
16
+ ### Feat
17
+
18
+ - `itqan autoupdate` writes `.github/workflows/update-itqan-assets.yml`, a GitHub Actions workflow
19
+ that runs `itqan install --force` on a schedule and opens a pull request (or updates the open
20
+ one) when that picks newer versions allowed by the manifest. It runs from the folder you ran it
21
+ in with the same `--manifest` and `--lockfile`, and adds `--flutter` in a Flutter project.
22
+ `--schedule` takes a cron expression (default every Monday at 06:00 UTC), `--output` writes the
23
+ file elsewhere and `--force` overwrites an existing one.
24
+ - for translations and tafsirs with several text columns, `itqan add` writes a
25
+ `# itqan:columns` note above the entry saying what the downloaded CSV holds: what one row is,
26
+ the header, and each column's English and Arabic label. `itqan install` keeps the note in line
27
+ with the version it installs. Only lines starting with `# itqan:columns` are ever changed.
28
+
29
+ ### Fix
30
+
31
+ - the column notes keep the manifest's line endings, and a label with line breaks is written on
32
+ one line.
33
+ - `itqan add name@range --no-install` no longer writes a note for the newest version when the
34
+ range accepts only older ones; `itqan install` adds the right one.
35
+ - an asset whose column layout the registry sends malformed no longer fails the command; it just
36
+ gets no note.
37
+
1
38
  ## v0.5.0 (2026-10-21)
2
39
 
3
40
  ### Feat
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: itqan-cli
3
- Version: 0.5.0
3
+ Version: 0.7.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
@@ -195,7 +195,8 @@ The asset's name is the one `itqan browse` and each asset's page in the
195
195
 
196
196
  In `itqan browse`, type to filter the list, press Enter to add the highlighted asset, or press
197
197
  Space on several and then Enter. `itqan browse tafsir` starts with a search, and
198
- `itqan browse --category translation` shows one kind of asset.
198
+ `itqan browse --tag translation` shows one kind of text asset (`--category font` does the same
199
+ for the other categories).
199
200
 
200
201
  ## Use the assets in your app
201
202
 
@@ -301,6 +302,25 @@ Each language of an asset has its own version history. That's why `tafsir-jalala
301
302
  2.x while its English translation is on 1.3.x. Leave out `language` to get the asset's original
302
303
  language.
303
304
 
305
+ Some translations and tafsirs have several text columns, such as a question and an answer for
306
+ each ayah. For those, `itqan add` writes a note above the entry saying what the downloaded CSV
307
+ holds:
308
+
309
+ ```yaml
310
+ assets:
311
+ # itqan:columns CSV, one row per ayah: surah, ayah, question, answer
312
+ # itqan:columns question: Question / السؤال
313
+ # itqan:columns answer: Answer / الجواب
314
+ ayah-qa:
315
+ version: "^1.2.0"
316
+ ```
317
+
318
+ `itqan install` keeps the note in line with the version it installs, for example when a newer
319
+ version adds a column. It only ever changes lines starting with `# itqan:columns`, and keeps your
320
+ own comments and the file's line endings. Assets with a single text column get no note, and
321
+ `itqan add name@range --no-install` leaves it to `itqan install` when the range doesn't accept the
322
+ newest version (the note would describe a version the entry won't install).
323
+
304
324
  The full format, including every rule and error, is specified in
305
325
  [ASSET_MANIFEST.md](https://github.com/Itqan-community/cms-backend/blob/main/docs/ASSET_MANIFEST.md).
306
326
 
@@ -371,7 +391,16 @@ Pass `install: true` to have the action run `itqan install` itself. The
371
391
  Dependabot and similar tools don't read `itqan-assets.yaml`. To get the same kind of pull request,
372
392
  add a workflow that runs `itqan install --force` on a schedule. It picks the newest versions your
373
393
  manifest allows, and when that changes the lockfile, it opens a pull request (or updates the open
374
- one):
394
+ one). Run `itqan autoupdate` to write it to `.github/workflows/update-itqan-assets.yml`:
395
+
396
+ ```bash
397
+ itqan autoupdate # every Monday at 06:00 UTC
398
+ itqan autoupdate --schedule "0 3 * * *" # or every day at 03:00 UTC
399
+ ```
400
+
401
+ It runs `itqan install` from the folder you ran `itqan autoupdate` in, with the same `--manifest`
402
+ and `--lockfile`, and adds `--flutter` in a [Flutter](#flutter) project. `--output` writes the file
403
+ somewhere else and `--force` overwrites an existing one. The workflow it writes looks like this:
375
404
 
376
405
  ```yaml
377
406
  # .github/workflows/update-itqan-assets.yml
@@ -432,6 +461,7 @@ example, `npm ci` for a project with `itqan-cli` in its `devDependencies`, or
432
461
  | `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
462
  | `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
463
  | `itqan sync` | Same as `itqan install`. |
464
+ | `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
465
  | `itqan outdated` | List the locked assets that have newer versions: the newest your range allows and the newest overall. `--json` prints JSON. |
436
466
  | `itqan login` | Save your API key for the registry, after checking it. `--api-key` passes the key without the prompt. |
437
467
  | `itqan logout` | Remove the saved key. |
@@ -164,7 +164,8 @@ The asset's name is the one `itqan browse` and each asset's page in the
164
164
 
165
165
  In `itqan browse`, type to filter the list, press Enter to add the highlighted asset, or press
166
166
  Space on several and then Enter. `itqan browse tafsir` starts with a search, and
167
- `itqan browse --category translation` shows one kind of asset.
167
+ `itqan browse --tag translation` shows one kind of text asset (`--category font` does the same
168
+ for the other categories).
168
169
 
169
170
  ## Use the assets in your app
170
171
 
@@ -270,6 +271,25 @@ Each language of an asset has its own version history. That's why `tafsir-jalala
270
271
  2.x while its English translation is on 1.3.x. Leave out `language` to get the asset's original
271
272
  language.
272
273
 
274
+ Some translations and tafsirs have several text columns, such as a question and an answer for
275
+ each ayah. For those, `itqan add` writes a note above the entry saying what the downloaded CSV
276
+ holds:
277
+
278
+ ```yaml
279
+ assets:
280
+ # itqan:columns CSV, one row per ayah: surah, ayah, question, answer
281
+ # itqan:columns question: Question / السؤال
282
+ # itqan:columns answer: Answer / الجواب
283
+ ayah-qa:
284
+ version: "^1.2.0"
285
+ ```
286
+
287
+ `itqan install` keeps the note in line with the version it installs, for example when a newer
288
+ version adds a column. It only ever changes lines starting with `# itqan:columns`, and keeps your
289
+ own comments and the file's line endings. Assets with a single text column get no note, and
290
+ `itqan add name@range --no-install` leaves it to `itqan install` when the range doesn't accept the
291
+ newest version (the note would describe a version the entry won't install).
292
+
273
293
  The full format, including every rule and error, is specified in
274
294
  [ASSET_MANIFEST.md](https://github.com/Itqan-community/cms-backend/blob/main/docs/ASSET_MANIFEST.md).
275
295
 
@@ -340,7 +360,16 @@ Pass `install: true` to have the action run `itqan install` itself. The
340
360
  Dependabot and similar tools don't read `itqan-assets.yaml`. To get the same kind of pull request,
341
361
  add a workflow that runs `itqan install --force` on a schedule. It picks the newest versions your
342
362
  manifest allows, and when that changes the lockfile, it opens a pull request (or updates the open
343
- one):
363
+ one). Run `itqan autoupdate` to write it to `.github/workflows/update-itqan-assets.yml`:
364
+
365
+ ```bash
366
+ itqan autoupdate # every Monday at 06:00 UTC
367
+ itqan autoupdate --schedule "0 3 * * *" # or every day at 03:00 UTC
368
+ ```
369
+
370
+ It runs `itqan install` from the folder you ran `itqan autoupdate` in, with the same `--manifest`
371
+ and `--lockfile`, and adds `--flutter` in a [Flutter](#flutter) project. `--output` writes the file
372
+ somewhere else and `--force` overwrites an existing one. The workflow it writes looks like this:
344
373
 
345
374
  ```yaml
346
375
  # .github/workflows/update-itqan-assets.yml
@@ -401,6 +430,7 @@ example, `npm ci` for a project with `itqan-cli` in its `devDependencies`, or
401
430
  | `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
431
  | `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
432
  | `itqan sync` | Same as `itqan install`. |
433
+ | `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
434
  | `itqan outdated` | List the locked assets that have newer versions: the newest your range allows and the newest overall. `--json` prints JSON. |
405
435
  | `itqan login` | Save your API key for the registry, after checking it. `--api-key` passes the key without the prompt. |
406
436
  | `itqan logout` | Remove the saved key. |
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "itqan-cli"
3
- version = "0.5.0"
3
+ version = "0.7.0"
4
4
  description = "Itqan Quranic Asset Manager: install and pin Itqan Quranic assets in your project."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -85,5 +85,6 @@ name = "cz_conventional_commits"
85
85
  version_provider = "pep621"
86
86
  version_scheme = "pep440"
87
87
  tag_format = "v$version"
88
+ major_version_zero = true
88
89
  update_changelog_on_bump = true
89
90
  changelog_file = "CHANGELOG.md"
@@ -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(self.name, self.language.latest_version, indent=indent, constraint=self.constraint)
38
- return entry_lines(
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 _ASSETS_KEY.match(line)), None)
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
 
@@ -188,13 +207,18 @@ def access_footer(packages: list[CatalogPackage], has_api_key: bool) -> list[str
188
207
  return lines
189
208
 
190
209
 
210
+ def kind_text(package: CatalogPackage) -> str:
211
+ """The category, narrowed by the tags of a text asset: `text: tafsir`."""
212
+ return f"{package.category}: {', '.join(package.tags)}" if package.tags else package.category
213
+
214
+
191
215
  def render_table(packages: list[CatalogPackage], targets: set[Target]) -> str:
192
216
  """Plain-text listing for pipes, CI and `--no-interactive`."""
193
217
  header = ("SLUG", "CATEGORY", "LANGUAGES", "LATEST", "ACCESS", "NAME")
194
218
  rows = [header] + [
195
219
  (
196
220
  ("* " if package.languages and not missing_languages(targets, package) else "") + package.slug,
197
- package.category,
221
+ kind_text(package),
198
222
  _languages_text(package),
199
223
  _latest_text(package),
200
224
  _access_column(package),
@@ -212,6 +236,7 @@ def to_json(packages: list[CatalogPackage], targets: set[Target]) -> list[dict]:
212
236
  "slug": package.slug,
213
237
  "name": package.name,
214
238
  "category": package.category,
239
+ "tags": list(package.tags),
215
240
  "is_open_access": package.is_open_access,
216
241
  "access": package.access,
217
242
  "access_request_url": package.access_request_url,
@@ -242,7 +267,7 @@ def choice_titles(packages: list[CatalogPackage], reasons: list[str | None], max
242
267
  """One aligned row per package that fits in ``max_width`` columns, leaving
243
268
  room for the picker's ` (<reason>)` note on rows that can't be picked."""
244
269
  columns = [
245
- (package.slug, package.category, _languages_text(package), _latest_text(package)) for package in packages
270
+ (package.slug, kind_text(package), _languages_text(package), _latest_text(package)) for package in packages
246
271
  ]
247
272
  widths = [max(len(row[col]) for row in columns) for col in range(4)]
248
273
  titles = []
@@ -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)
@@ -57,6 +63,8 @@ class CatalogPackage:
57
63
  # a registry that predates it, which only says whether the asset is open.
58
64
  access: str | None = None
59
65
  access_request_url: str | None = None
66
+ # Text assets: what kind of text it holds (tafsir, translation, ...). Slugs only.
67
+ tags: tuple[str, ...] = ()
60
68
 
61
69
 
62
70
  @dataclass(frozen=True, slots=True)
@@ -67,6 +75,40 @@ class Account:
67
75
  email: str
68
76
 
69
77
 
78
+ def parse_structure(raw: object) -> PackageStructure | None:
79
+ """A registry ``structure`` object, or None when it is absent, null or malformed.
80
+
81
+ The structure only feeds the column notes written above a manifest entry, so a
82
+ malformed one leaves the notes out rather than failing the whole registry call.
83
+ """
84
+ if not isinstance(raw, dict):
85
+ return None
86
+ template, header, columns = raw.get("template"), raw.get("header"), raw.get("columns")
87
+ if not isinstance(template, str) or not isinstance(header, list) or not isinstance(columns, list):
88
+ return None
89
+ if not all(isinstance(name, str) for name in header):
90
+ return None
91
+ if not all(isinstance(column, dict) and isinstance(column.get("key"), str) for column in columns):
92
+ return None
93
+ return PackageStructure(
94
+ template=template,
95
+ header=tuple(header),
96
+ columns=tuple(
97
+ PackageColumn(
98
+ key=column["key"],
99
+ label_en=_label(column.get("label_en")),
100
+ label_ar=_label(column.get("label_ar")),
101
+ )
102
+ for column in columns
103
+ ),
104
+ )
105
+
106
+
107
+ def _label(raw: object) -> str:
108
+ """A column label, or "" when it is missing or not text."""
109
+ return raw if isinstance(raw, str) else ""
110
+
111
+
70
112
  # The public API (not the cms.itqan.dev website, which serves the web app).
71
113
  DEFAULT_REGISTRY_URL = "https://api.cms.itqan.dev"
72
114
 
@@ -124,6 +166,7 @@ class RegistryClient:
124
166
  open_access: bool | None = None,
125
167
  search: str | None = None,
126
168
  category: str | None = None,
169
+ tag: str | None = None,
127
170
  page_size: int = 50,
128
171
  ) -> list[CatalogPackage]:
129
172
  """Call GET /packages/ for the first page of installable assets.
@@ -132,7 +175,7 @@ class RegistryClient:
132
175
  RegistryApiError when the registry can't be reached or answers badly.
133
176
  """
134
177
  packages, _ = self._catalog_page(
135
- open_access=open_access, search=search, category=category, page=1, page_size=page_size
178
+ open_access=open_access, search=search, category=category, tag=tag, page=1, page_size=page_size
136
179
  )
137
180
  return packages
138
181
 
@@ -142,6 +185,7 @@ class RegistryClient:
142
185
  open_access: bool | None = None,
143
186
  search: str | None = None,
144
187
  category: str | None = None,
188
+ tag: str | None = None,
145
189
  page_size: int = 50,
146
190
  ) -> list[CatalogPackage]:
147
191
  """Every installable asset matching the filters, following GET /packages/ pagination.
@@ -153,7 +197,7 @@ class RegistryClient:
153
197
  page = 1
154
198
  while True:
155
199
  batch, count = self._catalog_page(
156
- open_access=open_access, search=search, category=category, page=page, page_size=page_size
200
+ open_access=open_access, search=search, category=category, tag=tag, page=page, page_size=page_size
157
201
  )
158
202
  packages.extend(batch)
159
203
  if not batch or len(packages) >= count:
@@ -166,6 +210,7 @@ class RegistryClient:
166
210
  open_access: bool | None,
167
211
  search: str | None,
168
212
  category: str | None,
213
+ tag: str | None,
169
214
  page: int,
170
215
  page_size: int,
171
216
  ) -> tuple[list[CatalogPackage], int]:
@@ -179,6 +224,8 @@ class RegistryClient:
179
224
  params["search"] = search
180
225
  if category:
181
226
  params["category"] = category
227
+ if tag:
228
+ params["tag"] = tag
182
229
  try:
183
230
  response = self.session.get(
184
231
  f"{self.base_url}/packages/", params=params, headers=self._headers(), timeout=self.timeout
@@ -201,11 +248,13 @@ class RegistryClient:
201
248
  publisher_name=item.get("publisher_name"),
202
249
  access=item.get("access"),
203
250
  access_request_url=item.get("access_request_url"),
251
+ tags=tuple(tag_item["slug"] for tag_item in item.get("tags") or ()),
204
252
  languages=tuple(
205
253
  CatalogLanguage(
206
254
  language=lang["language"],
207
255
  is_source=lang["is_source"],
208
256
  latest_version=lang["latest_version"],
257
+ structure=parse_structure(lang.get("structure")),
209
258
  )
210
259
  for lang in item["languages"]
211
260
  ),
@@ -300,6 +349,7 @@ class RegistryClient:
300
349
  withdrawal_reason=item.get("withdrawal_reason"),
301
350
  latest_version=item.get("latest_version"),
302
351
  latest_in_range=item.get("latest_in_range"),
352
+ structure=parse_structure(item.get("structure")),
303
353
  )
304
354
  for item in results_raw
305
355
  ]
@@ -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