<s> """<STR_LIT>""" <EOL> import datetime <EOL> import copy <EOL> import pprint <EOL> import socket <EOL> import sys <EOL> import weakref <EOL> copy . _copy_dispatch [ weakref . ref ] = copy . _copy_immutable <EOL> copy . _deepcopy_dispatch [ weakref . ref ] = copy . _deepcopy_atomic <EOL> copy . _deepcopy_dispatch [ weakref . KeyedRef ] = copy . _deepcopy_atomic <EOL> from zope . interface import Interface , implements <EOL> from numpy import ndarray <EOL> from traits . api import HasTraits , Missing , Python , push_exception_handler , TraitType , CTrait <EOL> from traits . has_traits import FunctionType , _clone_trait , MetaHasTraits <EOL> from traits . trait_base import not_none <EOL> from multiprocessing import connection <EOL> from openmdao . main . datatypes . file import FileRef <EOL> from openmdao . main . datatypes . list import List <EOL> from openmdao . main . datatypes . slot import Slot <EOL> from openmdao . main . datatypes . vtree import VarTree <EOL> from openmdao . main . interfaces import ICaseIterator , IResourceAllocator , IContainer , IVariableTree , IContainerProxy , IOverrideSet <EOL> from openmdao . main . mp_support import ObjectManager , is_instance , CLASSES_TO_PROXY , has_interface <EOL> from openmdao . main . rbac import rbac <EOL> from openmdao . main . variable import Variable , is_legal_name , _missing <EOL> from openmdao . main . array_helpers import flattened_value , get_index <EOL> from openmdao . util . log import Logger , logger <EOL> from openmdao . util import eggloader , eggsaver , eggobserver <EOL> from openmdao . util . eggsaver import SAVE_CPICKLE <EOL> from openmdao . util . typegroups import int_types , complex_or_real_types <EOL> _copydict = { <EOL> '<STR_LIT>' : copy . deepcopy , <EOL> '<STR_LIT>' : copy . copy <EOL> } <EOL> _iodict = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:input>' } <EOL> __missing__ = object ( ) <EOL> def get_closest_proxy ( obj , pathname ) : <EOL> """<STR_LIT>""" <EOL> names = pathname . split ( '<STR_LIT:.>' ) <EOL> i = <NUM_LIT:0> <EOL> for name in names : <EOL> if IContainerProxy . providedBy ( obj ) : <EOL> return ( obj , '<STR_LIT:.>' . join ( names [ i : ] ) ) <EOL> try : <EOL> obj = getattr ( obj , name ) <EOL> except AttributeError : <EOL> break <EOL> i += <NUM_LIT:1> <EOL> return ( obj , '<STR_LIT:.>' . join ( names [ i : ] ) ) <EOL> def proxy_parent ( obj , pathname ) : <EOL> """<STR_LIT>""" <EOL> names = pathname . split ( '<STR_LIT:.>' ) <EOL> i = <NUM_LIT:0> <EOL> for name in names [ : - <NUM_LIT:1> ] : <EOL> if IContainerProxy . providedBy ( obj ) : <EOL> return ( obj , '<STR_LIT:.>' . join ( names [ i : ] ) ) <EOL> try : <EOL> obj = getattr ( obj , name ) <EOL> except AttributeError : <EOL> break <EOL> i += <NUM_LIT:1> <EOL> return ( obj , '<STR_LIT:.>' . join ( names [ i : ] ) ) <EOL> push_exception_handler ( handler = lambda o , t , ov , nv : None , <EOL> reraise_exceptions = True , <EOL> main = True , <EOL> locked = True ) <EOL> class _MetaSafe ( MetaHasTraits ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( mcs , class_name , bases , class_dict ) : <EOL> for name , obj in class_dict . items ( ) : <EOL> if isinstance ( obj , Variable ) : <EOL> for base in bases : <EOL> if name in base . __dict__ : <EOL> raise NameError ( '<STR_LIT>' <EOL> % ( class_name , name , base . __name__ ) ) <EOL> return super ( _MetaSafe , mcs ) . __new__ ( mcs , class_name , bases , class_dict ) <EOL> class SafeHasTraits ( HasTraits ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = _MetaSafe <EOL> def _check_bad_default ( name , trait , obj = None ) : <EOL> if trait . vartypename not in [ '<STR_LIT>' , '<STR_LIT>' ] and trait . required is True and not trait . assumed_default and trait . _illegal_default_ is True : <EOL> msg = "<STR_LIT>" % name <EOL> if obj is None : <EOL> raise RuntimeError ( msg ) <EOL> else : <EOL> obj . raise_exception ( msg , RuntimeError ) <EOL> class Container ( SafeHasTraits ) : <EOL> """<STR_LIT>""" <EOL> implements ( IContainer ) <EOL> def __init__ ( self ) : <EOL> self . _parent = None <EOL> self . _name = None <EOL> super ( Container , self ) . __init__ ( ) <EOL> self . _call_cpath_updated = True <EOL> self . _call_configure = True <EOL> self . _managers = { } <EOL> self . _added_traits = { } <EOL> self . _getcache = { } <EOL> self . _setcache = { } <EOL> self . _copycache = { } <EOL> self . _cached_traits_ = None <EOL> self . _repair_trait_info = None <EOL> self . _trait_metadata = { } <EOL> self . _logger = Logger ( '<STR_LIT>' ) <EOL> for name , obj in self . items ( ) : <EOL> if isinstance ( obj , FileRef ) : <EOL> setattr ( self , name , obj . copy ( owner = self ) ) <EOL> for name , obj in self . __class__ . __dict__ [ '<STR_LIT>' ] . items ( ) : <EOL> ttype = obj . trait_type <EOL> if isinstance ( ttype , VarTree ) : <EOL> variable_tree = getattr ( self , name ) <EOL> if not obj . required : <EOL> new_tree = variable_tree . copy ( ) <EOL> setattr ( self , name , new_tree ) <EOL> if obj . required : <EOL> _check_bad_default ( name , obj , self ) <EOL> @ property <EOL> def parent ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parent <EOL> @ parent . setter <EOL> def parent ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if self . _parent is not value : <EOL> self . _parent = value <EOL> self . _fix_loggers ( self , recurse = True ) <EOL> self . _branch_moved ( ) <EOL> def _branch_moved ( self ) : <EOL> self . _call_cpath_updated = True <EOL> for n , cont in self . items ( ) : <EOL> if is_instance ( cont , Container ) and cont is not self . _parent : <EOL> cont . _branch_moved ( ) <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _name is None : <EOL> if self . parent : <EOL> self . _name = find_name ( self . parent , self ) <EOL> self . _fix_loggers ( self , recurse = True ) <EOL> elif self . _call_cpath_updated is False : <EOL> self . _name = '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> return self . _name <EOL> @ name . setter <EOL> def name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if not is_legal_name ( name ) : <EOL> raise NameError ( "<STR_LIT>" % name ) <EOL> if self . _name != name : <EOL> self . _name = name <EOL> self . _fix_loggers ( self , recurse = True ) <EOL> def _fix_loggers ( self , container , recurse ) : <EOL> """<STR_LIT>""" <EOL> container . _logger . rename ( container . get_pathname ( ) . replace ( '<STR_LIT:.>' , '<STR_LIT:U+002C>' ) ) <EOL> if recurse : <EOL> for name in container . list_containers ( ) : <EOL> obj = getattr ( container , name ) <EOL> self . _fix_loggers ( obj , recurse ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def get_pathname ( self , rel_to_scope = None ) : <EOL> """<STR_LIT>""" <EOL> path = [ ] <EOL> obj = self <EOL> name = obj . name <EOL> while obj is not rel_to_scope and name : <EOL> path . append ( name ) <EOL> obj = obj . parent <EOL> if obj is None : <EOL> break <EOL> name = obj . name <EOL> return '<STR_LIT:.>' . join ( path [ : : - <NUM_LIT:1> ] ) <EOL> def get_trait ( self , name , copy = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _cached_traits_ is None : <EOL> self . _cached_traits_ = self . traits ( ) <EOL> self . _cached_traits_ . update ( self . _instance_traits ( ) ) <EOL> if copy : <EOL> if self . _cached_traits_ . get ( name ) : <EOL> return self . trait ( name , copy = copy ) <EOL> else : <EOL> return None <EOL> else : <EOL> return self . _cached_traits_ . get ( name ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> """<STR_LIT>""" <EOL> id_self = id ( self ) <EOL> if id_self in memo : <EOL> return memo [ id_self ] <EOL> memo [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> saved_p = self . _parent <EOL> saved_c = self . _cached_traits_ <EOL> saved_s = self . _setcache <EOL> saved_g = self . _getcache <EOL> self . _parent = None <EOL> self . _cached_traits_ = None <EOL> self . _getcache = { } <EOL> self . _setcache = { } <EOL> try : <EOL> result = super ( Container , self ) . __deepcopy__ ( memo ) <EOL> finally : <EOL> self . _parent = saved_p <EOL> self . _cached_traits_ = saved_c <EOL> self . _getcache = saved_g <EOL> self . _setcache = saved_s <EOL> olditraits = self . _instance_traits ( ) <EOL> for name , trait in olditraits . items ( ) : <EOL> if trait . type is not '<STR_LIT>' and name in self . _added_traits : <EOL> if isinstance ( trait . trait_type , VarTree ) : <EOL> if name not in result . _added_traits : <EOL> result . add_trait ( name , _clone_trait ( trait ) ) <EOL> else : <EOL> result . add_trait ( name , _clone_trait ( trait ) ) <EOL> if name in self . __dict__ : <EOL> result . __dict__ [ name ] = copy . deepcopy ( self . __dict__ [ name ] ) <EOL> return result <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = super ( Container , self ) . __getstate__ ( ) <EOL> dct = { } <EOL> for name , trait in state [ '<STR_LIT>' ] . items ( ) : <EOL> if trait . transient is not True : <EOL> dct [ name ] = trait <EOL> state [ '<STR_LIT>' ] = dct <EOL> state [ '<STR_LIT>' ] = None <EOL> state [ '<STR_LIT>' ] = { } <EOL> state [ '<STR_LIT>' ] = { } <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> super ( Container , self ) . __setstate__ ( { } ) <EOL> self . __dict__ . update ( state ) <EOL> self . _repair_trait_info = { } <EOL> self . _cached_traits_ = None <EOL> traits = self . _alltraits ( ) <EOL> for name , trait in self . _added_traits . items ( ) : <EOL> if name not in traits : <EOL> self . add_trait ( name , trait , refresh = False ) <EOL> fixups = [ ] <EOL> for name , trait in traits . items ( ) : <EOL> try : <EOL> get = trait . trait_type . get <EOL> except AttributeError : <EOL> continue <EOL> if get is not None : <EOL> if name not in self . _added_traits : <EOL> try : <EOL> val = getattr ( self , name ) <EOL> self . remove_trait ( name ) <EOL> self . add_trait ( name , trait ) <EOL> setattr ( self , name , val ) <EOL> except Exception as exc : <EOL> self . _logger . warning ( '<STR_LIT>' , <EOL> name , val , exc ) <EOL> fixups . append ( ( name , trait ) ) <EOL> self . _repair_trait_info [ '<STR_LIT>' ] = fixups <EOL> fixups = [ ] <EOL> for name , val in self . __dict__ . items ( ) : <EOL> if not name . startswith ( '<STR_LIT>' ) and not self . get_trait ( name ) : <EOL> try : <EOL> setattr ( self , name , val ) <EOL> except Exception as exc : <EOL> self . _logger . warning ( '<STR_LIT>' , <EOL> name , val , exc ) <EOL> fixups . append ( ( name , val ) ) <EOL> self . _repair_trait_info [ '<STR_LIT>' ] = fixups <EOL> fixups = [ ] <EOL> for name , trait in self . _alltraits ( ) . items ( ) : <EOL> if isinstance ( trait . trait_type , List ) : <EOL> try : <EOL> setattr ( self , name , getattr ( self , name ) ) <EOL> except Exception as exc : <EOL> self . _logger . warning ( '<STR_LIT>' , <EOL> name , val , exc ) <EOL> fixups . append ( name ) <EOL> self . _repair_trait_info [ '<STR_LIT:list>' ] = fixups <EOL> self . _cached_traits_ = None <EOL> def _repair_traits ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _repair_trait_info is None : <EOL> return <EOL> for name , trait in self . _repair_trait_info [ '<STR_LIT>' ] : <EOL> val = getattr ( self , name ) <EOL> self . remove_trait ( name ) <EOL> self . add_trait ( name , trait ) <EOL> setattr ( self , name , val ) <EOL> for name , val in self . _repair_trait_info [ '<STR_LIT>' ] : <EOL> setattr ( self , name , val ) <EOL> for name in self . _repair_trait_info [ '<STR_LIT:list>' ] : <EOL> setattr ( self , name , getattr ( self , name ) ) <EOL> self . _repair_trait_info = None <EOL> @ classmethod <EOL> def add_class_trait ( cls , name , * trait ) : <EOL> """<STR_LIT>""" <EOL> bases = [ cls ] <EOL> bases . extend ( cls . __bases__ ) <EOL> for base in bases : <EOL> if name in base . __dict__ : <EOL> raise NameError ( '<STR_LIT>' <EOL> % ( name , base . __name__ ) ) <EOL> for t in trait : <EOL> _check_bad_default ( name , t ) <EOL> break <EOL> if name in cls . _trait_metadata : <EOL> del cls . _trait_metadata [ name ] <EOL> return super ( Container , cls ) . add_class_trait ( name , * trait ) <EOL> def add_trait ( self , name , trait , refresh = True ) : <EOL> """<STR_LIT>""" <EOL> if name . endswith ( '<STR_LIT>' ) and trait . type == '<STR_LIT>' : <EOL> super ( Container , self ) . add_trait ( name , trait ) <EOL> return <EOL> bases = [ self . __class__ ] <EOL> bases . extend ( self . __class__ . __bases__ ) <EOL> for base in bases : <EOL> if name in base . __dict__ : <EOL> raise NameError ( '<STR_LIT>' <EOL> % ( name , base . __name__ ) ) <EOL> _check_bad_default ( name , trait , self ) <EOL> if name not in self . _added_traits : <EOL> self . _added_traits [ name ] = trait <EOL> super ( Container , self ) . add_trait ( name , trait ) <EOL> if self . _cached_traits_ is not None : <EOL> self . _cached_traits_ [ name ] = self . trait ( name ) <EOL> if name in self . _trait_metadata : <EOL> del self . _trait_metadata [ name ] <EOL> if refresh : <EOL> getattr ( self , name ) <EOL> def remove_trait ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . _added_traits [ name ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> del self . _cached_traits_ [ name ] <EOL> except ( KeyError , TypeError ) : <EOL> pass <EOL> try : <EOL> del self . _trait_metadata [ name ] <EOL> except KeyError : <EOL> pass <EOL> super ( Container , self ) . remove_trait ( name ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def get_attr_w_copy ( self , path ) : <EOL> """<STR_LIT>""" <EOL> obj = self . get ( path ) <EOL> copy = self . _copycache . get ( path , _missing ) <EOL> if copy is _missing : <EOL> copy = self . get_metadata ( path . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> self . _copycache [ path ] = copy <EOL> if copy : <EOL> if isinstance ( obj , Container ) : <EOL> obj = obj . copy ( ) <EOL> else : <EOL> obj = _copydict [ copy ] ( obj ) <EOL> return obj <EOL> def _add_after_parent_set ( self , name , obj ) : <EOL> pass <EOL> def _prep_for_add ( self , name , obj ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:.>' in name : <EOL> self . raise_exception ( <EOL> '<STR_LIT>' % <EOL> name , ValueError ) <EOL> elif not is_legal_name ( name ) : <EOL> self . raise_exception ( "<STR_LIT>" % name , <EOL> NameError ) <EOL> removed = False <EOL> if has_interface ( obj , IContainer ) : <EOL> if self . contains ( name ) and getattr ( self , name ) : <EOL> self . remove ( name ) <EOL> removed = True <EOL> if has_interface ( obj , IContainer ) : <EOL> self . _check_recursion ( obj ) <EOL> if IContainerProxy . providedBy ( obj ) : <EOL> obj . parent = self . _get_proxy ( obj ) <EOL> else : <EOL> obj . parent = self <EOL> obj . name = name <EOL> self . _add_after_parent_set ( name , obj ) <EOL> if self . _call_cpath_updated is False : <EOL> obj . cpath_updated ( ) <EOL> return removed <EOL> def _post_container_add ( self , name , obj , removed ) : <EOL> pass <EOL> def add ( self , name , obj ) : <EOL> """<STR_LIT>""" <EOL> removed = self . _prep_for_add ( name , obj ) <EOL> if has_interface ( obj , IContainer ) : <EOL> setattr ( self , name , obj ) <EOL> if self . _cached_traits_ is None : <EOL> self . get_trait ( name ) <EOL> else : <EOL> self . _cached_traits_ [ name ] = self . trait ( name ) <EOL> self . _post_container_add ( name , obj , removed ) <EOL> elif is_instance ( obj , TraitType ) : <EOL> self . add_trait ( name , obj ) <EOL> else : <EOL> setattr ( self , name , obj ) <EOL> return obj <EOL> def _check_recursion ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> ancestor = self <EOL> while is_instance ( ancestor , Container ) : <EOL> if obj is ancestor : <EOL> self . raise_exception ( '<STR_LIT>' , <EOL> ValueError ) <EOL> ancestor = ancestor . parent <EOL> def _get_proxy ( self , proxy ) : <EOL> """<STR_LIT>""" <EOL> addr_type = connection . address_type ( proxy . _token . address ) <EOL> addr = proxy . _token . address [ <NUM_LIT:0> ] if addr_type == '<STR_LIT>' else None <EOL> key = ( addr_type , addr , proxy . _authkey ) <EOL> try : <EOL> manager = self . _managers [ key ] <EOL> except KeyError : <EOL> if addr_type == '<STR_LIT>' : <EOL> ip_addr = socket . gethostbyname ( socket . gethostname ( ) ) <EOL> address = ( ip_addr , <NUM_LIT:0> ) <EOL> allowed_hosts = [ addr ] <EOL> if addr == ip_addr : <EOL> allowed_hosts . append ( '<STR_LIT:127.0.0.1>' ) <EOL> else : <EOL> address = None <EOL> allowed_hosts = None <EOL> name = self . name or '<STR_LIT>' <EOL> access = addr if addr_type == '<STR_LIT>' else addr_type <EOL> name = '<STR_LIT>' % ( name , access ) <EOL> manager = ObjectManager ( self , address , authkey = proxy . _authkey , <EOL> name = name , allowed_hosts = allowed_hosts ) <EOL> self . _managers [ key ] = manager <EOL> return manager . proxy <EOL> def _check_rename ( self , oldname , newname ) : <EOL> if '<STR_LIT:.>' in oldname or '<STR_LIT:.>' in newname : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( oldname , newname ) , RuntimeError ) <EOL> if not self . contains ( oldname ) : <EOL> self . raise_exception ( "<STR_LIT>" % <EOL> ( oldname , newname , oldname ) , RuntimeError ) <EOL> if self . contains ( newname ) : <EOL> self . raise_exception ( "<STR_LIT>" % <EOL> ( oldname , newname , newname ) , RuntimeError ) <EOL> def rename ( self , oldname , newname ) : <EOL> """<STR_LIT>""" <EOL> self . _check_rename ( oldname , newname ) <EOL> obj = self . remove ( oldname ) <EOL> self . add ( newname , obj ) <EOL> def remove ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:.>' in name : <EOL> self . raise_exception ( <EOL> '<STR_LIT>' % <EOL> name , NameError ) <EOL> try : <EOL> obj = getattr ( self , name ) <EOL> except AttributeError : <EOL> return None <EOL> trait = self . get_trait ( name ) <EOL> if trait is None : <EOL> delattr ( self , name ) <EOL> else : <EOL> if trait . is_trait_type ( Slot ) : <EOL> try : <EOL> setattr ( self , name , None ) <EOL> except TypeError as err : <EOL> self . raise_exception ( str ( err ) , RuntimeError ) <EOL> else : <EOL> self . remove_trait ( name ) <EOL> return obj <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def configure ( self ) : <EOL> pass <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> cp = copy . deepcopy ( self ) <EOL> cp . _relink ( ) <EOL> return cp <EOL> def _relink ( self ) : <EOL> """<STR_LIT>""" <EOL> for name in self . list_containers ( ) : <EOL> container = getattr ( self , name ) <EOL> if container is not self . _parent : <EOL> container . _parent = self <EOL> container . _relink ( ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def cpath_updated ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _fix_loggers ( self , recurse = False ) <EOL> self . _call_cpath_updated = False <EOL> for cont in self . list_containers ( ) : <EOL> cont = getattr ( self , cont ) <EOL> if cont is not self . _parent : <EOL> cont . cpath_updated ( ) <EOL> def revert_to_defaults ( self , recurse = True ) : <EOL> """<STR_LIT>""" <EOL> self . reset_traits ( iotype = '<STR_LIT>' ) <EOL> if recurse : <EOL> for cname in self . list_containers ( ) : <EOL> getattr ( self , cname ) . revert_to_defaults ( recurse ) <EOL> def _items ( self , visited , recurse = False , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> if id ( self ) not in visited : <EOL> visited . add ( id ( self ) ) <EOL> match_dict = self . _alltraits ( ** metadata ) <EOL> if recurse : <EOL> for name in self . list_containers ( ) : <EOL> obj = getattr ( self , name ) <EOL> if name in match_dict and id ( obj ) not in visited : <EOL> yield ( name , obj ) <EOL> if obj : <EOL> for chname , child in obj . _items ( visited , recurse , <EOL> ** metadata ) : <EOL> yield ( '<STR_LIT:.>' . join ( ( name , chname ) ) , child ) <EOL> for name , trait in match_dict . items ( ) : <EOL> obj = getattr ( self , name , Missing ) <EOL> if obj is not Missing : <EOL> if is_instance ( obj , ( Container , VarTree ) ) and id ( obj ) not in visited : <EOL> if not recurse : <EOL> yield ( name , obj ) <EOL> elif trait . iotype is not None : <EOL> yield ( name , obj ) <EOL> def items ( self , recurse = False , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> return self . _items ( set ( [ id ( self . parent ) ] ) , recurse , ** metadata ) <EOL> def list_containers ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ n for n , v in self . items ( ) if is_instance ( v , Container ) ] <EOL> def list_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ k for k , v in self . items ( iotype = not_none ) ] <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def _alltraits ( self , traits = None , events = False , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> if traits is None : <EOL> if self . _cached_traits_ : <EOL> traits = self . _cached_traits_ <EOL> else : <EOL> traits = self . traits ( ) <EOL> traits . update ( self . _instance_traits ( ) ) <EOL> self . _cached_traits_ = traits <EOL> result = { } <EOL> for name , trait in traits . items ( ) : <EOL> if not events and trait . type is '<STR_LIT>' : <EOL> continue <EOL> for meta_name , meta_eval in metadata . items ( ) : <EOL> if type ( meta_eval ) is FunctionType : <EOL> if not meta_eval ( getattr ( trait , meta_name ) ) : <EOL> break <EOL> elif meta_eval != getattr ( trait , meta_name ) : <EOL> break <EOL> else : <EOL> result [ name ] = trait <EOL> return result <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def contains ( self , path ) : <EOL> """<STR_LIT>""" <EOL> childname , _ , restofpath = path . partition ( '<STR_LIT:.>' ) <EOL> if restofpath : <EOL> obj = getattr ( self , childname , Missing ) <EOL> if obj is Missing : <EOL> return False <EOL> elif is_instance ( obj , Container ) : <EOL> return obj . contains ( restofpath ) <EOL> else : <EOL> return hasattr ( obj , restofpath ) <EOL> return hasattr ( self , path ) <EOL> def _get_metadata_failed ( self , traitpath , metaname ) : <EOL> self . raise_exception ( "<STR_LIT>" % traitpath , <EOL> AttributeError ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def get_metadata ( self , traitpath , metaname = None ) : <EOL> """<STR_LIT>""" <EOL> childname , _ , restofpath = traitpath . partition ( '<STR_LIT:.>' ) <EOL> if restofpath : <EOL> obj = getattr ( self , childname , Missing ) <EOL> if obj is Missing : <EOL> return self . _get_metadata_failed ( traitpath , metaname ) <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . get_metadata ( restofpath , metaname ) <EOL> else : <EOL> t = self . get_trait ( childname ) <EOL> if t is not None and t . iotype and metaname == '<STR_LIT>' : <EOL> return t . iotype <EOL> else : <EOL> self . _get_metadata_failed ( traitpath , metaname ) <EOL> varname , _ , _ = traitpath . partition ( '<STR_LIT:[>' ) <EOL> try : <EOL> mdict = self . _trait_metadata [ varname ] <EOL> except KeyError : <EOL> t = self . get_trait ( varname ) <EOL> if t : <EOL> t = t . trait_type <EOL> mdict = t . _metadata . copy ( ) <EOL> mdict . setdefault ( '<STR_LIT>' , t . __class__ . __name__ ) <EOL> else : <EOL> mdict = self . _get_metadata_failed ( traitpath , None ) <EOL> self . _trait_metadata [ varname ] = mdict <EOL> if metaname is None : <EOL> return mdict <EOL> else : <EOL> return mdict . get ( metaname , None ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def set_metadata ( self , traitpath , metaname , value ) : <EOL> """<STR_LIT>""" <EOL> if metaname in ( '<STR_LIT>' , ) : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> % ( metaname , traitpath ) , TypeError ) <EOL> self . get_metadata ( traitpath ) [ metaname ] = value <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) , proxy_types = [ FileRef ] ) <EOL> def get ( self , path ) : <EOL> """<STR_LIT>""" <EOL> expr = self . _getcache . get ( path ) <EOL> if expr is not None : <EOL> return eval ( expr , self . __dict__ ) <EOL> obj , restofpath = get_closest_proxy ( self , path ) <EOL> if restofpath and IContainerProxy . providedBy ( obj ) : <EOL> return obj . get ( restofpath ) <EOL> expr = compile ( path , path , mode = '<STR_LIT>' ) <EOL> try : <EOL> val = eval ( expr , self . __dict__ ) <EOL> except ( AttributeError , NameError ) as err : <EOL> if not restofpath : <EOL> return obj <EOL> self . raise_exception ( str ( err ) , AttributeError ) <EOL> else : <EOL> self . _getcache [ path ] = expr <EOL> return val <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) , proxy_types = [ FileRef ] ) <EOL> def get_flattened_value ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return flattened_value ( path , self . get ( path ) ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def set_flattened_value ( self , path , value ) : <EOL> obj , restofpath = proxy_parent ( self , path ) <EOL> if restofpath and IContainerProxy . providedBy ( obj ) : <EOL> obj . set_flattened_value ( restofpath , value ) <EOL> return <EOL> val = self . get ( path ) <EOL> if not isinstance ( val , int_types ) and isinstance ( val , complex_or_real_types ) : <EOL> self . set ( path , value [ <NUM_LIT:0> ] ) <EOL> return <EOL> elif hasattr ( val , '<STR_LIT>' ) : <EOL> val . set_flattened_value ( value ) <EOL> return <EOL> elif isinstance ( val , ndarray ) : <EOL> try : <EOL> newshape = value . shape <EOL> self . set ( path , value . reshape ( val . shape ) ) <EOL> except Exception as err : <EOL> self . reraise_exception ( "<STR_LIT>" <EOL> % ( self . get_pathname ( ) , path , val . shape , newshape ) , <EOL> sys . exc_info ( ) ) <EOL> return <EOL> val = self . get ( path . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> idx = get_index ( path ) <EOL> if isinstance ( val , int_types ) : <EOL> pass <EOL> elif hasattr ( val , '<STR_LIT>' ) and idx is not None : <EOL> if isinstance ( val [ idx ] , complex_or_real_types ) : <EOL> val [ idx ] = value [ <NUM_LIT:0> ] <EOL> else : <EOL> val [ idx ] = value <EOL> return <EOL> elif IVariableTree . providedBy ( val ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> elif hasattr ( val , '<STR_LIT>' ) : <EOL> val . set_flattened_value ( value ) <EOL> return <EOL> self . raise_exception ( "<STR_LIT>" % path , TypeError ) <EOL> def get_iotype ( self , name ) : <EOL> return self . get_trait ( name ) . iotype <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def set ( self , path , value ) : <EOL> """<STR_LIT>""" <EOL> _local_setter_ = value <EOL> expr = self . _setcache . get ( path ) <EOL> if expr is not None : <EOL> exec ( expr ) <EOL> return <EOL> obj , restofpath = proxy_parent ( self , path ) <EOL> if IOverrideSet . providedBy ( obj ) or ( restofpath and IContainerProxy . providedBy ( obj ) ) : <EOL> obj . set ( restofpath , value ) <EOL> return <EOL> assign = "<STR_LIT>" % path <EOL> expr = compile ( assign , assign , mode = '<STR_LIT>' ) <EOL> try : <EOL> exec ( expr ) <EOL> except Exception as err : <EOL> self . raise_exception ( str ( err ) , err . __class__ ) <EOL> else : <EOL> self . _setcache [ path ] = expr <EOL> def save_to_egg ( self , name , version , py_dir = None , src_dir = None , <EOL> src_files = None , child_objs = None , dst_dir = None , <EOL> observer = None , need_requirements = True ) : <EOL> """<STR_LIT>""" <EOL> assert name and isinstance ( name , basestring ) <EOL> assert version and isinstance ( version , basestring ) <EOL> if not version . endswith ( '<STR_LIT:.>' ) : <EOL> version += '<STR_LIT:.>' <EOL> now = datetime . datetime . now ( ) <EOL> tstamp = '<STR_LIT>' % ( now . year , now . month , now . day , now . hour , now . minute ) <EOL> version += tstamp <EOL> observer = eggobserver . EggObserver ( observer , self . _logger ) <EOL> entry_pts = [ ( self , name , _get_entry_group ( self ) ) ] <EOL> if child_objs is not None : <EOL> root_pathname = self . get_pathname ( ) <EOL> root_start = root_pathname . rfind ( '<STR_LIT:.>' ) <EOL> root_start = root_start + <NUM_LIT:1> if root_start >= <NUM_LIT:0> else <NUM_LIT:0> <EOL> root_pathname += '<STR_LIT:.>' <EOL> for child in child_objs : <EOL> pathname = child . get_pathname ( ) <EOL> if not pathname . startswith ( root_pathname ) : <EOL> msg = '<STR_LIT>' % ( pathname , root_pathname ) <EOL> observer . exception ( msg ) <EOL> self . raise_exception ( msg , RuntimeError ) <EOL> entry_pts . append ( ( child , pathname [ root_start : ] , <EOL> _get_entry_group ( child ) ) ) <EOL> parent = self . parent <EOL> self . parent = None <EOL> try : <EOL> return eggsaver . save_to_egg ( entry_pts , version , py_dir , <EOL> src_dir , src_files , dst_dir , <EOL> self . _logger , observer . observer , <EOL> need_requirements ) <EOL> except Exception : <EOL> self . reraise_exception ( info = sys . exc_info ( ) ) <EOL> finally : <EOL> self . parent = parent <EOL> def save ( self , outstream , fmt = SAVE_CPICKLE , proto = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> parent = self . parent <EOL> self . parent = None <EOL> try : <EOL> eggsaver . save ( self , outstream , fmt , proto , self . _logger ) <EOL> except Exception : <EOL> self . reraise_exception ( info = sys . exc_info ( ) ) <EOL> finally : <EOL> self . parent = parent <EOL> @ staticmethod <EOL> def load_from_eggfile ( filename , observer = None , log = None ) : <EOL> """<STR_LIT>""" <EOL> entry_group = '<STR_LIT>' <EOL> entry_name = '<STR_LIT>' <EOL> log = log or logger <EOL> return eggloader . load_from_eggfile ( filename , entry_group , entry_name , <EOL> log , observer ) <EOL> @ staticmethod <EOL> def load_from_eggpkg ( package , entry_name = None , instance_name = None , <EOL> observer = None ) : <EOL> """<STR_LIT>""" <EOL> entry_group = '<STR_LIT>' <EOL> if not entry_name : <EOL> entry_name = package <EOL> return eggloader . load_from_eggpkg ( package , entry_group , entry_name , <EOL> instance_name , logger , observer ) <EOL> @ staticmethod <EOL> def load ( instream , fmt = SAVE_CPICKLE , package = None , call_post_load = True , <EOL> name = None ) : <EOL> """<STR_LIT>""" <EOL> top = eggloader . load ( instream , fmt , package , logger ) <EOL> top . cpath_updated ( ) <EOL> if name : <EOL> top . name = name <EOL> if call_post_load : <EOL> top . parent = None <EOL> top . post_load ( ) <EOL> return top <EOL> def post_load ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _repair_traits ( ) <EOL> for name in self . list_containers ( ) : <EOL> getattr ( self , name ) . post_load ( ) <EOL> @ rbac ( '<STR_LIT>' ) <EOL> def pre_delete ( self ) : <EOL> """<STR_LIT>""" <EOL> for name in self . list_containers ( ) : <EOL> getattr ( self , name ) . pre_delete ( ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) , proxy_types = [ CTrait ] ) <EOL> def get_dyn_trait ( self , pathname , iotype = None , trait = None ) : <EOL> """<STR_LIT>""" <EOL> if pathname . startswith ( '<STR_LIT>' ) : <EOL> return None <EOL> cname , _ , restofpath = pathname . partition ( '<STR_LIT:.>' ) <EOL> if restofpath : <EOL> child = getattr ( self , cname ) <EOL> if is_instance ( child , Container ) : <EOL> return child . get_dyn_trait ( restofpath , iotype , trait ) <EOL> else : <EOL> if deep_hasattr ( child , restofpath ) : <EOL> return None <EOL> else : <EOL> trait = self . get_trait ( cname ) <EOL> if trait is not None : <EOL> if iotype is not None : <EOL> if isinstance ( trait . trait_type , Python ) : <EOL> obj = getattr ( self , cname ) <EOL> t_iotype = getattr ( obj , '<STR_LIT>' , None ) <EOL> else : <EOL> t_iotype = self . get_iotype ( cname ) <EOL> if ( iotype == '<STR_LIT>' and t_iotype not in ( '<STR_LIT>' , '<STR_LIT:state>' ) ) or ( iotype == '<STR_LIT>' and t_iotype not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:state>' , '<STR_LIT>' ) ) : <EOL> self . raise_exception ( "<STR_LIT>" % <EOL> ( pathname , _iodict [ iotype ] ) , <EOL> RuntimeError ) <EOL> return trait <EOL> elif trait is None and self . contains ( cname ) : <EOL> return None <EOL> self . raise_exception ( "<STR_LIT>" % <EOL> pathname , AttributeError ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def get_trait_typenames ( self , pathname , iotype = None ) : <EOL> """<STR_LIT>""" <EOL> if not pathname : <EOL> obj = self <EOL> else : <EOL> trait = self . get_dyn_trait ( pathname , iotype = iotype ) <EOL> if trait is None : <EOL> return [ ] <EOL> trait = trait . trait_type or trait . trait or trait <EOL> if trait . target : <EOL> trait = self . get_dyn_trait ( trait . target ) <EOL> try : <EOL> ttype = trait . trait_type <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if ttype is not None : <EOL> trait = ttype <EOL> if isinstance ( trait , Python ) : <EOL> obj = self . get ( pathname ) <EOL> else : <EOL> obj = trait <EOL> names = [ ] <EOL> Container . _bases ( type ( obj ) , names ) <EOL> return names <EOL> @ staticmethod <EOL> def _bases ( cls , names ) : <EOL> """<STR_LIT>""" <EOL> names . append ( '<STR_LIT>' % ( cls . __module__ , cls . __name__ ) ) <EOL> for base in cls . __bases__ : <EOL> Container . _bases ( base , names ) <EOL> def raise_exception ( self , msg , exception_class = Exception ) : <EOL> """<STR_LIT>""" <EOL> coords = '<STR_LIT>' <EOL> obj = self <EOL> while obj is not None : <EOL> try : <EOL> coords = obj . get_itername ( ) <EOL> except AttributeError : <EOL> try : <EOL> obj = obj . parent <EOL> except AttributeError : <EOL> break <EOL> else : <EOL> break <EOL> if coords : <EOL> full_msg = '<STR_LIT>' % ( self . get_pathname ( ) , coords , msg ) <EOL> else : <EOL> full_msg = '<STR_LIT>' % ( self . get_pathname ( ) , msg ) <EOL> raise exception_class ( full_msg ) <EOL> def reraise_exception ( self , msg = '<STR_LIT>' , info = None ) : <EOL> """<STR_LIT>""" <EOL> if info is None : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> else : <EOL> exc_type , exc_value , exc_traceback = info <EOL> if msg : <EOL> msg = '<STR_LIT>' % ( msg , exc_value ) <EOL> else : <EOL> msg = '<STR_LIT:%s>' % exc_value <EOL> prefix = '<STR_LIT>' % self . get_pathname ( ) <EOL> if not msg . startswith ( prefix ) : <EOL> msg = prefix + msg <EOL> new_exc = exc_type ( msg ) <EOL> raise exc_type , new_exc , exc_traceback <EOL> def build_trait ( self , ref_name , iotype = None , trait = None ) : <EOL> """<STR_LIT>""" <EOL> self . raise_exception ( '<STR_LIT>' , NotImplementedError ) <EOL> CLASSES_TO_PROXY . append ( Container ) <EOL> CLASSES_TO_PROXY . append ( FileRef ) <EOL> def _get_entry_group ( obj ) : <EOL> """<STR_LIT>""" <EOL> if _get_entry_group . group_map is None : <EOL> from openmdao . main . component import Component <EOL> from openmdao . main . driver import Driver <EOL> _get_entry_group . group_map = [ <EOL> ( Variable , '<STR_LIT>' ) , <EOL> ( Driver , '<STR_LIT>' ) , <EOL> ( ICaseIterator , '<STR_LIT>' ) , <EOL> ( IResourceAllocator , '<STR_LIT>' ) , <EOL> ( Component , '<STR_LIT>' ) , <EOL> ( Container , '<STR_LIT>' ) , <EOL> ] <EOL> for cls , group in _get_entry_group . group_map : <EOL> if issubclass ( cls , Interface ) : <EOL> if cls . providedBy ( obj ) : <EOL> return group <EOL> else : <EOL> if isinstance ( obj , cls ) : <EOL> return group <EOL> return None <EOL> _get_entry_group . group_map = None <EOL> def dump ( cont , recurse = False , stream = None , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> pprint . pprint ( dict ( [ ( n , str ( v ) ) <EOL> for n , v in cont . items ( recurse = recurse , <EOL> ** metadata ) ] ) , <EOL> stream ) <EOL> def find_name ( parent , obj ) : <EOL> """<STR_LIT>""" <EOL> for name , val in parent . __dict__ . items ( ) : <EOL> if val is obj : <EOL> return name <EOL> return '<STR_LIT>' <EOL> def get_default_name ( obj , scope ) : <EOL> """<STR_LIT>""" <EOL> classname = obj . __class__ . __name__ . lower ( ) <EOL> if scope is None : <EOL> sdict = { } <EOL> else : <EOL> sdict = scope . __dict__ <EOL> ver = <NUM_LIT:1> <EOL> while '<STR_LIT>' % ( classname , ver ) in sdict : <EOL> ver += <NUM_LIT:1> <EOL> return '<STR_LIT>' % ( classname , ver ) <EOL> def find_trait_and_value ( obj , pathname ) : <EOL> """<STR_LIT>""" <EOL> names = pathname . split ( '<STR_LIT:.>' ) <EOL> for name in names [ : - <NUM_LIT:1> ] : <EOL> obj = getattr ( obj , name ) <EOL> if is_instance ( obj , Container ) : <EOL> objtrait = obj . get_trait ( names [ - <NUM_LIT:1> ] ) <EOL> elif isinstance ( obj , HasTraits ) : <EOL> objtrait = obj . trait ( names [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> objtrait = None <EOL> return ( objtrait , getattr ( obj , names [ - <NUM_LIT:1> ] ) ) <EOL> def create_io_traits ( cont , obj_info , iotype = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj_info , ( basestring , tuple ) ) : <EOL> it = [ obj_info ] <EOL> else : <EOL> it = obj_info <EOL> for entry in it : <EOL> iostat = iotype <EOL> trait = None <EOL> if isinstance ( entry , basestring ) : <EOL> ref_name = entry <EOL> name = entry . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) <EOL> elif isinstance ( entry , tuple ) : <EOL> ref_name = entry [ <NUM_LIT:0> ] <EOL> name = entry [ <NUM_LIT:1> ] or ref_name . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) <EOL> try : <EOL> iostat = entry [ <NUM_LIT:2> ] <EOL> trait = entry [ <NUM_LIT:3> ] <EOL> except IndexError : <EOL> pass <EOL> else : <EOL> cont . raise_exception ( '<STR_LIT>' % entry , <EOL> RuntimeError ) <EOL> if '<STR_LIT:.>' in name : <EOL> cont . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % name , NameError ) <EOL> newtrait = cont . get_trait ( name ) <EOL> if newtrait is not None : <EOL> cont . raise_exception ( <EOL> "<STR_LIT>" % name , <EOL> RuntimeError ) <EOL> if not cont . contains ( ref_name ) : <EOL> cont . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % ref_name , AttributeError ) <EOL> cont . add_trait ( name , cont . build_trait ( ref_name , iostat , trait ) ) </s>
