-
Notifications
You must be signed in to change notification settings - Fork 1
/
cog.py
51 lines (32 loc) · 1.17 KB
/
cog.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
from inspect import getfullargspec
class Cogs(dict):
def __init__(self) -> None:
super().__init__()
self.kwargs = {}
def add_cog(self, obj: object) -> None:
if obj.__name__ in self: return
spec = getfullargspec(obj)
requirements = spec.args + spec.kwonlyargs
kwargs = {}
[kwargs.__setitem__(key, value) for key, value in self.kwargs.items() if key in requirements]
super().__setitem__(obj.__name__, obj(**kwargs))
def remove_cog(self, name: str) -> None:
obj = super().__getitem__(name)
del obj
super().pop(name)
def cog(self, obj: object) -> object:
self.add_cog(obj)
return obj
class CogsGroups(dict):
def __init__(self) -> None:
super().__init__()
def __setitem__(self, name: str, kwargs: dict) -> None:
obj = super().get(name) or Cogs()
obj.kwargs = kwargs
super().__setitem__(name, obj)
cogs_groups = CogsGroups()
def get_cogs(name: str = None) -> Cogs:
if name == None: name = "default"
if name not in cogs_groups: cogs_groups[name] = {}
obj = cogs_groups.get(name)
return obj