<s> import re <EOL> from st2common . exceptions . content import ParseException <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class ActionAliasFormatParser ( object ) : <EOL> def __init__ ( self , alias_format = None , param_stream = None ) : <EOL> self . _format = alias_format or '<STR_LIT>' <EOL> self . _param_stream = param_stream or '<STR_LIT>' <EOL> def get_extracted_param_value ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> param_stream = self . _param_stream <EOL> snippets = dict ( ) <EOL> snippets [ '<STR_LIT:key>' ] = r'<STR_LIT>' <EOL> snippets [ '<STR_LIT:value>' ] = r'<STR_LIT>' <EOL> snippets [ '<STR_LIT>' ] = r'<STR_LIT>' <EOL> snippets [ '<STR_LIT>' ] = r'<STR_LIT>' . format ( ** snippets ) <EOL> snippets [ '<STR_LIT>' ] = r'<STR_LIT>' . format ( ** snippets ) <EOL> snippets [ '<STR_LIT:default>' ] = r'<STR_LIT>' . format ( ** snippets ) <EOL> snippets [ '<STR_LIT>' ] = '<STR_LIT>' + snippets [ '<STR_LIT:key>' ] + snippets [ '<STR_LIT:default>' ] + '<STR_LIT>' <EOL> snippets [ '<STR_LIT>' ] = '<STR_LIT>' + snippets [ '<STR_LIT:key>' ] + '<STR_LIT>' <EOL> ending_pairs = re . match ( snippets [ '<STR_LIT>' ] , param_stream , re . DOTALL ) <EOL> has_ending_pairs = ending_pairs and ending_pairs . group ( <NUM_LIT:1> ) <EOL> if has_ending_pairs : <EOL> kv_pairs = re . findall ( snippets [ '<STR_LIT>' ] , ending_pairs . group ( <NUM_LIT:1> ) , re . DOTALL ) <EOL> param_stream = param_stream . replace ( ending_pairs . group ( <NUM_LIT:1> ) , '<STR_LIT>' ) <EOL> param_stream = "<STR_LIT>" % ( param_stream ) <EOL> optional = re . findall ( snippets [ '<STR_LIT>' ] , self . _format , re . DOTALL ) <EOL> param_match = r'<STR_LIT>' <EOL> reg = re . sub ( r'<STR_LIT>' + snippets [ '<STR_LIT>' ] , r'<STR_LIT>' + param_match + r'<STR_LIT>' , self . _format ) <EOL> reg = re . sub ( r'<STR_LIT>' + snippets [ '<STR_LIT>' ] , param_match , reg ) <EOL> reg = '<STR_LIT>' + reg + r'<STR_LIT>' <EOL> matched_stream = re . match ( reg , param_stream , re . DOTALL ) <EOL> if not matched_stream : <EOL> raise ParseException ( '<STR_LIT>' % <EOL> ( self . _param_stream , self . _format ) ) <EOL> if matched_stream : <EOL> result = matched_stream . groupdict ( ) <EOL> for param in optional : <EOL> matched_value = result [ param [ <NUM_LIT:0> ] ] if matched_stream else None <EOL> matched_result = matched_value or '<STR_LIT>' . join ( param [ <NUM_LIT:1> : ] ) <EOL> if matched_result is not None : <EOL> result [ param [ <NUM_LIT:0> ] ] = matched_result <EOL> if has_ending_pairs : <EOL> for pair in kv_pairs : <EOL> result [ pair [ <NUM_LIT:0> ] ] = '<STR_LIT>' . join ( pair [ <NUM_LIT:2> : ] ) <EOL> if self . _format and not ( self . _param_stream . strip ( ) or any ( result . values ( ) ) ) : <EOL> raise ParseException ( '<STR_LIT>' ) <EOL> return result <EOL> def extract_parameters_for_action_alias_db ( action_alias_db , format_str , param_stream ) : <EOL> """<STR_LIT>""" <EOL> formats = [ ] <EOL> formats = action_alias_db . get_format_strings ( ) <EOL> if format_str not in formats : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( format_str , action_alias_db . name ) ) <EOL> result = extract_parameters ( format_str = format_str , param_stream = param_stream ) <EOL> return result <EOL> def extract_parameters ( format_str , param_stream ) : <EOL> parser = ActionAliasFormatParser ( alias_format = format_str , param_stream = param_stream ) <EOL> return parser . get_extracted_param_value ( ) </s>
