]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/app/__init__.py
Add plugin infrastructure for wizard.app.
[wizard.git] / wizard / app / __init__.py
index ae99e3e58dfa34aebe3916c4f370f186a2bcfe00..1c8b477e997027abeabc5dd2944b32efca648d7b 100644 (file)
@@ -6,6 +6,18 @@ You'll need to know how to overload the :class:`Application` class
 and use some of the functions in this module in order to specify
 new applications.
 
+To specify custom applications as plugins,  add the following ``entry_points``
+configuration::
+
+    [wizard.app]
+    yourappname = your.module:Application
+    otherappname = your.other.module:Application
+
+.. note::
+
+    Wizard will complain loudly if ``yourappname`` conflicts with an
+    application name defined by someone else.
+
 There are some submodules for programming languages that define common
 functions and data that may be used by applications in that language.  See:
 
@@ -32,23 +44,48 @@ import random
 import string
 import urlparse
 import tempfile
+import pkg_resources
 
 import wizard
 from wizard import resolve, scripts, shell, util
 
-_application_list = [
+# SCRIPTS SPECIFIC
+_scripts_application_list = [
     "mediawiki", "wordpress", "joomla", "e107", "gallery2",
     "phpBB", "advancedbook", "phpical", "trac", "turbogears", "django",
     # these are technically deprecated
     "advancedpoll", "gallery",
 ]
-_applications = None
+def _scripts_make(name):
+    """Makes an application, but uses the correct subtype if available."""
+    try:
+        __import__("wizard.app." + name)
+        return getattr(wizard.app, name).Application(name)
+    except ImportError as error:
+        # XXX ugly hack to check if the import error is from the top level
+        # module we care about or a submodule. should be an archetectural change.
+        if error.args[0].split()[-1]==name:
+            return Application(name)
+        else:
+            raise
 
+_applications = None
 def applications():
     """Hash table for looking up string application name to instance"""
     global _applications
     if not _applications:
-        _applications = dict([(n,Application.make(n)) for n in _application_list ])
+        # SCRIPTS SPECIFIC
+        _applications = dict([(n,_scripts_make(n)) for n in _scripts_application_list ])
+        # setup plugins
+        for dist in pkg_resources.working_set:
+            for appname, appclass in dist.get_entry_map("wizard.app").items():
+                if appname in _applications:
+                    newname = dist.key + ":" + appname
+                    if newname in _applications:
+                        raise Exception("Unrecoverable application name conflict for %s from %s", appname, dist.key)
+                    logging.warning("Could not overwrite %s, used %s instead", appname, newname)
+                    appname = newname
+                _applications[appname] = appclass(appname)
     return _applications
 
 def getApplication(appname):
@@ -256,6 +293,8 @@ class Application(object):
                 shell.call("git", "rm", file)
                 continue
             # manual resolutions
+            # XXX: this functionality is mostly subsumed by the rerere
+            # tricks we do
             if file in self.resolutions:
                 contents = open(file, "r").read()
                 for spec, result in self.resolutions[file]:
@@ -418,19 +457,6 @@ class Application(object):
         be displayed in verbose mode.
         """
         return filename in self.parametrized_files
-    @staticmethod
-    def make(name):
-        """Makes an application, but uses the correct subtype if available."""
-        try:
-            __import__("wizard.app." + name)
-            return getattr(wizard.app, name).Application(name)
-        except ImportError as error:
-            # XXX ugly hack to check if the import error is from the top level
-            # module we care about or a submodule. should be an archetectural change.
-            if error.args[0].split()[-1]==name:
-                return Application(name)
-            else:
-                raise
 
 class ApplicationVersion(object):
     """Represents an abstract notion of a version for an application, where