basstler-core 4.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. basstler_core-4.2.0/.gitignore +13 -0
  2. basstler_core-4.2.0/LICENSE +202 -0
  3. basstler_core-4.2.0/NOTICE +13 -0
  4. basstler_core-4.2.0/PKG-INFO +15 -0
  5. basstler_core-4.2.0/pyproject.toml +49 -0
  6. basstler_core-4.2.0/src/basstler/bash.py +122 -0
  7. basstler_core-4.2.0/src/basstler/class_name.py +22 -0
  8. basstler_core-4.2.0/src/basstler/class_property.py +41 -0
  9. basstler_core-4.2.0/src/basstler/command_line.py +92 -0
  10. basstler_core-4.2.0/src/basstler/credential_origin.py +23 -0
  11. basstler_core-4.2.0/src/basstler/directory_tree.py +54 -0
  12. basstler_core-4.2.0/src/basstler/email_address.py +64 -0
  13. basstler_core-4.2.0/src/basstler/environment.py +177 -0
  14. basstler_core-4.2.0/src/basstler/exceptions.py +639 -0
  15. basstler_core-4.2.0/src/basstler/exit_codes.py +323 -0
  16. basstler_core-4.2.0/src/basstler/json_model.py +221 -0
  17. basstler_core-4.2.0/src/basstler/locations.py +831 -0
  18. basstler_core-4.2.0/src/basstler/package_layout.py +87 -0
  19. basstler_core-4.2.0/src/basstler/package_map.py +502 -0
  20. basstler_core-4.2.0/src/basstler/plugin_root.py +52 -0
  21. basstler_core-4.2.0/src/basstler/render_common.py +188 -0
  22. basstler_core-4.2.0/src/basstler/shell_configuration.py +141 -0
  23. basstler_core-4.2.0/src/basstler/standard_streams.py +84 -0
  24. basstler_core-4.2.0/src/basstler/text_file.py +41 -0
  25. basstler_core-4.2.0/tests/__init__.py +0 -0
  26. basstler_core-4.2.0/tests/command_failures.py +38 -0
  27. basstler_core-4.2.0/tests/conftest.py +17 -0
  28. basstler_core-4.2.0/tests/test_bash.py +125 -0
  29. basstler_core-4.2.0/tests/test_class_name.py +31 -0
  30. basstler_core-4.2.0/tests/test_class_property.py +53 -0
  31. basstler_core-4.2.0/tests/test_command_line.py +33 -0
  32. basstler_core-4.2.0/tests/test_directory_tree.py +41 -0
  33. basstler_core-4.2.0/tests/test_email_address.py +44 -0
  34. basstler_core-4.2.0/tests/test_environment.py +31 -0
  35. basstler_core-4.2.0/tests/test_exit_codes.py +44 -0
  36. basstler_core-4.2.0/tests/test_locations.py +127 -0
  37. basstler_core-4.2.0/tests/test_package_map.py +209 -0
  38. basstler_core-4.2.0/tests/test_plugin_root.py +105 -0
  39. basstler_core-4.2.0/tests/test_render_common.py +143 -0
  40. basstler_core-4.2.0/tests/test_shell_configuration.py +25 -0
  41. basstler_core-4.2.0/tests/test_standard_streams.py +93 -0
  42. basstler_core-4.2.0/tests/test_text_file.py +69 -0
@@ -0,0 +1,13 @@
1
+ __pycache__/
2
+ *.egg-info/
3
+ .venv/
4
+ .pytest_cache/
5
+
6
+ # What the plugin's own session start writes into the project it serves - this
7
+ # repository, when developing it with its own plugin enabled. Personal, never committed.
8
+ CLAUDE.local.md
9
+ .claude/settings.local.json
10
+
11
+ # Everything the last maintenance pass did, written by `python3 -m basstler.maintenance
12
+ # run-report` beside the board. Scratch state, never committed.
13
+ src/basstler/maintenance-report.json
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,13 @@
1
+ basstler
2
+ Copyright 2026 Abdelrhman Bassiouny and the basstler contributors
3
+
4
+ basstler (https://github.com/KAOS-repositories/basstler) is licensed under the
5
+ Apache License, Version 2.0 (see LICENSE). Abdelrhman Bassiouny wrote it
6
+ independently of any employer or institution and maintains it in the
7
+ KAOS-repositories organization on GitHub; contributors keep the copyright on
8
+ their contributions.
9
+
10
+ packages/core/src/basstler/{exceptions,class_property}.py contain code adapted
11
+ from krrood (https://github.com/cram2/cognitive_robot_abstract_machine/tree/main/krrood),
12
+ copyright the krrood authors, which remains licensed under the GNU Lesser
13
+ General Public License, version 3 only (LGPL-3.0-only).
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.5
2
+ Name: basstler-core
3
+ Version: 4.2.0
4
+ Summary: The foundation every basstler distribution builds on: locations, environment variables, exceptions and the package map.
5
+ Project-URL: Source, https://github.com/KAOS-repositories/basstler
6
+ Project-URL: Bug Tracker, https://github.com/KAOS-repositories/basstler/issues
7
+ Author-email: Abdelrhman Bassiouny <bido.bassuny@gmail.com>
8
+ License-Expression: Apache-2.0
9
+ Requires-Python: >=3.12
10
+ Requires-Dist: cyclopts>=5.3
11
+ Requires-Dist: jinja2>=3
12
+ Requires-Dist: markdown>=3
13
+ Requires-Dist: nh3>=0.2
14
+ Requires-Dist: pydantic>=2.11
15
+ Requires-Dist: rustworkx>=0.15
@@ -0,0 +1,49 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.24", "hatch-vcs>=0.4", "basstler-build"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "basstler-core"
7
+ description = "The foundation every basstler distribution builds on: locations, environment variables, exceptions and the package map."
8
+ requires-python = ">=3.12"
9
+ license = "Apache-2.0"
10
+ license-files = ["LICENSE", "NOTICE"]
11
+ authors = [{name = "Abdelrhman Bassiouny", email = "bido.bassuny@gmail.com"}]
12
+ dynamic = ["version", "dependencies"]
13
+
14
+ [project.urls]
15
+ "Source" = "https://github.com/KAOS-repositories/basstler"
16
+ "Bug Tracker" = "https://github.com/KAOS-repositories/basstler/issues"
17
+
18
+ [tool.hatch.version]
19
+ source = "vcs"
20
+ fallback-version = "0.0.0"
21
+ raw-options = {root = "../.."}
22
+
23
+ # Built with every basstler requirement pinned to this release.
24
+ [tool.hatch.metadata.hooks.basstler-sibling-pins]
25
+ family = "basstler"
26
+ dependencies = [
27
+ "cyclopts>=5.3",
28
+ "jinja2>=3",
29
+ "markdown>=3",
30
+ "nh3>=0.2",
31
+ "pydantic>=2.11",
32
+ "rustworkx>=0.15",
33
+ ]
34
+
35
+ [tool.hatch.build.targets.wheel]
36
+ packages = ["src/basstler"]
37
+
38
+ # The repository's licence and notices, put at the root of the source distribution so
39
+ # the wheel built from it carries them.
40
+ [tool.hatch.build.targets.sdist.force-include]
41
+ "../../LICENSE" = "LICENSE"
42
+ "../../NOTICE" = "NOTICE"
43
+
44
+ # What this distribution's tests need besides itself.
45
+ [dependency-groups]
46
+ dev = [
47
+ "pytest",
48
+ "pytest-xdist",
49
+ ]
@@ -0,0 +1,122 @@
1
+ """
2
+ The bash the plugin's scripts and its shell configuration run in.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import os
8
+ import shutil
9
+ from collections.abc import Iterator, Mapping
10
+ from dataclasses import dataclass
11
+ from enum import StrEnum
12
+ from pathlib import Path
13
+
14
+ from basstler.environment import OperatingSystemVariable
15
+ from basstler.exceptions import BashNotFoundError
16
+ from basstler.locations import ClaudeCodeVariable
17
+
18
+ # %% the programs searched for
19
+
20
+
21
+ class LocatedProgram(StrEnum):
22
+ """
23
+ The programs searched for to find bash, by the name a search finds them under.
24
+ """
25
+
26
+ BASH = "bash"
27
+ """
28
+ The shell the scripts are written for.
29
+ """
30
+
31
+ GIT = "git"
32
+ """
33
+ Git, whose Windows installation ships Git Bash beside it.
34
+ """
35
+
36
+
37
+ # %% the bash itself
38
+
39
+
40
+ @dataclass(frozen=True)
41
+ class Bash:
42
+ """
43
+ A bash program, named by its full path so Windows cannot start WSL's launcher in its
44
+ place.
45
+ """
46
+
47
+ executable: Path
48
+ """
49
+ The program itself.
50
+ """
51
+
52
+ @classmethod
53
+ def locate(cls, environment: Mapping[str, str]) -> Bash:
54
+ """
55
+ Find the bash to run scripts in: the one Claude Code is configured with, else the
56
+ first on the search path, else the one installed with the git on the search
57
+ path - never one in the Windows directory, which starts WSL.
58
+
59
+ :param environment: The environment the scripts will run in.
60
+ :return: The bash found.
61
+ :raises BashNotFoundError: If every bash found is in the Windows directory, or
62
+ none is found.
63
+ """
64
+ configured = environment.get(ClaudeCodeVariable.GIT_BASH_PATH)
65
+ if configured:
66
+ return cls(Path(configured))
67
+ found = list(
68
+ cls.candidates(
69
+ environment.get(OperatingSystemVariable.EXECUTABLE_SEARCH_PATH)
70
+ )
71
+ )
72
+ windows_directory = environment.get(OperatingSystemVariable.WINDOWS_DIRECTORY)
73
+ usable = [
74
+ candidate
75
+ for candidate in found
76
+ if not cls.is_within(candidate, windows_directory)
77
+ ]
78
+ if not usable:
79
+ raise BashNotFoundError(passed_over=found)
80
+ return cls(usable[0])
81
+
82
+ @staticmethod
83
+ def candidates(search_path: str | None) -> Iterator[Path]:
84
+ """
85
+ :param search_path: The directories to search, the process's own when ``None``.
86
+ :return: Every bash found, in the order they are preferred: the first on the
87
+ search path, then those in a ``bin`` directory of an ancestor of the first
88
+ git on it.
89
+ """
90
+ on_search_path = shutil.which(LocatedProgram.BASH, path=search_path)
91
+ if on_search_path is not None:
92
+ yield Path(on_search_path)
93
+ git = shutil.which(LocatedProgram.GIT, path=search_path)
94
+ if git is None:
95
+ return
96
+ for installation in Path(git).parents:
97
+ beside_git = shutil.which(
98
+ LocatedProgram.BASH, path=str(installation / "bin")
99
+ )
100
+ if beside_git is not None:
101
+ yield Path(beside_git)
102
+
103
+ @staticmethod
104
+ def is_within(program: Path, directory: str | None) -> bool:
105
+ """
106
+ :param program: A program found.
107
+ :param directory: A directory, or ``None`` when there is none.
108
+ :return: Whether *program* is inside *directory*, compared as the platform
109
+ compares paths.
110
+ """
111
+ if directory is None:
112
+ return False
113
+ return Path(os.path.normcase(program)).is_relative_to(
114
+ os.path.normcase(directory)
115
+ )
116
+
117
+ def command(self, *arguments: str | os.PathLike[str]) -> list[str]:
118
+ """
119
+ :param arguments: The script to run and its arguments, or ``-c`` and a command.
120
+ :return: The command line running them in this bash.
121
+ """
122
+ return [str(self.executable), *(os.fspath(argument) for argument in arguments)]
@@ -0,0 +1,22 @@
1
+ """
2
+ A class's own name, spelled the way a command line or a document spells it.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import re
8
+
9
+
10
+ class HyphenatedClassName:
11
+ """
12
+ Gives a class the hyphenated, lower-case form of its own name.
13
+ """
14
+
15
+ @classmethod
16
+ def hyphenated_class_name(cls, suffix: str = "") -> str:
17
+ """
18
+ :param suffix: A trailing part of the class name to leave out.
19
+ :return: The class name in lower case, words joined by hyphens.
20
+ """
21
+ words = cls.__name__.removesuffix(suffix)
22
+ return re.sub(r"(?<!^)(?=[A-Z])", "-", words).lower()
@@ -0,0 +1,41 @@
1
+ """
2
+ A property that answers on the class rather than on an instance.
3
+
4
+ Copied from krrood :cite:`krrood` until krrood's light core is released.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from collections.abc import Callable
10
+ from typing import Any
11
+
12
+
13
+ class classproperty:
14
+ """
15
+ Reads a value off the class itself, and stays abstract until one is supplied.
16
+
17
+ ``property`` answers only on instances, and chaining ``classmethod`` with it was
18
+ removed in Python 3.13, so a value that belongs to the class rather than to any
19
+ instance needs a descriptor of its own. Written in this repository rather than taken
20
+ from ``krrood`` because this package's first version is deliberately independent of
21
+ it, which is the boundary decision 12 records for this tooling.
22
+
23
+ An abstract accessor answers with the descriptor instead of calling it, which is what
24
+ lets :class:`abc.ABCMeta` still see it as abstract on a subclass that supplied
25
+ nothing - the refusal then lands where every other missing abstract member's does.
26
+ """
27
+
28
+ def __init__(self, accessor: Callable[[type], Any]) -> None:
29
+ """:param accessor: The function answering the value, given the owning class."""
30
+ self.accessor = accessor
31
+ self.__isabstractmethod__ = getattr(accessor, "__isabstractmethod__", False)
32
+ self.__doc__ = accessor.__doc__
33
+
34
+ def __get__(self, instance: Any, owner: type | None = None) -> Any:
35
+ """:param instance: The instance read through, absent on class access.
36
+ :param owner: The class read from.
37
+ :return: The value for the owning class, or this descriptor while it is
38
+ abstract."""
39
+ if self.__isabstractmethod__:
40
+ return self
41
+ return self.accessor(owner if owner is not None else type(instance))
@@ -0,0 +1,92 @@
1
+ """
2
+ One command of a command-line tool, and how a tool finds every one of its commands.
3
+
4
+ A command owns its own name, its own flags and its own work, so adding one is writing a
5
+ subclass: :meth:`Command.every_command` finds it, and nothing else has to be told it
6
+ exists. What a command is handed to run and what it answers with differ per tool, so each
7
+ tool's own base declares ``run``.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+ import functools
14
+ from abc import ABC, abstractmethod
15
+ from dataclasses import dataclass
16
+ from typing import TypeVar
17
+
18
+ from cyclopts import Group, validators
19
+
20
+ from basstler.class_name import HyphenatedClassName
21
+ from basstler.class_property import classproperty
22
+
23
+ # %% parameter groups
24
+
25
+ plan_item_selection = Group(
26
+ "Item selection", validator=validators.LimitedChoice(1), default_parameter=None
27
+ )
28
+ """
29
+ The two ways a command names one plan item - by id or by branch - of which exactly one
30
+ is given.
31
+ """
32
+
33
+ # %% a command
34
+
35
+
36
+ CommandType = TypeVar("CommandType", bound="Command")
37
+ """
38
+ The concrete command type one tool's commands are found as.
39
+ """
40
+
41
+
42
+ @dataclass(frozen=True)
43
+ class Command(HyphenatedClassName, ABC):
44
+ """
45
+ One operation a command-line tool offers.
46
+
47
+ The name and the description belong to the class rather than to an instance - the
48
+ parser reads them to build the command line before anything is constructed - so they
49
+ are :class:`basstler.class_property.classproperty` members. The name follows from the
50
+ class name; the description is abstract, and :meth:`every_command` builds every
51
+ subclass, so a command that cannot say what it does is refused as its tool is
52
+ imported.
53
+ """
54
+
55
+ @classmethod
56
+ @functools.cache
57
+ def every_command(cls: type[CommandType]) -> tuple[CommandType, ...]:
58
+ """
59
+ Every command of the tool whose base this is asked of, in the order they are
60
+ defined.
61
+
62
+ Asked of a tool's own base, never of :class:`Command` itself: only direct
63
+ subclasses are found.
64
+
65
+ :return: One instance of every direct subclass of the class asked.
66
+ """
67
+ return tuple(subclass() for subclass in cls.__subclasses__())
68
+
69
+ @classproperty
70
+ def invoked_as(cls) -> str:
71
+ """
72
+ The name it is invoked by on the command line: its class name in lower case,
73
+ words joined by hyphens, without a trailing ``Command``.
74
+ """
75
+ return cls.hyphenated_class_name(Command.__name__)
76
+
77
+ @classproperty
78
+ @abstractmethod
79
+ def description(cls) -> str:
80
+ """
81
+ What it does, as ``--help`` puts it.
82
+ """
83
+
84
+ def declare_arguments(self, parser: argparse.ArgumentParser) -> None:
85
+ """
86
+ Declare this command's own flags.
87
+
88
+ Concrete rather than abstract: most commands take none, and requiring an empty
89
+ override of every one of them would say nothing.
90
+
91
+ :param parser: The subparser to declare them on.
92
+ """
@@ -0,0 +1,23 @@
1
+ """
2
+ Naming where a GitHub token came from, so a refusal can say which token to renew.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+
8
+ class CredentialOrigin:
9
+ """
10
+ Where the token a GitHub request authenticates with comes from.
11
+ """
12
+
13
+ def describe_credential(self) -> str:
14
+ """
15
+ :return: The token, named by where it came from.
16
+ """
17
+ raise NotImplementedError
18
+
19
+ def suggest_renewal(self) -> str:
20
+ """
21
+ :return: How to replace the token with a valid one.
22
+ """
23
+ raise NotImplementedError
@@ -0,0 +1,54 @@
1
+ """
2
+ Removing a directory and everything in it, read-only entries included.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import os
8
+ import shutil
9
+ import stat
10
+ from collections.abc import Callable
11
+ from dataclasses import dataclass
12
+ from pathlib import Path
13
+
14
+ # %% the tree
15
+
16
+
17
+ @dataclass(frozen=True)
18
+ class DirectoryTree:
19
+ """
20
+ A directory and everything in it, such as a clone whose object files git left
21
+ read-only, which Windows refuses to delete.
22
+ """
23
+
24
+ root: Path
25
+ """
26
+ The directory at the top of the tree.
27
+ """
28
+
29
+ def remove(self) -> None:
30
+ """
31
+ Delete the tree, making each entry that refuses deletion writable first; a tree
32
+ that is not there is left as it is.
33
+ """
34
+ if not self.root.exists():
35
+ return
36
+ shutil.rmtree(self.root, onexc=self.remove_writable)
37
+
38
+ @staticmethod
39
+ def remove_writable(
40
+ removal: Callable[[str], object], path: str, refusal: BaseException
41
+ ) -> None:
42
+ """
43
+ Make *path* and the directory holding it writable, then remove it again.
44
+
45
+ :param removal: The function that refused to remove *path*.
46
+ :param path: The entry it refused.
47
+ :param refusal: Why it refused, raised again when it is not a permission error.
48
+ """
49
+ if not isinstance(refusal, PermissionError):
50
+ raise refusal
51
+ writable = stat.S_IRWXU
52
+ os.chmod(os.path.dirname(path), writable)
53
+ os.chmod(path, writable)
54
+ removal(path)