|
1 | | -import sifter.grammar |
2 | | -import sifter.validators |
| 1 | +from email.message import Message |
| 2 | +from typing import ( |
| 3 | + Text |
| 4 | +) |
3 | 5 |
|
4 | | -__all__ = ('CommandRewrite',) |
| 6 | +from sifter.grammar.string import expand_variables |
| 7 | +from sifter.grammar.command import Command |
| 8 | +from sifter.validators.stringlist import StringList |
| 9 | +from sifter.grammar.state import EvaluationState |
5 | 10 |
|
6 | | -# section 4.1 |
7 | | -class CommandRewrite(sifter.grammar.Command): |
8 | 11 |
|
9 | | - RULE_IDENTIFIER = 'REWRITE' |
| 12 | +class CommandRewrite(Command): |
10 | 13 |
|
11 | | - def __init__(self, arguments=None, tests=None, block=None): |
12 | | - super(CommandRewrite, self).__init__(arguments, tests, block) |
13 | | - _, positional_args = self.validate_arguments( |
14 | | - {}, |
15 | | - [ sifter.validators.StringList(length=1), |
16 | | - sifter.validators.StringList(length=1), |
17 | | - ], |
18 | | - ) |
19 | | - self.validate_tests_size(0) |
20 | | - self.validate_block_size(0) |
21 | | - self.search = positional_args[0][0] |
22 | | - self.replace = positional_args[1][0] |
| 14 | + HANDLER_ID: Text = 'REWRITE' |
| 15 | + EXTENSION_NAME = 'rewrite' |
| 16 | + POSITIONAL_ARGS = [ |
| 17 | + StringList(length=1), |
| 18 | + StringList(length=1), |
| 19 | + ] |
23 | 20 |
|
24 | | - def evaluate(self, message, state): |
| 21 | + def evaluate(self, message: Message, state: EvaluationState) -> None: |
25 | 22 | state.check_required_extension('rewrite', 'REWRITE') |
26 | | - search = sifter.grammar.string.expand_variables(self.search, state) |
27 | | - replace = sifter.grammar.string.expand_variables(self.replace, state) |
28 | | - state.actions.append('rewrite', (search, replace)) |
29 | | - |
30 | | -CommandRewrite.register() |
| 23 | + search = self.positional_args[0][0] # type: ignore |
| 24 | + replace = self.positional_args[1][0] # type: ignore |
| 25 | + search = expand_variables(search, state) |
| 26 | + replace = expand_variables(replace, state) |
| 27 | + state.actions.append('rewrite', (search, replace)) # type: ignore |
0 commit comments