Skip to content

Commit b6f28b2

Browse files
committed
Add base Filter class
1 parent e7fdf5a commit b6f28b2

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

pyrogram/client/filters/filter.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
20+
class Filter:
21+
def __call__(self, message):
22+
raise NotImplementedError
23+
24+
def __invert__(self):
25+
return InvertFilter(self)
26+
27+
def __and__(self, other):
28+
return AndFilter(self, other)
29+
30+
def __or__(self, other):
31+
return OrFilter(self, other)
32+
33+
34+
class InvertFilter(Filter):
35+
def __init__(self, base):
36+
self.base = base
37+
38+
def __call__(self, message):
39+
return not self.base(message)
40+
41+
42+
class AndFilter(Filter):
43+
def __init__(self, base, other):
44+
self.base = base
45+
self.other = other
46+
47+
def __call__(self, message):
48+
return self.base(message) and self.other(message)
49+
50+
51+
class OrFilter(Filter):
52+
def __init__(self, base, other):
53+
self.base = base
54+
self.other = other
55+
56+
def __call__(self, message):
57+
return self.base(message) or self.other(message)

0 commit comments

Comments
 (0)