Skip to content

Commit fa022e7

Browse files
added type hints
1 parent f96a51e commit fa022e7

4 files changed

Lines changed: 34 additions & 30 deletions

File tree

sifter/commands/notify.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import re
2+
from email.message import Message
3+
from typing import Optional
4+
25
from sifter.grammar.command import Command
36
from sifter.grammar.string import expand_variables
47
from sifter.validators.stringlist import StringList
58
from sifter.validators.tag import Tag
69
from sifter.grammar.rule import RuleSyntaxError
710
from sifter.extensions import ExtensionRegistry
11+
from sifter.grammar.state import EvaluationState
12+
from sifter.grammar.actions import Actions
813

914

1015
# RFC 5435
@@ -21,39 +26,39 @@ class CommandNotify(Command):
2126
StringList(length=1)
2227
]
2328

24-
def __init__(self, arguments=None, tests=None, block=None):
25-
super(CommandNotify, self).__init__(arguments, tests, block)
26-
27-
self.notify_from = self.notify_importance = self.notify_message = None
28-
self.notify_options = []
29+
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
30+
notify_from = notify_importance = self.notify_message = None
31+
notify_options = [] # type: ignore
2932
if 'from' in self.tagged_args:
30-
self.notify_from = self.tagged_args['from'][1][0]
33+
notify_from = self.tagged_args['from'][1][0] # type: ignore
3134
if 'importance' in self.tagged_args:
32-
self.notify_importance = self.tagged_args['importance'][1][0]
35+
notify_importance = self.tagged_args['importance'][1][0] # type: ignore
3336
if 'options' in self.tagged_args:
34-
self.notify_options = self.tagged_args['options'][1]
37+
notify_options = self.tagged_args['options'][1] # type: ignore
38+
notify_message = None
3539
if 'message' in self.tagged_args:
36-
self.notify_message = self.tagged_args['message'][1][0] # type: ignore
37-
self.notify_method = self.positional_args[0][0] # type: ignore
40+
notify_message = self.tagged_args['message'][1][0] # type: ignore
41+
notify_method = self.positional_args[0][0] # type: ignore
3842

39-
def evaluate(self, message, state):
4043
state.check_required_extension('enotify', 'NOTIFY')
41-
notify_from = expand_variables(self.notify_from, state)
42-
notify_importance = expand_variables(self.notify_importance, state)
43-
notify_options = map(lambda s: expand_variables(s, state), self.notify_options)
44-
notify_message = expand_variables(self.notify_message, state)
45-
notify_method = expand_variables(self.notify_method, state)
44+
notify_from = expand_variables(notify_from, state) # type: ignore
45+
notify_importance = expand_variables(notify_importance, state) # type: ignore
46+
notify_options = list(map(lambda s: expand_variables(s, state), notify_options))
47+
notify_message = expand_variables(notify_message, state) # type: ignore
48+
notify_method = expand_variables(notify_method, state)
4649

4750
m = re.match('^([A-Za-z][A-Za-z0-9.+-]*):', notify_method)
4851
if not m:
4952
raise RuleSyntaxError("Notification method must be an URI, e.g. 'mailto:email@example.com'")
5053
if notify_importance and notify_importance not in ["1", "2", "3"]:
51-
raise RuleSyntaxError("Illegal notify importance '%s' encountered" % self.notify_importance)
54+
raise RuleSyntaxError("Illegal notify importance '%s' encountered" % notify_importance)
5255
notify_method_cls = ExtensionRegistry.get_notification_method(m.group(1).lower())
5356
if not notify_method_cls:
5457
raise RuleSyntaxError("Unsupported notification method '%s'" % m.group(1))
5558
(res, msg) = notify_method_cls.test_valid(notify_method)
5659
if not res:
5760
raise RuleSyntaxError(msg)
5861

59-
state.actions.append('notify', (notify_method, notify_from, notify_importance, notify_options, notify_message))
62+
state.actions.append('notify', (notify_method, notify_from, notify_importance, notify_options, notify_message)) # type: ignore
63+
64+
return None

sifter/commands/variables.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
import urllib
2+
from urllib.parse import quote
33
from sifter.grammar.command import Command
44
from sifter.grammar.rule import RuleSyntaxError
55
from sifter.validators.stringlist import StringList
@@ -55,10 +55,7 @@ def evaluate(self, message, state):
5555
if 'quoteregex' in self.variable_modifier:
5656
variable_value = re.escape(variable_value)
5757
if 'encodeurl' in self.variable_modifier:
58-
try:
59-
variable_value = urllib.quote(variable_value, safe='-._~')
60-
except AttributeError:
61-
variable_value = urllib.parse.quote(variable_value, safe='-._~')
58+
variable_value = quote(variable_value, safe='-._~')
6259
if 'length' in self.variable_modifier:
6360
variable_value = "" + len(variable_value)
6461
state.named_variables[self.variable_name] = variable_value

sifter/tests/body.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import re
2+
from typing import (
3+
Text
4+
)
5+
from sifter.grammar.state import EvaluationState
26

37
from sifter.grammar.test import Test
4-
import sifter.grammar.string
5-
import sifter.validators
68
from sifter.validators.stringlist import StringList
79
from sifter.validators.tag import Comparator, MatchType, BodyTransform
10+
from sifter.grammar.string import compare as string_compare, expand_variables
811

912

1013
# RFC 5173
@@ -69,10 +72,9 @@ def evaluate(self, message, state):
6972
return True
7073
return False
7174

72-
def evaluate_part(self, part_str, state):
75+
def evaluate_part(self, part_str: Text, state: EvaluationState) -> bool:
7376
for key in self.keylist:
74-
key = sifter.grammar.string.expand_variables(key, state)
75-
if sifter.grammar.string.compare(part_str, key, state,
76-
self.comparator, self.match_type):
77+
key = expand_variables(key, state)
78+
if string_compare(part_str, key, state, self.comparator, self.match_type):
7779
return True
7880
return False

sifter/tests/notify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class TestNotifyMethodCapability(Test):
5151
StringList(),
5252
]
5353

54-
def evaluate(self, message, state):
54+
def evaluate(self, message: Message, state: EvaluationState) -> Optional[bool]:
5555
state.check_required_extension('enotify', 'NOTIFY')
5656

5757
if 'comparator' in self.tagged_args:

0 commit comments

Comments
 (0)