<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import shelve <EOL> import os <EOL> from traits . api import ( Any , Bool , Callable , DictStrAny , Event , HasPrivateTraits , <EOL> Instance , Int , List , Property , Str , TraitError , on_trait_change , <EOL> property_depends_on ) <EOL> from traits . trait_base import traits_home , is_str <EOL> from . editor import Editor <EOL> from . view_elements import ViewElements <EOL> from . handler import Handler , ViewHandler <EOL> from . toolkit import toolkit <EOL> from . ui_info import UIInfo <EOL> from . item import Item <EOL> from . group import Group , ShadowGroup <EOL> kind_must_have_parent = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class UI ( HasPrivateTraits ) : <EOL> """<STR_LIT>""" <EOL> view_elements = Instance ( ViewElements ) <EOL> context = DictStrAny <EOL> handler = Instance ( Handler ) <EOL> view = Instance ( '<STR_LIT>' ) <EOL> control = Any <EOL> parent = Instance ( '<STR_LIT>' ) <EOL> owner = Any <EOL> info = Instance ( UIInfo ) <EOL> result = Bool ( False ) <EOL> history = Any <EOL> key_bindings = Property <EOL> id = Str <EOL> modified = Bool ( False ) <EOL> updated = Event ( Bool ) <EOL> title = Str <EOL> icon = Any <EOL> scrollable = Bool ( False ) <EOL> errors = Int <EOL> rebuild = Callable <EOL> destroyed = Bool ( False ) <EOL> _context = DictStrAny <EOL> _revert = DictStrAny <EOL> _defined = List <EOL> _visible = List <EOL> _enabled = List <EOL> _checked = List <EOL> _search = List <EOL> _dispatchers = List <EOL> _editors = List <EOL> _names = List <EOL> _active_group = Int <EOL> _groups = Property <EOL> _groups_cache = Any <EOL> _undoable = Int ( - <NUM_LIT:1> ) <EOL> _rebuild = Callable <EOL> _statusbar = List <EOL> _scrollable = Bool ( False ) <EOL> recyclable_traits = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> disposable_traits = [ <EOL> '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> def traits_init ( self ) : <EOL> """<STR_LIT>""" <EOL> self . info = UIInfo ( ui = self ) <EOL> self . handler . init_info ( self . info ) <EOL> def ui ( self , parent , kind ) : <EOL> """<STR_LIT>""" <EOL> if ( parent is None ) and ( kind in kind_must_have_parent ) : <EOL> kind = '<STR_LIT>' <EOL> self . view . on_trait_change ( self . _updated_changed , '<STR_LIT>' , <EOL> dispatch = '<STR_LIT>' ) <EOL> self . rebuild = getattr ( toolkit ( ) , '<STR_LIT>' + kind ) <EOL> self . rebuild ( self , parent ) <EOL> def dispose ( self , result = None , abort = False ) : <EOL> """<STR_LIT>""" <EOL> if result is not None : <EOL> self . result = result <EOL> if self . control is not None : <EOL> if not abort : <EOL> self . save_prefs ( ) <EOL> self . finish ( ) <EOL> def recycle ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reset ( destroy = False ) <EOL> self . control . _object = None <EOL> self . reset_traits ( self . recyclable_traits ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reset ( destroy = True ) <EOL> for object in self . context . values ( ) : <EOL> object . on_trait_change ( self . _evaluate_when , remove = True ) <EOL> self . handler . closed ( self . info , self . result ) <EOL> self . info . ui = None <EOL> self . control . _object = None <EOL> toolkit ( ) . destroy_control ( self . control ) <EOL> self . control = None <EOL> if self . key_bindings is not None : <EOL> self . key_bindings . dispose ( ) <EOL> self . context . clear ( ) <EOL> self . reset_traits ( self . recyclable_traits ) <EOL> self . reset_traits ( self . disposable_traits ) <EOL> self . destroyed = True <EOL> def reset ( self , destroy = True ) : <EOL> """<STR_LIT>""" <EOL> for editor in self . _editors : <EOL> if editor . _ui is not None : <EOL> editor . _ui . result = self . result <EOL> editor . dispose ( ) <EOL> editor . control = None <EOL> for object , handler , name in self . _statusbar : <EOL> object . on_trait_change ( handler , name , remove = True ) <EOL> del self . _statusbar [ : ] <EOL> if destroy : <EOL> toolkit ( ) . destroy_children ( self . control ) <EOL> for dispatcher in self . _dispatchers : <EOL> dispatcher . remove ( ) <EOL> def find ( self , include ) : <EOL> """<STR_LIT>""" <EOL> context = self . context <EOL> result = None <EOL> if len ( context ) == <NUM_LIT:1> : <EOL> object = context . values ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> object = context . get ( '<STR_LIT:object>' ) <EOL> ve = self . view_elements <EOL> if ( ve is None ) and ( object is not None ) : <EOL> ve = object . trait_view_elements ( ) <EOL> if ve is not None : <EOL> result = ve . find ( include . id , self . _search ) <EOL> if result is None : <EOL> handler = context . get ( '<STR_LIT>' ) <EOL> if handler is not None : <EOL> method = getattr ( handler , include . id , None ) <EOL> if callable ( method ) : <EOL> result = method ( ) <EOL> if ( result is None ) and ( object is not None ) : <EOL> method = getattr ( object , include . id , None ) <EOL> if callable ( method ) : <EOL> result = method ( ) <EOL> return result <EOL> def push_level ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _search ) <EOL> def pop_level ( self , level ) : <EOL> """<STR_LIT>""" <EOL> del self . _search [ : len ( self . _search ) - level ] <EOL> def prepare_ui ( self ) : <EOL> """<STR_LIT>""" <EOL> info = self . info . set ( initialized = False ) <EOL> for method in self . _defined : <EOL> method ( info ) <EOL> del self . _defined [ : ] <EOL> self . sync_view ( ) <EOL> toolkit ( ) . hook_events ( self , self . control , '<STR_LIT>' , self . key_handler ) <EOL> handler = self . handler <EOL> if isinstance ( handler , ViewHandler ) : <EOL> toolkit ( ) . hook_events ( self , self . control ) <EOL> if handler . init ( info ) == False : <EOL> raise TraitError ( '<STR_LIT>' ) <EOL> context = self . context <EOL> for name in self . _each_trait_method ( handler ) : <EOL> if name [ - <NUM_LIT:8> : ] == '<STR_LIT>' : <EOL> prefix = name [ : - <NUM_LIT:8> ] <EOL> col = prefix . find ( '<STR_LIT:_>' , <NUM_LIT:1> ) <EOL> if col >= <NUM_LIT:0> : <EOL> object = context . get ( prefix [ : col ] ) <EOL> if object is not None : <EOL> method = getattr ( handler , name ) <EOL> trait_name = prefix [ col + <NUM_LIT:1> : ] <EOL> self . _dispatchers . append ( Dispatcher ( <EOL> method , info , object , trait_name ) ) <EOL> if object . base_trait ( trait_name ) . type != '<STR_LIT>' : <EOL> method ( info ) <EOL> if ( len ( self . _visible ) + <EOL> len ( self . _enabled ) + <EOL> len ( self . _checked ) ) > <NUM_LIT:0> : <EOL> for object in context . values ( ) : <EOL> object . on_trait_change ( self . _evaluate_when , dispatch = '<STR_LIT>' ) <EOL> self . _do_evaluate_when ( at_init = True ) <EOL> info . initialized = True <EOL> def sync_view ( self ) : <EOL> """<STR_LIT>""" <EOL> for name , object in self . context . items ( ) : <EOL> self . _sync_view ( name , object , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . _sync_view ( name , object , '<STR_LIT>' , '<STR_LIT:to>' ) <EOL> self . _sync_view ( name , object , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _sync_view ( self , name , object , metadata , direction ) : <EOL> info = self . info <EOL> for trait_name , trait in object . traits ( ** { metadata : is_str } ) . items ( ) : <EOL> for sync in getattr ( trait , metadata ) . split ( '<STR_LIT:U+002C>' ) : <EOL> try : <EOL> editor_id , editor_name = [ item . strip ( ) <EOL> for item in sync . split ( '<STR_LIT:.>' ) ] <EOL> except : <EOL> raise TraitError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( metadata , trait_name , name ) ) <EOL> editor = getattr ( info , editor_id , None ) <EOL> if editor is not None : <EOL> editor . sync_value ( '<STR_LIT>' % ( name , trait_name ) , <EOL> editor_name , direction ) <EOL> else : <EOL> raise TraitError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( editor_id , metadata , trait_name , name ) ) <EOL> def get_extended_value ( self , name ) : <EOL> """<STR_LIT>""" <EOL> names = name . split ( '<STR_LIT:.>' ) <EOL> if len ( names ) > <NUM_LIT:1> : <EOL> value = self . context [ names [ <NUM_LIT:0> ] ] <EOL> del names [ <NUM_LIT:0> ] <EOL> else : <EOL> value = self . context [ '<STR_LIT:object>' ] <EOL> for name in names : <EOL> value = getattr ( value , name ) <EOL> return value <EOL> def restore_prefs ( self ) : <EOL> """<STR_LIT>""" <EOL> id = self . id <EOL> if id != '<STR_LIT>' : <EOL> db = self . get_ui_db ( ) <EOL> if db is not None : <EOL> try : <EOL> ui_prefs = db . get ( id ) <EOL> db . close ( ) <EOL> return self . set_prefs ( ui_prefs ) <EOL> except : <EOL> pass <EOL> return None <EOL> def set_prefs ( self , prefs ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( prefs , dict ) : <EOL> info = self . info <EOL> for name in self . _names : <EOL> editor = getattr ( info , name , None ) <EOL> if isinstance ( editor , Editor ) and ( editor . ui is self ) : <EOL> editor_prefs = prefs . get ( name ) <EOL> if editor_prefs != None : <EOL> editor . restore_prefs ( editor_prefs ) <EOL> if self . key_bindings is not None : <EOL> key_bindings = prefs . get ( '<STR_LIT:$>' ) <EOL> if key_bindings is not None : <EOL> self . key_bindings . merge ( key_bindings ) <EOL> return prefs . get ( '<STR_LIT>' ) <EOL> return None <EOL> def save_prefs ( self , prefs = None ) : <EOL> """<STR_LIT>""" <EOL> if prefs is None : <EOL> toolkit ( ) . save_window ( self ) <EOL> return <EOL> id = self . id <EOL> if id != '<STR_LIT>' : <EOL> db = self . get_ui_db ( mode = '<STR_LIT:c>' ) <EOL> if db is not None : <EOL> db [ id ] = self . get_prefs ( prefs ) <EOL> db . close ( ) <EOL> def get_prefs ( self , prefs = None ) : <EOL> """<STR_LIT>""" <EOL> ui_prefs = { } <EOL> if prefs is not None : <EOL> ui_prefs [ '<STR_LIT>' ] = prefs <EOL> if self . key_bindings is not None : <EOL> ui_prefs [ '<STR_LIT:$>' ] = self . key_bindings <EOL> info = self . info <EOL> for name in self . _names : <EOL> editor = getattr ( info , name , None ) <EOL> if isinstance ( editor , Editor ) and ( editor . ui is self ) : <EOL> prefs = editor . save_prefs ( ) <EOL> if prefs != None : <EOL> ui_prefs [ name ] = prefs <EOL> return ui_prefs <EOL> def get_ui_db ( self , mode = '<STR_LIT:r>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return shelve . open ( os . path . join ( traits_home ( ) , '<STR_LIT>' ) , <EOL> flag = mode , protocol = - <NUM_LIT:1> ) <EOL> except : <EOL> return None <EOL> def get_editors ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return [ editor for editor in self . _editors if editor . name == name ] <EOL> def get_error_controls ( self ) : <EOL> """<STR_LIT>""" <EOL> controls = [ ] <EOL> for editor in self . _editors : <EOL> control = editor . get_error_control ( ) <EOL> if isinstance ( control , list ) : <EOL> controls . extend ( control ) <EOL> else : <EOL> controls . append ( control ) <EOL> return controls <EOL> def add_defined ( self , method ) : <EOL> """<STR_LIT>""" <EOL> self . _defined . append ( method ) <EOL> def add_visible ( self , visible_when , editor ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _visible . append ( ( compile ( visible_when , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> editor ) ) <EOL> except : <EOL> pass <EOL> def add_enabled ( self , enabled_when , editor ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _enabled . append ( ( compile ( enabled_when , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> editor ) ) <EOL> except : <EOL> pass <EOL> def add_checked ( self , checked_when , editor ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _checked . append ( ( compile ( checked_when , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> editor ) ) <EOL> except : <EOL> pass <EOL> def do_undoable ( self , action , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> undoable = self . _undoable <EOL> try : <EOL> if ( undoable == - <NUM_LIT:1> ) and ( self . history is not None ) : <EOL> self . _undoable = self . history . now <EOL> action ( * args , ** kw ) <EOL> finally : <EOL> if undoable == - <NUM_LIT:1> : <EOL> self . _undoable = - <NUM_LIT:1> <EOL> def route_event ( self , event ) : <EOL> """<STR_LIT>""" <EOL> toolkit ( ) . route_event ( self , event ) <EOL> def key_handler ( self , event , skip = True ) : <EOL> """<STR_LIT>""" <EOL> key_bindings = self . key_bindings <EOL> handled = ( ( key_bindings is not None ) and <EOL> key_bindings . do ( event , [ ] , self . info , <EOL> recursive = ( self . parent is None ) ) ) <EOL> if ( not handled ) and ( self . parent is not None ) : <EOL> handled = self . parent . key_handler ( event , False ) <EOL> if ( not handled ) and skip : <EOL> toolkit ( ) . skip_event ( event ) <EOL> return handled <EOL> def evaluate ( self , function , * args , ** kw_args ) : <EOL> """<STR_LIT>""" <EOL> if function is None : <EOL> return None <EOL> if callable ( function ) : <EOL> return function ( * args , ** kw_args ) <EOL> context = self . context . copy ( ) <EOL> context [ '<STR_LIT>' ] = self <EOL> context [ '<STR_LIT>' ] = self . handler <EOL> return eval ( function , globals ( ) , context ) ( * args , ** kw_args ) <EOL> def eval_when ( self , when , result = True ) : <EOL> """<STR_LIT>""" <EOL> context = self . _get_context ( self . context ) <EOL> try : <EOL> result = eval ( when , globals ( ) , context ) <EOL> except : <EOL> from traitsui . api import raise_to_debug <EOL> raise_to_debug ( ) <EOL> del context [ '<STR_LIT>' ] <EOL> return result <EOL> def _get_context ( self , context ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT:object>' <EOL> n = len ( context ) <EOL> if ( n == <NUM_LIT:2> ) and ( '<STR_LIT>' in context ) : <EOL> for name , value in context . items ( ) : <EOL> if name != '<STR_LIT>' : <EOL> break <EOL> elif n == <NUM_LIT:1> : <EOL> name = context . keys ( ) [ <NUM_LIT:0> ] <EOL> value = context . get ( name ) <EOL> if value is not None : <EOL> context2 = value . trait_get ( ) <EOL> context2 . update ( context ) <EOL> else : <EOL> context2 = context . copy ( ) <EOL> context2 [ '<STR_LIT>' ] = self <EOL> return context2 <EOL> def _evaluate_when ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _do_evaluate_when ( at_init = False ) <EOL> def _do_evaluate_when ( self , at_init = False ) : <EOL> """<STR_LIT>""" <EOL> self . _evaluate_condition ( self . _visible , '<STR_LIT>' , at_init ) <EOL> self . _evaluate_condition ( self . _enabled , '<STR_LIT>' , at_init ) <EOL> self . _evaluate_condition ( self . _checked , '<STR_LIT>' , at_init ) <EOL> def _evaluate_condition ( self , conditions , trait , at_init = False ) : <EOL> """<STR_LIT>""" <EOL> context = self . _get_context ( self . context ) <EOL> activate = [ ] <EOL> deactivate = [ ] <EOL> for when , editor in conditions : <EOL> try : <EOL> cond_value = eval ( when , globals ( ) , context ) <EOL> editor_state = getattr ( editor , trait ) <EOL> if cond_value and ( at_init or not editor_state ) : <EOL> activate . append ( editor ) <EOL> if not cond_value and ( at_init or editor_state ) : <EOL> deactivate . append ( editor ) <EOL> except Exception : <EOL> from traitsui . api import raise_to_debug <EOL> raise_to_debug ( ) <EOL> for editor in deactivate : <EOL> setattr ( editor , trait , False ) <EOL> for editor in activate : <EOL> setattr ( editor , trait , True ) <EOL> def _get__groups ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _groups_cache is None : <EOL> shadow_group = self . view . content . get_shadow ( self ) <EOL> self . _groups_cache = shadow_group . get_content ( ) <EOL> for item in self . _groups_cache : <EOL> if isinstance ( item , Item ) : <EOL> self . _groups_cache = [ <EOL> ShadowGroup ( shadow = Group ( * self . _groups_cache ) , <EOL> content = self . _groups_cache , <EOL> groups = <NUM_LIT:1> ) <EOL> ] <EOL> break <EOL> return self . _groups_cache <EOL> @ property_depends_on ( '<STR_LIT>' ) <EOL> def _get_key_bindings ( self ) : <EOL> view , context = self . view , self . context <EOL> if ( view is None ) or ( context is None ) : <EOL> return None <EOL> values = context . values ( ) <EOL> key_bindings = view . key_bindings <EOL> if key_bindings is None : <EOL> from . key_bindings import KeyBindings <EOL> return KeyBindings ( controllers = values ) <EOL> return key_bindings . clone ( controllers = values ) <EOL> def _updated_changed ( self ) : <EOL> if self . rebuild is not None : <EOL> toolkit ( ) . rebuild_ui ( self ) <EOL> def _title_changed ( self ) : <EOL> if self . control is not None : <EOL> toolkit ( ) . set_title ( self ) <EOL> def _icon_changed ( self ) : <EOL> if self . control is not None : <EOL> toolkit ( ) . set_icon ( self ) <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _pvc_changed ( self ) : <EOL> parent = self . parent <EOL> if ( parent is not None ) and ( self . key_bindings is not None ) : <EOL> if self . history is None : <EOL> self . history = parent . history <EOL> if parent . key_bindings is not None : <EOL> parent . key_bindings . children . append ( self . key_bindings ) <EOL> class Dispatcher ( object ) : <EOL> def __init__ ( self , method , info , object , method_name ) : <EOL> """<STR_LIT>""" <EOL> self . method = method <EOL> self . info = info <EOL> self . object = object <EOL> self . method_name = method_name <EOL> object . on_trait_change ( self . dispatch , method_name , dispatch = '<STR_LIT>' ) <EOL> def dispatch ( self ) : <EOL> """<STR_LIT>""" <EOL> self . method ( self . info ) <EOL> def remove ( self ) : <EOL> """<STR_LIT>""" <EOL> self . object . on_trait_change ( self . dispatch , self . method_name , <EOL> remove = True ) </s>
