Passed
Pull Request — main (#378)
by
unknown
01:44
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
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# Full MIT License can be found in `LICENSE` at the project root.
3
4
from __future__ import annotations
5
6
from enum import Enum
7
from typing import Tuple, Optional
8
9
10
class PermissionEnums(Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
11
    CREATE_INSTANT_INVITE = 1 << 0
12
    KICK_MEMBERS = 1 << 1
13
    BAN_MEMBERS = 1 << 2
14
    ADMINISTRATOR = 1 << 3
15
    MANAGE_CHANNELS = 1 << 4
16
    MANAGE_GUIlD = 1 << 5
17
    ADD_REACTIONS = 1 << 6
18
    VIEW_AUDIT_LOG = 1 << 7
19
    PRIORITY_SPEAKER = 1 << 8
20
    STREAM = 1 << 9
21
    VIEW_CHANNEL = 1 << 10
22
    SEND_MESSAGES = 1 << 11
23
    SEND_TTS_MESSAGES = 1 << 12
24
    MANAGE_MESSAGES = 1 << 13
25
    EMBED_LINKS = 1 << 14
26
    ATTACH_FILES = 1 << 15
27
    READ_MESSAGE_HISTORY = 1 << 16
28
    MENTION_EVERYONE = 1 << 17
29
    USE_EXTERNAL_EMOJIS = 1 << 18
30
    VIEW_GUILD_INSIGHTS = 1 << 19
31
    CONNECT = 1 << 20
32
    SPEAK = 1 << 21
33
    MUTE_MEMBERS = 1 << 22
34
    DEAFEN_MEMBERS = 1 << 23
35
    MOVE_MEMBERS = 1 << 24
36
    USE_VAD = 1 << 25
37
    CHANGE_NICKNAME = 1 << 26
38
    MANAGE_NICKNAMES = 1 << 27
39
    MANAGE_ROLES = 1 << 28
40
    MANAGE_WEBHOOKS = 1 << 29
41
    MANAGE_EMOJIS_AND_STICKERS = 1 << 30
42
    USE_APPLICATION_COMMANDS = 1 << 31
43
    REQUEST_TO_SPEAK = 1 << 32
44
    MANAGE_EVENTS = 1 << 33
45
    MANAGE_THREADS = 1 << 34
46
    CREATE_PUBLIC_THREADS = 1 << 35
47
    CREATE_PRIVATE_THREADS = 1 << 36
48
    USE_EXTERNAL_STICKERS = 1 << 37
49
    SEND_MESSAGES_IN_THREADS = 1 << 38
50
    START_EMBEDDED_ACTIVITIES = 1 << 39
51
    MODERATE_MEMBERS = 1 << 40
52
53
54
class Permission:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
55
    def __init__(self, **kwargs: Optional[bool]) -> None:
56
        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...
57
        for perm in valid_perms:
58
            setattr(self, perm, kwargs.pop(perm, None))
59
60
        if kwargs:
61
            invalid_perms = ', '.join(kwargs.keys())
62
            raise ValueError(f"Invalid permissions were passed in: {invalid_perms}")
63
64
    def __setattr__(self, name: str, value: Optional[bool]) -> None:
65
        if not (value is None or isinstance(value, bool)):
66
            raise ValueError(f"Permission {name!r} must be a boolean or None")
67
        return super().__setattr__(name, value)
68
69
    def __eq__(self, object) -> bool:
0 ignored issues
show
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
Bug Best Practice introduced by
This seems to re-define the built-in object.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
70
        """
71
        Permission equality is determined by comparing the integer values of the permissions
72
        """
73
        if isinstance(object, Permission):
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
74
            return self.to_int() == object.to_int()
75
        elif isinstance(object, tuple):
76
            return self.to_int() == object
77
78
    @classmethod
79
    def from_int(cls, allow: int, deny: int) -> Permission:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
80
        clsobj = cls()
81
82
        for enum in PermissionEnums:
83
            if bool(enum.value & allow):
84
                setattr(clsobj, enum.name.lower(), True)
85
            elif bool(enum.value & deny):
86
                setattr(clsobj, enum.name.lower(), False)
87
            else:
88
                setattr(clsobj, enum.name.lower(), None)
89
90
        return clsobj
91
92
    def to_int(self) -> Tuple[int]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
93
        allow = 0
94
        deny = 0
95
        for enum in PermissionEnums:
96
            if getattr(self, enum.name.lower()):
97
                allow |= enum.value
98
            elif getattr(self, enum.name.lower()) is False:
99
                deny |= enum.value
100
101
        return allow, deny
102