-
Notifications
You must be signed in to change notification settings - Fork 2
/
group.py
812 lines (684 loc) · 23.5 KB
/
group.py
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# Copyright (c) 2023-2025 Feud Developers.
# Distributed under the terms of the MIT License (see the LICENSE file).
# SPDX-License-Identifier: MIT
# This source code is part of the Feud project (https://feud.wiki).
"""Generation of :py:class:`click.Group`s from Python classes,
with automatically generated :py:func:`click.Command`s defined as
methods on the class.
"""
from __future__ import annotations
import copy
import inspect
import types
import typing as t
import warnings
from collections import OrderedDict
from itertools import chain
import pydantic as pyd
import feud.exceptions
from feud import click
from feud._internal import _group, _metaclass
from feud.config import Config
__all__ = ["Group", "Section"]
class Section(pyd.BaseModel, extra="forbid"):
"""Commands or subgroups to display in a separate section on the help page
of a :py:class:`.Group`.
"""
#: Name of the command section.
name: str
#: Description of the command section.
#:
#: .. deprecated:: 0.3.0
#: Not yet supported by ``rich-click``.
description: str | None = None
#: Names of commands or subgroups to include in the section.
#:
#: If :py:func:`.rename` was used to rename a command, the new command
#: name should be used.
items: list[str] = []
class Group(metaclass=_metaclass.GroupBase):
"""Representation of a command group, compiling into a
:py:class:`click.Group`.
Functions defined in the class body represent commands within the group
and are automatically decorated with :py:func:`.command`
(if not already decorated and do not begin with an underscore).
Groups may be registered as subgroups to another parent group.
Similarly to providing configuration keyword arguments to
:py:func:`.command` (directly or with a :py:class:`.Config`),
group-level configuration can be specified when subclassing
:py:class:`.Group`.
>>> import feud
>>> class CLI(feud.Group, show_help_defaults=False, name="my-cli"):
... def func(*, opt: int):
... pass
Note that Click-level keyword arguments such as ``name``, which are not
:ref:`Feud configuration parameters <configuration>`, are passed to
:py:func:`click.group`.
Feud configuration parameters defined on a group are automatically
forwarded to the commands within the group, provided that the function in
the class body is not manually decorated with
:py:func:`.command`. In the above example, ``func`` is automatically
wrapped with ``@feud.command(show_help_defaults=False)``.
.. caution::
The following function names should **NOT** be used in a group:
- :py:func:`~commands`
- :py:func:`~compile`
- :py:func:`~deregister`
- :py:func:`~descendants`
- :py:func:`~name`
- :py:func:`~register`
- :py:func:`~subgroups`
See :py:func:`.rename` if you wish to define a command with one of the
above names.
"""
__feud_config__: t.ClassVar[Config]
__feud_click_kwargs__: t.ClassVar[dict[str, t.Any]]
__feud_subgroups__: t.ClassVar[list[type[Group]]]
__feud_commands__: t.ClassVar[list[str]]
@staticmethod
def __new__(
cls: type[Group], args: list[str] | None = None, /, **kwargs: t.Any
) -> t.Any:
"""Compile and run the group.
.. warning::
This function should be considered internal. The preferred way to
run a group is to use the :py:func:`.run` function.
Parameters
----------
cls:
:py:class:`.Group` class reference.
args:
Command-line arguments provided to
:py:class:`click.Command`.
**kwargs:
Additional keyword arguments provided to
:py:class:`click.Command`.
Returns
-------
typing.Any
Output of the called :py:class:`click.Command`.
Examples
--------
>>> import feud
>>> class CLI(feud.Group):
... def func(*, opt: int) -> int:
... return opt
>>> CLI(["func", "--opt", "3"], standalone_mode=False)
3
See Also
--------
.run:
Run a command or group.
"""
return cls.__compile__()(args, **kwargs)
@classmethod
def __compile__(
cls: type[Group],
*,
parent: click.Group | None = None,
) -> click.Group:
"""Compile the group into a :py:class:`click.Group`.
.. warning::
This is an internal function that should not be used directly,
:py:func:`.compile` should be used instead.
Parameters
----------
parent:
Parent :py:class:`click.Group` to attach the compiled
group to as a subgroup.
Returns
-------
click.Group
The generated group.
Examples
--------
>>> import feud, click
>>> class CLI(feud.Group):
... def func(*, opt: int) -> int:
... return opt
>>> isinstance(CLI.__compile__(), click.Group)
True
"""
# check for circular dependencies
cls._check_descendants()
# create the group
click_group: click.Group = _group.get_group(cls)
# add commands to the group
for name in cls.__feud_commands__:
command: click.Command = getattr(cls, name)
click_group.add_command(command)
# compile all subgroups
for subgroup in cls.__feud_subgroups__:
subgroup.__compile__(parent=click_group)
# add the command group to the parent group if there is one
if parent:
parent.add_command(click_group)
return click_group
@staticmethod
def __main__() -> None: # noqa: D105
pass
@classmethod
def __sections__(cls: type[Group]) -> list[feud.Section]:
"""Sections to partition commands and subgroups into.
These sections are displayed on the group help page if ``rich-click``
is installed.
Returns
-------
list[Section]
Command sections.
Examples
--------
>>> import feud
>>> class Test(feud.Group):
... def one():
... pass
... def two():
... pass
... def three():
... pass
... def __sections__() -> list[feud.Section]:
... return [
... feud.Section(
... name="Odd commands", items=["one", "three"]
... ),
... feud.Section(name="Even commands", items=["two"]),
... feud.Section(name="Groups", items=["subgroup"]),
... ]
>>> class Subgroup(feud.Group):
... pass
>>> Test.register(Subgroup)
"""
return [
feud.Section(
name="Command groups",
items=cls.subgroups(name=True),
)
]
@classmethod
def name(cls: type[Group]) -> str:
"""Return the name of the group.
Returns
-------
str
The group name.
Examples
--------
>>> import feud
>>> class A(feud.Group):
... pass
>>> A.name()
'a'
"""
return cls.__feud_click_kwargs__["name"]
@classmethod
def compile(cls: type[Group]) -> click.Group: # noqa: A003
"""Compile the group into a :py:class:`click.Group`.
Returns
-------
click.Group
The generated group.
Examples
--------
>>> import feud, click
>>> class CLI(feud.Group):
... def func(*, opt: int) -> int:
... return opt
>>> isinstance(CLI.compile(), click.Group)
True
"""
return cls.__compile__()
@classmethod
def commands(
cls: type[Group], *, name: bool = False
) -> list[click.Command] | list[str]:
"""Commands defined in the group.
Parameters
----------
name:
Whether or not to return the command names.
Returns
-------
list[click.Command] | list[str]
Group commands.
Examples
--------
>>> import feud
>>> class Test(feud.Group):
... def func_a():
... pass
... def func_b():
... pass
>>> Test.commands()
[<Command func_a>, <Command func_b>]
"""
commands: list[click.Command] = [
getattr(cls, cmd) for cmd in cls.__feud_commands__
]
if name:
return [command.name for command in commands]
return commands
@classmethod
def subgroups(
cls: type[Group], *, name: bool = False
) -> list[type[Group]] | list[str]:
"""Registered subgroups.
Parameters
----------
name:
Whether or not to return the subgroup names.
Returns
-------
list[type[Group]] | list[str]
Registered subgroups.
Examples
--------
>>> import feud
>>> class A(feud.Group):
... pass
>>> class B(feud.Group):
... pass
>>> class C(feud.Group):
... pass
>>> A.register([B, C])
>>> A.subgroups() # doctest: +SKIP
[<class 'group.B'>, <class 'group.C'>]
See Also
--------
descendants:
Directed acyclic graph of subgroup descendants.
""" # noqa: D401
if name:
return [sub.name() for sub in cls.__feud_subgroups__]
return list(cls.__feud_subgroups__)
@classmethod
def descendants(cls: type[Group]) -> OrderedDict[type[Group], OrderedDict]:
"""Directed acyclic graph of subgroup descendants.
Returns
-------
collections.OrderedDict[type[Group], collections.OrderedDict]
Subgroup descendants.
Examples
--------
>>> import feud
>>> class A(feud.Group):
... pass
>>> class B(feud.Group):
... pass
>>> class C(feud.Group):
... pass
>>> A.register(B)
>>> B.register(C)
>>> A.descendants() # doctest: +SKIP
OrderedDict([
(
<class 'group.B'>,
OrderedDict([
(
<class 'group.C'>,
OrderedDict()
)
])
)
])
See Also
--------
subgroups:
Registered subgroups.
"""
return OrderedDict(
(group, group.descendants()) for group in cls.__feud_subgroups__
)
@classmethod
def _descendants(cls: type[Group]) -> t.Generator[type[Group]]:
for group in cls.__feud_subgroups__:
yield group
yield from group._descendants() # noqa: SLF001
@classmethod
def _check_descendants(
cls: type[Group], __target: type[Group] | None = None, /
) -> None:
group: type[Group] = __target or cls
if cls is __target:
msg = f"Group {cls.__name__!r} cannot be a subgroup of itself."
raise feud.RegistrationError(msg)
if cls in group._descendants(): # noqa: SLF001
msg = (
f"Group {cls.__name__!r} is a descendant subgroup of "
f"{group.__name__!r}, causing a circular dependency."
)
raise feud.RegistrationError(msg)
@classmethod
def register(
cls: type[Group],
sub: type[Group] | list[type[Group]],
/,
) -> None:
"""Register one or more subgroups.
Parameters
----------
sub:
The subgroup(s) to register.
Examples
--------
Registering a single subgroup.
>>> import feud
>>> class A(feud.Group):
... pass
>>> class B(feud.Group):
... pass
>>> A.register(B)
>>> A.subgroups()
[<class 'group.B'>]
Registering multiple subgroups.
>>> import feud
>>> class A(feud.Group):
... pass
>>> class B(feud.Group):
... pass
>>> class C(feud.Group):
... pass
>>> A.register([B, C])
>>> A.subgroups()
[<class 'group.B'>, <class 'group.C'>]
See Also
--------
deregister:
Deregister one or more subgroups.
"""
subgroups: list[type[Group]] = []
# sub is a list of groups - check each group
if isinstance(sub, list):
for group in sub:
# check if already registered or about to be registered
if group in cls.__feud_subgroups__ + subgroups:
msg = (
f"Group {group.__name__!r} is already registered as a "
f"subgroup under {cls.__name__!r} and will be ignored."
)
warnings.warn(msg, RuntimeWarning, stacklevel=1)
continue
# check for circular dependencies
cls._check_descendants(group)
subgroups.append(group)
# sub is a group - check if already registered
elif sub in cls.__feud_subgroups__:
msg = (
f"Group {sub.__name__!r} is already registered as a "
f"subgroup under {cls.__name__!r} and will be ignored."
)
warnings.warn(msg, RuntimeWarning, stacklevel=1)
# sub is an unregistered group - register it
else:
# check for circular dependencies
cls._check_descendants(sub)
subgroups.append(sub)
# update subgroups
cls.__feud_subgroups__.extend(subgroups)
@classmethod
def deregister(
cls: type[Group],
sub: type[Group] | list[type[Group]] | None = None,
/,
) -> None:
"""Deregister one or more subgroups.
Parameters
----------
sub:
The subgroup(s) to register.
Examples
--------
Deregistering a single subgroup.
>>> import feud
>>> class A(feud.Group):
... pass
>>> class B(feud.Group):
... pass
>>> A.register(B)
>>> A.subgroups()
[<class 'group.B'>]
>>> A.deregister(B)
>>> A.subgroups()
[]
Deregistering multiple subgroups.
>>> import feud
>>> class A(feud.Group):
... pass
>>> class B(feud.Group):
... pass
>>> class C(feud.Group):
... pass
>>> A.register([B, C])
>>> A.subgroups()
[<class 'group.B'>, <class 'group.C'>]
>>> A.deregister([B, C])
>>> A.subgroups()
[]
See Also
--------
register:
Register one or more subgroups.
"""
subgroups: list[type[Group]] = []
if sub:
# sub is a list of groups - check each group
if isinstance(sub, list):
for group in sub:
# check if not registered or about to be deregistered
if (
group not in cls.__feud_subgroups__
or group in subgroups
):
msg = (
f"Group {group.__name__!r} is not a registered "
f"subgroup under {cls.__name__!r} and will be "
"ignored."
)
warnings.warn(msg, RuntimeWarning, stacklevel=1)
continue
subgroups.append(group)
# sub is a group - check if not registered
elif sub not in cls.__feud_subgroups__:
msg = (
f"Group {sub.__name__!r} is not a registered subgroup "
f"under {cls.__name__!r} and will be ignored."
)
warnings.warn(msg, RuntimeWarning, stacklevel=1)
# sub is a registered group - deregister it
else:
subgroups.append(sub)
# deregister subgroups
cls.__feud_subgroups__[:] = [
group
for group in cls.__feud_subgroups__
if group not in subgroups
]
else:
# deregister all subgroups
cls.__feud_subgroups__ = []
@classmethod
def from_dict(
cls: type[Group],
obj: dict[str, click.Command | type[Group] | t.Callable],
/,
**kwargs: t.Any,
) -> type[Group]:
"""Create a group from a :py:obj:`dict` of runnable objects.
Parameters
----------
obj:
:py:obj:`dict` of runnable function, command or group objects.
**kwargs:
Click keywords or Feud configuration to apply:
- ``name``: :py:obj:`str`
- ``help``: :py:obj:`str`
- ``epilog``: :py:obj:`str`
- ``config``: :py:func:`.config`
Returns
-------
Group
The generated group.
"""
# split commands and subgroups
commands: dict[str, click.Command | t.Callable] = obj.copy()
subgroups: dict[str, type[Group]] = {
k: commands.pop(k)
for k, v in obj.items()
if inspect.isclass(v) and issubclass(v, Group)
}
# rename commands (if necessary)
funcs: list[str] = []
for name, command in commands.copy().items():
if isinstance(command, click.Command):
if name != command.name:
# copy command
commands[name] = copy.copy(command)
commands[name].name = name
elif isinstance(command, t.Callable) and name != command.__name__:
# note commands generated by functions to be renamed later
funcs.append(name)
# rename groups
for name, subgroup in subgroups.copy().items():
group_name: str = subgroup.__feud_click_kwargs__.get("name")
if name != group_name:
# copy (subclass) group
subgroups[name] = types.new_class(
"__feud_group__",
bases=(subgroup,),
kwds={"name": name},
exec_body=(
lambda body: body.update(
{"__doc__": subgroup.__doc__}, # noqa: B023
)
),
)
# create group (only with commands)
group: type[Group] = types.new_class(
"__feud_group__",
bases=(feud.Group,),
kwds={k: v for k, v in kwargs.items() if v is not None},
exec_body=(lambda body: body.update(commands)),
)
# rename commands generated from functions (if necessary)
for name in funcs:
command: click.Command = getattr(group, name)
command.name = name
# register subgroups (if any)
if subgroups:
group.register(list(subgroups.values()))
return group
@classmethod
def from_iter(
cls: type[Group],
obj: t.Iterable[click.Command | type[Group] | t.Callable],
/,
**kwargs: t.Any,
) -> type[Group]:
"""Create a group from an iterable of runnable objects.
Parameters
----------
obj:
:py:obj:`dict` of runnable function, command or group objects.
**kwargs:
Click keywords or Feud configuration to apply:
- ``name``: :py:obj:`str`
- ``help``: :py:obj:`str`
- ``epilog``: :py:obj:`str`
- ``config``: :py:func:`.config`
Returns
-------
Group
The generated group.
"""
# convert to list
obj: list[click.Command | type[Group] | t.Callable] = list(obj)
# split commands and subgroups
commands: list[click.Command | t.Callable] = obj.copy()
subgroups: list[type[Group]] = [
commands.pop(i)
for i, v in enumerate(obj)
if inspect.isclass(v) and issubclass(v, Group)
]
# function to get the name of a command or function
def get_name(o: click.Command | t.Callable) -> str:
return o.name if isinstance(o, click.Command) else o.__name__
# group members
members: dict[str, click.Command | t.Callable] = {
get_name(cmd): cmd for cmd in commands
}
# create group (only with commands)
group: type[Group] = types.new_class(
"__feud_group__",
bases=(feud.Group,),
kwds={k: v for k, v in kwargs.items() if v is not None},
exec_body=(lambda body: body.update(members)),
)
# register subgroups (if any)
if subgroups:
group.register(subgroups)
return group
@classmethod
def from_module(
cls: type[Group],
obj: types.ModuleType,
/,
**kwargs: t.Any,
) -> type[Group]:
"""Create a group from a module of runnable function, command or
group objects.
Parameters
----------
obj:
Module of runnable function, command or group objects.
**kwargs:
Click keywords or Feud configuration to apply:
- ``name``: :py:obj:`str`
- ``help``: :py:obj:`str`
- ``epilog``: :py:obj:`str`
- ``config``: :py:func:`.config`
Returns
-------
Group
The generated group.
"""
def is_command(item: t.Any) -> bool:
if inspect.isfunction(item):
return inspect.getmodule(item) == obj
return isinstance(item, click.Command)
# function to get the name of a command or function
def get_name(o: click.Command | t.Callable) -> str:
return o.name if isinstance(o, click.Command) else o.__name__
# split function/click.Command/click.Group from feud.Group
commands: list[t.Callable | click.Command] = []
groups: list[type[Group]] = []
for item in obj.__dict__.values():
if is_command(item):
commands.append(item)
elif inspect.isclass(item) and issubclass(item, Group):
groups.append(item)
# group members
members: dict[str, click.Command | t.Callable] = {
get_name(cmd): cmd for cmd in commands
}
# add module docstring
members["__doc__"] = obj.__doc__
# set group name as module name if none provided
kwargs["name"] = kwargs.get("name") or obj.__name__.split(".")[-1]
# discard non-root groups
non_root: set[type[Group]] = set(
chain.from_iterable(
group._descendants() # noqa: SLF001
for group in groups
)
)
subgroups = [group for group in groups if group not in non_root]
# create group (only with commands)
group: type[Group] = types.new_class(
"__feud_group__",
bases=(feud.Group,),
kwds={k: v for k, v in kwargs.items() if v is not None},
exec_body=(lambda body: body.update(members)),
)
# register subgroups (if any)
if subgroups:
group.register(subgroups)
return group