11import re
2+ from email .message import Message
3+ from typing import (
4+ Optional ,
5+ Text
6+ )
7+
28from urllib .parse import quote
39from sifter .grammar .command import Command
410from sifter .grammar .rule import RuleSyntaxError
511from sifter .validators .stringlist import StringList
612from sifter .validators .tag import Tag
713from sifter .grammar .string import expand_variables
14+ from sifter .grammar .state import EvaluationState
15+ from sifter .grammar .actions import Actions
816
917__all__ = ('CommandSet' ,)
1018
@@ -28,34 +36,33 @@ class CommandSet(Command):
2836 StringList (length = 1 ),
2937 ]
3038
31- def __init__ (self , arguments = None , tests = None , block = None ) :
32- super ( CommandSet , self ). __init__ ( arguments , tests , block )
39+ def evaluate (self , message : Message , state : EvaluationState ) -> Optional [ Actions ] :
40+ state . check_required_extension ( 'variables' , 'VARIABLES' )
3341
34- self . variable_modifier = self .tagged_args
35- self . variable_name = self .positional_args [0 ][0 ]
36- if (not re .match (r'^[A-Za-z_][A-Za-z0-9_]*$' , self . variable_name )):
37- raise RuleSyntaxError ("Illegal variable name '%s' encountered" % self . variable_name )
38- self . variable_value = self .positional_args [1 ][0 ]
42+ variable_modifier = self .tagged_args
43+ variable_name = self .positional_args [0 ][0 ] # type: ignore
44+ if (not re .match (r'^[A-Za-z_][A-Za-z0-9_]*$' , variable_name )):
45+ raise RuleSyntaxError ("Illegal variable name '%s' encountered" % variable_name )
46+ variable_value : Text = self .positional_args [1 ][0 ] # type: ignore
3947
40- def evaluate (self , message , state ):
41- state .check_required_extension ('variables' , 'VARIABLES' )
42- variable_value = expand_variables (self .variable_value , state )
43- if 'lower' in self .variable_modifier :
48+ variable_value = expand_variables (variable_value , state )
49+ if 'lower' in variable_modifier :
4450 variable_value = variable_value .lower ()
45- if 'upper' in self . variable_modifier :
51+ if 'upper' in variable_modifier :
4652 variable_value = variable_value .upper ()
47- if 'lowerfirst' in self . variable_modifier :
53+ if 'lowerfirst' in variable_modifier :
4854 variable_value = variable_value [:1 ].lower () + variable_value [1 :]
49- if 'upperfirst' in self . variable_modifier :
55+ if 'upperfirst' in variable_modifier :
5056 variable_value = variable_value [:1 ].upper () + variable_value [1 :]
51- if 'quotewildcard' in self . variable_modifier :
57+ if 'quotewildcard' in variable_modifier :
5258 variable_value = variable_value .replace ('*' , '\\ *' )
5359 variable_value = variable_value .replace ('?' , '\\ ?' )
5460 variable_value = variable_value .replace ('\\ ' , '\\ \\ ' )
55- if 'quoteregex' in self . variable_modifier :
61+ if 'quoteregex' in variable_modifier :
5662 variable_value = re .escape (variable_value )
57- if 'encodeurl' in self . variable_modifier :
63+ if 'encodeurl' in variable_modifier :
5864 variable_value = quote (variable_value , safe = '-._~' )
59- if 'length' in self .variable_modifier :
60- variable_value = "" + len (variable_value )
61- state .named_variables [self .variable_name ] = variable_value
65+ if 'length' in variable_modifier :
66+ variable_value = "" + str (len (variable_value ))
67+ state .named_variables [variable_name ] = variable_value
68+ return None
0 commit comments