mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-12 16:59:50 +00:00
Make Permissions partially-ordered.
Specifically: * P1 <= P2 iff P1 expresses a subset of the permissions expressed by P2. * P1 < P2 iff P1 <= P2 and P1 != P2 * vice versa for P1 >= P2 and P1 > P2
This commit is contained in:
parent
7bae6dc5bc
commit
21c88cf727
@ -36,6 +36,18 @@ class Permissions:
|
|||||||
+-----------+------------------------------------------+
|
+-----------+------------------------------------------+
|
||||||
| x != y | Checks if two permissions are not equal. |
|
| x != y | Checks if two permissions are not equal. |
|
||||||
+-----------+------------------------------------------+
|
+-----------+------------------------------------------+
|
||||||
|
| x <= y | Checks if a permission is a subset |
|
||||||
|
| | of another permission. |
|
||||||
|
+-----------+------------------------------------------+
|
||||||
|
| x >= y | Checks if a permission is a superset |
|
||||||
|
| | of another permission. |
|
||||||
|
+-----------+------------------------------------------+
|
||||||
|
| x < y | Checks if a permission is a strict |
|
||||||
|
| | subset of another permission. |
|
||||||
|
+-----------+------------------------------------------+
|
||||||
|
| x > y | Checks if a permission is a strict |
|
||||||
|
| | superset of another permission. |
|
||||||
|
+-----------+------------------------------------------+
|
||||||
| hash(x) | Return the permission's hash. |
|
| hash(x) | Return the permission's hash. |
|
||||||
+-----------+------------------------------------------+
|
+-----------+------------------------------------------+
|
||||||
|
|
||||||
@ -63,6 +75,33 @@ class Permissions:
|
|||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self.value)
|
return hash(self.value)
|
||||||
|
|
||||||
|
def is_subset(self, other):
|
||||||
|
"""Returns True if other has the same or fewer permissions as self."""
|
||||||
|
if isinstance(other, Permissions):
|
||||||
|
return (self.value & other.value) == self.value
|
||||||
|
else:
|
||||||
|
raise TypeError("cannot compare {} with {}".format(self.__class__.__name__, other.__class__name))
|
||||||
|
|
||||||
|
def is_superset(self, other):
|
||||||
|
"""Returns True if other has the same or more permissions as self."""
|
||||||
|
if isinstance(other, Permissions):
|
||||||
|
return (self.value | other.value) == self.value
|
||||||
|
else:
|
||||||
|
raise TypeError("cannot compare {} with {}".format(self.__class__.__name__, other.__class__name))
|
||||||
|
|
||||||
|
def is_strict_subset(self, other):
|
||||||
|
"""Returns True if the permissions on other are a strict subset of those on self."""
|
||||||
|
return self.is_subset(other) and self != other
|
||||||
|
|
||||||
|
def is_strict_superset(self, other):
|
||||||
|
"""Returns True if the permissions on other are a strict superset of those on self."""
|
||||||
|
return self.is_superset(other) and self != other
|
||||||
|
|
||||||
|
__le__ = is_subset
|
||||||
|
__ge__ = is_superset
|
||||||
|
__lt__ = is_strict_subset
|
||||||
|
__gt__ = is_strict_superset
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def none(cls):
|
def none(cls):
|
||||||
"""A factory method that creates a :class:`Permissions` with all
|
"""A factory method that creates a :class:`Permissions` with all
|
||||||
|
Loading…
x
Reference in New Issue
Block a user