<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , with_statement <EOL> import copy <EOL> import os . path <EOL> import re <EOL> import parse <EOL> import six <EOL> from parse_type import cfparse <EOL> from behave . _types import ChainedExceptionUtil , ExceptionUtil <EOL> from behave . model_core import Argument , FileLocation , Replayable <EOL> class StepParseError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text = None , exc_cause = None ) : <EOL> if not text and exc_cause : <EOL> text = six . text_type ( exc_cause ) <EOL> if exc_cause and six . PY2 : <EOL> cause_text = ExceptionUtil . describe ( exc_cause , <EOL> use_traceback = True , <EOL> prefix = "<STR_LIT>" ) <EOL> text += u"<STR_LIT:\n>" + cause_text <EOL> ValueError . __init__ ( self , text ) <EOL> if exc_cause : <EOL> ChainedExceptionUtil . set_cause ( self , exc_cause ) <EOL> class Match ( Replayable ) : <EOL> """<STR_LIT>""" <EOL> type = "<STR_LIT>" <EOL> def __init__ ( self , func , arguments = None ) : <EOL> super ( Match , self ) . __init__ ( ) <EOL> self . func = func <EOL> self . arguments = arguments <EOL> self . location = None <EOL> if func : <EOL> self . location = self . make_location ( func ) <EOL> def __repr__ ( self ) : <EOL> if self . func : <EOL> func_name = self . func . __name__ <EOL> else : <EOL> func_name = '<STR_LIT>' <EOL> return '<STR_LIT>' % ( func_name , self . location ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , Match ) : <EOL> return False <EOL> return ( self . func , self . location ) == ( other . func , other . location ) <EOL> def with_arguments ( self , arguments ) : <EOL> match = copy . copy ( self ) <EOL> match . arguments = arguments <EOL> return match <EOL> def run ( self , context ) : <EOL> args = [ ] <EOL> kwargs = { } <EOL> for arg in self . arguments : <EOL> if arg . name is not None : <EOL> kwargs [ arg . name ] = arg . value <EOL> else : <EOL> args . append ( arg . value ) <EOL> with context . use_with_user_mode ( ) : <EOL> self . func ( context , * args , ** kwargs ) <EOL> @ staticmethod <EOL> def make_location ( step_function ) : <EOL> """<STR_LIT>""" <EOL> return FileLocation . for_function ( step_function ) <EOL> class NoMatch ( Match ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> Match . __init__ ( self , func = None ) <EOL> self . func = None <EOL> self . arguments = [ ] <EOL> self . location = None <EOL> class MatchWithError ( Match ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func , error ) : <EOL> if not ExceptionUtil . has_traceback ( error ) : <EOL> ExceptionUtil . set_traceback ( error ) <EOL> Match . __init__ ( self , func = func ) <EOL> self . stored_error = error <EOL> def run ( self , context ) : <EOL> """<STR_LIT>""" <EOL> raise StepParseError ( exc_cause = self . stored_error ) <EOL> class Matcher ( object ) : <EOL> """<STR_LIT>""" <EOL> schema = u"<STR_LIT>" <EOL> def __init__ ( self , func , string , step_type = None ) : <EOL> self . func = func <EOL> self . string = string <EOL> self . step_type = step_type <EOL> self . _location = None <EOL> @ property <EOL> def location ( self ) : <EOL> if self . _location is None : <EOL> self . _location = Match . make_location ( self . func ) <EOL> return self . _location <EOL> def describe ( self , schema = None ) : <EOL> """<STR_LIT>""" <EOL> step_type = self . step_type or "<STR_LIT>" <EOL> if not schema : <EOL> schema = self . schema <EOL> return schema % ( step_type , self . string ) <EOL> def check_match ( self , step ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def match ( self , step ) : <EOL> try : <EOL> result = self . check_match ( step ) <EOL> except Exception as e : <EOL> return MatchWithError ( self . func , e ) <EOL> if result is None : <EOL> return None <EOL> return Match ( self . func , result ) <EOL> def __repr__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . __class__ . __name__ , self . string ) <EOL> class ParseMatcher ( Matcher ) : <EOL> custom_types = { } <EOL> def __init__ ( self , func , string , step_type = None ) : <EOL> super ( ParseMatcher , self ) . __init__ ( func , string , step_type ) <EOL> self . parser = parse . compile ( self . string , self . custom_types ) <EOL> def check_match ( self , step ) : <EOL> result = self . parser . parse ( step ) <EOL> if not result : <EOL> return None <EOL> args = [ ] <EOL> for index , value in enumerate ( result . fixed ) : <EOL> start , end = result . spans [ index ] <EOL> args . append ( Argument ( start , end , step [ start : end ] , value ) ) <EOL> for name , value in result . named . items ( ) : <EOL> start , end = result . spans [ name ] <EOL> args . append ( Argument ( start , end , step [ start : end ] , value , name ) ) <EOL> args . sort ( key = lambda x : x . start ) <EOL> return args <EOL> class CFParseMatcher ( ParseMatcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func , string , step_type = None ) : <EOL> super ( CFParseMatcher , self ) . __init__ ( func , string , step_type ) <EOL> self . parser = cfparse . Parser ( self . string , self . custom_types ) <EOL> def register_type ( ** kw ) : <EOL> """<STR_LIT>""" <EOL> ParseMatcher . custom_types . update ( kw ) <EOL> class RegexMatcher ( Matcher ) : <EOL> def __init__ ( self , func , string , step_type = None ) : <EOL> super ( RegexMatcher , self ) . __init__ ( func , string , step_type ) <EOL> assert not ( string . startswith ( "<STR_LIT>" ) or string . endswith ( "<STR_LIT:$>" ) ) , "<STR_LIT>" + string <EOL> expression = "<STR_LIT>" % self . string <EOL> self . regex = re . compile ( expression ) <EOL> def check_match ( self , step ) : <EOL> m = self . regex . match ( step ) <EOL> if not m : <EOL> return None <EOL> groupindex = dict ( ( y , x ) for x , y in self . regex . groupindex . items ( ) ) <EOL> args = [ ] <EOL> for index , group in enumerate ( m . groups ( ) ) : <EOL> index += <NUM_LIT:1> <EOL> name = groupindex . get ( index , None ) <EOL> args . append ( Argument ( m . start ( index ) , m . end ( index ) , group , <EOL> group , name ) ) <EOL> return args <EOL> matcher_mapping = { <EOL> "<STR_LIT>" : ParseMatcher , <EOL> "<STR_LIT>" : CFParseMatcher , <EOL> "<STR_LIT>" : RegexMatcher , <EOL> } <EOL> current_matcher = ParseMatcher <EOL> def use_step_matcher ( name ) : <EOL> """<STR_LIT>""" <EOL> global current_matcher <EOL> current_matcher = matcher_mapping [ name ] <EOL> def step_matcher ( name ) : <EOL> """<STR_LIT>""" <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" , <EOL> PendingDeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> use_step_matcher ( name ) <EOL> def get_matcher ( func , string ) : <EOL> return current_matcher ( func , string ) </s>
