<s> from utils import xstr <EOL> from curator . api . filter import apply_filter <EOL> from easydict import EasyDict <EOL> import curator . api as api <EOL> import sys <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class ItemsFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> def build ( self , ** opts ) : <EOL> """<STR_LIT>""" <EOL> self . opts = EasyDict ( opts ) <EOL> self . built_list = self . _build ( ) <EOL> return self <EOL> def get_timebased ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for f in self . built_list : <EOL> if f . get ( '<STR_LIT>' , None ) in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> result [ f [ '<STR_LIT>' ] ] = f <EOL> if len ( result ) == <NUM_LIT:2> : <EOL> break <EOL> return ( result . get ( '<STR_LIT>' , None ) , result . get ( '<STR_LIT>' , None ) ) <EOL> @ property <EOL> def all_items ( self ) : <EOL> return self . opts . get ( '<STR_LIT>' . format ( self . act_on ) , None ) <EOL> @ property <EOL> def filter_list ( self ) : <EOL> return self . built_list <EOL> @ property <EOL> def closed_timerange ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( filter ( None , self . get_timebased ( ) ) ) == <NUM_LIT:2> <EOL> def apply ( self , working_list , act_on ) : <EOL> """<STR_LIT>""" <EOL> self . act_on = act_on <EOL> result_list = self . _apply_closed_timerange ( working_list ) <EOL> if self . all_items : <EOL> logger . info ( '<STR_LIT>' , self . act_on ) <EOL> if result_list is None : <EOL> result_list = working_list <EOL> for f in self . built_list : <EOL> is_timebased = f . get ( '<STR_LIT>' , None ) in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . closed_timerange and is_timebased : <EOL> continue <EOL> if self . all_items and '<STR_LIT>' not in f : <EOL> continue <EOL> logger . debug ( '<STR_LIT>' , f ) <EOL> result_list = apply_filter ( result_list , ** f ) <EOL> return result_list <EOL> def _apply_closed_timerange ( self , working_list ) : <EOL> """<STR_LIT>""" <EOL> if self . closed_timerange : <EOL> newer_than , older_than = self . get_timebased ( ) <EOL> if newer_than [ '<STR_LIT:value>' ] < older_than [ '<STR_LIT:value>' ] : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not self . all_items : <EOL> logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , newer_than , older_than ) <EOL> newer_range = set ( apply_filter ( working_list , ** newer_than ) ) <EOL> older_range = set ( apply_filter ( working_list , ** older_than ) ) <EOL> result_list = list ( newer_range & older_range ) <EOL> return result_list <EOL> def _build ( self ) : <EOL> """<STR_LIT>""" <EOL> opts = self . opts <EOL> filter_list = [ ] <EOL> if not opts . timestring and any ( ( xstr ( opts . newer_than ) , <EOL> xstr ( opts . older_than ) ) ) : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if opts . timestring is not None and not all ( ( xstr ( opts . newer_than ) , <EOL> xstr ( opts . older_than ) ) ) : <EOL> f = api . filter . build_filter ( kindOf = '<STR_LIT>' , <EOL> value = opts . timestring ) <EOL> if f : <EOL> filter_list . append ( f ) <EOL> timebased = zip ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( opts . newer_than , <EOL> opts . older_than ) ) <EOL> for opt , value in timebased : <EOL> if value is None : <EOL> continue <EOL> f = api . filter . build_filter ( kindOf = opt , value = value , <EOL> timestring = opts . timestring , <EOL> time_unit = opts . time_unit ) <EOL> if f : <EOL> filter_list . append ( f ) <EOL> patternbased = zip ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( opts . suffix , opts . prefix , opts . regex ) ) <EOL> for opt , value in patternbased : <EOL> if value is None : <EOL> continue <EOL> f = api . filter . build_filter ( kindOf = opt , value = value ) <EOL> if f : <EOL> filter_list . append ( f ) <EOL> for pattern in opts . exclude or [ ] : <EOL> f = { '<STR_LIT>' : pattern , '<STR_LIT>' : True } <EOL> filter_list . append ( f ) <EOL> return filter_list </s>
