<s> from curator . api . utils import get_indices <EOL> from curator . api . utils import get_snapshots <EOL> from items_filter import ItemsFilter <EOL> from easydict import EasyDict <EOL> from utils import xstr <EOL> import sys <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class ItemsSelector ( object ) : <EOL> def __init__ ( self , client , ** opts ) : <EOL> self . opts = EasyDict ( opts ) <EOL> self . client = client <EOL> self . ifilter = ItemsFilter ( ) . build ( ** opts ) <EOL> def _apply_filters ( self , source_items , act_on ) : <EOL> """<STR_LIT>""" <EOL> opts = self . opts <EOL> all_items_selected = opts . get ( '<STR_LIT>' . format ( act_on ) , None ) <EOL> if act_on == '<STR_LIT>' : <EOL> explicit_items = opts . index or [ ] <EOL> else : <EOL> explicit_items = opts . snapshot or [ ] <EOL> if not any ( ( xstr ( opts . newer_than ) , xstr ( opts . older_than ) , opts . dry_run ) ) and opts . timestring : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . warn ( '<STR_LIT>' , act_on , opts . timestring ) <EOL> logger . debug ( "<STR_LIT>" , act_on , source_items ) <EOL> if not source_items : <EOL> print '<STR_LIT>' . format ( act_on ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> working_list = source_items <EOL> if not all_items_selected and not self . ifilter . filter_list : <EOL> working_list = [ ] <EOL> else : <EOL> working_list = self . ifilter . apply ( working_list , act_on = act_on ) <EOL> if explicit_items : <EOL> working_list . extend ( ( i for i in explicit_items if i in source_items ) ) <EOL> if not working_list : <EOL> logger . error ( '<STR_LIT>' , act_on , opts ) <EOL> print "<STR_LIT>" . format ( act_on ) <EOL> sys . exit ( <NUM_LIT> ) <EOL> return sorted ( list ( set ( working_list ) ) ) <EOL> def snapshots ( self , on_nofilters_showall = False ) : <EOL> """<STR_LIT>""" <EOL> if not any ( ( self . opts . all_snapshots , self . opts . snapshot , self . ifilter . filter_list ) ) : <EOL> if on_nofilters_showall : <EOL> self . opts . all_snapshots = True <EOL> else : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not self . opts . repository : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> snapshots = get_snapshots ( self . client , repository = self . opts . repository ) <EOL> return self . _apply_filters ( snapshots , act_on = '<STR_LIT>' ) <EOL> def indices ( self , on_nofilters_showall = False ) : <EOL> """<STR_LIT>""" <EOL> if not any ( ( self . opts . all_indices , self . opts . index , self . ifilter . filter_list ) ) : <EOL> if on_nofilters_showall : <EOL> self . opts . all_indices = True <EOL> else : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> indices = get_indices ( self . client ) <EOL> return self . _apply_filters ( indices , act_on = '<STR_LIT>' ) <EOL> def fetch ( self , act_on , on_nofilters_showall = False ) : <EOL> if act_on not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( '<STR_LIT>' , act_on ) <EOL> if act_on == '<STR_LIT>' : <EOL> return self . indices ( on_nofilters_showall = on_nofilters_showall ) <EOL> else : <EOL> return self . snapshots ( on_nofilters_showall = on_nofilters_showall ) </s>
