Skip to content

Commit 37e14f6

Browse files
committed
Improve documentation of 'telegram.ext.filters' module
1 parent 78fee3c commit 37e14f6

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

docs/source/telegram.ext.filters.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@ telegram.ext.filters Module
22
===========================
33

44
.. automodule:: telegram.ext.filters
5-
:members:
5+
:members: BaseFilter
66
:show-inheritance:
7+
8+
.. autoclass:: Filters
9+
:members:
10+
:show-inheritance:
11+
12+
.. autoclass:: telegram.ext.filters::Filters._Document
13+
:members:
14+
:show-inheritance:

telegram/ext/filters.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def __init__(self, pattern):
198198
# the matched groups and groupdict to the context object.
199199

200200
def filter(self, message):
201+
""""""
201202
return bool(self.pattern.search(message.text))
202203

203204
class _Reply(BaseFilter):
@@ -219,82 +220,114 @@ def filter(self, message):
219220
""":obj:`Filter`: Messages that contain :class:`telegram.Audio`."""
220221

221222
class _Document(BaseFilter):
223+
"""
224+
Private inner class for filtering documents
225+
226+
Note:
227+
This class should not be instantiated by the user. It's instantiated as
228+
``Filters.document``, so its members can be used like this: ``Filters.document.apk``
229+
"""
222230
name = 'Filters.document'
223231

224232
class category(BaseFilter):
225-
"""This Filter filters documents by their category in the mime-type attribute
233+
"""
234+
This Filter filters documents by their category in the mime-type attribute.
235+
Matching is performed using ``mime_type.startswith(category)``
226236
227237
Note:
228-
This Filter only filters by the mime_type of the document,
229-
it doesn't check the validity of the document.
238+
This Filter only filters by the ``mime_type`` of the document,
239+
it doesn't check the validity of the document.
230240
The user can manipulate the mime-type of a message and
231-
send media with wrong types that don't fit to this handler.
241+
send media with wrong types that doesn't fit to this handler.
242+
243+
Args:
244+
category (:obj:`str`, optional): category of the media you want to filter
232245
233246
Examples:
234-
Filters.documents.category('audio/') returnes `True` for all types
235-
of audio sent as file, for example 'audio/mpeg' or 'audio/x-wav'
247+
``Filters.document.category('audio/')`` returns ``True`` for all types
248+
of audio sent as file, for example ``'audio/mpeg'`` or ``'audio/x-wav'``
236249
"""
237250

238251
def __init__(self, category):
239-
"""Initialize the category you want to filter
240-
241-
Args:
242-
category (str, optional): category of the media you want to filter"""
252+
"""Initialize the category you want to filter"""
243253
self.category = category
244254
self.name = "Filters.document.category('{}')".format(self.category)
245255

246256
def filter(self, message):
257+
""""""
247258
if message.document:
248259
return message.document.mime_type.startswith(self.category)
249260

250261
application = category('application/')
262+
""":obj:`Filter`: Filters `application` documents"""
251263
audio = category('audio/')
264+
""":obj:`Filter`: Filters `audio` documents"""
252265
image = category('image/')
266+
""":obj:`Filter`: Filters `image` documents"""
253267
video = category('video/')
268+
""":obj:`Filter`: Filters `video` documents"""
254269
text = category('text/')
270+
""":obj:`Filter`: Filters `text` documents"""
255271

256272
class mime_type(BaseFilter):
257273
"""This Filter filters documents by their mime-type attribute
258274
259275
Note:
260-
This Filter only filters by the mime_type of the document,
261-
it doesn't check the validity of document.
276+
This Filter only filters by the ``mime_type`` of the document,
277+
it doesn't check the validity of document.
262278
The user can manipulate the mime-type of a message and
263-
send media with wrong types that don't fit to this handler.
279+
send media with wrong types that doesn't fit to this handler.
280+
281+
Args:
282+
mimetype (:obj:`str`, optional): ``mime_type`` of the media you want to filter
264283
265284
Examples:
266-
Filters.documents.mime_type('audio/mpeg') filters all audio in mp3 format.
285+
``Filters.document.mime_type('audio/mpeg')`` filters all audio in mp3 format.
267286
"""
268287

269288
def __init__(self, mimetype):
270-
"""Initialize the category you want to filter
271-
272-
Args:
273-
filetype (str, optional): mime_type of the media you want to filter"""
289+
"""Initialize the category you want to filter"""
274290
self.mimetype = mimetype
275291
self.name = "Filters.document.mime_type('{}')".format(self.mimetype)
276292

277293
def filter(self, message):
294+
""""""
278295
if message.document:
279296
return message.document.mime_type == self.mimetype
280297

281298
apk = mime_type('application/vnd.android.package-archive')
299+
""":obj:`Filter`: Filters `apk` documents"""
282300
doc = mime_type('application/msword')
301+
""":obj:`Filter`: Filters `doc` documents"""
283302
docx = mime_type('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
303+
""":obj:`Filter`: Filters `docx` documents"""
284304
exe = mime_type('application/x-ms-dos-executable')
305+
""":obj:`Filter`: Filters `exe` documents"""
285306
gif = mime_type('video/mp4')
307+
""":obj:`Filter`: Filters `gif` documents"""
286308
jpg = mime_type('image/jpeg')
309+
""":obj:`Filter`: Filters `jpg` documents"""
287310
mp3 = mime_type('audio/mpeg')
311+
""":obj:`Filter`: Filters `mp3` documents"""
288312
pdf = mime_type('application/pdf')
313+
""":obj:`Filter`: Filters `pdf` documents"""
289314
py = mime_type('text/x-python')
315+
""":obj:`Filter`: Filters `py` documents"""
290316
svg = mime_type('image/svg+xml')
317+
""":obj:`Filter`: Filters `svg` documents"""
291318
txt = mime_type('text/plain')
319+
""":obj:`Filter`: Filters `txt` documents"""
292320
targz = mime_type('application/x-compressed-tar')
321+
""":obj:`Filter`: Filters `targz` documents"""
293322
wav = mime_type('audio/x-wav')
323+
""":obj:`Filter`: Filters `wav` documents"""
294324
xml = mime_type('application/xml')
325+
""":obj:`Filter`: Filters `xml` documents"""
295326
zip = mime_type('application/zip')
327+
""":obj:`Filter`: Filters `zip` documents"""
296328

297329
def filter(self, message):
330+
""""""
298331
return bool(message.document)
299332

300333
document = _Document()
@@ -541,6 +574,7 @@ def __init__(self, entity_type):
541574
self.name = 'Filters.entity({})'.format(self.entity_type)
542575

543576
def filter(self, message):
577+
""""""
544578
return any(entity.type == self.entity_type for entity in message.entities)
545579

546580
class caption_entity(BaseFilter):
@@ -562,6 +596,7 @@ def __init__(self, entity_type):
562596
self.name = 'Filters.caption_entity({})'.format(self.entity_type)
563597

564598
def filter(self, message):
599+
""""""
565600
return any(entity.type == self.entity_type for entity in message.caption_entities)
566601

567602
class _Private(BaseFilter):
@@ -613,6 +648,7 @@ def __init__(self, user_id=None, username=None):
613648
self.usernames = [user.replace('@', '') for user in username]
614649

615650
def filter(self, message):
651+
""""""
616652
if self.user_ids is not None:
617653
return bool(message.from_user and message.from_user.id in self.user_ids)
618654
else:
@@ -651,6 +687,7 @@ def __init__(self, chat_id=None, username=None):
651687
self.usernames = [chat.replace('@', '') for chat in username]
652688

653689
def filter(self, message):
690+
""""""
654691
if self.chat_ids is not None:
655692
return bool(message.chat_id in self.chat_ids)
656693
else:
@@ -699,5 +736,6 @@ def __init__(self, lang):
699736
self.name = 'Filters.language({})'.format(self.lang)
700737

701738
def filter(self, message):
739+
""""""
702740
return message.from_user.language_code and any(
703741
[message.from_user.language_code.startswith(x) for x in self.lang])

0 commit comments

Comments
 (0)