coolabah 0.1.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.
- coolabah-0.1.0/LICENSE.txt +373 -0
- coolabah-0.1.0/PKG-INFO +81 -0
- coolabah-0.1.0/README.md +63 -0
- coolabah-0.1.0/pyproject.toml +31 -0
- coolabah-0.1.0/pyproject.toml.orig +28 -0
- coolabah-0.1.0/src/coolabah/__init__.py +28 -0
- coolabah-0.1.0/src/coolabah/_io.py +93 -0
- coolabah-0.1.0/src/coolabah/assembler.py +185 -0
- coolabah-0.1.0/src/coolabah/nodes.py +226 -0
- coolabah-0.1.0/src/coolabah/types.py +136 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
Mozilla Public License Version 2.0
|
|
2
|
+
==================================
|
|
3
|
+
|
|
4
|
+
1. Definitions
|
|
5
|
+
--------------
|
|
6
|
+
|
|
7
|
+
1.1. "Contributor"
|
|
8
|
+
means each individual or legal entity that creates, contributes to
|
|
9
|
+
the creation of, or owns Covered Software.
|
|
10
|
+
|
|
11
|
+
1.2. "Contributor Version"
|
|
12
|
+
means the combination of the Contributions of others (if any) used
|
|
13
|
+
by a Contributor and that particular Contributor's Contribution.
|
|
14
|
+
|
|
15
|
+
1.3. "Contribution"
|
|
16
|
+
means Covered Software of a particular Contributor.
|
|
17
|
+
|
|
18
|
+
1.4. "Covered Software"
|
|
19
|
+
means Source Code Form to which the initial Contributor has attached
|
|
20
|
+
the notice in Exhibit A, the Executable Form of such Source Code
|
|
21
|
+
Form, and Modifications of such Source Code Form, in each case
|
|
22
|
+
including portions thereof.
|
|
23
|
+
|
|
24
|
+
1.5. "Incompatible With Secondary Licenses"
|
|
25
|
+
means
|
|
26
|
+
|
|
27
|
+
(a) that the initial Contributor has attached the notice described
|
|
28
|
+
in Exhibit B to the Covered Software; or
|
|
29
|
+
|
|
30
|
+
(b) that the Covered Software was made available under the terms of
|
|
31
|
+
version 1.1 or earlier of the License, but not also under the
|
|
32
|
+
terms of a Secondary License.
|
|
33
|
+
|
|
34
|
+
1.6. "Executable Form"
|
|
35
|
+
means any form of the work other than Source Code Form.
|
|
36
|
+
|
|
37
|
+
1.7. "Larger Work"
|
|
38
|
+
means a work that combines Covered Software with other material, in
|
|
39
|
+
a separate file or files, that is not Covered Software.
|
|
40
|
+
|
|
41
|
+
1.8. "License"
|
|
42
|
+
means this document.
|
|
43
|
+
|
|
44
|
+
1.9. "Licensable"
|
|
45
|
+
means having the right to grant, to the maximum extent possible,
|
|
46
|
+
whether at the time of the initial grant or subsequently, any and
|
|
47
|
+
all of the rights conveyed by this License.
|
|
48
|
+
|
|
49
|
+
1.10. "Modifications"
|
|
50
|
+
means any of the following:
|
|
51
|
+
|
|
52
|
+
(a) any file in Source Code Form that results from an addition to,
|
|
53
|
+
deletion from, or modification of the contents of Covered
|
|
54
|
+
Software; or
|
|
55
|
+
|
|
56
|
+
(b) any new file in Source Code Form that contains any Covered
|
|
57
|
+
Software.
|
|
58
|
+
|
|
59
|
+
1.11. "Patent Claims" of a Contributor
|
|
60
|
+
means any patent claim(s), including without limitation, method,
|
|
61
|
+
process, and apparatus claims, in any patent Licensable by such
|
|
62
|
+
Contributor that would be infringed, but for the grant of the
|
|
63
|
+
License, by the making, using, selling, offering for sale, having
|
|
64
|
+
made, import, or transfer of either its Contributions or its
|
|
65
|
+
Contributor Version.
|
|
66
|
+
|
|
67
|
+
1.12. "Secondary License"
|
|
68
|
+
means either the GNU General Public License, Version 2.0, the GNU
|
|
69
|
+
Lesser General Public License, Version 2.1, the GNU Affero General
|
|
70
|
+
Public License, Version 3.0, or any later versions of those
|
|
71
|
+
licenses.
|
|
72
|
+
|
|
73
|
+
1.13. "Source Code Form"
|
|
74
|
+
means the form of the work preferred for making modifications.
|
|
75
|
+
|
|
76
|
+
1.14. "You" (or "Your")
|
|
77
|
+
means an individual or a legal entity exercising rights under this
|
|
78
|
+
License. For legal entities, "You" includes any entity that
|
|
79
|
+
controls, is controlled by, or is under common control with You. For
|
|
80
|
+
purposes of this definition, "control" means (a) the power, direct
|
|
81
|
+
or indirect, to cause the direction or management of such entity,
|
|
82
|
+
whether by contract or otherwise, or (b) ownership of more than
|
|
83
|
+
fifty percent (50%) of the outstanding shares or beneficial
|
|
84
|
+
ownership of such entity.
|
|
85
|
+
|
|
86
|
+
2. License Grants and Conditions
|
|
87
|
+
--------------------------------
|
|
88
|
+
|
|
89
|
+
2.1. Grants
|
|
90
|
+
|
|
91
|
+
Each Contributor hereby grants You a world-wide, royalty-free,
|
|
92
|
+
non-exclusive license:
|
|
93
|
+
|
|
94
|
+
(a) under intellectual property rights (other than patent or trademark)
|
|
95
|
+
Licensable by such Contributor to use, reproduce, make available,
|
|
96
|
+
modify, display, perform, distribute, and otherwise exploit its
|
|
97
|
+
Contributions, either on an unmodified basis, with Modifications, or
|
|
98
|
+
as part of a Larger Work; and
|
|
99
|
+
|
|
100
|
+
(b) under Patent Claims of such Contributor to make, use, sell, offer
|
|
101
|
+
for sale, have made, import, and otherwise transfer either its
|
|
102
|
+
Contributions or its Contributor Version.
|
|
103
|
+
|
|
104
|
+
2.2. Effective Date
|
|
105
|
+
|
|
106
|
+
The licenses granted in Section 2.1 with respect to any Contribution
|
|
107
|
+
become effective for each Contribution on the date the Contributor first
|
|
108
|
+
distributes such Contribution.
|
|
109
|
+
|
|
110
|
+
2.3. Limitations on Grant Scope
|
|
111
|
+
|
|
112
|
+
The licenses granted in this Section 2 are the only rights granted under
|
|
113
|
+
this License. No additional rights or licenses will be implied from the
|
|
114
|
+
distribution or licensing of Covered Software under this License.
|
|
115
|
+
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
|
116
|
+
Contributor:
|
|
117
|
+
|
|
118
|
+
(a) for any code that a Contributor has removed from Covered Software;
|
|
119
|
+
or
|
|
120
|
+
|
|
121
|
+
(b) for infringements caused by: (i) Your and any other third party's
|
|
122
|
+
modifications of Covered Software, or (ii) the combination of its
|
|
123
|
+
Contributions with other software (except as part of its Contributor
|
|
124
|
+
Version); or
|
|
125
|
+
|
|
126
|
+
(c) under Patent Claims infringed by Covered Software in the absence of
|
|
127
|
+
its Contributions.
|
|
128
|
+
|
|
129
|
+
This License does not grant any rights in the trademarks, service marks,
|
|
130
|
+
or logos of any Contributor (except as may be necessary to comply with
|
|
131
|
+
the notice requirements in Section 3.4).
|
|
132
|
+
|
|
133
|
+
2.4. Subsequent Licenses
|
|
134
|
+
|
|
135
|
+
No Contributor makes additional grants as a result of Your choice to
|
|
136
|
+
distribute the Covered Software under a subsequent version of this
|
|
137
|
+
License (see Section 10.2) or under the terms of a Secondary License (if
|
|
138
|
+
permitted under the terms of Section 3.3).
|
|
139
|
+
|
|
140
|
+
2.5. Representation
|
|
141
|
+
|
|
142
|
+
Each Contributor represents that the Contributor believes its
|
|
143
|
+
Contributions are its original creation(s) or it has sufficient rights
|
|
144
|
+
to grant the rights to its Contributions conveyed by this License.
|
|
145
|
+
|
|
146
|
+
2.6. Fair Use
|
|
147
|
+
|
|
148
|
+
This License is not intended to limit any rights You have under
|
|
149
|
+
applicable copyright doctrines of fair use, fair dealing, or other
|
|
150
|
+
equivalents.
|
|
151
|
+
|
|
152
|
+
2.7. Conditions
|
|
153
|
+
|
|
154
|
+
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
|
|
155
|
+
in Section 2.1.
|
|
156
|
+
|
|
157
|
+
3. Responsibilities
|
|
158
|
+
-------------------
|
|
159
|
+
|
|
160
|
+
3.1. Distribution of Source Form
|
|
161
|
+
|
|
162
|
+
All distribution of Covered Software in Source Code Form, including any
|
|
163
|
+
Modifications that You create or to which You contribute, must be under
|
|
164
|
+
the terms of this License. You must inform recipients that the Source
|
|
165
|
+
Code Form of the Covered Software is governed by the terms of this
|
|
166
|
+
License, and how they can obtain a copy of this License. You may not
|
|
167
|
+
attempt to alter or restrict the recipients' rights in the Source Code
|
|
168
|
+
Form.
|
|
169
|
+
|
|
170
|
+
3.2. Distribution of Executable Form
|
|
171
|
+
|
|
172
|
+
If You distribute Covered Software in Executable Form then:
|
|
173
|
+
|
|
174
|
+
(a) such Covered Software must also be made available in Source Code
|
|
175
|
+
Form, as described in Section 3.1, and You must inform recipients of
|
|
176
|
+
the Executable Form how they can obtain a copy of such Source Code
|
|
177
|
+
Form by reasonable means in a timely manner, at a charge no more
|
|
178
|
+
than the cost of distribution to the recipient; and
|
|
179
|
+
|
|
180
|
+
(b) You may distribute such Executable Form under the terms of this
|
|
181
|
+
License, or sublicense it under different terms, provided that the
|
|
182
|
+
license for the Executable Form does not attempt to limit or alter
|
|
183
|
+
the recipients' rights in the Source Code Form under this License.
|
|
184
|
+
|
|
185
|
+
3.3. Distribution of a Larger Work
|
|
186
|
+
|
|
187
|
+
You may create and distribute a Larger Work under terms of Your choice,
|
|
188
|
+
provided that You also comply with the requirements of this License for
|
|
189
|
+
the Covered Software. If the Larger Work is a combination of Covered
|
|
190
|
+
Software with a work governed by one or more Secondary Licenses, and the
|
|
191
|
+
Covered Software is not Incompatible With Secondary Licenses, this
|
|
192
|
+
License permits You to additionally distribute such Covered Software
|
|
193
|
+
under the terms of such Secondary License(s), so that the recipient of
|
|
194
|
+
the Larger Work may, at their option, further distribute the Covered
|
|
195
|
+
Software under the terms of either this License or such Secondary
|
|
196
|
+
License(s).
|
|
197
|
+
|
|
198
|
+
3.4. Notices
|
|
199
|
+
|
|
200
|
+
You may not remove or alter the substance of any license notices
|
|
201
|
+
(including copyright notices, patent notices, disclaimers of warranty,
|
|
202
|
+
or limitations of liability) contained within the Source Code Form of
|
|
203
|
+
the Covered Software, except that You may alter any license notices to
|
|
204
|
+
the extent required to remedy known factual inaccuracies.
|
|
205
|
+
|
|
206
|
+
3.5. Application of Additional Terms
|
|
207
|
+
|
|
208
|
+
You may choose to offer, and to charge a fee for, warranty, support,
|
|
209
|
+
indemnity or liability obligations to one or more recipients of Covered
|
|
210
|
+
Software. However, You may do so only on Your own behalf, and not on
|
|
211
|
+
behalf of any Contributor. You must make it absolutely clear that any
|
|
212
|
+
such warranty, support, indemnity, or liability obligation is offered by
|
|
213
|
+
You alone, and You hereby agree to indemnify every Contributor for any
|
|
214
|
+
liability incurred by such Contributor as a result of warranty, support,
|
|
215
|
+
indemnity or liability terms You offer. You may include additional
|
|
216
|
+
disclaimers of warranty and limitations of liability specific to any
|
|
217
|
+
jurisdiction.
|
|
218
|
+
|
|
219
|
+
4. Inability to Comply Due to Statute or Regulation
|
|
220
|
+
---------------------------------------------------
|
|
221
|
+
|
|
222
|
+
If it is impossible for You to comply with any of the terms of this
|
|
223
|
+
License with respect to some or all of the Covered Software due to
|
|
224
|
+
statute, judicial order, or regulation then You must: (a) comply with
|
|
225
|
+
the terms of this License to the maximum extent possible; and (b)
|
|
226
|
+
describe the limitations and the code they affect. Such description must
|
|
227
|
+
be placed in a text file included with all distributions of the Covered
|
|
228
|
+
Software under this License. Except to the extent prohibited by statute
|
|
229
|
+
or regulation, such description must be sufficiently detailed for a
|
|
230
|
+
recipient of ordinary skill to be able to understand it.
|
|
231
|
+
|
|
232
|
+
5. Termination
|
|
233
|
+
--------------
|
|
234
|
+
|
|
235
|
+
5.1. The rights granted under this License will terminate automatically
|
|
236
|
+
if You fail to comply with any of its terms. However, if You become
|
|
237
|
+
compliant, then the rights granted under this License from a particular
|
|
238
|
+
Contributor are reinstated (a) provisionally, unless and until such
|
|
239
|
+
Contributor explicitly and finally terminates Your grants, and (b) on an
|
|
240
|
+
ongoing basis, if such Contributor fails to notify You of the
|
|
241
|
+
non-compliance by some reasonable means prior to 60 days after You have
|
|
242
|
+
come back into compliance. Moreover, Your grants from a particular
|
|
243
|
+
Contributor are reinstated on an ongoing basis if such Contributor
|
|
244
|
+
notifies You of the non-compliance by some reasonable means, this is the
|
|
245
|
+
first time You have received notice of non-compliance with this License
|
|
246
|
+
from such Contributor, and You become compliant prior to 30 days after
|
|
247
|
+
Your receipt of the notice.
|
|
248
|
+
|
|
249
|
+
5.2. If You initiate litigation against any entity by asserting a patent
|
|
250
|
+
infringement claim (excluding declaratory judgment actions,
|
|
251
|
+
counter-claims, and cross-claims) alleging that a Contributor Version
|
|
252
|
+
directly or indirectly infringes any patent, then the rights granted to
|
|
253
|
+
You by any and all Contributors for the Covered Software under Section
|
|
254
|
+
2.1 of this License shall terminate.
|
|
255
|
+
|
|
256
|
+
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
|
|
257
|
+
end user license agreements (excluding distributors and resellers) which
|
|
258
|
+
have been validly granted by You or Your distributors under this License
|
|
259
|
+
prior to termination shall survive termination.
|
|
260
|
+
|
|
261
|
+
************************************************************************
|
|
262
|
+
* *
|
|
263
|
+
* 6. Disclaimer of Warranty *
|
|
264
|
+
* ------------------------- *
|
|
265
|
+
* *
|
|
266
|
+
* Covered Software is provided under this License on an "as is" *
|
|
267
|
+
* basis, without warranty of any kind, either expressed, implied, or *
|
|
268
|
+
* statutory, including, without limitation, warranties that the *
|
|
269
|
+
* Covered Software is free of defects, merchantable, fit for a *
|
|
270
|
+
* particular purpose or non-infringing. The entire risk as to the *
|
|
271
|
+
* quality and performance of the Covered Software is with You. *
|
|
272
|
+
* Should any Covered Software prove defective in any respect, You *
|
|
273
|
+
* (not any Contributor) assume the cost of any necessary servicing, *
|
|
274
|
+
* repair, or correction. This disclaimer of warranty constitutes an *
|
|
275
|
+
* essential part of this License. No use of any Covered Software is *
|
|
276
|
+
* authorized under this License except under this disclaimer. *
|
|
277
|
+
* *
|
|
278
|
+
************************************************************************
|
|
279
|
+
|
|
280
|
+
************************************************************************
|
|
281
|
+
* *
|
|
282
|
+
* 7. Limitation of Liability *
|
|
283
|
+
* -------------------------- *
|
|
284
|
+
* *
|
|
285
|
+
* Under no circumstances and under no legal theory, whether tort *
|
|
286
|
+
* (including negligence), contract, or otherwise, shall any *
|
|
287
|
+
* Contributor, or anyone who distributes Covered Software as *
|
|
288
|
+
* permitted above, be liable to You for any direct, indirect, *
|
|
289
|
+
* special, incidental, or consequential damages of any character *
|
|
290
|
+
* including, without limitation, damages for lost profits, loss of *
|
|
291
|
+
* goodwill, work stoppage, computer failure or malfunction, or any *
|
|
292
|
+
* and all other commercial damages or losses, even if such party *
|
|
293
|
+
* shall have been informed of the possibility of such damages. This *
|
|
294
|
+
* limitation of liability shall not apply to liability for death or *
|
|
295
|
+
* personal injury resulting from such party's negligence to the *
|
|
296
|
+
* extent applicable law prohibits such limitation. Some *
|
|
297
|
+
* jurisdictions do not allow the exclusion or limitation of *
|
|
298
|
+
* incidental or consequential damages, so this exclusion and *
|
|
299
|
+
* limitation may not apply to You. *
|
|
300
|
+
* *
|
|
301
|
+
************************************************************************
|
|
302
|
+
|
|
303
|
+
8. Litigation
|
|
304
|
+
-------------
|
|
305
|
+
|
|
306
|
+
Any litigation relating to this License may be brought only in the
|
|
307
|
+
courts of a jurisdiction where the defendant maintains its principal
|
|
308
|
+
place of business and such litigation shall be governed by laws of that
|
|
309
|
+
jurisdiction, without reference to its conflict-of-law provisions.
|
|
310
|
+
Nothing in this Section shall prevent a party's ability to bring
|
|
311
|
+
cross-claims or counter-claims.
|
|
312
|
+
|
|
313
|
+
9. Miscellaneous
|
|
314
|
+
----------------
|
|
315
|
+
|
|
316
|
+
This License represents the complete agreement concerning the subject
|
|
317
|
+
matter hereof. If any provision of this License is held to be
|
|
318
|
+
unenforceable, such provision shall be reformed only to the extent
|
|
319
|
+
necessary to make it enforceable. Any law or regulation which provides
|
|
320
|
+
that the language of a contract shall be construed against the drafter
|
|
321
|
+
shall not be used to construe this License against a Contributor.
|
|
322
|
+
|
|
323
|
+
10. Versions of the License
|
|
324
|
+
---------------------------
|
|
325
|
+
|
|
326
|
+
10.1. New Versions
|
|
327
|
+
|
|
328
|
+
Mozilla Foundation is the license steward. Except as provided in Section
|
|
329
|
+
10.3, no one other than the license steward has the right to modify or
|
|
330
|
+
publish new versions of this License. Each version will be given a
|
|
331
|
+
distinguishing version number.
|
|
332
|
+
|
|
333
|
+
10.2. Effect of New Versions
|
|
334
|
+
|
|
335
|
+
You may distribute the Covered Software under the terms of the version
|
|
336
|
+
of the License under which You originally received the Covered Software,
|
|
337
|
+
or under the terms of any subsequent version published by the license
|
|
338
|
+
steward.
|
|
339
|
+
|
|
340
|
+
10.3. Modified Versions
|
|
341
|
+
|
|
342
|
+
If you create software not governed by this License, and you want to
|
|
343
|
+
create a new license for such software, you may create and use a
|
|
344
|
+
modified version of this License if you rename the license and remove
|
|
345
|
+
any references to the name of the license steward (except to note that
|
|
346
|
+
such modified license differs from this License).
|
|
347
|
+
|
|
348
|
+
10.4. Distributing Source Code Form that is Incompatible With Secondary
|
|
349
|
+
Licenses
|
|
350
|
+
|
|
351
|
+
If You choose to distribute Source Code Form that is Incompatible With
|
|
352
|
+
Secondary Licenses under the terms of this version of the License, the
|
|
353
|
+
notice described in Exhibit B of this License must be attached.
|
|
354
|
+
|
|
355
|
+
Exhibit A - Source Code Form License Notice
|
|
356
|
+
-------------------------------------------
|
|
357
|
+
|
|
358
|
+
This Source Code Form is subject to the terms of the Mozilla Public
|
|
359
|
+
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
360
|
+
file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
361
|
+
|
|
362
|
+
If it is not possible or desirable to put the notice in a particular
|
|
363
|
+
file, then You may include the notice in a location (such as a LICENSE
|
|
364
|
+
file in a relevant directory) where a recipient would be likely to look
|
|
365
|
+
for such a notice.
|
|
366
|
+
|
|
367
|
+
You may add additional accurate notices of copyright ownership.
|
|
368
|
+
|
|
369
|
+
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
|
370
|
+
---------------------------------------------------------
|
|
371
|
+
|
|
372
|
+
This Source Code Form is "Incompatible With Secondary Licenses", as
|
|
373
|
+
defined by the Mozilla Public License, v. 2.0.
|
coolabah-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: coolabah
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Generate and assemble file trees using Python generators.
|
|
5
|
+
Author: Jahin Z.
|
|
6
|
+
Author-email: Jahin Z. <git@jahinzee.net>
|
|
7
|
+
License-Expression: MPL-2.0
|
|
8
|
+
License-File: LICENSE.txt
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Topic :: System :: Filesystems
|
|
12
|
+
Classifier: Typing :: Typed
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Requires-Python: >=3.12
|
|
15
|
+
Project-URL: Homepage, https://codeberg.org/jahinzee/coolabah
|
|
16
|
+
Project-URL: Issues, https://codeberg.org/jahinzee/coolabah/issues
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# Coolabah
|
|
20
|
+
|
|
21
|
+
[](https://codeberg.org/jahinzee/coolabah)
|
|
22
|
+
[](https://opensource.org/licenses/MPL-2.0)
|
|
23
|
+
[](https://brainmade.org/)
|
|
24
|
+
|
|
25
|
+
Generate and assemble file trees using Python generators.
|
|
26
|
+
|
|
27
|
+
- Composable and easy-to-use interface.
|
|
28
|
+
- Smart directory writing, only updates files between if necessary, and cleans unknown files.
|
|
29
|
+
- Supports parallel file generation.
|
|
30
|
+
- No third-party dependencies.
|
|
31
|
+
|
|
32
|
+
To get started, see the [Handbook](docs/handbook.md).
|
|
33
|
+
|
|
34
|
+
```py
|
|
35
|
+
from coolabah import directory, text_file, assemble
|
|
36
|
+
|
|
37
|
+
@directory
|
|
38
|
+
def root():
|
|
39
|
+
yield "foo.txt", text_file("Hello, World!")
|
|
40
|
+
yield "baz.txt", text_file("Ave, munde!")
|
|
41
|
+
yield "bar.txt", text_file("こんにちは、世界!")
|
|
42
|
+
|
|
43
|
+
if __name__ == "__main__":
|
|
44
|
+
assemble(root(), destination="serve/")
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
$ python ./fruit_shop.py
|
|
49
|
+
...
|
|
50
|
+
[Info] Tree generation complete.
|
|
51
|
+
$ tree serve
|
|
52
|
+
serve/
|
|
53
|
+
├── foo.txt
|
|
54
|
+
├── bar.txt
|
|
55
|
+
└── baz.txt
|
|
56
|
+
|
|
57
|
+
1 directory, 3 files
|
|
58
|
+
|
|
59
|
+
$ cat serve/*.txt
|
|
60
|
+
Hello, World!
|
|
61
|
+
Ave, munde!
|
|
62
|
+
こんにちは、世界!
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
## License and Acknowledgements
|
|
67
|
+
|
|
68
|
+
Coolabah is [open-source software][osd], and is licensed under the
|
|
69
|
+
[Mozilla Public License, v. 2.0.][mplv2.0].
|
|
70
|
+
|
|
71
|
+
[osd]: https://opensource.org/osd
|
|
72
|
+
[mplv2.0]: https://www.mozilla.org/en-US/MPL/2.0/
|
|
73
|
+
|
|
74
|
+
The [Coolabah logos](docs/coolabah.svg) are available under
|
|
75
|
+
[Creative Commons Attribution-ShareAlike 4.0 International][cc-by-sa-4.0].
|
|
76
|
+
|
|
77
|
+
[cc-by-sa-4.0]: https://creativecommons.org/licenses/by-sa/4.0/
|
|
78
|
+
|
|
79
|
+
Coolabah is a [Brainmade] project, no generative AI tools were used during development.
|
|
80
|
+
|
|
81
|
+
[Brainmade]: https://brainmade.org/
|
coolabah-0.1.0/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Coolabah
|
|
2
|
+
|
|
3
|
+
[](https://codeberg.org/jahinzee/coolabah)
|
|
4
|
+
[](https://opensource.org/licenses/MPL-2.0)
|
|
5
|
+
[](https://brainmade.org/)
|
|
6
|
+
|
|
7
|
+
Generate and assemble file trees using Python generators.
|
|
8
|
+
|
|
9
|
+
- Composable and easy-to-use interface.
|
|
10
|
+
- Smart directory writing, only updates files between if necessary, and cleans unknown files.
|
|
11
|
+
- Supports parallel file generation.
|
|
12
|
+
- No third-party dependencies.
|
|
13
|
+
|
|
14
|
+
To get started, see the [Handbook](docs/handbook.md).
|
|
15
|
+
|
|
16
|
+
```py
|
|
17
|
+
from coolabah import directory, text_file, assemble
|
|
18
|
+
|
|
19
|
+
@directory
|
|
20
|
+
def root():
|
|
21
|
+
yield "foo.txt", text_file("Hello, World!")
|
|
22
|
+
yield "baz.txt", text_file("Ave, munde!")
|
|
23
|
+
yield "bar.txt", text_file("こんにちは、世界!")
|
|
24
|
+
|
|
25
|
+
if __name__ == "__main__":
|
|
26
|
+
assemble(root(), destination="serve/")
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
$ python ./fruit_shop.py
|
|
31
|
+
...
|
|
32
|
+
[Info] Tree generation complete.
|
|
33
|
+
$ tree serve
|
|
34
|
+
serve/
|
|
35
|
+
├── foo.txt
|
|
36
|
+
├── bar.txt
|
|
37
|
+
└── baz.txt
|
|
38
|
+
|
|
39
|
+
1 directory, 3 files
|
|
40
|
+
|
|
41
|
+
$ cat serve/*.txt
|
|
42
|
+
Hello, World!
|
|
43
|
+
Ave, munde!
|
|
44
|
+
こんにちは、世界!
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## License and Acknowledgements
|
|
49
|
+
|
|
50
|
+
Coolabah is [open-source software][osd], and is licensed under the
|
|
51
|
+
[Mozilla Public License, v. 2.0.][mplv2.0].
|
|
52
|
+
|
|
53
|
+
[osd]: https://opensource.org/osd
|
|
54
|
+
[mplv2.0]: https://www.mozilla.org/en-US/MPL/2.0/
|
|
55
|
+
|
|
56
|
+
The [Coolabah logos](docs/coolabah.svg) are available under
|
|
57
|
+
[Creative Commons Attribution-ShareAlike 4.0 International][cc-by-sa-4.0].
|
|
58
|
+
|
|
59
|
+
[cc-by-sa-4.0]: https://creativecommons.org/licenses/by-sa/4.0/
|
|
60
|
+
|
|
61
|
+
Coolabah is a [Brainmade] project, no generative AI tools were used during development.
|
|
62
|
+
|
|
63
|
+
[Brainmade]: https://brainmade.org/
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "coolabah"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Generate and assemble file trees using Python generators."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MPL-2.0"
|
|
7
|
+
license-files = ["LICENSE.txt"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
dependencies = []
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 3 - Alpha",
|
|
12
|
+
"Environment :: Console",
|
|
13
|
+
"Topic :: System :: Filesystems",
|
|
14
|
+
"Typing :: Typed",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[[project.authors]]
|
|
19
|
+
name = "Jahin Z."
|
|
20
|
+
email = "git@jahinzee.net"
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://codeberg.org/jahinzee/coolabah"
|
|
24
|
+
Issues = "https://codeberg.org/jahinzee/coolabah/issues"
|
|
25
|
+
|
|
26
|
+
[build-system]
|
|
27
|
+
requires = ["uv_build>=0.13.6,<0.14.0"]
|
|
28
|
+
build-backend = "uv_build"
|
|
29
|
+
|
|
30
|
+
[tool.ruff]
|
|
31
|
+
line-length = 100
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "coolabah"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Generate and assemble file trees using Python generators."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MPL-2.0"
|
|
7
|
+
license-files = ["LICENSE.txt"]
|
|
8
|
+
authors = [{ name = "Jahin Z.", email = "git@jahinzee.net" }]
|
|
9
|
+
requires-python = ">=3.12"
|
|
10
|
+
dependencies = []
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 3 - Alpha",
|
|
13
|
+
"Environment :: Console",
|
|
14
|
+
"Topic :: System :: Filesystems",
|
|
15
|
+
"Typing :: Typed",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["uv_build>=0.13.6,<0.14.0"]
|
|
21
|
+
build-backend = "uv_build"
|
|
22
|
+
|
|
23
|
+
[tool.ruff]
|
|
24
|
+
line-length = 100
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://codeberg.org/jahinzee/coolabah"
|
|
28
|
+
Issues = "https://codeberg.org/jahinzee/coolabah/issues"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# coolabah -- basic exports.
|
|
2
|
+
#
|
|
3
|
+
# Author: Jahin Z. <git@jahinzee.net>
|
|
4
|
+
#
|
|
5
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
6
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
7
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
8
|
+
#
|
|
9
|
+
|
|
10
|
+
from coolabah.assembler import assemble
|
|
11
|
+
from coolabah.nodes import (
|
|
12
|
+
copy_file,
|
|
13
|
+
directory,
|
|
14
|
+
iterable_directory,
|
|
15
|
+
json_file,
|
|
16
|
+
mapping_directory,
|
|
17
|
+
text_file,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = (
|
|
21
|
+
"assemble",
|
|
22
|
+
"copy_file",
|
|
23
|
+
"directory",
|
|
24
|
+
"iterable_directory",
|
|
25
|
+
"json_file",
|
|
26
|
+
"mapping_directory",
|
|
27
|
+
"text_file",
|
|
28
|
+
)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# coolabah -- Internal submodule for status logging.
|
|
2
|
+
#
|
|
3
|
+
# Author: Jahin Z. <git@jahinzee.net>
|
|
4
|
+
#
|
|
5
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
6
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
7
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
8
|
+
#
|
|
9
|
+
|
|
10
|
+
__package__ = "coolabah"
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
import platform
|
|
14
|
+
import sys
|
|
15
|
+
from enum import Enum
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _eprint(*msg: object, sep: str = " "):
|
|
19
|
+
print(*msg, file=sys.stderr, sep=sep)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class _Colour(Enum):
|
|
23
|
+
Reset = "0"
|
|
24
|
+
Red = "0;31"
|
|
25
|
+
Green = "0;32"
|
|
26
|
+
Yellow = "0;33"
|
|
27
|
+
Blue = "0;34"
|
|
28
|
+
Grey = "0;37"
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def _colour_allowed() -> bool:
|
|
32
|
+
return all(
|
|
33
|
+
(
|
|
34
|
+
sys.stdout.isatty(),
|
|
35
|
+
platform.system() != "Windows",
|
|
36
|
+
os.environ.get("NO_COLOR") != "1",
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
def __str__(self) -> str:
|
|
41
|
+
return f"\033[{self.value}m" if self._colour_allowed() else ""
|
|
42
|
+
|
|
43
|
+
def fmt(self, s: str, /) -> str:
|
|
44
|
+
return f"{self!s}{s}{_Colour.Reset!s}"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Status(Enum):
|
|
48
|
+
Created = ("Created", _Colour.Green)
|
|
49
|
+
Updated = ("Updated", _Colour.Green)
|
|
50
|
+
Skipped = ("Skipped", _Colour.Grey)
|
|
51
|
+
Failed = ("Failed", _Colour.Red)
|
|
52
|
+
Removed = ("Removed", _Colour.Yellow)
|
|
53
|
+
Warning = ("Warning", _Colour.Yellow)
|
|
54
|
+
Error = ("Error", _Colour.Red)
|
|
55
|
+
Info = ("Info", _Colour.Blue)
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def emblem(self) -> str:
|
|
59
|
+
label, colour = self.value
|
|
60
|
+
return colour.fmt(f"[{label}]")
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def emblem_continue(self) -> str:
|
|
64
|
+
label, colour = self.value
|
|
65
|
+
return colour.fmt(f"{'..':>{len(label) + 2}}")
|
|
66
|
+
|
|
67
|
+
def eprint(self, msg: object, *more: object):
|
|
68
|
+
_eprint(self.emblem, str(msg))
|
|
69
|
+
for m in more:
|
|
70
|
+
_eprint(self.emblem_continue, str(m))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def fmt_exception(exc: BaseException | None) -> str:
|
|
74
|
+
return f"{type(exc).__name__}: {exc!s}" if exc is not None else ""
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def log_warning(msg: object, *more: object):
|
|
78
|
+
Status.Warning.eprint(msg, *more)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def log_fatal_error(msg: object, *more: object):
|
|
82
|
+
Status.Error.eprint(msg, *more)
|
|
83
|
+
sys.exit(1)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def make_logger(log: bool, status: Status):
|
|
87
|
+
# fmt: off
|
|
88
|
+
if log:
|
|
89
|
+
def _log(*msg: object): status.eprint(*msg)
|
|
90
|
+
else:
|
|
91
|
+
def _log(*_msg: object): pass
|
|
92
|
+
# fmt: on
|
|
93
|
+
return _log
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# coolabah -- The assembler and helper functions.
|
|
2
|
+
#
|
|
3
|
+
# Author: Jahin Z. <git@jahinzee.net>
|
|
4
|
+
#
|
|
5
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
6
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
7
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
8
|
+
#
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
__package__ = "coolabah"
|
|
13
|
+
|
|
14
|
+
import enum
|
|
15
|
+
from multiprocessing.pool import Pool
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import NamedTuple
|
|
18
|
+
|
|
19
|
+
from coolabah._io import Status, fmt_exception, log_fatal_error, log_warning, make_logger
|
|
20
|
+
from coolabah.nodes import Directory
|
|
21
|
+
from coolabah.types import File, PopulationError, StrPath
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _ProcessResultStatus(enum.Enum):
|
|
25
|
+
SUCCESS_SKIP = enum.auto()
|
|
26
|
+
SUCCESS_CREATE = enum.auto()
|
|
27
|
+
SUCCESS_UPDATE = enum.auto()
|
|
28
|
+
FAIL_WARN = enum.auto()
|
|
29
|
+
FAIL_FATAL = enum.auto()
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def success(self) -> bool:
|
|
33
|
+
return self not in {self.FAIL_WARN, self.FAIL_FATAL}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class _ProcessResult(NamedTuple):
|
|
37
|
+
status: _ProcessResultStatus
|
|
38
|
+
path: Path
|
|
39
|
+
exc: BaseException | None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _process_file(path_and_file: tuple[Path, File]) -> _ProcessResult:
|
|
43
|
+
path, file = path_and_file
|
|
44
|
+
|
|
45
|
+
if path.exists() and not file.differs(path):
|
|
46
|
+
return _ProcessResult(_ProcessResultStatus.SUCCESS_SKIP, path, None)
|
|
47
|
+
|
|
48
|
+
update = path.exists()
|
|
49
|
+
path.unlink(missing_ok=True)
|
|
50
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
51
|
+
try:
|
|
52
|
+
try:
|
|
53
|
+
file.populate(path)
|
|
54
|
+
except PopulationError as pe:
|
|
55
|
+
if not pe.fatal:
|
|
56
|
+
return _ProcessResult(_ProcessResultStatus.FAIL_WARN, path, pe)
|
|
57
|
+
raise
|
|
58
|
+
except Exception as e:
|
|
59
|
+
return _ProcessResult(_ProcessResultStatus.FAIL_FATAL, path, e)
|
|
60
|
+
raise
|
|
61
|
+
|
|
62
|
+
return _ProcessResult(
|
|
63
|
+
_ProcessResultStatus.SUCCESS_UPDATE if update else _ProcessResultStatus.SUCCESS_CREATE,
|
|
64
|
+
path,
|
|
65
|
+
None,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# ---
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def assemble(
|
|
73
|
+
directory: Directory,
|
|
74
|
+
*,
|
|
75
|
+
destination: StrPath,
|
|
76
|
+
clean: bool = True,
|
|
77
|
+
clobber_ok: bool = False,
|
|
78
|
+
log: bool = True,
|
|
79
|
+
log_skips: bool = True,
|
|
80
|
+
jobs: int | None = None,
|
|
81
|
+
):
|
|
82
|
+
"""
|
|
83
|
+
Assembles a directory into a destination.
|
|
84
|
+
|
|
85
|
+
## Keyword arguments
|
|
86
|
+
|
|
87
|
+
### `destination` (`str | Path`, required)
|
|
88
|
+
The destination path to create the file tree.
|
|
89
|
+
|
|
90
|
+
### `clean` (`bool`, default: `True`)
|
|
91
|
+
If `True`, removes any unsuccessful or unknown files and directories after generation.
|
|
92
|
+
|
|
93
|
+
### `clobber_ok`: (`bool`, default: `False`)
|
|
94
|
+
If `True`, only shows a warning if multiple files are mapped to the same path. If `False`,
|
|
95
|
+
assembly halts before file generation.
|
|
96
|
+
|
|
97
|
+
### `log`: (`bool`, default: `True`)
|
|
98
|
+
If `True`, Logs info and file operation statuses to standard error. Note that warnings and
|
|
99
|
+
errors are always logged.
|
|
100
|
+
|
|
101
|
+
### `log_skips`: (`bool`, default: `True`)
|
|
102
|
+
If `False`, do not log skipped files, even if `log` is `True`.
|
|
103
|
+
|
|
104
|
+
### `jobs`: (`int`, optional)
|
|
105
|
+
If not `None`, uses a subprocess pool with the given number of processes for file population.
|
|
106
|
+
Uses the main processs if `None`. Note that clean operations (if `clean` is `True`) do not use
|
|
107
|
+
subprocesses.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
log_info = make_logger(log, Status.Info)
|
|
111
|
+
log_created = make_logger(log, Status.Created)
|
|
112
|
+
log_removed = make_logger(log, Status.Removed)
|
|
113
|
+
log_updated = make_logger(log, Status.Updated)
|
|
114
|
+
log_skipped = make_logger(log, Status.Skipped)
|
|
115
|
+
|
|
116
|
+
def collect_and_count(root, dir):
|
|
117
|
+
paths, counts = {}, {}
|
|
118
|
+
for p, f in dir.enumerate_eager():
|
|
119
|
+
paths[root / p] = f
|
|
120
|
+
counts[p] = counts.get(p, 0) + 1
|
|
121
|
+
|
|
122
|
+
return paths, counts
|
|
123
|
+
|
|
124
|
+
def directory_is_empty(path) -> bool:
|
|
125
|
+
return path.is_dir() and next(path.iterdir(), None) is None
|
|
126
|
+
|
|
127
|
+
def clean_root(keep):
|
|
128
|
+
existing = {f for f in Path(destination).glob("**/*") if not f.is_dir()}
|
|
129
|
+
to_delete = existing - keep
|
|
130
|
+
for path in to_delete:
|
|
131
|
+
path.unlink()
|
|
132
|
+
log_removed(path)
|
|
133
|
+
|
|
134
|
+
while empty_dirs := set(filter(directory_is_empty, Path(destination).glob("**/*"))):
|
|
135
|
+
for dir in empty_dirs:
|
|
136
|
+
dir.rmdir()
|
|
137
|
+
log_removed(path)
|
|
138
|
+
|
|
139
|
+
def get_results():
|
|
140
|
+
if jobs is None:
|
|
141
|
+
yield from (_process_file(pi) for pi in paths.items())
|
|
142
|
+
return
|
|
143
|
+
log_info(f"Processing files using {jobs} subprocess(es).")
|
|
144
|
+
with Pool(processes=jobs) as pool:
|
|
145
|
+
yield from pool.imap_unordered(_process_file, paths.items())
|
|
146
|
+
|
|
147
|
+
# ---
|
|
148
|
+
|
|
149
|
+
paths, counts = collect_and_count(destination, directory)
|
|
150
|
+
|
|
151
|
+
log_info(f"Collected {len(paths)} file object(s).")
|
|
152
|
+
for path, count in counts.items():
|
|
153
|
+
if count <= 1:
|
|
154
|
+
continue
|
|
155
|
+
message = f"{destination / path}: Path was yielded {count} times, paths may be clobbered."
|
|
156
|
+
if clobber_ok:
|
|
157
|
+
log_warning(message, "Using latest file object, continuing...")
|
|
158
|
+
else:
|
|
159
|
+
log_fatal_error(message)
|
|
160
|
+
|
|
161
|
+
successful_paths = set()
|
|
162
|
+
|
|
163
|
+
for r in get_results():
|
|
164
|
+
if r.status.success:
|
|
165
|
+
successful_paths.add(r.path)
|
|
166
|
+
match r.status:
|
|
167
|
+
case _ProcessResultStatus.SUCCESS_CREATE:
|
|
168
|
+
log_created(r.path)
|
|
169
|
+
case _ProcessResultStatus.SUCCESS_UPDATE:
|
|
170
|
+
log_updated(r.path)
|
|
171
|
+
case _ProcessResultStatus.SUCCESS_SKIP:
|
|
172
|
+
log_skipped(r.path)
|
|
173
|
+
case _ProcessResultStatus.FAIL_WARN:
|
|
174
|
+
log_warning(f"{r.path}: Non-fatal exception", fmt_exception(r.exc))
|
|
175
|
+
case _ProcessResultStatus.FAIL_FATAL:
|
|
176
|
+
log_fatal_error(
|
|
177
|
+
f"{r.path}: Fatal exception",
|
|
178
|
+
fmt_exception(r.exc),
|
|
179
|
+
f"{destination}: destination may be in an inconsistent state",
|
|
180
|
+
)
|
|
181
|
+
if clean:
|
|
182
|
+
log_info("Cleaning unknown or unsuccessful files...")
|
|
183
|
+
clean_root(keep=successful_paths)
|
|
184
|
+
|
|
185
|
+
log_info("Tree generation complete.")
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# coolabah -- Basic file and folder types.
|
|
2
|
+
#
|
|
3
|
+
# Author: Jahin Z. <git@jahinzee.net>
|
|
4
|
+
#
|
|
5
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
6
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
7
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
8
|
+
#
|
|
9
|
+
|
|
10
|
+
__package__ = "coolabah"
|
|
11
|
+
|
|
12
|
+
import dataclasses
|
|
13
|
+
import filecmp
|
|
14
|
+
import io
|
|
15
|
+
import json
|
|
16
|
+
import shutil
|
|
17
|
+
from collections.abc import Iterable, Mapping
|
|
18
|
+
from io import StringIO
|
|
19
|
+
from itertools import zip_longest
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import TYPE_CHECKING, Never, ParamSpec
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING: from _typeshed import SupportsRead # noqa: I001 # fmt: skip
|
|
24
|
+
|
|
25
|
+
from coolabah.types import Directory, DirectoryCallable, File, Node, PopulationError, StrPath
|
|
26
|
+
|
|
27
|
+
P = ParamSpec("P")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
31
|
+
class _EmptyFile(File):
|
|
32
|
+
def populate(self, path: Path):
|
|
33
|
+
path.touch()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
37
|
+
class _CopyFile(File):
|
|
38
|
+
_source: Path
|
|
39
|
+
|
|
40
|
+
def populate(self, path):
|
|
41
|
+
shutil.copy(self._source, path)
|
|
42
|
+
|
|
43
|
+
def differs(self, existing):
|
|
44
|
+
return not filecmp.cmp(self._source, existing)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _files_differ[R: SupportsRead[str | bytes]](
|
|
48
|
+
a: R,
|
|
49
|
+
b: R,
|
|
50
|
+
*,
|
|
51
|
+
max_bytes: int = 8192,
|
|
52
|
+
chunk_size: int = 1,
|
|
53
|
+
) -> bool:
|
|
54
|
+
def _iter_read(f):
|
|
55
|
+
while c := f.read(chunk_size):
|
|
56
|
+
yield c
|
|
57
|
+
|
|
58
|
+
curr_byte = 0
|
|
59
|
+
for ac, bc in zip_longest(_iter_read(a), _iter_read(b), fillvalue=None):
|
|
60
|
+
if ac != bc or curr_byte > max_bytes:
|
|
61
|
+
return True
|
|
62
|
+
curr_byte += chunk_size
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
67
|
+
class _BufferedTextFile(File):
|
|
68
|
+
_inner: io.TextIOBase
|
|
69
|
+
|
|
70
|
+
def populate(self, path):
|
|
71
|
+
with open(path, "w") as f:
|
|
72
|
+
self._inner.seek(0)
|
|
73
|
+
shutil.copyfileobj(self._inner, f)
|
|
74
|
+
self._inner.close()
|
|
75
|
+
|
|
76
|
+
def differs(self, existing):
|
|
77
|
+
with open(existing, "r") as f:
|
|
78
|
+
try:
|
|
79
|
+
self._inner.seek(0)
|
|
80
|
+
return _files_differ(self._inner, f)
|
|
81
|
+
finally:
|
|
82
|
+
self._inner.seek(0)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
86
|
+
class _BufferedBinaryFile(File):
|
|
87
|
+
_inner: io.BytesIO
|
|
88
|
+
|
|
89
|
+
def populate(self, path):
|
|
90
|
+
with open(path, "wb") as f:
|
|
91
|
+
self._inner.seek(0)
|
|
92
|
+
shutil.copyfileobj(self._inner, f)
|
|
93
|
+
self._inner.close()
|
|
94
|
+
|
|
95
|
+
def differs(self, existing):
|
|
96
|
+
with open(existing, "rb") as f:
|
|
97
|
+
try:
|
|
98
|
+
self._inner.seek(0)
|
|
99
|
+
return _files_differ(self._inner, f)
|
|
100
|
+
finally:
|
|
101
|
+
self._inner.seek(0)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
105
|
+
class _BadFile(File):
|
|
106
|
+
def populate(self, path) -> Never:
|
|
107
|
+
raise PopulationError("Bad file!")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
111
|
+
class _LessBadFile(File):
|
|
112
|
+
def populate(self, path) -> Never:
|
|
113
|
+
raise PopulationError("Less bad file!", fatal=False)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
117
|
+
class IterableDirectory(Directory):
|
|
118
|
+
_inner: Iterable[tuple[StrPath, Node]]
|
|
119
|
+
|
|
120
|
+
def enumerate(self):
|
|
121
|
+
yield from ((Path(p), f) for p, f in self._inner)
|
|
122
|
+
|
|
123
|
+
def __iter__(self):
|
|
124
|
+
return iter(self._inner)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
128
|
+
class CallableDirectory[**P]:
|
|
129
|
+
_inner: DirectoryCallable[P]
|
|
130
|
+
|
|
131
|
+
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> IterableDirectory:
|
|
132
|
+
return IterableDirectory(iter(self._inner(*args, **kwargs)))
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
# ---
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def empty_file() -> File:
|
|
139
|
+
"""
|
|
140
|
+
Creates an empty file with no contents.
|
|
141
|
+
"""
|
|
142
|
+
return _EmptyFile()
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def copy_file(source: StrPath, /) -> File:
|
|
146
|
+
"""
|
|
147
|
+
Copies a source file's contents at population time.
|
|
148
|
+
"""
|
|
149
|
+
return _CopyFile(_source=Path(source))
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def copy_path(source: StrPath, /) -> tuple[StrPath, File]:
|
|
153
|
+
"""
|
|
154
|
+
Wrapper for `yield <source>, copy_file(<source>)`.
|
|
155
|
+
"""
|
|
156
|
+
return source, copy_file(source)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def text_file(s: str | io.TextIOBase, /, *, end: str = "\n") -> File:
|
|
160
|
+
"""
|
|
161
|
+
Creates a basic text file from a string, or a TextIOBase object.
|
|
162
|
+
|
|
163
|
+
NOTE: If an IO object is passed, the file will be closed when population completes.
|
|
164
|
+
|
|
165
|
+
Appends a newline at the end of file contents by default (as per POSIX standards). Disable
|
|
166
|
+
or customise this behaviour with the `end` kwarg.
|
|
167
|
+
"""
|
|
168
|
+
match s:
|
|
169
|
+
case io.TextIOBase():
|
|
170
|
+
s.write(end)
|
|
171
|
+
return _BufferedTextFile(s)
|
|
172
|
+
case str():
|
|
173
|
+
buf = io.StringIO()
|
|
174
|
+
buf.write(s)
|
|
175
|
+
buf.write(end)
|
|
176
|
+
return _BufferedTextFile(buf)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def binary_file(b: bytes | bytearray | io.BytesIO, /) -> File:
|
|
180
|
+
"""
|
|
181
|
+
Creates a basic binary file from a byte string or bytearray, or a BytesIO object.
|
|
182
|
+
|
|
183
|
+
NOTE: If an IO object is passed, the file will be closed when population completes.
|
|
184
|
+
"""
|
|
185
|
+
match b:
|
|
186
|
+
case io.BytesIO():
|
|
187
|
+
return _BufferedBinaryFile(b)
|
|
188
|
+
case bytes() | bytearray():
|
|
189
|
+
return _BufferedBinaryFile(io.BytesIO(b))
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def json_file(
|
|
193
|
+
obj: object,
|
|
194
|
+
/,
|
|
195
|
+
*,
|
|
196
|
+
indent: int | None = None,
|
|
197
|
+
ensure_ascii: bool = True,
|
|
198
|
+
) -> File:
|
|
199
|
+
"""
|
|
200
|
+
Creates a JSON text file, serialised from a Python object.
|
|
201
|
+
|
|
202
|
+
Keywords `indent` and `ensure_ascii` are passed to `json.dump`.
|
|
203
|
+
"""
|
|
204
|
+
json.dump(obj, (buf := StringIO()), indent=indent, ensure_ascii=ensure_ascii)
|
|
205
|
+
return _BufferedTextFile(buf)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def directory(fn: DirectoryCallable) -> CallableDirectory:
|
|
209
|
+
"""
|
|
210
|
+
Creates a directory from a generator function yielding paths and files or subdirectories.
|
|
211
|
+
"""
|
|
212
|
+
return CallableDirectory(fn)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def iterable_directory(i: Iterable[tuple[StrPath, Node]], /):
|
|
216
|
+
"""
|
|
217
|
+
Creates a directory from an iterator of paths and files or subdirectories.
|
|
218
|
+
"""
|
|
219
|
+
return IterableDirectory(i)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def mapping_directory(m: Mapping[StrPath, Node], /):
|
|
223
|
+
"""
|
|
224
|
+
Creates a directory from a mapping of paths and files or subdirectories.
|
|
225
|
+
"""
|
|
226
|
+
return IterableDirectory(m.items())
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# coolabah -- Core types and protocols.
|
|
2
|
+
#
|
|
3
|
+
# Author: Jahin Z. <git@jahinzee.net>
|
|
4
|
+
#
|
|
5
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
6
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
7
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
8
|
+
#
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
__package__ = "coolabah"
|
|
13
|
+
|
|
14
|
+
from collections.abc import Callable, Generator, Iterable
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import Never, ParamSpec, Protocol, runtime_checkable
|
|
17
|
+
|
|
18
|
+
P = ParamSpec("P")
|
|
19
|
+
|
|
20
|
+
type Node = File | Directory
|
|
21
|
+
type StrPath = Path | str
|
|
22
|
+
type Raises[E: BaseException] = Never
|
|
23
|
+
DirectoryCallable = Callable[P, Generator[tuple[StrPath, Node]]]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class PopulationError(Exception):
|
|
27
|
+
"""
|
|
28
|
+
A general error has occured when generating a file. Can be fatal or non-fatal.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, msg: str, fatal: bool = True):
|
|
32
|
+
super().__init__()
|
|
33
|
+
self.msg = msg
|
|
34
|
+
self.fatal = fatal
|
|
35
|
+
|
|
36
|
+
def __str__(self):
|
|
37
|
+
return self.msg
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@runtime_checkable
|
|
41
|
+
class File(Protocol):
|
|
42
|
+
"""
|
|
43
|
+
Protocol for an object that represents the *contents* of a file.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def populate(self, destination: Path) -> None | Raises[PopulationError]:
|
|
47
|
+
"""
|
|
48
|
+
Populates `destination` with `self`'s contents.
|
|
49
|
+
|
|
50
|
+
## Notes for Implementors
|
|
51
|
+
|
|
52
|
+
You can assume that `destination` does not exist, i.e. `assert not destination.exists()`.
|
|
53
|
+
|
|
54
|
+
Avoid parsing, marshalling, or validating file content in this function. This function's
|
|
55
|
+
sole responsibility should consist of writing prepared data into the filesystem.
|
|
56
|
+
|
|
57
|
+
Exceptions do not need to be explicitly caught, assembly will halt if any are raised.
|
|
58
|
+
|
|
59
|
+
You may raise an instance of `coolabah.types.PopulationError` for custom errors,
|
|
60
|
+
including for non-fatal errors (kwarg: `fatal=False`). Non-fatal errors will be
|
|
61
|
+
acknowledged by the assembler, but assembly may continue.
|
|
62
|
+
|
|
63
|
+
Note that `populate` might be called in parallel with other files, so avoid sharing
|
|
64
|
+
resource objects without coordination. You may assume that this function will only
|
|
65
|
+
be called once, so it is safe to close or release any owned resources in this function.
|
|
66
|
+
"""
|
|
67
|
+
...
|
|
68
|
+
|
|
69
|
+
def differs(self, existing: Path) -> bool:
|
|
70
|
+
"""
|
|
71
|
+
Returns `True` if the contents of the `existing` file is identical to `self`'s
|
|
72
|
+
contents.
|
|
73
|
+
|
|
74
|
+
## Notes for Implementors
|
|
75
|
+
|
|
76
|
+
For correctness reasons, adopt a "different until proven otherwise" model. When in doubt,
|
|
77
|
+
return `True`, or fallback to the default implementation, which always returns `True`.
|
|
78
|
+
|
|
79
|
+
You should ensure that `self` (and any members) are in the same state between entry
|
|
80
|
+
and return; ensure any seek pointers are reset to what they originally were, and any
|
|
81
|
+
any file handles are closed before returning.
|
|
82
|
+
|
|
83
|
+
You can assume that `existing` exists, i.e. `assert existing.exists()`, and its
|
|
84
|
+
contents are unknown. You should not attempt to write to, modify, or delete `existing`, as
|
|
85
|
+
this may cause unintended side effects in cases where clobbering is permitted.
|
|
86
|
+
"""
|
|
87
|
+
return True
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@runtime_checkable
|
|
91
|
+
class Directory(Protocol):
|
|
92
|
+
"""
|
|
93
|
+
Protocol for an object that represents a lazy directory tree, yielding paths and files or
|
|
94
|
+
subdirectories.
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
def enumerate(self) -> Iterable[tuple[StrPath, Node]]:
|
|
98
|
+
"""
|
|
99
|
+
Yields the contents of `self`, a tuple consisting of a relative path, and a file or
|
|
100
|
+
subdirectory.
|
|
101
|
+
|
|
102
|
+
## Notes for Implementors
|
|
103
|
+
|
|
104
|
+
Avoid parsing, marshalling, or validating files contents or filename informations in this
|
|
105
|
+
function.
|
|
106
|
+
|
|
107
|
+
When working with subdirectories, note the different between `yield "subdir", subdir()`
|
|
108
|
+
and `yield from subdir()`; the latter will *merge* the contents of `subdir()` into the
|
|
109
|
+
directory.
|
|
110
|
+
|
|
111
|
+
Take care when yielding filenames or paths to not yield the same path twice, especially
|
|
112
|
+
when using `yield from`. The assembler may halt if it detects duplicated paths.
|
|
113
|
+
"""
|
|
114
|
+
...
|
|
115
|
+
|
|
116
|
+
def enumerate_eager(self) -> Iterable[tuple[Path, File]]:
|
|
117
|
+
"""
|
|
118
|
+
Eagerly enumerate all files *including* subdirectories, which are separated as a file
|
|
119
|
+
|
|
120
|
+
## Notes for Implementors
|
|
121
|
+
|
|
122
|
+
This method should not be overridden.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
def _walk(path, tree):
|
|
126
|
+
for p, n in tree.enumerate():
|
|
127
|
+
match n:
|
|
128
|
+
case Directory():
|
|
129
|
+
yield from _walk(path / p, n)
|
|
130
|
+
case File():
|
|
131
|
+
yield path / p, n
|
|
132
|
+
|
|
133
|
+
yield from _walk(Path("."), self)
|
|
134
|
+
|
|
135
|
+
def __iter__(self):
|
|
136
|
+
return iter(self.enumerate())
|