<s> """<STR_LIT>""" <EOL> import os , sys <EOL> from datetime import datetime <EOL> import dir_watcher <EOL> import inspect <EOL> from IPython . utils . coloransi import TermColors as color <EOL> class PluginManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , plugin_callback , plugin_dir = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . plugin_callback = plugin_callback <EOL> self . plugin_dir = plugin_dir <EOL> self . load_all_plugins ( ) <EOL> self . watcher = dir_watcher . DirWatcher ( self . plugin_path ) <EOL> self . watcher . register_callbacks ( self . on_created , self . on_modified , self . on_deleted ) <EOL> self . watcher . start_monitoring ( ) <EOL> def load_all_plugins ( self ) : <EOL> """<STR_LIT>""" <EOL> self . plugin_path = os . path . realpath ( self . plugin_dir ) <EOL> sys . path . append ( self . plugin_dir ) <EOL> print '<STR_LIT>' <EOL> for f in [ os . path . join ( self . plugin_dir , child ) for child in os . listdir ( self . plugin_dir ) ] : <EOL> if '<STR_LIT>' in f or '<STR_LIT>' in f : <EOL> continue <EOL> self . add_plugin ( f ) <EOL> def on_created ( self , file_list ) : <EOL> """<STR_LIT>""" <EOL> for plugin in file_list : <EOL> self . add_plugin ( plugin ) <EOL> def on_modified ( self , file_list ) : <EOL> """<STR_LIT>""" <EOL> for plugin in file_list : <EOL> self . add_plugin ( plugin ) <EOL> def on_deleted ( self , file_list ) : <EOL> """<STR_LIT>""" <EOL> for plugin in file_list : <EOL> self . remove_plugin ( plugin ) <EOL> def remove_plugin ( self , f ) : <EOL> """<STR_LIT>""" <EOL> if f . endswith ( '<STR_LIT>' ) : <EOL> plugin_name = os . path . splitext ( os . path . basename ( f ) ) [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' % ( plugin_name , color . Red ) <EOL> print '<STR_LIT>' % ( color . Yellow , color . Normal ) <EOL> def add_plugin ( self , f ) : <EOL> """<STR_LIT>""" <EOL> if f . endswith ( '<STR_LIT>' ) : <EOL> plugin_name = os . path . splitext ( os . path . basename ( f ) ) [ <NUM_LIT:0> ] <EOL> if plugin_name in sys . modules : <EOL> try : <EOL> handler = reload ( sys . modules [ plugin_name ] ) <EOL> print '<STR_LIT>' % ( plugin_name , color . Yellow , color . Normal ) <EOL> except ImportError , error : <EOL> print '<STR_LIT>' % ( plugin_name , error ) <EOL> return <EOL> else : <EOL> try : <EOL> handler = __import__ ( plugin_name , globals ( ) , locals ( ) , [ ] , - <NUM_LIT:1> ) <EOL> except ImportError , error : <EOL> print '<STR_LIT>' % ( plugin_name , error ) <EOL> return <EOL> plugin = self . validate ( handler ) <EOL> print '<STR_LIT>' % ( plugin_name , color . Green , color . Normal ) <EOL> if plugin : <EOL> plugin [ '<STR_LIT:name>' ] = plugin_name <EOL> plugin [ '<STR_LIT>' ] = plugin [ '<STR_LIT:class>' ] . dependencies <EOL> plugin [ '<STR_LIT>' ] = plugin [ '<STR_LIT:class>' ] . __doc__ <EOL> plugin [ '<STR_LIT>' ] = datetime . utcfromtimestamp ( os . path . getmtime ( f ) ) <EOL> try : <EOL> plugin [ '<STR_LIT>' ] = getattr ( plugin [ '<STR_LIT:class>' ] , '<STR_LIT>' ) <EOL> except AttributeError : <EOL> plugin [ '<STR_LIT>' ] = False <EOL> self . plugin_callback ( plugin ) <EOL> def validate ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> test_method = self . plugin_test_validation ( handler ) <EOL> if not test_method : <EOL> return None <EOL> for name , plugin_class in inspect . getmembers ( handler , inspect . isclass ) : <EOL> if self . plugin_class_validation ( plugin_class ) : <EOL> return { '<STR_LIT:class>' : plugin_class , '<STR_LIT:test>' : test_method } <EOL> print '<STR_LIT>' % ( handler . __name__ ) <EOL> print '<STR_LIT>' <EOL> return None <EOL> def plugin_test_validation ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> methods = { name : func for name , func in inspect . getmembers ( handler , callable ) } <EOL> if '<STR_LIT:test>' not in methods . keys ( ) : <EOL> print '<STR_LIT>' % ( handler . __name__ ) <EOL> print '<STR_LIT>' <EOL> return None <EOL> else : <EOL> return methods [ '<STR_LIT:test>' ] <EOL> def plugin_class_validation ( self , plugin_class ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> getattr ( plugin_class , '<STR_LIT>' ) <EOL> getattr ( plugin_class , '<STR_LIT>' ) <EOL> except AttributeError : <EOL> return False <EOL> return True <EOL> def test ( ) : <EOL> """<STR_LIT>""" <EOL> def new_plugin ( plugin ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT:%s>' % ( plugin [ '<STR_LIT:name>' ] ) <EOL> plugin_dir = os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , '<STR_LIT>' ) <EOL> PluginManager ( new_plugin , plugin_dir = plugin_dir ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test ( ) </s>
