changeset 60:09cc6fd3ddd2

Added a really basic plugin architecture. Also implemented a rc-file mechanism whereby the ~/.ensorc file is interpreted as a Python file and run before Enso starts. It's intended to make any changes it wants to the default enso.config--such as adding to config.PLUGINS--before Enso starts running.
author Atul Varma <varmaa@toolness.com>
date Wed, 27 Feb 2008 11:35:04 -0800
parents d3c0220c5895
children 2d7464119dd0
files enso/__init__.py enso/config.py enso/plugins.py scripts/run_enso.py
diffstat 4 files changed, 60 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/enso/__init__.py	Wed Feb 27 10:50:26 2008 -0800
+++ b/enso/__init__.py	Wed Feb 27 11:35:04 2008 -0800
@@ -37,10 +37,18 @@
     Initializes and runs Enso.
     """
 
+    import enso.plugins
     import enso.ui
 
     enso.ui.init()
     try:
-        enso.ui.run()
+        enso.ui.events.eventManager.registerResponder(
+            enso.plugins.init,
+            "init"
+            )
+        try:
+            enso.ui.run()
+        finally:
+            enso.plugins.shutdown()
     finally:
         enso.ui.shutdown()
--- a/enso/config.py	Wed Feb 27 10:50:26 2008 -0800
+++ b/enso/config.py	Wed Feb 27 11:35:04 2008 -0800
@@ -55,5 +55,10 @@
     " these mini-messages.</p>"
 
 # List of modules/packages that support the provider interface to
-# provide platform-specific functionality to Enso.
+# provide required platform-specific functionality to Enso.
 PROVIDERS = ["enso_osx"]
+
+# List of modules/packages that support the plugin interface to
+# extend Enso.  The plugins are loaded in the order that they
+# are specified in this list.
+PLUGINS = []
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/enso/plugins.py	Wed Feb 27 11:35:04 2008 -0800
@@ -0,0 +1,34 @@
+# TODO: Add documentation for this module.
+
+import logging
+
+import enso.config
+
+_plugins = []
+
+def init():
+    for moduleName in enso.config.PLUGINS:
+        try:
+            # Import the module; most of this code was taken from the
+            # Python Library Reference documentation for __import__().
+            module = __import__( moduleName, {}, {}, [], 0 )
+            components = moduleName.split( "." )
+            for component in components[1:]:
+                module = getattr( module, component )
+
+            module.load()
+            _plugins.append( (module, moduleName) )
+        except:
+            logging.warn( "Error while loading plugin '%s'." % moduleName )
+            raise
+        logging.info( "Loaded plugin '%s'." % moduleName )
+
+def shutdown():
+    for module, moduleName in _plugins:
+        try:
+            module.unload()
+        except:
+            logging.warn( "Error while unloading plugin '%s'." % moduleName )
+            raise
+        logging.info( "Unloaded plugin '%s'." % moduleName )
+    _plugins[:] = []
--- a/scripts/run_enso.py	Wed Feb 27 10:50:26 2008 -0800
+++ b/scripts/run_enso.py	Wed Feb 27 11:35:04 2008 -0800
@@ -1,9 +1,20 @@
 #! /usr/bin/env python
 
+import os
 import logging
 
 import enso
 
 if __name__ == "__main__":
     logging.basicConfig( level=logging.INFO )
+
+    homeDir = os.getenv( "HOME" )
+    if homeDir:
+        ensorcPath = os.path.join( homeDir, ".ensorc" )
+        if os.path.exists( ensorcPath ):
+            logging.info( "Loading '%s'." % ensorcPath )
+            contents = open( ensorcPath, "r" ).read()
+            compiledContents = compile( contents, ensorcPath, "exec" )
+            exec compiledContents in {}, {}
+
     enso.run()