<s> """<STR_LIT>""" <EOL> from builtins import object <EOL> from future . utils import raise_ <EOL> import sys <EOL> from importlib import import_module <EOL> from inspect import trace <EOL> from django . conf import settings <EOL> from evennia . utils import logger , utils <EOL> from evennia . commands . cmdset import CmdSet <EOL> from evennia . server . models import ServerConfig <EOL> from django . utils . translation import ugettext as _ <EOL> __all__ = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> _CACHED_CMDSETS = { } <EOL> _CMDSET_PATHS = utils . make_iter ( settings . CMDSET_PATHS ) <EOL> class _ErrorCmdSet ( CmdSet ) : <EOL> """<STR_LIT>""" <EOL> key = "<STR_LIT>" <EOL> errmessage = "<STR_LIT>" <EOL> class _EmptyCmdSet ( CmdSet ) : <EOL> """<STR_LIT>""" <EOL> key = "<STR_LIT>" <EOL> priority = - <NUM_LIT> <EOL> mergetype = "<STR_LIT>" <EOL> def import_cmdset ( path , cmdsetobj , emit_to_obj = None , no_logging = False ) : <EOL> """<STR_LIT>""" <EOL> python_paths = [ path ] + [ "<STR_LIT>" % ( prefix , path ) <EOL> for prefix in _CMDSET_PATHS if not path . startswith ( prefix ) ] <EOL> errstring = "<STR_LIT>" <EOL> for python_path in python_paths : <EOL> if "<STR_LIT:.>" in path : <EOL> modpath , classname = python_path . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> else : <EOL> raise ImportError ( "<STR_LIT>" % path ) <EOL> try : <EOL> cmdsetclass = _CACHED_CMDSETS . get ( python_path , None ) <EOL> if not cmdsetclass : <EOL> try : <EOL> module = import_module ( modpath , package = "<STR_LIT>" ) <EOL> except ImportError : <EOL> if len ( trace ( ) ) > <NUM_LIT:2> : <EOL> exc = sys . exc_info ( ) <EOL> raise_ ( exc [ <NUM_LIT:1> ] , None , exc [ <NUM_LIT:2> ] ) <EOL> else : <EOL> errstring += _ ( "<STR_LIT>" % python_path ) <EOL> continue <EOL> try : <EOL> cmdsetclass = getattr ( module , classname ) <EOL> except AttributeError : <EOL> if len ( trace ( ) ) > <NUM_LIT:2> : <EOL> exc = sys . exc_info ( ) <EOL> raise_ ( exc [ <NUM_LIT:1> ] , None , exc [ <NUM_LIT:2> ] ) <EOL> else : <EOL> errstring += _ ( "<STR_LIT>" % python_path ) <EOL> continue <EOL> _CACHED_CMDSETS [ python_path ] = cmdsetclass <EOL> if callable ( cmdsetclass ) : <EOL> cmdsetclass = cmdsetclass ( cmdsetobj ) <EOL> errstring = "<STR_LIT>" <EOL> return cmdsetclass <EOL> except ImportError as e : <EOL> logger . log_trace ( ) <EOL> errstring += _ ( "<STR_LIT>" ) <EOL> errstring = errstring . format ( path = python_path , error = e ) <EOL> break <EOL> except KeyError : <EOL> logger . log_trace ( ) <EOL> errstring += _ ( "<STR_LIT>" ) <EOL> errstring = errstring . format ( classname = classname , path = python_path ) <EOL> break <EOL> except SyntaxError as e : <EOL> logger . log_trace ( ) <EOL> errstring += _ ( "<STR_LIT>" ) <EOL> errstring = errstring . format ( path = python_path , error = e ) <EOL> break <EOL> except Exception as e : <EOL> logger . log_trace ( ) <EOL> errstring += _ ( "<STR_LIT>" ) <EOL> errstring = errstring . format ( path = python_path , error = e ) <EOL> break <EOL> if errstring : <EOL> errstring = errstring . strip ( ) <EOL> if not no_logging : <EOL> logger . log_err ( errstring ) <EOL> if emit_to_obj and not ServerConfig . objects . conf ( "<STR_LIT>" ) : <EOL> emit_to_obj . msg ( errstring ) <EOL> err_cmdset = _ErrorCmdSet ( ) <EOL> err_cmdset . errmessage = errstring + _ ( "<STR_LIT>" ) <EOL> return err_cmdset <EOL> class CmdSetHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , obj , init_true = True ) : <EOL> """<STR_LIT>""" <EOL> self . obj = obj <EOL> self . key = None <EOL> self . current = None <EOL> self . cmdset_stack = [ _EmptyCmdSet ( cmdsetobj = self . obj ) ] <EOL> self . mergetype_stack = [ "<STR_LIT>" ] <EOL> self . permanent_paths = [ "<STR_LIT>" ] <EOL> if init_true : <EOL> self . update ( init_mode = True ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> string = "<STR_LIT>" <EOL> mergelist = [ ] <EOL> if len ( self . cmdset_stack ) > <NUM_LIT:1> : <EOL> for snum , cmdset in enumerate ( self . cmdset_stack ) : <EOL> mergetype = self . mergetype_stack [ snum ] <EOL> permstring = "<STR_LIT>" <EOL> if cmdset . permanent : <EOL> permstring = "<STR_LIT>" <EOL> if mergetype != cmdset . mergetype : <EOL> mergetype = "<STR_LIT>" % ( mergetype ) <EOL> string += "<STR_LIT>" % ( snum , cmdset . key , mergetype , <EOL> cmdset . priority , permstring , cmdset ) <EOL> mergelist . append ( str ( snum ) ) <EOL> string += "<STR_LIT:\n>" <EOL> mergetype = self . mergetype_stack [ - <NUM_LIT:1> ] <EOL> if mergetype != self . current . mergetype : <EOL> merged_on = self . cmdset_stack [ - <NUM_LIT:2> ] . key <EOL> mergetype = _ ( "<STR_LIT>" ) <EOL> mergetype = mergetype . format ( mergetype = mergetype , cmdset = merged_on ) <EOL> if mergelist : <EOL> tmpstring = _ ( "<STR_LIT>" ) <EOL> string += tmpstring . format ( mergelist = "<STR_LIT:+>" . join ( mergelist ) , <EOL> mergetype = mergetype , prio = self . current . priority , <EOL> current = self . current ) <EOL> else : <EOL> permstring = "<STR_LIT>" <EOL> if self . current . permanent : <EOL> permstring = "<STR_LIT>" <EOL> tmpstring = _ ( "<STR_LIT>" ) <EOL> string += tmpstring . format ( key = self . current . key , mergetype = mergetype , <EOL> prio = self . current . priority , <EOL> permstring = permstring , <EOL> keylist = "<STR_LIT:U+002CU+0020>" . join ( cmd . key for <EOL> cmd in sorted ( self . current , key = lambda o : o . key ) ) ) <EOL> return string . strip ( ) <EOL> def _import_cmdset ( self , cmdset_path , emit_to_obj = None ) : <EOL> """<STR_LIT>""" <EOL> if not emit_to_obj : <EOL> emit_to_obj = self . obj <EOL> return import_cmdset ( cmdset_path , self . obj , emit_to_obj ) <EOL> def update ( self , init_mode = False ) : <EOL> """<STR_LIT>""" <EOL> if init_mode : <EOL> storage = self . obj . cmdset_storage <EOL> if storage : <EOL> self . cmdset_stack = [ ] <EOL> for pos , path in enumerate ( storage ) : <EOL> if pos == <NUM_LIT:0> and not path : <EOL> self . cmdset_stack = [ _EmptyCmdSet ( cmdsetobj = self . obj ) ] <EOL> elif path : <EOL> cmdset = self . _import_cmdset ( path ) <EOL> if cmdset : <EOL> cmdset . permanent = cmdset . key != '<STR_LIT>' <EOL> self . cmdset_stack . append ( cmdset ) <EOL> new_current = None <EOL> self . mergetype_stack = [ ] <EOL> for cmdset in self . cmdset_stack : <EOL> try : <EOL> new_current = cmdset + new_current <EOL> except TypeError : <EOL> continue <EOL> self . mergetype_stack . append ( new_current . actual_mergetype ) <EOL> self . current = new_current <EOL> def add ( self , cmdset , emit_to_obj = None , permanent = False , default_cmdset = False ) : <EOL> """<STR_LIT>""" <EOL> if not ( isinstance ( cmdset , basestring ) or utils . inherits_from ( cmdset , CmdSet ) ) : <EOL> string = _ ( "<STR_LIT>" ) <EOL> raise Exception ( string ) <EOL> if callable ( cmdset ) : <EOL> cmdset = cmdset ( self . obj ) <EOL> elif isinstance ( cmdset , basestring ) : <EOL> cmdset = self . _import_cmdset ( cmdset ) <EOL> if cmdset and cmdset . key != '<STR_LIT>' : <EOL> cmdset . permanent = permanent <EOL> if permanent and cmdset . key != '<STR_LIT>' : <EOL> storage = self . obj . cmdset_storage or [ "<STR_LIT>" ] <EOL> if default_cmdset : <EOL> storage [ <NUM_LIT:0> ] = cmdset . path <EOL> else : <EOL> storage . append ( cmdset . path ) <EOL> self . obj . cmdset_storage = storage <EOL> if default_cmdset : <EOL> self . cmdset_stack [ <NUM_LIT:0> ] = cmdset <EOL> else : <EOL> self . cmdset_stack . append ( cmdset ) <EOL> self . update ( ) <EOL> def add_default ( self , cmdset , emit_to_obj = None , permanent = True ) : <EOL> """<STR_LIT>""" <EOL> self . add ( cmdset , emit_to_obj = emit_to_obj , permanent = permanent , default_cmdset = True ) <EOL> def remove ( self , cmdset = None , default_cmdset = False ) : <EOL> """<STR_LIT>""" <EOL> if default_cmdset : <EOL> if self . cmdset_stack : <EOL> cmdset = self . cmdset_stack [ <NUM_LIT:0> ] <EOL> if cmdset . permanent : <EOL> storage = self . obj . cmdset_storage or [ "<STR_LIT>" ] <EOL> storage [ <NUM_LIT:0> ] = "<STR_LIT>" <EOL> self . obj . cmdset_storage = storage <EOL> self . cmdset_stack [ <NUM_LIT:0> ] = _EmptyCmdSet ( cmdsetobj = self . obj ) <EOL> else : <EOL> self . cmdset_stack = [ _EmptyCmdSet ( cmdsetobj = self . obj ) ] <EOL> self . update ( ) <EOL> return <EOL> if len ( self . cmdset_stack ) < <NUM_LIT:2> : <EOL> return <EOL> if not cmdset : <EOL> cmdset = self . cmdset_stack . pop ( ) <EOL> if cmdset . permanent : <EOL> storage = self . obj . cmdset_storage <EOL> storage . pop ( ) <EOL> self . obj . cmdset_storage = storage <EOL> else : <EOL> if callable ( cmdset ) and hasattr ( cmdset , '<STR_LIT:path>' ) : <EOL> delcmdsets = [ cset for cset in self . cmdset_stack [ <NUM_LIT:1> : ] <EOL> if cset . path == cmdset . path ] <EOL> else : <EOL> delcmdsets = [ cset for cset in self . cmdset_stack [ <NUM_LIT:1> : ] <EOL> if cset . path == cmdset or cset . key == cmdset ] <EOL> storage = [ ] <EOL> if any ( cset . permanent for cset in delcmdsets ) : <EOL> storage = self . obj . cmdset_storage <EOL> updated = False <EOL> for cset in delcmdsets : <EOL> if cset . permanent : <EOL> try : <EOL> storage . remove ( cset . path ) <EOL> updated = True <EOL> except ValueError : <EOL> pass <EOL> if updated : <EOL> self . obj . cmdset_storage = storage <EOL> for cset in delcmdsets : <EOL> try : <EOL> self . cmdset_stack . remove ( cset ) <EOL> except ValueError : <EOL> pass <EOL> self . update ( ) <EOL> delete = remove <EOL> def remove_default ( self ) : <EOL> """<STR_LIT>""" <EOL> self . remove ( default_cmdset = True ) <EOL> delete_default = remove_default <EOL> def all ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . cmdset_stack <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cmdset_stack = [ self . cmdset_stack [ <NUM_LIT:0> ] ] <EOL> storage = self . obj . cmdset_storage <EOL> if storage : <EOL> storage = storage [ <NUM_LIT:0> ] <EOL> self . obj . cmdset_storage = storage <EOL> self . update ( ) <EOL> def has_cmdset ( self , cmdset_key , must_be_default = False ) : <EOL> """<STR_LIT>""" <EOL> if must_be_default : <EOL> return self . cmdset_stack and self . cmdset_stack [ <NUM_LIT:0> ] . key == cmdset_key <EOL> else : <EOL> return any ( [ cmdset . key == cmdset_key for cmdset in self . cmdset_stack ] ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> new_cmdset_stack = [ ] <EOL> for cmdset in self . cmdset_stack : <EOL> if cmdset . key == "<STR_LIT>" : <EOL> new_cmdset_stack . append ( cmdset ) <EOL> else : <EOL> new_cmdset_stack . append ( self . _import_cmdset ( cmdset . path ) ) <EOL> self . cmdset_stack = new_cmdset_stack <EOL> self . update ( ) </s>
