<s> '''<STR_LIT>''' <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> from kivy . event import EventDispatcher <EOL> from kivy . properties import StringProperty , ObjectProperty , AliasProperty <EOL> knspace = None <EOL> '''<STR_LIT>''' <EOL> class KNSpace ( EventDispatcher ) : <EOL> '''<STR_LIT>''' <EOL> parent = None <EOL> '''<STR_LIT>''' <EOL> __has_applied = None <EOL> def __init__ ( self , parent = None , ** kwargs ) : <EOL> super ( KNSpace , self ) . __init__ ( ** kwargs ) <EOL> self . parent = parent <EOL> self . __has_applied = set ( self . properties ( ) . keys ( ) ) <EOL> def __setattr__ ( self , name , value ) : <EOL> prop = super ( KNSpace , self ) . property ( name , quiet = True ) <EOL> has_applied = self . __has_applied <EOL> if prop is None : <EOL> if hasattr ( self , name ) : <EOL> super ( KNSpace , self ) . __setattr__ ( name , value ) <EOL> else : <EOL> self . apply_property ( <EOL> ** { name : <EOL> ObjectProperty ( None , rebind = True , allownone = True ) } <EOL> ) <EOL> value = getattr ( value , '<STR_LIT>' , value ) <EOL> has_applied . add ( name ) <EOL> super ( KNSpace , self ) . __setattr__ ( name , value ) <EOL> elif name not in has_applied : <EOL> self . apply_property ( ** { name : prop } ) <EOL> has_applied . add ( name ) <EOL> value = getattr ( value , '<STR_LIT>' , value ) <EOL> super ( KNSpace , self ) . __setattr__ ( name , value ) <EOL> else : <EOL> value = getattr ( value , '<STR_LIT>' , value ) <EOL> super ( KNSpace , self ) . __setattr__ ( name , value ) <EOL> def __getattribute__ ( self , name ) : <EOL> if name in super ( KNSpace , self ) . __getattribute__ ( '<STR_LIT>' ) : <EOL> return super ( KNSpace , self ) . __getattribute__ ( name ) <EOL> try : <EOL> value = super ( KNSpace , self ) . __getattribute__ ( name ) <EOL> except AttributeError : <EOL> parent = super ( KNSpace , self ) . __getattribute__ ( '<STR_LIT>' ) <EOL> if parent is None : <EOL> raise AttributeError ( name ) <EOL> return getattr ( parent , name ) <EOL> if value is not None : <EOL> return value <EOL> parent = super ( KNSpace , self ) . __getattribute__ ( '<STR_LIT>' ) <EOL> if parent is None : <EOL> return None <EOL> return getattr ( parent , name ) <EOL> def property ( self , name , quiet = False ) : <EOL> prop = super ( KNSpace , self ) . property ( name , quiet = True ) <EOL> if prop is not None : <EOL> return prop <EOL> prop = ObjectProperty ( None , rebind = True , allownone = True ) <EOL> self . apply_property ( ** { name : prop } ) <EOL> self . __has_applied . add ( name ) <EOL> return prop <EOL> def fork ( self ) : <EOL> '''<STR_LIT>''' <EOL> return KNSpace ( parent = self ) <EOL> class KNSpaceBehavior ( object ) : <EOL> '''<STR_LIT>''' <EOL> _knspace = ObjectProperty ( None , allownone = True ) <EOL> _knsname = StringProperty ( '<STR_LIT>' ) <EOL> __last_knspace = None <EOL> __callbacks = None <EOL> def __init__ ( self , knspace = None , ** kwargs ) : <EOL> self . knspace = knspace <EOL> super ( KNSpaceBehavior , self ) . __init__ ( ** kwargs ) <EOL> def __knspace_clear_callbacks ( self , * largs ) : <EOL> for obj , name , uid in self . __callbacks : <EOL> obj . unbind_uid ( name , uid ) <EOL> last = self . __last_knspace <EOL> self . __last_knspace = self . __callbacks = None <EOL> assert self . _knspace is None <EOL> assert last <EOL> new = self . __set_parent_knspace ( ) <EOL> if new is last : <EOL> return <EOL> self . property ( '<STR_LIT>' ) . dispatch ( self ) <EOL> name = self . knsname <EOL> if not name : <EOL> return <EOL> if getattr ( last , name ) == self : <EOL> setattr ( last , name , None ) <EOL> if new : <EOL> setattr ( new , name , self ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . <EOL> format ( name ) ) <EOL> def __set_parent_knspace ( self ) : <EOL> callbacks = self . __callbacks = [ ] <EOL> fbind = self . fbind <EOL> append = callbacks . append <EOL> parent_key = self . knspace_key <EOL> clear = self . __knspace_clear_callbacks <EOL> append ( ( self , '<STR_LIT>' , fbind ( '<STR_LIT>' , clear ) ) ) <EOL> if not parent_key : <EOL> self . __last_knspace = knspace <EOL> return knspace <EOL> append ( ( self , parent_key , fbind ( parent_key , clear ) ) ) <EOL> parent = getattr ( self , parent_key , None ) <EOL> while parent is not None : <EOL> fbind = parent . fbind <EOL> parent_knspace = getattr ( parent , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if parent_knspace is not <NUM_LIT:0> : <EOL> append ( ( parent , '<STR_LIT>' , fbind ( '<STR_LIT>' , clear ) ) ) <EOL> self . __last_knspace = parent_knspace <EOL> return parent_knspace <EOL> append ( ( parent , parent_key , fbind ( parent_key , clear ) ) ) <EOL> new_parent = getattr ( parent , parent_key , None ) <EOL> if new_parent is parent : <EOL> break <EOL> parent = new_parent <EOL> self . __last_knspace = knspace <EOL> return knspace <EOL> def _get_knspace ( self ) : <EOL> _knspace = self . _knspace <EOL> if _knspace is not None : <EOL> return _knspace <EOL> if self . __callbacks is not None : <EOL> return self . __last_knspace <EOL> return self . __set_parent_knspace ( ) <EOL> def _set_knspace ( self , value ) : <EOL> if value is self . _knspace : <EOL> return <EOL> knspace = self . _knspace or self . __last_knspace <EOL> name = self . knsname <EOL> if name and knspace and getattr ( knspace , name ) == self : <EOL> setattr ( knspace , name , None ) <EOL> if value == '<STR_LIT>' : <EOL> if not knspace : <EOL> knspace = self . knspace <EOL> if knspace : <EOL> value = knspace . fork ( ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> for obj , prop_name , uid in self . __callbacks or [ ] : <EOL> obj . unbind_uid ( prop_name , uid ) <EOL> self . __last_knspace = self . __callbacks = None <EOL> if name : <EOL> if value is None : <EOL> knspace = self . __set_parent_knspace ( ) <EOL> if knspace : <EOL> setattr ( knspace , name , self ) <EOL> self . _knspace = None <EOL> else : <EOL> setattr ( value , name , self ) <EOL> knspace = self . _knspace = value <EOL> if not knspace : <EOL> raise ValueError ( '<STR_LIT>' . <EOL> format ( name ) ) <EOL> else : <EOL> if value is None : <EOL> self . __set_parent_knspace ( ) <EOL> self . _knspace = value <EOL> knspace = AliasProperty ( <EOL> _get_knspace , _set_knspace , bind = ( '<STR_LIT>' , ) , cache = False , <EOL> rebind = True , allownone = True ) <EOL> '''<STR_LIT>''' <EOL> knspace_key = StringProperty ( '<STR_LIT>' , allownone = True ) <EOL> '''<STR_LIT>''' <EOL> def _get_knsname ( self ) : <EOL> return self . _knsname <EOL> def _set_knsname ( self , value ) : <EOL> old_name = self . _knsname <EOL> knspace = self . knspace <EOL> if old_name and knspace and getattr ( knspace , old_name ) == self : <EOL> setattr ( knspace , old_name , None ) <EOL> self . _knsname = value <EOL> if value : <EOL> if knspace : <EOL> setattr ( knspace , value , self ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . <EOL> format ( value ) ) <EOL> knsname = AliasProperty ( <EOL> _get_knsname , _set_knsname , bind = ( '<STR_LIT>' , ) , cache = False ) <EOL> '''<STR_LIT>''' <EOL> knspace = KNSpace ( ) </s>
