itqan-cli 0.4.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.
Files changed (66) hide show
  1. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/CHANGELOG.md +33 -0
  2. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/PKG-INFO +36 -3
  3. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/README.md +35 -2
  4. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/pyproject.toml +1 -1
  5. itqan_cli-0.6.0/src/itqan_cli/autoupdate.py +93 -0
  6. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/browse.py +24 -5
  7. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/client.py +44 -9
  8. itqan_cli-0.6.0/src/itqan_cli/columns_comment.py +118 -0
  9. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/downloader.py +4 -1
  10. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/main.py +136 -2
  11. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/manifest.py +5 -0
  12. itqan_cli-0.6.0/src/itqan_cli/usage.py +97 -0
  13. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/conftest.py +3 -0
  14. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_add.py +97 -0
  15. itqan_cli-0.6.0/tests/test_autoupdate.py +177 -0
  16. itqan_cli-0.6.0/tests/test_cli_integration.py +437 -0
  17. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_client.py +110 -0
  18. itqan_cli-0.6.0/tests/test_columns_comment.py +137 -0
  19. itqan_cli-0.6.0/tests/test_usage.py +287 -0
  20. itqan_cli-0.4.0/tests/test_cli_integration.py +0 -234
  21. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/.gitignore +0 -0
  22. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/LICENSE +0 -0
  23. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/RELEASING.md +0 -0
  24. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/.gitignore +0 -0
  25. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/README.md +0 -0
  26. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/analysis_options.yaml +0 -0
  27. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/bin/itqan.dart +0 -0
  28. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/lib/itqan_cli.dart +0 -0
  29. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/lib/src/launcher.dart +0 -0
  30. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/lib/src/version.dart +0 -0
  31. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/pubspec.yaml +0 -0
  32. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/dart/test/launcher_test.dart +0 -0
  33. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/npm/README.md +0 -0
  34. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/npm/bin/itqan.js +0 -0
  35. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/scripts/dart_package.py +0 -0
  36. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/scripts/homebrew_formula.py +0 -0
  37. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/scripts/npm_packages.py +0 -0
  38. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/__init__.py +0 -0
  39. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/__main__.py +0 -0
  40. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/add.py +0 -0
  41. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/credentials.py +0 -0
  42. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/exceptions.py +0 -0
  43. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/flutter.py +0 -0
  44. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/init_template.py +0 -0
  45. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/lockfile.py +0 -0
  46. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/progress.py +0 -0
  47. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/semver.py +0 -0
  48. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/src/itqan_cli/updates.py +0 -0
  49. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/__init__.py +0 -0
  50. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_access.py +0 -0
  51. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_assets_dir.py +0 -0
  52. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_browse.py +0 -0
  53. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_dart_package.py +0 -0
  54. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_downloader.py +0 -0
  55. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_entry_points.py +0 -0
  56. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_flutter.py +0 -0
  57. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_homebrew_formula.py +0 -0
  58. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_init.py +0 -0
  59. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_install_progress.py +0 -0
  60. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_languages.py +0 -0
  61. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_lockfile.py +0 -0
  62. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_login.py +0 -0
  63. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_manifest.py +0 -0
  64. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_npm_packages.py +0 -0
  65. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_progress.py +0 -0
  66. {itqan_cli-0.4.0 → itqan_cli-0.6.0}/tests/test_updates.py +0 -0
@@ -1,3 +1,36 @@
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
+
25
+ ## v0.5.0 (2026-10-21)
26
+
27
+ ### Feat
28
+
29
+ - every request, including asset downloads, sends `User-Agent: itqan-cli/<version>`, a random
30
+ install ID (`X-Itqan-Install-Id`, kept in `<config dir>/install-id`) and the command's name
31
+ (`X-Itqan-Command`), so the registry can count CLI installs and usage. `DO_NOT_TRACK=1` or
32
+ `ITQAN_NO_TELEMETRY=1` leaves out the ID and command and creates no `install-id` file.
33
+
1
34
  ## v0.4.0 (2026-10-18)
2
35
 
3
36
  ### Feat
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: itqan-cli
3
- Version: 0.4.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. |
@@ -447,13 +476,17 @@ Add `--help` to any command for all its options. `install` and `browse` read the
447
476
  | `--api-key` | `ITQAN_API_KEY` | The key saved by `itqan login`, if any. |
448
477
  | `--registry-url` | `ITQAN_REGISTRY_URL` | `https://api.cms.itqan.dev`, the public registry. |
449
478
  | | `ITQAN_NO_UPDATE_NOTICE` | Set to `1` to stop `itqan install` saying that newer versions exist. |
450
- | | `ITQAN_CONFIG_DIR` | Where `itqan login` saves keys: `~/.config/itqan` (`%APPDATA%\itqan` on Windows). |
479
+ | | `DO_NOT_TRACK`, `ITQAN_NO_TELEMETRY` | Set to `1` to send no install ID or command name with requests (see below). |
480
+ | | `ITQAN_CONFIG_DIR` | Where `itqan login` saves keys and the install ID: `~/.config/itqan` (`%APPDATA%\itqan` on Windows). |
451
481
  | | `ITQAN_CACHE_DIR` | Dart package only. Where the downloaded program is kept: `~/Library/Caches/itqan` (macOS), `~/.cache/itqan` (Linux), `%LOCALAPPDATA%\itqan` (Windows). |
452
482
  | | `ITQAN_CLI_DOWNLOAD_URL` | Dart package only. A folder (`https://` or `file://`) with a release's programs and `SHA256SUMS`, such as a mirror, used instead of the GitHub Release. |
453
483
 
454
484
  A key passed with `--api-key` or `ITQAN_API_KEY` is used instead of the saved one. The saved key
455
485
  file is readable only by you.
456
486
 
487
+ **Usage counts.** Every request sends the CLI version (`User-Agent`), a random install ID kept in the config folder's `install-id` file, and the running command's name, so Itqan can count installs; nothing else about you or your project is sent.
488
+ Set `DO_NOT_TRACK=1` or `ITQAN_NO_TELEMETRY=1` to send only the version: no install ID or command, and no `install-id` file is created.
489
+
457
490
  ## Troubleshooting
458
491
 
459
492
  **"command not found: itqan".** The folder `itqan` was installed into isn't on your `PATH`. For
@@ -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. |
@@ -416,13 +445,17 @@ Add `--help` to any command for all its options. `install` and `browse` read the
416
445
  | `--api-key` | `ITQAN_API_KEY` | The key saved by `itqan login`, if any. |
417
446
  | `--registry-url` | `ITQAN_REGISTRY_URL` | `https://api.cms.itqan.dev`, the public registry. |
418
447
  | | `ITQAN_NO_UPDATE_NOTICE` | Set to `1` to stop `itqan install` saying that newer versions exist. |
419
- | | `ITQAN_CONFIG_DIR` | Where `itqan login` saves keys: `~/.config/itqan` (`%APPDATA%\itqan` on Windows). |
448
+ | | `DO_NOT_TRACK`, `ITQAN_NO_TELEMETRY` | Set to `1` to send no install ID or command name with requests (see below). |
449
+ | | `ITQAN_CONFIG_DIR` | Where `itqan login` saves keys and the install ID: `~/.config/itqan` (`%APPDATA%\itqan` on Windows). |
420
450
  | | `ITQAN_CACHE_DIR` | Dart package only. Where the downloaded program is kept: `~/Library/Caches/itqan` (macOS), `~/.cache/itqan` (Linux), `%LOCALAPPDATA%\itqan` (Windows). |
421
451
  | | `ITQAN_CLI_DOWNLOAD_URL` | Dart package only. A folder (`https://` or `file://`) with a release's programs and `SHA256SUMS`, such as a mirror, used instead of the GitHub Release. |
422
452
 
423
453
  A key passed with `--api-key` or `ITQAN_API_KEY` is used instead of the saved one. The saved key
424
454
  file is readable only by you.
425
455
 
456
+ **Usage counts.** Every request sends the CLI version (`User-Agent`), a random install ID kept in the config folder's `install-id` file, and the running command's name, so Itqan can count installs; nothing else about you or your project is sent.
457
+ Set `DO_NOT_TRACK=1` or `ITQAN_NO_TELEMETRY=1` to send only the version: no install ID or command, and no `install-id` file is created.
458
+
426
459
  ## Troubleshooting
427
460
 
428
461
  **"command not found: itqan".** The folder `itqan` was installed into isn't on your `PATH`. For
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "itqan-cli"
3
- version = "0.4.0"
3
+ version = "0.6.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"
@@ -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
 
@@ -3,11 +3,12 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  from dataclasses import dataclass
6
- from importlib.metadata import PackageNotFoundError, version
7
6
 
8
7
  import requests
9
8
 
9
+ from itqan_cli.columns_comment import PackageColumn, PackageStructure
10
10
  from itqan_cli.exceptions import RegistryApiError
11
+ from itqan_cli.usage import usage_headers
11
12
 
12
13
 
13
14
  @dataclass(frozen=True, slots=True)
@@ -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,15 +73,42 @@ class Account:
67
73
  email: str
68
74
 
69
75
 
70
- # The public API (not the cms.itqan.dev website, which serves the web app).
71
- DEFAULT_REGISTRY_URL = "https://api.cms.itqan.dev"
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
+ )
72
103
 
73
104
 
74
- def _cli_version() -> str:
75
- try:
76
- return version("itqan-cli")
77
- except PackageNotFoundError: # running from a source checkout that isn't installed
78
- return "unknown"
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
+
110
+ # The public API (not the cms.itqan.dev website, which serves the web app).
111
+ DEFAULT_REGISTRY_URL = "https://api.cms.itqan.dev"
79
112
 
80
113
 
81
114
  def _error_detail(response: requests.Response) -> str:
@@ -119,7 +152,7 @@ class RegistryClient:
119
152
  headers = {
120
153
  "Accept": "application/json",
121
154
  "Content-Type": "application/json",
122
- "User-Agent": f"itqan-cli/{_cli_version()}",
155
+ **usage_headers(),
123
156
  }
124
157
  if self.api_key:
125
158
  headers["X-API-Key"] = self.api_key
@@ -213,6 +246,7 @@ class RegistryClient:
213
246
  language=lang["language"],
214
247
  is_source=lang["is_source"],
215
248
  latest_version=lang["latest_version"],
249
+ structure=parse_structure(lang.get("structure")),
216
250
  )
217
251
  for lang in item["languages"]
218
252
  ),
@@ -307,6 +341,7 @@ class RegistryClient:
307
341
  withdrawal_reason=item.get("withdrawal_reason"),
308
342
  latest_version=item.get("latest_version"),
309
343
  latest_in_range=item.get("latest_in_range"),
344
+ structure=parse_structure(item.get("structure")),
310
345
  )
311
346
  for item in results_raw
312
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
@@ -13,6 +13,7 @@ import requests
13
13
 
14
14
  from itqan_cli.client import ResolvedAssetPayload
15
15
  from itqan_cli.exceptions import DownloadError
16
+ from itqan_cli.usage import usage_headers
16
17
 
17
18
  # Version markers live in <assets_dir>/.itqan/<entry name>, outside the asset folders, so
18
19
  # bundlers that ship a whole folder (Flutter) don't ship them. The folder name is reserved.
@@ -200,7 +201,9 @@ class AssetDownloader:
200
201
 
201
202
  try:
202
203
  with open(temp_fd, "wb") as f_out:
203
- with self.session.get(asset.download_url, stream=True, timeout=self.timeout) as resp:
204
+ with self.session.get(
205
+ asset.download_url, stream=True, timeout=self.timeout, headers=usage_headers()
206
+ ) as resp:
204
207
  if resp.status_code != 200:
205
208
  raise DownloadError(
206
209
  f"Failed to download '{asset.slug}' from {asset.download_url}: HTTP {resp.status_code}"