Passed
Pull Request — main (#378)
by
unknown
01:45
created

Permission.__setattr__()   A

Complexity

Conditions 3

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 3
nop 3
1
from __future__ import annotations
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
3
from enum import Enum
4
from typing import Tuple, Optional
5
6
7
class PermissionEnums(Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
8
    CREATE_INSTANT_INVITE = 1 << 0
9
    KICK_MEMBERS = 1 << 1
10
    BAN_MEMBERS = 1 << 2
11
    ADMINISTRATOR = 1 << 3
12
    MANAGE_CHANNELS = 1 << 4
13
    MANAGE_GUIlD = 1 << 5
14
    ADD_REACTIONS = 1 << 6
15
    VIEW_AUDIT_LOG = 1 << 7
16
    PRIORITY_SPEAKER = 1 << 8
17
    STREAM = 1 << 9
18
    VIEW_CHANNEL = 1 << 10
19
    SEND_MESSAGES = 1 << 11
20
    SEND_TTS_MESSAGES = 1 << 12
21
    MANAGE_MESSAGES = 1 << 13
22
    EMBED_LINKS = 1 << 14
23
    ATTACH_FILES = 1 << 15
24
    READ_MESSAGE_HISTORY = 1 << 16
25
    MENTION_EVERYONE = 1 << 17
26
    USE_EXTERNAL_EMOJIS = 1 << 18
27
    VIEW_GUILD_INSIGHTS = 1 << 19
28
    CONNECT = 1 << 20
29
    SPEAK = 1 << 21
30
    MUTE_MEMBERS = 1 << 22
31
    DEAFEN_MEMBERS = 1 << 23
32
    MOVE_MEMBERS = 1 << 24
33
    USE_VAD = 1 << 25
34
    CHANGE_NICKNAME = 1 << 26
35
    MANAGE_NICKNAMES = 1 << 27
36
    MANAGE_ROLES = 1 << 28
37
    MANAGE_WEBHOOKS = 1 << 29
38
    MANAGE_EMOJIS_AND_STICKERS = 1 << 30
39
    USE_APPLICATION_COMMANDS = 1 << 31
40
    REQUEST_TO_SPEAK = 1 << 32
41
    MANAGE_EVENTS = 1 << 33
42
    MANAGE_THREADS = 1 << 34
43
    CREATE_PUBLIC_THREADS = 1 << 35
44
    CREATE_PRIVATE_THREADS = 1 << 36
45
    USE_EXTERNAL_STICKERS = 1 << 37
46
    SEND_MESSAGES_IN_THREADS = 1 << 38
47
    START_EMBEDDED_ACTIVITIES = 1 << 39
48
    MODERATE_MEMBERS = 1 << 40
49
50
51
class Permission:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
52
    def __init__(self, **kwargs: Optional[bool]) -> None:
53
        valid_perms = (enum.name.lower() for enum in PermissionEnums)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable enum does not seem to be defined.
Loading history...
54
        for perm in valid_perms:
55
            setattr(self, perm, kwargs.pop(perm, None))
56
57
        if kwargs:
58
            invalid_perms = ', '.join(kwargs.keys())
59
            raise ValueError(f"Invalid permissions were passed in: {invalid_perms}")
60
61
    def __setattr__(self, name: str, value: Optional[bool]) -> None:
62
        if not (value is None or isinstance(value, bool)):
63
            raise ValueError(f"Permission {name!r} must be a boolean or None")
64
        return super().__setattr__(name, value)
65
66
    @classmethod
67
    def from_int(cls, allow: int, deny: int) -> Permission:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
68
        clsobj = cls()
69
70
        for enum in PermissionEnums:
71
            if bool(enum.value & allow):
72
                setattr(clsobj, enum.name.lower(), True)
73
            elif bool(enum.value & deny):
74
                setattr(clsobj, enum.name.lower(), False)
75
            else:
76
                setattr(clsobj, enum.name.lower(), None)
77
78
        return clsobj
79
80
    def to_int(self) -> Tuple[int]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
81
        allow = 0
82
        deny = 0
83
        for enum in PermissionEnums:
84
            if getattr(self, enum.name.lower()):
85
                allow |= enum.value
86
            elif getattr(self, enum.name.lower()) is False:
87
                deny |= enum.value
88
89
        return allow, deny
90