<s> from metakernel import Magic , option <EOL> import inspect <EOL> import ast <EOL> import os <EOL> class MacroMagic ( Magic ) : <EOL> macros = { "<STR_LIT>" : """<STR_LIT>""" } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( MacroMagic , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _load_macros ( ) <EOL> @ option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> @ option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> @ option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> def line_macro ( self , name , delete = False , list = False , show = False ) : <EOL> """<STR_LIT>""" <EOL> if list : <EOL> self . _list_macros ( name ) <EOL> elif show : <EOL> retval = "<STR_LIT>" % name <EOL> if name in self . macros : <EOL> retval += self . macros [ name ] <EOL> elif name in self . learned : <EOL> retval += self . learned [ name ] <EOL> self . kernel . Print ( retval ) <EOL> elif name in self . learned : <EOL> if delete : <EOL> del self . learned [ name ] <EOL> self . _save_macros ( ) <EOL> else : <EOL> self . code = self . code . strip ( ) <EOL> if self . code : <EOL> self . code += "<STR_LIT:\n>" <EOL> self . code += self . learned [ name ] <EOL> elif name in self . macros : <EOL> if delete : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> self . code = self . code . strip ( ) <EOL> if self . code : <EOL> self . code += "<STR_LIT:\n>" <EOL> self . code += self . macros [ name ] <EOL> elif name == "<STR_LIT>" : <EOL> self . _list_macros ( ) <EOL> else : <EOL> self . _list_macros ( retval = "<STR_LIT>" % name , <EOL> error = True ) <EOL> self . evaluate = True <EOL> def _list_macros ( self , name = "<STR_LIT:all>" , retval = "<STR_LIT>" , error = False ) : <EOL> retval += "<STR_LIT>" <EOL> if name in [ "<STR_LIT:all>" , "<STR_LIT>" ] : <EOL> retval += "<STR_LIT>" <EOL> for macro in sorted ( self . macros . keys ( ) ) : <EOL> retval += "<STR_LIT>" % macro <EOL> if name in [ "<STR_LIT:all>" , "<STR_LIT>" ] : <EOL> retval += "<STR_LIT>" <EOL> for macro in sorted ( self . learned . keys ( ) ) : <EOL> retval += "<STR_LIT>" % macro <EOL> if error : <EOL> self . kernel . Error ( retval ) <EOL> else : <EOL> self . kernel . Print ( retval ) <EOL> def _load_macros ( self ) : <EOL> self . learned = { } <EOL> local_macros_dir = self . kernel . get_local_magics_dir ( ) <EOL> files = [ os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , "<STR_LIT>" ) , <EOL> os . path . join ( os . path . dirname ( os . path . abspath ( inspect . getfile ( self . __class__ ) ) ) , "<STR_LIT>" ) , <EOL> os . path . join ( local_macros_dir , "<STR_LIT>" ) ] <EOL> for macro_file in files : <EOL> try : <EOL> data = ast . literal_eval ( open ( macro_file ) . read ( ) ) <EOL> except : <EOL> continue <EOL> self . learned . update ( data ) <EOL> def _save_macros ( self ) : <EOL> local_macros_dir = self . kernel . get_local_magics_dir ( ) <EOL> filename = os . path . join ( local_macros_dir , "<STR_LIT>" ) <EOL> open ( filename , "<STR_LIT:w>" ) . write ( str ( self . learned ) ) <EOL> def cell_macro ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . learned [ name ] = self . code <EOL> self . _save_macros ( ) <EOL> self . evaluate = False <EOL> def register_magics ( kernel ) : <EOL> kernel . register_magics ( MacroMagic ) </s>
