Back to index

plone3  3.1.7
registration.py
Go to the documentation of this file.
00001 from wicked import utils
00002 from wicked.at.seeker import IWickedQuery, AdvQueryMatchingSeeker
00003 from wicked.cache import ICacheManager, ContentCacheManager
00004 from wicked.fieldevent.interfaces import IFieldRenderEvent, IFieldStorageEvent
00005 from wicked.fieldevent.interfaces import IFieldValueSetter
00006 from wicked.interfaces import IAmWicked
00007 from wicked.interfaces import IAmWickedField
00008 from wicked.interfaces import IWickedEvent
00009 from wicked.interfaces import IWickedFilter
00010 from wicked.txtfilter import BrackettedWickedFilter
00011 from wicked.txtfilter import backlink, brackettedbacklink
00012 from wicked.txtfilter import IAmWickedField, IFieldEvent
00013 from wicked.txtfilter import WickedFilter, IWickedFilter
00014 from wicked.txtfilter import WickedFilter, wicked_listener, bracketted_wicked_listener
00015 from zope.app.schema.vocabulary import IVocabularyFactory
00016 from zope.component import getSiteManager
00017 from zope.interface import implements
00018 from zope.schema.vocabulary import SimpleVocabulary
00019 
00020 import logging
00021 logger = logging.getLogger('wicked.registration')
00022 
00023 class BaseWickedRegistration(object):
00024     """abstract base: add a docstring to display a description on the
00025     wicked dashboard"""
00026     title = "((Basic))"
00027     field = IAmWickedField
00028     content = IAmWicked
00029     event = IFieldRenderEvent
00030     txtfilter = WickedFilter
00031     txtfilter_provides = IWickedFilter
00032     subscriber = (wicked_listener,)
00033     
00034     def __init__(self, context, **kw):
00035         self.site = context
00036         for key, item in kw.items():
00037             if hasattr(self, key):
00038                 setattr(self, key, item)
00039 
00040     @utils.memoizedproperty
00041     def sm(self):
00042         return getSiteManager(self.site)
00043 
00044     @property
00045     def required(self):
00046         return (self.field, self.content, self.event)
00047     
00048     def handle(self, unregister=False):
00049         # @@ add logging
00050         handle_adapter_reg = self.sm.registerAdapter
00051         handle_subscriber_reg = self.sm.registerHandler
00052         handle_sub_adapter_reg = self.sm.registerSubscriptionAdapter
00053         if unregister:
00054             handle_adapter_reg = self.sm.unregisterAdapter
00055             handle_subscriber_reg = self.sm.unregisterHandler
00056             handle_sub_adapter_reg = self.sm.unregisterSubscriptionAdapter
00057         
00058         self.handle_txtfilter(handle_adapter_reg)
00059         for subscriber in self.subscriber:
00060             handle_subscriber_reg(subscriber, required=self.required)
00061         for bh in self.backlink_handler:
00062             handle_sub_adapter_reg(bh)
00063         return (handle_adapter_reg, handle_subscriber_reg, handle_sub_adapter_reg)
00064 
00065     def handle_txtfilter(self, handle, event=None, txtfilter=None, unregister=False):
00066         """helper for common reg of txtfilter"""
00067         txtfilter = txtfilter and txtfilter or self.txtfilter
00068         required = list(self.required)
00069         if event:
00070             required[2] = event
00071 
00072         handle(self.txtfilter,
00073                required=required,
00074                provided=self.txtfilter_provides)
00075 
00076     def is_installed(self):
00077         raise NotImplemented
00078 
00079 
00080 class BasePloneWickedRegistration(BaseWickedRegistration):
00081     """Basic wicked behavior for Plone. Registers a fieldevent
00082     subscriber and the wicked filter."""
00083     subscriber = (wicked_listener, bracketted_wicked_listener)
00084     backlink_handler = (backlink, brackettedbacklink)
00085     #txtfilter = EasyWickedFilter
00086 
00087 class BasePloneMediaWickedRegistration(BasePloneWickedRegistration):
00088     """Same as basic but with square bracket style linking"""
00089     title = "[[Bracketted]]"
00090     txtfilter = BrackettedWickedFilter
00091 
00092 class SelectiveRegistration(BaseWickedRegistration):
00093     """by dropping direct registration to IAmWicked for content, we
00094     can selectively turn on and off the creation of wiki behavior by
00095     content interface. To do this though, we have to hand register a
00096     seeker and a cache manager"""
00097     cache = ContentCacheManager
00098     cache_provides = ICacheManager
00099     seeker = AdvQueryMatchingSeeker
00100     seeker_provides = IWickedQuery
00101     other_events = (IWickedEvent, IFieldStorageEvent)
00102     backlink_handler = (backlink, brackettedbacklink)
00103     subscriber = (wicked_listener, bracketted_wicked_listener)
00104     
00105     @property
00106     def cache_required(self):
00107         return (self.txtfilter_provides, self.content)
00108     
00109     def handle(self, unregister=False):
00110         handle_adapter_reg, handle_subscriber_reg, handle_sub_adapter_reg = super(SelectiveRegistration,
00111                                        self).handle(unregister=unregister)
00112         self.handle_cache(handle_adapter_reg)
00113         self.handle_seeker(handle_adapter_reg)
00114         
00115         for event in self.other_events:
00116             self.handle_txtfilter(handle_adapter_reg, event=event)
00117         return (handle_adapter_reg,
00118                 handle_subscriber_reg,
00119                 handle_sub_adapter_reg)
00120 
00121     def handle_cache(self, handle):
00122         handle(self.cache,
00123                required=self.cache_required,
00124                provided=self.cache_provides)
00125 
00126     def handle_seeker(self, handle):
00127         handle(self.seeker,
00128                required=(self.content,),
00129                provided=self.seeker_provides)
00130 
00131 ## vocabulary
00132 
00133 # @@ this will have to wait since plone does not have an egg story yet
00134 
00135 class BaseConfigurationOptionsFactory(object):
00136     implements(IVocabularyFactory)
00137 
00138     def __call__(self, context):
00139 ##         entry_points = get_points("wicked.base_registration")
00140 ##         items = [(bc.name, bc.load().title) for bc in entry_points]
00141 ##         items.sort()
00142 ##         items.insert(0, ('off', 'Off'))
00143 ##         return SimpleVocabulary.fromItems(items)
00144         pass
00145 
00146 class CacheConfigurationOptionsFactory(object):
00147     implements(IVocabularyFactory)
00148 
00149     def __call__(self, context):
00150         return SimpleVocabulary.fromItems(list())
00151 
00152 
00153 CacheConfigurationOptions = CacheConfigurationOptionsFactory()
00154 BaseConfigurationOptions = BaseConfigurationOptionsFactory()
00155