from firmant.plugins import PluginMount
from firmant.configuration import settings

class AtomProvider(object):

    __metaclass__ = PluginMount

    def __init__(self, settings):
        provider = filter(lambda m: m.__module__ == \
                          settings['ATOM_PROVIDER'],
                          self.plugins)
        if len(provider) < 1:
            raise RuntimeError('No Atom Provider specified')
        if len(provider) > 1:
            raise RuntimeError('Multiple Atom Providers available')

        self._provider = provider[0]
        self._eperma = EntryPermalinkProvider(settings)
        self._fperma = FeedPermalinkProvider(settings)

    def get_entry(self):
        e = self._provider.entry
        e.permalink = lambda e_self: self._eperma.authoritative(e_self)
        return e

    entry = property(get_entry, None, None, "The Atom Entry class")

    def get_feed(self):
        f = self._provider.feed
        f.permalink = lambda f_self: self._fperma.authoritative(f_self)
        return f

    feed = property(get_feed, None, None, "The Atom Feed class")

    def get_slug_re(self):
        return self._provider.slug_re

    slug_re = property(get_slug_re, None, None, "The Atom slug re")


class EntryPermalinkProvider(object):

    __metaclass__ = PluginMount

    def __init__(self, settings):
        provider = filter(lambda m: m.__module__ == \
                          settings['ENTRY_PERMALINK'],
                          self.plugins)
        if len(provider) < 1:
            raise RuntimeError('No Entry Permalink Provider specified')
        if len(provider) > 1:
            raise RuntimeError('Multiple Entry Permalink Providers available')

        self._provider = provider[0](settings)

    def authoritative(self, entry):
        return self._provider.authoritative(entry)


class FeedPermalinkProvider(object):

    __metaclass__ = PluginMount

    def __init__(self, settings):
        provider = filter(lambda m: m.__module__ == \
                          settings['FEED_PERMALINK'],
                          self.plugins)
        if len(provider) < 1:
            raise RuntimeError('No Feed Permalink Provider specified')
        if len(provider) > 1:
            raise RuntimeError('Multiple Feed Permalink Providers available')

        self._provider = provider[0](settings)

    def authoritative(self, feed):
        return self._provider.authoritative(feed)

@@ -81,242 +81,139 @@
 gs%0A%0A
-class AtomProvider(object):%0A%0A    __metaclass__ = PluginMount%0A%0A    def __init__(self, settings):%0A        provider = filter(lambda m: m.__module__ == %5C%0A                          settings%5B'ATOM_PROVIDER'%5D,%0A                          self.
+%0Adef select_canonical_plugin(plugins, settings, config_var):%0A    provider = filter(lambda m: m.__module__ == settings%5Bconfig_var%5D, 
 plug
@@ -213,36 +213,32 @@
 %5D, plugins)%0A    
-  
-  
 if len(provider)
@@ -235,36 +235,32 @@
 (provider) %3C 1:%0A
-    
         raise Ru
@@ -278,38 +278,49 @@
 'No 
-Atom Provider specified')%0A    
+plugin for %22%25s%22 specified' %25 config_var)%0A
     
@@ -333,36 +333,32 @@
 (provider) %3E 1:%0A
-    
         raise Ru
@@ -382,21 +382,93 @@
 ple 
+plugins for %22%25s%22 available' %25 config_var)%0A    return provider%5B0%5D%0A%0A%0Aclass 
 Atom
- 
 Provider
 s av
@@ -467,59 +467,273 @@
 ider
-s available')%0A%0A        self._provider = provider%5B0%5D
+(object):%0A%0A    __metaclass__ = PluginMount%0A%0A    def __init__(self, settings):%0A        self._provider = select_canonical_plugin(self.plugins,%0A                                                 settings,%0A                                                 'ATOM_PROVIDER')
 %0A   
@@ -1512,32 +1512,38 @@
 tings):%0A        
+self._
 provider = filte
@@ -1541,386 +1541,172 @@
 r = 
-filter(lambda m: m.__module__ == %5C%0A                          settings%5B'ENTRY_PERMALINK'%5D,%0A                          self.plugins)%0A        if len(provider) %3C 1:%0A            raise RuntimeError('No Entry Permalink Provider specified')%0A        if len(provider) %3E 1:%0A            raise RuntimeError('Multiple Entry Permalink Providers available')%0A%0A        self._provider = provider%5B0%5D
+select_canonical_plugin(self.plugins,%0A                                                 settings,%0A                                                 'ENTRY_PERMALINK')
 (set
@@ -1911,24 +1911,30 @@
 s):%0A        
+self._
 provider = f
@@ -1936,383 +1936,171 @@
 r = 
-filter(lambda m: m.__module__ == %5C%0A                          settings%5B'FEED_PERMALINK'%5D,%0A                          self.plugins)%0A        if len(provider) %3C 1:%0A            raise RuntimeError('No Feed Permalink Provider specified')%0A        if len(provider) %3E 1:%0A            raise RuntimeError('Multiple Feed Permalink Providers available')%0A%0A        self._provider = provider%5B0%5D
+select_canonical_plugin(self.plugins,%0A                                                 settings,%0A                                                 'FEED_PERMALINK')
 (set

Refactor common code in firmant.backend.atom