<s> __all__ = [ '<STR_LIT>' ] <EOL> import re <EOL> def get_name ( x ) : <EOL> assert isinstance ( x , ( basestring , dict ) ) <EOL> if type ( x ) == dict : <EOL> return x [ '<STR_LIT:name>' ] <EOL> else : <EOL> return x <EOL> def filter_expr1 ( links , p ) : <EOL> if not links : <EOL> return links <EOL> if re . match ( r'<STR_LIT>' , p ) : <EOL> matched = [ ] <EOL> for p in re . split ( r'<STR_LIT>' , p [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) : <EOL> assert re . match ( r'<STR_LIT>' , p ) , p <EOL> if re . match ( r'<STR_LIT>' , p ) : <EOL> i = int ( p ) <EOL> matched . append ( ( i , links [ i ] ) ) <EOL> elif '<STR_LIT:->' in p : <EOL> start , end = p . split ( '<STR_LIT:->' ) <EOL> if not start : <EOL> start = <NUM_LIT:0> <EOL> if not end : <EOL> end = len ( links ) - <NUM_LIT:1> <EOL> start = int ( start ) <EOL> end = int ( end ) <EOL> assert <NUM_LIT:0> <= start < len ( links ) <EOL> assert <NUM_LIT:0> <= end < len ( links ) <EOL> if start <= end : <EOL> matched += list ( enumerate ( links ) ) [ start : end + <NUM_LIT:1> ] <EOL> else : <EOL> matched += reversed ( list ( enumerate ( links ) ) [ end : start + <NUM_LIT:1> ] ) <EOL> elif p . startswith ( '<STR_LIT:.>' ) : <EOL> matched += filter ( lambda ( i , x ) : get_name ( x ) . lower ( ) . endswith ( p . lower ( ) ) , enumerate ( links ) ) <EOL> else : <EOL> raise NotImplementedError ( p ) <EOL> indexes = [ ] <EOL> for i , _ in matched : <EOL> if i not in indexes : <EOL> indexes . append ( i ) <EOL> return [ links [ x ] for x in indexes ] <EOL> elif re . match ( r'<STR_LIT>' , p ) : <EOL> n = int ( p ) <EOL> if <NUM_LIT:0> <= n < len ( links ) : <EOL> return [ links [ int ( p ) ] ] <EOL> else : <EOL> return filter ( lambda x : re . search ( p , get_name ( x ) , re . I ) , links ) <EOL> elif p == '<STR_LIT:*>' : <EOL> return links <EOL> elif re . match ( r'<STR_LIT>' , p ) : <EOL> return filter ( lambda x : get_name ( x ) . lower ( ) . endswith ( p . lower ( ) ) , links ) <EOL> else : <EOL> import lixian_plugins . filters <EOL> filter_results = lixian_plugins . filters . filter_things ( links , p ) <EOL> if filter_results is None : <EOL> return filter ( lambda x : re . search ( p , get_name ( x ) , re . I ) , links ) <EOL> else : <EOL> return filter_results <EOL> def filter_expr ( links , expr ) : <EOL> for p in expr . split ( '<STR_LIT:/>' ) : <EOL> links = filter_expr1 ( links , p ) <EOL> return links </s>
