-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
753 lines (717 loc) · 28.4 KB
/
project.py
File metadata and controls
753 lines (717 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
from __future__ import annotations
from dataclasses import asdict
from typing import TYPE_CHECKING
from ozi_spec import METADATA
from prompt_toolkit.shortcuts import button_dialog # pyright: ignore
from prompt_toolkit.shortcuts import checkboxlist_dialog # pyright: ignore
from prompt_toolkit.shortcuts import message_dialog # pyright: ignore
from prompt_toolkit.shortcuts import radiolist_dialog # pyright: ignore
from prompt_toolkit.shortcuts.dialogs import yes_no_dialog # pyright: ignore
from prompt_toolkit.validation import DynamicValidator # pyright: ignore
from prompt_toolkit.validation import Validator # pyright: ignore
from ozi_core._i18n import TRANSLATION as _
from ozi_core.config import read_user_config
from ozi_core.new.interactive.menu import main_menu
from ozi_core.new.interactive.validator import LengthValidator
from ozi_core.new.interactive.validator import NotReservedValidator
from ozi_core.new.interactive.validator import PackageValidator
from ozi_core.new.interactive.validator import ProjectNameValidator
from ozi_core.new.interactive.validator import validate_message
from ozi_core.trove import Prefix
from ozi_core.trove import from_prefix
from ozi_core.ui._style import _style
from ozi_core.ui.dialog import admonition_dialog
from ozi_core.ui.dialog import input_dialog
from ozi_core.ui.menu import MenuButton
if TYPE_CHECKING: # pragma: no cover
from collections.abc import Sequence
class Project: # pragma: no cover
def __init__( # noqa: DC104,ANN101,RUF100
self: Project,
allow_file: list[str] | None = None,
ci_provider: str | None = None,
copyright_head: str | None = None,
enable_cython: bool | None = None,
enable_uv: bool | None = None,
github_harden_runner: bool | None = None,
strict: bool | None = None,
verify_email: bool | None = None,
check_package_exists: bool | None = None,
update_wrapfile: bool | None = None,
) -> None:
self.allow_file = allow_file
self.ci_provider = ci_provider
self.copyright_head = copyright_head
self.enable_cython = enable_cython if enable_cython is not None else False
self.enable_uv = enable_uv if enable_uv is not None else False
self.github_harden_runner = (
github_harden_runner if github_harden_runner is not None else False
)
self.strict = strict if strict is not None else True
self.verify_email = verify_email if verify_email is not None else False
self.check_package_exists = (
check_package_exists if check_package_exists is not None else True
)
self.update_wrapfile = update_wrapfile if update_wrapfile is not None else False
_.locale = asdict(read_user_config())['interactive']['language']
_.mime_type = 'text/plain;charset=UTF-8'
def __call__(self: Project) -> list[str]: # noqa: C901 # pragma: no cover
"""Start the interactive prompt."""
if (
admonition_dialog(
title=_('new-dlg-title'),
heading_label=_('adm-disclaimer-title'),
text=_('adm-disclaimer-text'),
).run()
is None
):
return []
prefix: dict[str, str] = {}
output: dict[str, list[str]] = {}
project_name = '""'
result, output, prefix = self.name(output, prefix)
if isinstance(result, list):
return result
if isinstance(result, str):
project_name = result
for i in ('summary', 'keywords', 'author', 'author_email'):
result, output, prefix = getattr(self, i)(project_name, output, prefix)
if isinstance(result, list):
return result
result, output, prefix = self.license_(project_name, output, prefix)
if isinstance(result, list):
return result
_license = result if result else ''
result, output, prefix = self.license_expression(
project_name,
_license,
output,
prefix,
)
if isinstance(result, list):
return result
if yes_no_dialog(
title=_('new-dlg-title'),
text=_('adm-maintainers', project_name=project_name),
style=_style,
yes_text=_('btn-yes'),
no_text=_('btn-no'),
).run():
result, output, prefix = self.maintainer(project_name, output, prefix)
if isinstance(result, list):
return result
result, output, prefix = self.maintainer_email(project_name, output, prefix)
if isinstance(result, list):
return result
result, output, prefix = self.requires_dist(project_name, output, prefix)
if isinstance(result, list):
return result
while not admonition_dialog(
title=_('new-dlg-title'),
heading_label=_('adm-confirm'),
text='\n'.join(prefix.values()),
ok_text=_('btn-ok'),
cancel_text=_('btn-menu'),
).run():
result, output, prefix = main_menu(self, output, prefix)
if isinstance(result, list):
return result
ret_args = ['project']
for k, v in output.items():
for i in v:
if len(i) > 0:
ret_args += [k, i]
return ret_args
def _check_package_exists(self: Project) -> Validator:
if self.check_package_exists:
return NotReservedValidator(ProjectNameValidator())
else:
return ProjectNameValidator()
def name(
self: Project,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str | bool, dict[str, list[str]], dict[str, str]]:
while True:
result, output, prefix = self.header_input(
'Name',
output,
prefix,
_('pro-name'),
validator=DynamicValidator(self._check_package_exists),
)
if result is True:
return prefix.get('Name', '').replace('Name', '').strip(': '), output, prefix
if isinstance(result, list):
return result, output, prefix
def summary(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str | bool, dict[str, list[str]], dict[str, str]]:
while True:
result, output, prefix = self.header_input(
'Summary',
output,
prefix,
_('pro-summary', projectname=project_name),
validator=LengthValidator(),
)
if result is True:
return result, output, prefix
if isinstance(result, list):
return result, output, prefix
def keywords(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str | bool, dict[str, list[str]], dict[str, str]]:
while True:
result, output, prefix = self.header_input(
'Keywords',
output,
prefix,
_('pro-keywords', projectname=project_name),
validator=LengthValidator(),
)
if result is True:
return result, output, prefix
if isinstance(result, list):
return result, output, prefix
def license_file(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str | bool, dict[str, list[str]], dict[str, str]]:
while True:
result = radiolist_dialog(
values=(('LICENSE.txt', 'LICENSE.txt'),),
title=_('new-dlg-title'),
text=_('pro-license-file', projectname=project_name),
style=_style,
default='LICENSE.txt',
ok_text=_('btn-ok'),
cancel_text=_('btn-back'),
).run()
if result is not None:
output.update(
{'--license-file': [result] if isinstance(result, str) else []},
)
prefix.update(
(
{
'License-File ::': f'License-File :: {result}', # noqa: B950, RUF100, E501
}
if result
else {}
),
)
def author(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str | bool, dict[str, list[str]], dict[str, str]]:
while True:
config = asdict(read_user_config())
key = config['new']['author']
output['--author'] = [key] if key is not None else []
result, output, prefix = self.header_input(
'Author',
output,
prefix,
_('pro-author', projectname=project_name),
validator=LengthValidator(),
split_on=',',
)
if result is True:
return result, output, prefix
if isinstance(result, list):
return result, output, prefix
def author_email(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str | bool, dict[str, list[str]], dict[str, str]]:
while True:
config = asdict(read_user_config())
key = config['new']['author_email']
output['--author-email'] = [key] if key is not None else []
result, output, prefix = self.header_input(
'Author-email',
output,
prefix,
_('pro-author-email', projectname=project_name),
validator=LengthValidator(),
split_on=',',
)
if result is True:
return result, output, prefix
if isinstance(result, list):
return result, output, prefix
def license_( # noqa: C901,RUF100
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str, dict[str, list[str]], dict[str, str]]:
_default = output.setdefault('--license', [])
while True:
license_ = radiolist_dialog(
values=sorted(
(zip(from_prefix(Prefix().license), from_prefix(Prefix().license))),
),
title=_('new-dlg-title'),
text=_('pro-license', projectname=project_name),
style=_style,
default=_default,
cancel_text=_('btn-menu'),
ok_text=_('btn-ok'),
).run()
if license_ is None:
result, output, prefix = main_menu(self, output, prefix)
if isinstance(result, list):
output.update({'--license': _default})
return result, output, prefix
else:
if validate_message(
license_ if license_ and isinstance(license_, str) else '',
LengthValidator(),
)[0]:
break
message_dialog(
style=_style,
title=_('new-dlg-title'),
text=_(
'msg-invalid-input',
value=license_ if license_ and isinstance(license_, str) else '',
errmsg='',
),
ok_text=_('btn-ok'),
).run()
prefix.update(
{f'{Prefix().license}': f'{Prefix().license}{license_ if license_ else ""}'},
)
if isinstance(license_, str):
output.update({'--license': [license_]})
else:
output.update({'--license': _default})
return str(license_), output, prefix
def license_expression( # noqa: C901
self: Project,
project_name: str,
_license: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str, dict[str, list[str]], dict[str, str]]:
_license_expression: str = ''
while True:
_possible_spdx: Sequence[str] | None = (
METADATA.spec.python.pkg.license.ambiguous.get(
_license,
None,
)
)
possible_spdx: Sequence[str] = _possible_spdx if _possible_spdx else ['']
_default = output.setdefault('--license-expression', [possible_spdx[0]])
if len(possible_spdx) < 1:
_license_expression = input_dialog(
title=_('new-dlg-title'),
text=_(
'pro-license-expression-input',
license=_license,
projectname=project_name,
),
default=_default[0],
style=_style,
cancel_text=_('btn-skip'),
).run()
elif len(possible_spdx) == 1:
_license_expression = input_dialog(
title=_('new-dlg-title'),
text=_(
'pro-license-expression-input',
license=_license,
projectname=project_name,
),
default=_default[0],
style=_style,
cancel_text=_('btn-skip'),
ok_text=_('btn-ok'),
).run()
else:
license_id = radiolist_dialog(
values=sorted(zip(possible_spdx, possible_spdx)),
title=_('new-dlg-title'),
text=_(
'pro-license-expression-radio',
license=_license,
projectname=project_name,
),
style=_style,
cancel_text=_('btn-menu'),
ok_text=_('btn-ok'),
).run()
if license_id is None:
output.update({'--license-expression': _default})
result, output, prefix = main_menu(self, output, prefix)
if isinstance(result, list):
return result, output, prefix
else:
_license_expression = input_dialog(
title=_('new-dlg-title'),
text=_(
'pro-license-expression-input',
license=_license,
projectname=project_name,
),
default=license_id,
style=_style,
cancel_text=_('btn-skip'),
ok_text=_('btn-ok'),
).run()
if validate_message(license_id if license_id else '', LengthValidator())[
0
]:
break
else:
message_dialog(
style=_style,
title=_('new-dlg-title'),
text=_(
'msg-invalid-input',
value=license_id,
errmsg='',
),
ok_text=_('btn-ok'),
).run()
break
if _license_expression:
output.update({'--license-expression': [_license_expression]})
else:
output.update({'--license-expression': _default})
prefix.update(
{
'License-Expression ::': f'License-Expression :: {_license_expression if _license_expression else ""}', # pyright: ignore # noqa: B950, RUF100, E501
},
) # pyright: ignore # noqa: B950, RUF100
return _license_expression, output, prefix
def maintainer(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str | bool, dict[str, list[str]], dict[str, str]]:
while True:
config = asdict(read_user_config())
key = config['new']['maintainer']
output['--maintainer'] = [key] if key is not None else []
result, output, prefix = self.header_input(
'Maintainer',
output,
prefix,
_('pro-maintainer', projectname=project_name),
validator=LengthValidator(),
split_on=',',
)
if result is True:
return result, output, prefix
if isinstance(result, list):
return result, output, prefix
def maintainer_email(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[None | list[str] | str | bool, dict[str, list[str]], dict[str, str]]:
while True:
config = asdict(read_user_config())
key = config['new']['maintainer_email']
output['--maintainer-email'] = [key] if key is not None else []
result, output, prefix = self.header_input(
'Maintainer-email',
output,
prefix,
_('pro-maintainer-email', projectname=project_name),
validator=LengthValidator(),
split_on=',',
)
if result is True:
return result, output, prefix
if isinstance(result, list):
return result, output, prefix
def requires_dist(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[list[str] | str | bool | None, dict[str, list[str]], dict[str, str]]:
_requires_dist: list[str] = []
output.setdefault('--requires-dist', [])
while True:
match button_dialog(
title=_('new-dlg-title'),
text='\n'.join(
(
'Requires-Dist:',
'\n'.join(_requires_dist),
'\n',
_('pro-requires-dist', projectname=project_name),
),
),
buttons=[
(_('btn-add'), 'btn-add'),
(_('btn-remove'), 'btn-remove'),
(_('btn-ok'), 'btn-ok'),
(_('btn-menu'), 'btn-menu'),
],
style=_style,
).run():
case MenuButton.ADD._str:
requirement = input_dialog(
title=_('new-dlg-title'),
text=_('pro-requires-dist-search'),
validator=PackageValidator(),
style=_style,
cancel_text=_('btn-back'),
).run()
if requirement:
_requires_dist += [requirement]
prefix.update(
{
f'Requires-Dist: {requirement}': (
f'Requires-Dist: {requirement}'
),
},
)
output['--requires-dist'].append(requirement)
case MenuButton.REMOVE._str:
if len(_requires_dist) != 0:
del_requirement = checkboxlist_dialog(
title=_('new-dlg-title'),
text=_('pro-requires-dist-cbl-remove'),
values=list(zip(_requires_dist, _requires_dist)),
style=_style,
cancel_text=_('btn-back'),
).run()
if del_requirement:
_requires_dist = list(
set(_requires_dist).symmetric_difference(
set(del_requirement),
),
)
for req in del_requirement:
output['--requires-dist'].remove(req)
prefix.pop(f'Requires-Dist: {req}')
else:
message_dialog(
title=_('new-dlg-title'),
text=_('pro-requires-dist-msg-remove-no-requirements'),
style=_style,
ok_text=_('btn-ok'),
).run()
case MenuButton.OK._str:
break
case MenuButton.MENU._str:
result, output, prefix = main_menu(self, output, prefix)
if result is not None:
return result, output, prefix
return None, output, prefix
def readme_type(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[str | list[str], dict[str, list[str]], dict[str, str]]:
config = asdict(read_user_config())
key = config['new']['readme_type']
_default = output.setdefault('--readme-type', [key] if key is not None else [])
readme_type = radiolist_dialog(
values=(
('rst', 'ReStructuredText'),
('md', 'Markdown'),
('txt', 'Plaintext'),
),
title=_('new-dlg-title'),
text=_('pro-readme-type', projectname=project_name),
style=_style,
default=_default,
ok_text=_('btn-ok'),
cancel_text=_('btn-back'),
).run()
if readme_type is not None:
output.update(
{'--readme-type': [readme_type] if isinstance(readme_type, str) else []},
)
else:
output.update({'--readme-type': _default})
prefix.update(
(
{
'Description-Content-Type:': f'Description-Content-Type: {readme_type}', # noqa: B950, RUF100, E501
}
if readme_type
else {}
),
)
return str(readme_type), output, prefix
def typing(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[str | list[str], dict[str, list[str]], dict[str, str]]:
_default = output.setdefault('--typing', [])
result = radiolist_dialog(
values=(
('Typed', _('pro-typing-radio-typed')),
('Stubs Only', _('pro-typing-radio-stubs-only')),
),
title=_('new-dlg-title'),
text=_('pro-typing', projectname=project_name),
style=_style,
ok_text=_('btn-ok'),
default=_default,
cancel_text=_('btn-back'),
).run()
if result is not None:
output.update({'--typing': [result] if isinstance(result, str) else []})
else:
output.update({'--typing': _default})
prefix.update(
(
{
'Typing ::': f'Typing :: {result}', # noqa: B950, RUF100, E501
}
if result
else {}
),
)
return str(result), output, prefix
def project_urls(
self: Project,
project_name: str,
output: dict[str, list[str]],
prefix: dict[str, str],
) -> tuple[str, dict[str, list[str]], dict[str, str]]:
_default = output.setdefault('--project-url', [])
url = None
while True:
result = checkboxlist_dialog(
values=(
(
'Changelog',
_('pro-project-urls-cbl-changelog'),
),
(
'Documentation',
_('pro-project-urls-cbl-documentation'),
),
(
'Bug Report',
_('pro-project-urls-cbl-bug-report'),
),
(
'Funding',
_('pro-project-urls-cbl-funding'),
),
(
'Source',
_('pro-project-urls-cbl-source'),
),
),
title=_('new-dlg-title'),
text=_('pro-project-urls-cbl', projectname=project_name),
style=_style,
ok_text=_('btn-ok'),
cancel_text=_('btn-back'),
).run()
if result is not None:
for i in result:
urltype = f'pro-project-urls-cbl-{"-".join(map(str.lower, i.split()))}'
url = input_dialog(
title=_('new-dlg-title'),
text=_(
'pro-project-urls-input',
urltype=_(urltype),
projectname=project_name,
),
ok_text=_('btn-ok'),
cancel_text=_('btn-back'),
default='https://',
style=_style,
).run()
if url is None:
break
output['--project-url'].append(f'{i}, {url}')
prefix.update(
(
{
f'Project-URL: {i}': f'Project-URL: {i}, {url}', # noqa: B950, RUF100, E501
}
if i
else {}
),
)
continue
else:
output.update({'--project-url': _default})
break
return f'{result}, {url}', output, prefix
def header_input(
self: Project,
label: str,
output: dict[str, list[str]],
prefix: dict[str, str],
*args: str,
validator: Validator | None = None,
split_on: str | None = None,
) -> tuple[
bool | None | list[str],
dict[str, list[str]],
dict[str, str],
]: # pragma: no cover
_default = output.setdefault(f'--{label.lower()}', [])
header = input_dialog(
title=_('new-dlg-title'),
text='\n'.join(args),
validator=validator,
default=_default[0] if len(_default) > 0 else '',
style=_style,
cancel_text=_('btn-menu'),
ok_text=_('btn-ok'),
).run()
if header is None:
output.update(
{
f'--{label.lower()}': _default if len(_default) > 0 else [],
},
)
result, output, prefix = main_menu(self, output, prefix)
return result, output, prefix
else:
if validator is not None:
valid, errmsg = validate_message(header, validator)
if valid:
prefix.update({label: f'{label}: {header}'})
if split_on:
output.update(
{f'--{label.lower()}': header.rstrip(split_on).split(split_on)},
)
else:
output.update({f'--{label.lower()}': [header]})
return True, output, prefix
message_dialog(
title=_('new-dlg-title'),
text=_('msg-input-invalid', value=header, errmsg=errmsg),
style=_style,
ok_text=_('btn-ok'),
).run()
output.update(
{f'--{label.lower()}': _default if len(_default) > 0 else []},
)
return None, output, prefix