Debian Codegraph @

docs/extending.rst

raw ◀ ▶
1 Debian patch touch this file
1 Extending afew
2 ==============
3
4 You can put python files in `~/.config/afew/` and they will be imported by
5 afew. If you use that python file to define a `Filter` class and use the
6 `register_filter` decorator then you can refer to it in your filter
7 configuration.
8
9 So an example small filter you could add might be:
10
11 .. code-block:: python
12
13 from afew.filters.BaseFilter import Filter
14 from afew.FilterRegistry import register_filter
15
16 PROJECT_MAPPING = {
17 'fabric': 'deployment',
18 'oldname': 'new-name',
19 }
20
21 @register_filter
22 class RedmineFilter(Filter):
23 message = 'Create tag based on redmine project'
24 query = 'NOT tag:redmine'
25
26 def handle_message(self, message):
27 project = message.get_header('X-Redmine-Project')
28 if project in PROJECT_MAPPING:
29 project = PROJECT_MAPPING[project]
30 self.add_tags(message, 'redmine', project)
31
32 We have defined the `message` and `query` class variables that are used
33 by the parent class `Filter`. The `message` is printed when running with
34 verbose flags. The `query` is used to select messages to run against - here
35 we ensure we don't bother looking at messages we've already looked at.
36
37 The `handle_message()` method is the key one to implement. This will be called
38 for each message that matches the query. The argument is a `notmuch message object`_
•39 and the key methods used by the afew filters are `header()`, `filename()`
40 and `get_thread()`.
41
42 .. _notmuch message object: http://pythonhosted.org/notmuch/#message-a-single-message
43
44 Of the methods inherited from the `Filter` class the key ones are `add_tags()` and
45 `remove_tags()`, but read about the :doc:`implementation` or just read the source
46 code to get your own ideas.
47
48 Once you've defined your filter, you can add it to your config like any other filter:
49
50 .. code-block:: ini
51
52 [RedmineFilter]