<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from traits . api import ( Any , Bool , Callable , Enum , Event , Expression , HasPrivateTraits , <EOL> Instance , List , Str , Trait ) <EOL> from . editor_factory import EditorFactory <EOL> from . editors . api import EnumEditor <EOL> from . group import Group <EOL> from . include import Include <EOL> from . item import Item <EOL> from . menu import Action <EOL> from . table_column import ObjectColumn <EOL> from . view import View <EOL> GenericTableFilterRuleOperation = Trait ( '<STR_LIT:=>' , { <EOL> '<STR_LIT:=>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:<>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:>>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> class TableFilter ( HasPrivateTraits ) : <EOL> """<STR_LIT>""" <EOL> name = Str ( '<STR_LIT>' ) <EOL> _name = Str ( '<STR_LIT>' ) <EOL> desc = Str ( '<STR_LIT>' ) <EOL> allowed = Callable ( lambda object : True , transient = True ) <EOL> template = Bool ( False ) <EOL> ignored_traits = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> traits_view = View ( <EOL> '<STR_LIT>' , '<STR_LIT:_>' , <EOL> Include ( '<STR_LIT>' ) , <EOL> title = '<STR_LIT>' , <EOL> width = <NUM_LIT> , <EOL> buttons = [ '<STR_LIT:OK>' , <EOL> '<STR_LIT>' , <EOL> Action ( <EOL> name = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> defined_when = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> ] <EOL> ) <EOL> searchable_view = View ( [ <EOL> [ Include ( '<STR_LIT>' ) , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' ] , <EOL> title = '<STR_LIT>' , <EOL> kind = '<STR_LIT>' , <EOL> width = <NUM_LIT> ) <EOL> search_view = Group ( Include ( '<STR_LIT>' ) ) <EOL> filter_view = Group ( ) <EOL> def filter ( self , object ) : <EOL> """<STR_LIT>""" <EOL> return self . allowed ( object ) <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . desc <EOL> def edit ( self , object ) : <EOL> """<STR_LIT>""" <EOL> return self . edit_traits ( view = self . edit_view ( object ) , <EOL> kind = '<STR_LIT>' ) <EOL> def edit_view ( self , object ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def _anytrait_changed ( self , name , old , new ) : <EOL> if ( ( name not in self . ignored_traits ) and <EOL> ( ( self . name == self . _name ) or ( self . name == '<STR_LIT>' ) ) ) : <EOL> self . name = self . _name = self . description ( ) <EOL> class EvalTableFilter ( TableFilter ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> expression = Expression <EOL> filter_view = Group ( '<STR_LIT>' ) <EOL> def filter ( self , object ) : <EOL> """<STR_LIT>""" <EOL> if self . _traits is None : <EOL> self . _traits = object . trait_names ( ) <EOL> try : <EOL> return eval ( self . expression_ , globals ( ) , <EOL> object . get ( * self . _traits ) ) <EOL> except : <EOL> return False <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . expression <EOL> class GenericTableFilterRule ( HasPrivateTraits ) : <EOL> """<STR_LIT>""" <EOL> filter = Instance ( '<STR_LIT>' ) <EOL> enabled = Bool ( False ) <EOL> and_or = Enum ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> name_editor = Instance ( EditorFactory ) <EOL> name = Str <EOL> operation = GenericTableFilterRuleOperation <EOL> value_editor = Instance ( EditorFactory ) <EOL> value = Any <EOL> ignored_traits = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , ** traits ) : <EOL> super ( GenericTableFilterRule , self ) . __init__ ( ** traits ) <EOL> if self . name == '<STR_LIT>' : <EOL> names = self . filter . _trait_values . keys ( ) <EOL> if len ( names ) > <NUM_LIT:0> : <EOL> names . sort ( ) <EOL> self . name = names [ <NUM_LIT:0> ] <EOL> self . enabled = False <EOL> def _name_changed ( self , name ) : <EOL> """<STR_LIT>""" <EOL> filter = self . filter <EOL> if ( filter is not None ) and ( filter . _object is not None ) : <EOL> self . value = filter . _trait_values . get ( name ) <EOL> self . value_editor = filter . _object . base_trait ( name ) . get_editor ( ) <EOL> def _anytrait_changed ( self , name , old , new ) : <EOL> if ( name not in self . ignored_traits ) and ( self . filter is not None ) : <EOL> self . filter . modified = True <EOL> if name != '<STR_LIT>' : <EOL> self . enabled = True <EOL> def clone_traits ( self , traits = None , memo = None , copy = None , <EOL> ** metadata ) : <EOL> """<STR_LIT>""" <EOL> return super ( GenericTableFilterRule , self ) . clone_traits ( traits , <EOL> memo , copy , ** metadata ) . set ( <EOL> enabled = self . enabled , <EOL> name = self . name ) <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . name , self . operation , self . value ) <EOL> def is_true ( self , object ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value1 = getattr ( object , self . name ) <EOL> type1 = type ( value1 ) <EOL> value2 = self . value <EOL> if type1 is not type ( value2 ) : <EOL> value2 = type1 ( value2 ) <EOL> return getattr ( self , self . operation_ ) ( value1 , value2 ) <EOL> except : <EOL> return False <EOL> def eq ( self , value1 , value2 ) : <EOL> return ( value1 == value2 ) <EOL> def ne ( self , value1 , value2 ) : <EOL> return ( value1 != value2 ) <EOL> def lt ( self , value1 , value2 ) : <EOL> return ( value1 < value2 ) <EOL> def le ( self , value1 , value2 ) : <EOL> return ( value1 <= value2 ) <EOL> def gt ( self , value1 , value2 ) : <EOL> return ( value1 > value2 ) <EOL> def ge ( self , value1 , value2 ) : <EOL> return ( value1 >= value2 ) <EOL> def contains ( self , value1 , value2 ) : <EOL> return ( value1 . lower ( ) . find ( value2 . lower ( ) ) >= <NUM_LIT:0> ) <EOL> def starts_with ( self , value1 , value2 ) : <EOL> return ( value1 [ : len ( value2 ) ] . lower ( ) == value2 . lower ( ) ) <EOL> def ends_with ( self , value1 , value2 ) : <EOL> return ( value1 [ - len ( value2 ) : ] . lower ( ) == value2 . lower ( ) ) <EOL> class GenericTableFilterRuleEnabledColumn ( ObjectColumn ) : <EOL> """<STR_LIT>""" <EOL> def get_value ( self , object ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] [ object . enabled ] <EOL> class GenericTableFilterRuleAndOrColumn ( ObjectColumn ) : <EOL> """<STR_LIT>""" <EOL> def get_value ( self , object ) : <EOL> """<STR_LIT>""" <EOL> if object . and_or == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> class GenericTableFilterRuleNameColumn ( ObjectColumn ) : <EOL> """<STR_LIT>""" <EOL> def get_editor ( self , object ) : <EOL> """<STR_LIT>""" <EOL> return object . name_editor <EOL> class GenericTableFilterRuleValueColumn ( ObjectColumn ) : <EOL> """<STR_LIT>""" <EOL> def get_editor ( self , object ) : <EOL> """<STR_LIT>""" <EOL> return object . value_editor <EOL> generic_table_filter_rule_columns = [ <EOL> GenericTableFilterRuleAndOrColumn ( name = '<STR_LIT>' , label = '<STR_LIT>' ) , <EOL> GenericTableFilterRuleNameColumn ( name = '<STR_LIT:name>' ) , <EOL> ObjectColumn ( name = '<STR_LIT>' ) , <EOL> GenericTableFilterRuleValueColumn ( name = '<STR_LIT:value>' ) <EOL> ] <EOL> class RuleTableFilter ( TableFilter ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> rules = List ( GenericTableFilterRule ) <EOL> modified = Event <EOL> view_id = Str ( '<STR_LIT>' ) <EOL> _object = Any <EOL> _trait_values = Any <EOL> error_view = View ( <EOL> Item ( label = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> title = '<STR_LIT>' , <EOL> kind = '<STR_LIT>' , <EOL> close_result = False , <EOL> buttons = [ '<STR_LIT>' ] <EOL> ) <EOL> def filter ( self , object ) : <EOL> """<STR_LIT>""" <EOL> is_first = is_true = True <EOL> for rule in self . rules : <EOL> if rule . and_or == '<STR_LIT>' : <EOL> if is_true and ( not is_first ) : <EOL> return True <EOL> is_true = True <EOL> if is_true : <EOL> is_true = rule . is_true ( object ) <EOL> is_first = False <EOL> return is_true <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> ors = [ ] <EOL> ands = [ ] <EOL> if len ( self . rules ) > <NUM_LIT:0> : <EOL> for rule in self . rules : <EOL> if rule . and_or == '<STR_LIT>' : <EOL> if len ( ands ) > <NUM_LIT:0> : <EOL> ors . append ( '<STR_LIT>' . join ( ands ) ) <EOL> ands = [ ] <EOL> ands . append ( rule . description ( ) ) <EOL> if len ( ands ) > <NUM_LIT:0> : <EOL> ors . append ( '<STR_LIT>' . join ( ands ) ) <EOL> if len ( ors ) == <NUM_LIT:1> : <EOL> return ors [ <NUM_LIT:0> ] <EOL> if len ( ors ) > <NUM_LIT:1> : <EOL> return '<STR_LIT>' . join ( [ '<STR_LIT>' % t for t in ors ] ) <EOL> return super ( RuleTableFilter , self ) . description ( ) <EOL> def edit_view ( self , object ) : <EOL> """<STR_LIT>""" <EOL> self . _object = object <EOL> if object is None : <EOL> return self . edit_traits ( view = '<STR_LIT>' ) <EOL> names = object . editable_traits ( ) <EOL> self . _trait_values = object . get ( names ) <EOL> return View ( <EOL> [ [ '<STR_LIT>' , '<STR_LIT:_>' ] , <EOL> [ Item ( '<STR_LIT>' , <EOL> id = '<STR_LIT>' , <EOL> editor = self . _get_table_editor ( names ) ) , <EOL> '<STR_LIT>' ] ] , <EOL> id = self . view_id , <EOL> title = '<STR_LIT>' , <EOL> kind = '<STR_LIT>' , <EOL> resizable = True , <EOL> buttons = [ '<STR_LIT:OK>' , '<STR_LIT>' ] , <EOL> width = <NUM_LIT> , <EOL> height = <NUM_LIT> ) <EOL> def _get_table_editor ( self , names ) : <EOL> """<STR_LIT>""" <EOL> from . api import TableEditor <EOL> return TableEditor ( columns = generic_table_filter_rule_columns , <EOL> orientation = '<STR_LIT>' , <EOL> deletable = True , <EOL> sortable = False , <EOL> configurable = False , <EOL> auto_size = False , <EOL> auto_add = True , <EOL> row_factory = GenericTableFilterRule , <EOL> row_factory_kw = { <EOL> '<STR_LIT>' : self , <EOL> '<STR_LIT>' : EnumEditor ( values = names ) } ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> dict = self . __dict__ . copy ( ) <EOL> if '<STR_LIT>' in dict : <EOL> del dict [ '<STR_LIT>' ] <EOL> del dict [ '<STR_LIT>' ] <EOL> return dict <EOL> def _rules_changed ( self , rules ) : <EOL> """<STR_LIT>""" <EOL> for rule in rules : <EOL> rule . filter = self <EOL> menu_table_filter_rule_columns = [ <EOL> GenericTableFilterRuleEnabledColumn ( name = '<STR_LIT>' , label = '<STR_LIT>' ) , <EOL> GenericTableFilterRuleNameColumn ( name = '<STR_LIT:name>' ) , <EOL> ObjectColumn ( name = '<STR_LIT>' ) , <EOL> GenericTableFilterRuleValueColumn ( name = '<STR_LIT:value>' ) <EOL> ] <EOL> class MenuTableFilter ( RuleTableFilter ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> view_id = Str ( '<STR_LIT>' ) <EOL> def filter ( self , object ) : <EOL> """<STR_LIT>""" <EOL> for rule in self . rules : <EOL> if rule . enabled and ( not rule . is_true ( object ) ) : <EOL> return False <EOL> return True <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> result = '<STR_LIT>' . join ( [ rule . description ( ) for rule in self . rules <EOL> if rule . enabled ] ) <EOL> if result != '<STR_LIT>' : <EOL> return result <EOL> return '<STR_LIT>' <EOL> def _get_table_editor ( self , names ) : <EOL> """<STR_LIT>""" <EOL> from . api import TableEditor <EOL> names = self . _object . editable_traits ( ) <EOL> name_editor = EnumEditor ( values = names ) <EOL> if len ( self . rules ) == <NUM_LIT:0> : <EOL> self . rules = [ GenericTableFilterRule ( <EOL> filter = self , <EOL> name_editor = name_editor ) . set ( <EOL> name = name ) <EOL> for name in names ] <EOL> for rule in self . rules : <EOL> rule . enabled = False <EOL> return TableEditor ( columns = menu_table_filter_rule_columns , <EOL> orientation = '<STR_LIT>' , <EOL> deletable = True , <EOL> sortable = False , <EOL> configurable = False , <EOL> auto_size = False , <EOL> auto_add = True , <EOL> row_factory = GenericTableFilterRule , <EOL> row_factory_kw = { <EOL> '<STR_LIT>' : self , <EOL> '<STR_LIT>' : name_editor } ) <EOL> EvalFilterTemplate = EvalTableFilter ( name = '<STR_LIT>' , <EOL> template = True ) <EOL> RuleFilterTemplate = RuleTableFilter ( name = '<STR_LIT>' , <EOL> template = True ) <EOL> MenuFilterTemplate = MenuTableFilter ( name = '<STR_LIT>' , <EOL> template = True ) </s>
