<s> '''<STR_LIT>''' <EOL> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> import types <EOL> import numpy <EOL> import copy <EOL> from . utils import Closure , fixName , isValidName <EOL> from . import site_config <EOL> class Group ( object ) : <EOL> """<STR_LIT>""" <EOL> __private = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , name = None , ** kws ) : <EOL> if name is None : <EOL> name = hex ( id ( self ) ) <EOL> self . __name__ = name <EOL> for key , val in kws . items ( ) : <EOL> setattr ( self , key , val ) <EOL> def __len__ ( self ) : <EOL> return max ( <NUM_LIT:1> , len ( dir ( self ) ) - <NUM_LIT:1> ) <EOL> def __repr__ ( self ) : <EOL> if self . __name__ is not None : <EOL> return '<STR_LIT>' % self . __name__ <EOL> return '<STR_LIT>' <EOL> def __copy__ ( self ) : <EOL> out = Group ( ) <EOL> for k , v in self . __dict__ . items ( ) : <EOL> if k != '<STR_LIT>' : <EOL> setattr ( out , k , copy . copy ( v ) ) <EOL> return out <EOL> def __deepcopy__ ( self , memo ) : <EOL> out = Group ( ) <EOL> for k , v in self . __dict__ . items ( ) : <EOL> if k != '<STR_LIT>' : <EOL> setattr ( out , k , copy . deepcopy ( v , memo ) ) <EOL> return out <EOL> def __id__ ( self ) : <EOL> return id ( self ) <EOL> def __dir__ ( self ) : <EOL> "<STR_LIT>" <EOL> cls_members = [ ] <EOL> cname = self . __class__ . __name__ <EOL> if cname != '<STR_LIT>' and hasattr ( self , '<STR_LIT>' ) : <EOL> cls_members = dir ( self . __class__ ) <EOL> dict_keys = [ key for key in self . __dict__ if key not in cls_members ] <EOL> return [ key for key in cls_members + dict_keys <EOL> if ( not key . startswith ( '<STR_LIT>' ) and <EOL> not key . startswith ( '<STR_LIT>' ) and <EOL> not key . startswith ( '<STR_LIT>' % cname ) and <EOL> not ( key . startswith ( '<STR_LIT>' ) and key . endswith ( '<STR_LIT>' ) ) and <EOL> key not in self . __private ) ] <EOL> def _subgroups ( self ) : <EOL> "<STR_LIT>" <EOL> return [ k for k in self . _members ( ) if isgroup ( self . __dict__ [ k ] ) ] <EOL> def _members ( self ) : <EOL> "<STR_LIT>" <EOL> r = { } <EOL> for key in self . __dir__ ( ) : <EOL> if key in self . __dict__ : <EOL> r [ key ] = self . __dict__ [ key ] <EOL> return r <EOL> def isgroup ( grp , * args ) : <EOL> """<STR_LIT>""" <EOL> ret = isinstance ( grp , Group ) <EOL> if ret and len ( args ) > <NUM_LIT:0> : <EOL> try : <EOL> ret = all ( [ hasattr ( grp , a ) for a in args ] ) <EOL> except TypeError : <EOL> return False <EOL> return ret <EOL> class InvalidName : <EOL> """<STR_LIT>""" <EOL> pass <EOL> GroupDocs = { } <EOL> GroupDocs [ '<STR_LIT>' ] = """<STR_LIT>""" <EOL> GroupDocs [ '<STR_LIT>' ] = """<STR_LIT>""" <EOL> GroupDocs [ '<STR_LIT>' ] = """<STR_LIT>""" <EOL> class SymbolTable ( Group ) : <EOL> """<STR_LIT>""" <EOL> top_group = '<STR_LIT>' <EOL> core_groups = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> __invalid_name = InvalidName ( ) <EOL> _private = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , larch = None ) : <EOL> Group . __init__ ( self , name = self . top_group ) <EOL> self . _larch = larch <EOL> self . _sys = None <EOL> setattr ( self , self . top_group , self ) <EOL> for gname in self . core_groups : <EOL> thisgroup = Group ( name = gname ) <EOL> if gname in GroupDocs : <EOL> thisgroup . __doc__ = GroupDocs [ gname ] <EOL> setattr ( self , gname , thisgroup ) <EOL> self . _sys . frames = [ ] <EOL> self . _sys . searchGroups = [ self . top_group ] <EOL> self . _sys . path = [ '<STR_LIT:.>' ] <EOL> self . _sys . localGroup = self <EOL> self . _sys . valid_commands = [ ] <EOL> self . _sys . moduleGroup = self <EOL> self . _sys . paramGroup = None <EOL> self . _sys . __cache__ = [ None ] * <NUM_LIT:5> <EOL> for g in self . core_groups : <EOL> self . _sys . searchGroups . append ( g ) <EOL> self . _sys . core_groups = tuple ( self . _sys . searchGroups [ : ] ) <EOL> self . __callbacks = { } <EOL> orig_sys_path = sys . path [ : ] <EOL> if site_config . modules_path is not None : <EOL> for idir in site_config . modules_path : <EOL> idirfull = os . path . abspath ( idir ) <EOL> if idirfull not in self . _sys . path and os . path . exists ( idirfull ) : <EOL> self . _sys . path . append ( idirfull ) <EOL> sys . path = self . _sys . path [ : ] <EOL> for idir in orig_sys_path : <EOL> idirfull = os . path . abspath ( idir ) <EOL> if idirfull not in sys . path : <EOL> sys . path . append ( idirfull ) <EOL> self . _sys . modules = { '<STR_LIT>' : self } <EOL> for gname in self . core_groups : <EOL> self . _sys . modules [ gname ] = getattr ( self , gname ) <EOL> self . _fix_searchGroups ( ) <EOL> self . _sys . config = Group ( home_dir = site_config . home_dir , <EOL> history_file = site_config . history_file , <EOL> init_files = site_config . init_files , <EOL> modules_path = site_config . modules_path , <EOL> larchdir = site_config . larchdir , <EOL> user_larchdir = site_config . usr_larchdir , <EOL> ) <EOL> def save_frame ( self ) : <EOL> "<STR_LIT>" <EOL> self . _sys . frames . append ( ( self . _sys . localGroup , <EOL> self . _sys . moduleGroup ) ) <EOL> def restore_frame ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> lgrp , mgrp = self . _sys . frames . pop ( ) <EOL> self . _sys . localGroup = lgrp <EOL> self . _sys . moduleGroup = mgrp <EOL> self . _fix_searchGroups ( ) <EOL> except : <EOL> pass <EOL> def set_frame ( self , groups ) : <EOL> "<STR_LIT>" <EOL> self . _sys . localGroup , self . _sys . moduleGroup = groups <EOL> self . _fix_searchGroups ( ) <EOL> def _fix_searchGroups ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> sys = self . _sys <EOL> cache = sys . __cache__ <EOL> if len ( cache ) < <NUM_LIT:5> : <EOL> cache = [ None ] * <NUM_LIT:5> <EOL> if ( sys . paramGroup == cache [ <NUM_LIT:0> ] and <EOL> sys . localGroup == cache [ <NUM_LIT:1> ] and <EOL> sys . moduleGroup == cache [ <NUM_LIT:2> ] and <EOL> sys . searchGroups == cache [ <NUM_LIT:3> ] and <EOL> cache [ <NUM_LIT:4> ] is not None and not force ) : <EOL> return cache [ <NUM_LIT:4> ] <EOL> if sys . moduleGroup is None : <EOL> sys . moduleGroup = self . top_group <EOL> if sys . localGroup is None : <EOL> sys . localGroup = self . moduleGroup <EOL> if not isgroup ( sys . paramGroup ) : sys . paramGroup = None <EOL> cache [ <NUM_LIT:0> ] = sys . paramGroup <EOL> cache [ <NUM_LIT:1> ] = sys . localGroup <EOL> cache [ <NUM_LIT:2> ] = sys . moduleGroup <EOL> snames = [ ] <EOL> sgroups = [ ] <EOL> for grp in ( sys . localGroup , sys . paramGroup , sys . moduleGroup ) : <EOL> if grp is not None and grp not in sgroups : <EOL> sgroups . append ( grp ) <EOL> snames . append ( grp . __name__ ) <EOL> sysmods = list ( self . _sys . modules . values ( ) ) <EOL> searchGroups = sys . searchGroups [ : ] <EOL> searchGroups . extend ( self . _sys . core_groups ) <EOL> for name in searchGroups : <EOL> grp = None <EOL> if name in self . _sys . modules : <EOL> grp = self . _sys . modules [ name ] <EOL> elif hasattr ( self , name ) : <EOL> gtest = getattr ( self , name ) <EOL> if isinstance ( gtest , Group ) : <EOL> grp = gtest <EOL> elif '<STR_LIT:.>' in name : <EOL> parent , child = name . split ( '<STR_LIT:.>' ) <EOL> for sgrp in sysmods : <EOL> if ( parent == sgrp . __name__ and <EOL> hasattr ( sgrp , child ) ) : <EOL> grp = getattr ( sgrp , child ) <EOL> break <EOL> else : <EOL> for sgrp in sysmods : <EOL> if hasattr ( sgrp , name ) : <EOL> grp = getattr ( sgrp , name ) <EOL> break <EOL> if grp is not None and grp not in sgroups : <EOL> sgroups . append ( grp ) <EOL> snames . append ( name ) <EOL> self . _sys . searchGroups = cache [ <NUM_LIT:3> ] = snames [ : ] <EOL> sys . searchGroupObjects = cache [ <NUM_LIT:4> ] = sgroups [ : ] <EOL> return sys . searchGroupObjects <EOL> def get_parentpath ( self , sym ) : <EOL> """<STR_LIT>""" <EOL> obj = self . _lookup ( sym ) <EOL> if obj is None : <EOL> return <EOL> out = [ ] <EOL> for s in reversed ( self . __parents ) : <EOL> if s . __name__ is not '<STR_LIT>' or '<STR_LIT>' not in out : <EOL> out . append ( s . __name__ ) <EOL> out . reverse ( ) <EOL> return '<STR_LIT:.>' . join ( out ) <EOL> def _lookup ( self , name = None , create = False ) : <EOL> """<STR_LIT>""" <EOL> debug = False <EOL> if debug : print ( '<STR_LIT>' , name ) <EOL> searchGroups = self . _fix_searchGroups ( ) <EOL> self . __parents = [ ] <EOL> if self not in searchGroups : <EOL> searchGroups . append ( self ) <EOL> def public_attr ( grp , name ) : <EOL> return ( hasattr ( grp , name ) and <EOL> not ( grp is self and name in self . _private ) ) <EOL> parts = name . split ( '<STR_LIT:.>' ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> for grp in searchGroups : <EOL> if public_attr ( grp , name ) : <EOL> self . __parents . append ( grp ) <EOL> return getattr ( grp , name ) <EOL> parts . reverse ( ) <EOL> top = parts . pop ( ) <EOL> out = self . __invalid_name <EOL> if top == self . top_group : <EOL> out = self <EOL> else : <EOL> for grp in searchGroups : <EOL> if public_attr ( grp , top ) : <EOL> self . __parents . append ( grp ) <EOL> out = getattr ( grp , top ) <EOL> if out is self . __invalid_name : <EOL> raise NameError ( "<STR_LIT>" % name ) <EOL> if len ( parts ) == <NUM_LIT:0> : <EOL> return out <EOL> while parts : <EOL> prt = parts . pop ( ) <EOL> if hasattr ( out , prt ) : <EOL> out = getattr ( out , prt ) <EOL> elif create : <EOL> val = None <EOL> if len ( parts ) > <NUM_LIT:0> : <EOL> val = Group ( name = prt ) <EOL> setattr ( out , prt , val ) <EOL> out = getattr ( out , prt ) <EOL> else : <EOL> raise LookupError ( <EOL> "<STR_LIT>" % ( prt , out ) ) <EOL> return out <EOL> def has_symbol ( self , symname ) : <EOL> try : <EOL> g = self . get_symbol ( symname ) <EOL> return True <EOL> except ( LookupError , NameError , ValueError ) : <EOL> return False <EOL> def has_group ( self , gname ) : <EOL> try : <EOL> g = self . get_group ( gname ) <EOL> return True <EOL> except ( NameError , LookupError ) : <EOL> return False <EOL> def isgroup ( self , sym ) : <EOL> "<STR_LIT>" <EOL> return isgroup ( sym ) <EOL> def get_group ( self , gname ) : <EOL> "<STR_LIT>" <EOL> sym = self . _lookup ( gname , create = False ) <EOL> if isgroup ( sym ) : <EOL> return sym <EOL> else : <EOL> raise LookupError ( <EOL> "<STR_LIT>" % ( gname ) ) <EOL> def create_group ( self , ** kw ) : <EOL> "<STR_LIT>" <EOL> return Group ( ** kw ) <EOL> def new_group ( self , name , ** kws ) : <EOL> name = fixName ( name ) <EOL> grp = Group ( __name__ = name , ** kws ) <EOL> self . set_symbol ( name , value = grp ) <EOL> return grp <EOL> def get_symbol ( self , sym , create = False ) : <EOL> "<STR_LIT>" <EOL> return self . _lookup ( sym , create = create ) <EOL> def set_symbol ( self , name , value = None , group = None ) : <EOL> "<STR_LIT>" <EOL> grp = self . _sys . localGroup <EOL> if group is not None : <EOL> grp = self . get_group ( group ) <EOL> names = [ ] <EOL> for n in name . split ( '<STR_LIT:.>' ) : <EOL> if not isValidName ( n ) : <EOL> raise SyntaxError ( "<STR_LIT>" % n ) <EOL> names . append ( n ) <EOL> child = names . pop ( ) <EOL> for nam in names : <EOL> if hasattr ( grp , nam ) : <EOL> grp = getattr ( grp , nam ) <EOL> if not isgroup ( grp ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % grp ) <EOL> else : <EOL> setattr ( grp , nam , Group ( ) ) <EOL> setattr ( grp , child , value ) <EOL> if ( grp , child ) in self . __callbacks : <EOL> for func , args , kws in self . __callbacks [ ( grp , child ) ] : <EOL> kws . update ( { '<STR_LIT>' : grp , '<STR_LIT:value>' : value , <EOL> '<STR_LIT>' : child } ) <EOL> func ( * args , ** kws ) <EOL> return getattr ( grp , child ) <EOL> def del_symbol ( self , name ) : <EOL> "<STR_LIT>" <EOL> sym = self . _lookup ( name , create = False ) <EOL> parent , child = self . get_parent ( name ) <EOL> self . clear_callbacks ( name ) <EOL> delattr ( parent , child ) <EOL> def clear_callbacks ( self , name , index = None ) : <EOL> """<STR_LIT>""" <EOL> parent , child = self . get_parent ( name ) <EOL> if child is not None and ( parent , child ) in self . __callbacks : <EOL> if index is not None and index <= len ( self . __callbacks [ ( parent , child ) ] ) : <EOL> self . __callbacks [ ( parent , child ) ] . pop ( index ) <EOL> else : <EOL> while self . __callbacks [ ( parent , child ) ] : <EOL> self . __callbacks [ ( parent , child ) ] . pop ( ) <EOL> def add_callback ( self , name , func , args = None , kws = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> var = self . get_symbol ( name ) <EOL> except NameError : <EOL> raise NameError ( <EOL> "<STR_LIT>" % ( name ) ) <EOL> key = self . get_parent ( name ) <EOL> if key not in self . __callbacks : <EOL> self . __callbacks [ key ] = [ ] <EOL> if args is None : args = ( ) <EOL> if kws is None : kws = { } <EOL> self . __callbacks [ key ] . append ( ( func , args , kws ) ) <EOL> def get_parent ( self , name ) : <EOL> """<STR_LIT>""" <EOL> tnam = name . split ( '<STR_LIT:.>' ) <EOL> if len ( tnam ) < <NUM_LIT:1> or name == self . top_group : <EOL> return ( self , None ) <EOL> child = tnam . pop ( ) <EOL> sym = self <EOL> if len ( tnam ) > <NUM_LIT:0> : <EOL> sym = self . _lookup ( '<STR_LIT:.>' . join ( tnam ) ) <EOL> return sym , child <EOL> def add_plugin ( self , plugin , on_error , ** kws ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( plugin , types . ModuleType ) : <EOL> on_error ( "<STR_LIT>" % repr ( plugin ) ) <EOL> registrar = getattr ( plugin , '<STR_LIT>' , None ) <EOL> if registrar is None : <EOL> return <EOL> groupname , syms = registrar ( ) <EOL> if not self . has_group ( groupname ) : <EOL> self . new_group ( groupname ) <EOL> if groupname not in self . _sys . searchGroups : <EOL> self . _sys . searchGroups . append ( groupname ) <EOL> self . _fix_searchGroups ( force = True ) <EOL> for key , val in syms . items ( ) : <EOL> if hasattr ( val , '<STR_LIT>' ) : <EOL> kws . update ( { '<STR_LIT>' : val , '<STR_LIT>' : key } ) <EOL> nvars = val . __code__ . co_argcount <EOL> if ( ( val . __code__ . co_flags & <NUM_LIT:8> != <NUM_LIT:0> ) or <EOL> '<STR_LIT>' in val . __code__ . co_varnames [ : nvars ] ) : <EOL> kws . update ( { '<STR_LIT>' : self . _larch } ) <EOL> val = Closure ( ** kws ) <EOL> self . set_symbol ( "<STR_LIT>" % ( groupname , key ) , val ) <EOL> plugin_init = getattr ( plugin , '<STR_LIT>' , None ) <EOL> if plugin_init is not None : <EOL> plugin_init ( _larch = self . _larch ) <EOL> def show_group ( self , groupname ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> try : <EOL> group = self . get_group ( groupname ) <EOL> except ( NameError , LookupError ) : <EOL> return '<STR_LIT>' % groupname <EOL> title = group . __name__ <EOL> members = dir ( group ) <EOL> out = [ '<STR_LIT>' % ( title , len ( members ) ) ] <EOL> for item in members : <EOL> obj = getattr ( group , item ) <EOL> dval = None <EOL> if isinstance ( obj , numpy . ndarray ) : <EOL> if len ( obj ) > <NUM_LIT:10> or len ( obj . shape ) > <NUM_LIT:1> : <EOL> dval = "<STR_LIT>" % ( repr ( obj . shape ) , <EOL> repr ( obj . dtype ) ) <EOL> if dval is None : <EOL> dval = repr ( obj ) <EOL> out . append ( '<STR_LIT>' % ( item , dval ) ) <EOL> self . _larch . writer . write ( "<STR_LIT>" % '<STR_LIT:\n>' . join ( out ) ) </s>
