from pkg_resources import iter_entry_points

from spicedham.backend import load_backend

_plugins = None

def load_plugins():
    """
    If not already loaded, load plugins.
    """
    global _plugins
    if _plugins == None:
        # In order to use the plugins config and backend must be loaded.
        load_backend()
        _plugins = []
        for plugin in iter_entry_points(group='spicedham.classifiers', name=None):
            pluginClass = plugin.load()
            _plugins.append(pluginClass())


def train(training_data, is_spam):
    """
    Calls each plugin's train function.
    """
    for plugin in _plugins:
        plugin.train(training_data, is_spam)


def classify(classification_data):
    """
    Calls each plugin's classify function and averages the results.
    """
    average_score = 0
    total = 0
    for plugin in _plugins:
        value = plugin.classify(classification_data)
        # Skip _plugins which give a score of None
        if value != None:
            total += 1
            average_score += value
    # On rare occasions no _plugins will give scores. If so, return 0
    if total > 0:
        return average_score / total
    else:
        return 0

@@ -53,23 +53,22 @@
 icedham.
-backend
+config
  import 
@@ -76,25 +76,98 @@
 oad_
-backend%0A%0A_plugins
+config%0A%0A#TODO: wrap all of these in a threadsafe class%0A_classifier_plugins = None%0A_backend
  = N
@@ -259,16 +259,27 @@
  global 
+_classifier
 _plugins
@@ -286,16 +286,27 @@
 %0A    if 
+_classifier
 _plugins
@@ -388,16 +388,49 @@
 loaded.%0A
+        _classifier_plugins = %5B%5D%0A
         
@@ -448,30 +448,8 @@
 d()%0A
-        _plugins = %5B%5D%0A
     
@@ -579,16 +579,27 @@
         
+_classifier
 _plugins
@@ -622,16 +622,841 @@
 ass())%0A%0A
+def load_backend():%0A    global _backend%0A    if _backend == None:%0A        # If django is installed and the djangoorm plugin is registered, choose that%0A        try:%0A            import django%0A            #TODO: This is ugly%0A            djangoorm = iter_entry_points(group='spicedham.backends', name='djangoorm')%0A            if djangoorm:%0A                entry_point = next(djangoorm)%0A                djangoOrmClass = entry_point.load()%0A                _backend = djangoOrmClass()%0A        # Else choose the first one%0A        except ImportError:%0A            #TODO: handle the case where no plugins are installed%0A            #TODO: maybe we should pull the name from the config instead%0A            plugin = next(iter_entry_points(group='spicedham.backends', name=None))%0A            pluginClass =  plugin.load()%0A    return _backend%0A
 %0Adef tra
@@ -1549,32 +1549,43 @@
   for plugin in 
+_classifier
 _plugins:%0A      
@@ -1798,16 +1798,27 @@
 ugin in 
+_classifier
 _plugins

Move load_backend to __init__