Skip to content

Commit dac4e6f

Browse files
replaced all validation function with a single validation function
1 parent c67e7f6 commit dac4e6f

18 files changed

Lines changed: 51 additions & 46 deletions

File tree

sifter/commands/discard.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ def __init__(
3131
block: Optional[CommandList] = None
3232
) -> None:
3333
super().__init__(arguments, tests, block)
34-
self.validate_arguments()
35-
self.validate_tests_size(0)
36-
self.validate_block_size(0)
34+
self.validate()
3735

3836
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
3937
state.actions.cancel_implicit_keep()

sifter/commands/fileinto.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def __init__(
3333
block: Optional[CommandList] = None
3434
) -> None:
3535
super().__init__(arguments, tests, block)
36-
_, positional_args = self.validate_arguments()
37-
self.validate_tests_size(0)
38-
self.validate_block_size(0)
36+
_, positional_args = self.validate()
3937
self.file_dest = positional_args[0]
4038

4139
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:

sifter/commands/if_cmd.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
# section 3.1
2323
class CommandIfBase(Command):
2424

25+
TESTS_MIN = 1
26+
HAS_BLOCKS = False
27+
2528
def __init__(
2629
self,
2730
arguments: Optional[List[Union['TagGrammar', SupportsInt, List[Union[Text, 'String']]]]] = None,
2831
tests: Optional[List['Test']] = None,
2932
block: Optional[CommandList] = None
3033
) -> None:
3134
super().__init__(arguments, tests, block)
32-
self.validate_arguments()
33-
self.validate_tests_size(1)
35+
self.validate()
3436

3537
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
3638
if self.tests[0].evaluate(message, state):
@@ -65,6 +67,8 @@ def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions
6567
class CommandElse(Command):
6668

6769
RULE_IDENTIFIER = 'ELSE'
70+
TESTS_MIN = 0
71+
HAS_BLOCKS = False
6872

6973
def __init__(
7074
self,
@@ -73,8 +77,7 @@ def __init__(
7377
block: Optional[CommandList] = None
7478
) -> None:
7579
super().__init__(arguments, tests, block)
76-
self.validate_arguments()
77-
self.validate_tests_size(0)
80+
self.validate()
7881

7982
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
8083
if state.last_if:

sifter/commands/keep.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class CommandKeep(Command):
2424

2525
RULE_IDENTIFIER = 'KEEP'
26+
HAS_BLOCKS = False
2627

2728
def __init__(
2829
self,
@@ -31,9 +32,7 @@ def __init__(
3132
block: Optional[CommandList] = None
3233
) -> None:
3334
super().__init__(arguments, tests, block)
34-
self.validate_arguments()
35-
self.validate_tests_size(0)
36-
self.validate_block_size(0)
35+
self.validate()
3736

3837
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
3938
state.actions.append('keep')

sifter/commands/redirect.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,9 @@ def __init__(
3535
block: Optional[CommandList] = None
3636
) -> None:
3737
super().__init__(arguments, tests, block)
38-
_, positional_args = self.validate_arguments()
39-
self.validate_tests_size(0)
40-
self.validate_block_size(0)
41-
if not isinstance(positional_args, list):
42-
raise ValueError("CommandRedirect positional argument error")
43-
if not isinstance(positional_args[0], list):
44-
raise ValueError("CommandRedirect positional argument error")
45-
self.email_address = positional_args[0][0]
38+
_, positional_args = self.validate()
39+
40+
self.email_address = positional_args[0][0] # type: ignore
4641
# TODO: section 2.4.2.3 constrains the email address to a limited
4742
# subset of valid address formats. need to check if python's
4843
# email.utils also uses this subset or if we need to do our own

sifter/commands/require.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@ def __init__(
3535
block: Optional[CommandList] = None
3636
) -> None:
3737
super().__init__(arguments, tests, block)
38-
_, positional_args = self.validate_arguments()
39-
self.validate_tests_size(0)
40-
self.validate_block_size(0)
38+
_, positional_args = self.validate()
4139
self.ext_names = positional_args[0]
4240

4341
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
44-
if not isinstance(self.ext_names, list):
45-
raise ValueError("CommandRequire.ext_names must be a list!")
4642
for ext_name in self.ext_names:
4743
if not sifter.handler.get('extension', ext_name):
4844
raise RuntimeError(

sifter/commands/stop.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ def __init__(
3131
block: Optional[CommandList] = None
3232
) -> None:
3333
super().__init__(arguments, tests, block)
34-
self.validate_arguments()
35-
self.validate_tests_size(0)
36-
self.validate_block_size(0)
34+
self.validate()
3735

3836
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
3937
state.actions.append('stop')

sifter/grammar/command.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
Optional,
66
List,
77
Union,
8-
SupportsInt
8+
SupportsInt,
9+
Tuple,
10+
Dict
911
)
1012

1113
import sifter.grammar
@@ -24,6 +26,8 @@
2426
class Command(Rule):
2527

2628
RULE_TYPE: Text = 'command'
29+
HAS_BLOCKS: bool = True
30+
BLOCKS_MAX: int = 0
2731

2832
def __init__(
2933
self,
@@ -52,5 +56,10 @@ def validate_block_size(self, max_commands: int) -> None:
5256
"%s takes no more than %d commands" % (self.RULE_IDENTIFIER, max_commands)
5357
)
5458

59+
def validate(self) -> Tuple[Dict[Text, List[Union['TagGrammar', SupportsInt, List[Union[Text, 'String']]]]], List[Union['TagGrammar', SupportsInt, List[Union[Text, 'String']]]]]:
60+
if self.HAS_BLOCKS:
61+
self.validate_block_size(max_commands=self.BLOCKS_MAX)
62+
return super().validate()
63+
5564
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
5665
raise NotImplementedError

sifter/grammar/rule.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Rule(object):
3232
TAGGED_ARGS: Optional[Union[List[Validator], Dict[Text, Validator]]] = None
3333
POSITIONAL_ARGS: Optional[List[Validator]] = None
3434

35+
HAS_TESTS: bool = True
36+
TESTS_MIN: Optional[int] = 0
37+
TESTS_MAX: Optional[int] = None
38+
3539
@classmethod
3640
def register(cls) -> None:
3741
try:
@@ -125,3 +129,12 @@ def validate_tests_size(self, min_tests: int, max_tests: Optional[int] = None) -
125129
msg = "between %d and %d" % (min_tests, max_tests)
126130
raise RuleSyntaxError("%s takes %s tests" % (
127131
self.RULE_IDENTIFIER, msg))
132+
133+
def validate(self) -> Tuple[
134+
Dict[Text, List[Union[Tag, SupportsInt, List[Union[Text, 'String']]]]],
135+
List[Union[Tag, SupportsInt, List[Union[Text, 'String']]]]
136+
]:
137+
tagged_args, positional_args = self.validate_arguments()
138+
if self.HAS_TESTS:
139+
self.validate_tests_size(min_tests=self.TESTS_MIN, max_tests=self.TESTS_MAX)
140+
return tagged_args, positional_args

sifter/tests/address.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ def __init__(
4242
tests: Optional[List['Test']] = None
4343
) -> None:
4444
super().__init__(arguments, tests)
45-
tagged_args, positional_args = self.validate_arguments()
46-
self.validate_tests_size(0)
45+
tagged_args, positional_args = self.validate()
4746

4847
self.headers, self.keylist = positional_args
4948
self.match_type = self.comparator = self.address_part = None

0 commit comments

Comments
 (0)