Ñò
ôÙqKc        
   @   s…   d  Z  d d d !Z d d d !Z d d d !Z d d k l Z d d	 k l Z l Z l	 Z	 l
 Z
 d Z d d d „  ƒ  YZ e ƒ  Z d S(   s'  
$RCSfile: ProcessorRegistry.py,v $

ZopeXMLMethods provides filters to apply to Zope objects for XML/XSLT
processing.  XSLTMethod associates XSLT transformers with XML
documents.  XSLTMethod automatically transforms an XML document via
XSLT, where the XML document is obtained from another Zope object (the
'source' object) via acquisition.  XPathMethod automatically evaluates
an XPath method on an XML document obtained from the Zope object to
which it is applied.

ProcessorRegistry automatically detects which of the supported
processor(s) are available in the environment, and makes it possible
to dynamically choose/switch processors at runtime

Author: <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
Release: 1.0

$Id: ProcessorRegistry.py,v 1.3 2005/09/06 02:08:59 arielpartners Exp $
s	   $Name:  $i   iþÿÿÿs   $Date: 2005/09/06 02:08:59 $s   $Revision: 1.3 $i
   iÿÿÿÿ(   t
   implements(   t   IProcessorRegistryt   IXPathProcessort   IXSLTProcessort   IDTDValidatort   FourSuite11Processort   FourSuiteProcessort   LibXsltProcessort   PyanaProcessort   SabPythProcessort   ProcessorRegistryc           B   s‡   e  Z d  Z e e ƒ d „  Z d „  Z d d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   sÁ  

    ProcessorRegistry automatically detects which of the supported
    processor(s) are available in the environment, and makes it
    possible to dynamically choose/switch processors at runtime.

    I designed this to do lazy loading to enable ZSyncing from one
    Zope instance to another where the two Zope instances *do not*
    share the exact same set of XSLT libraries.  For a small
    performance price, we get lots more flexibility.  Besides, if you
    want performance, you should be using the CacheManager...

    CKS 10/31/2002

    @@ Rework in Python2.2 to function as an iterator

    Adapted from ProcessorChooser and redesigned to resemble
    GeneratorRegistry, CKS 6/10/2003

    c         C   s   h  |  _  h  |  _ d  S(   N(   t	   _registryt
   _preferred(   t   self(    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   __init__G   s    	c      	   C   sA   x: t  D]2 } y" t | t ƒ  t ƒ  | g ƒ } Wq q Xq Wd S(   s.   Load the processors: they register themselves.N(   t   CandidateProcessorst
   __import__t   globalst   locals(   R   t	   classNamet   klass(    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   reloadK   s     i    c         C   s   | | f |  i  | <d S(   sÌ  
        For a given processor type, indicate a preference for one
        processor over another.  Set the 'exclusive' flag to indicate
        that no other choices should be offered, even if available.
        To change your preference, simply call this method again.

        This is used to indicate global preferences.  Each XSLTMethod
        or XPathMethod instance stores their own choice.  Today, this
        method is used only in testing.
        N(   R   (   R   t   processorTypet   processorNamet	   exclusive(    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   preferT   s    c         C   s
   |  i  ƒ  S(   s}   Only save preferences, do not pickle processor instances or we
        wont be able to ZSync to a differently-configured Zope(   R   (   R   (    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   __getstate__g   s    c         C   s   |  i  i | ƒ d S(   s%   called with what we saved in getstateN(   R   t   update(   R   t   state(    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   __setstate__l   s    c         C   s   d S(   s   helper method for pickling(    (    (   R   (    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   __getinitargs__p   s    c         C   s   | |  i  | i <d S(   s   register the given processorN(   R   t   name(   R   t   proc(    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   registert   s    c         C   s+   g  } |  i  | ƒ D] } | | i q ~ S(   sr  
        
        Return list of names of the currently available processors of
        the given type.

        If an exclusive preference has been expressed, *and* the
        preferred processor is available, return only that.

        Call reload() first to ensure we pick up new processor
        libraries if we just got ZSynced to another Zope instance.

        (   t   itemsR   (   R   R   t   _[1]t   ob(    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   namesx   s    c   	      C   sØ   |  i  ƒ  |  i i | d ƒ \ } } g  } |  i i ƒ  D]! } | i | ƒ o | | q9 q9 ~ } | of | d j oQ g  } | D]! } | i | j o | | q‚ q‚ ~ } t | ƒ d j o | SqÔ | Sn g  Sd S(   sU  

        Return list of currently available processors of the given type.

        If an exclusive preference has been expressed, *and* the
        preferred processor is available, return only that.

        Call reload() first to ensure we pick up new processor
        libraries if we just got ZSynced to another Zope instance.

        i    i   N(   Ni    (	   R   R   t   gett   NoneR   t   valuest   isImplementedByR   t   len(	   R   R   t   prefR   R#   R$   t
   candidatest   _[2]t	   preferred(    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyR"   ‡   s    
>5	c         C   sP   |  i  ƒ  |  i i | d ƒ } | d j o  d G| Gd GH|  i | ƒ } n | S(   s¡   
        
        Obtain the object encapsulating the named processor, if it is
        available.

        Otherwise, return the defaultItem()
        
        s   Sorry, processors   not available.N(   R   R   R&   R'   t   defaultItem(   R   R   R   R$   (    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   item¤   s    	
c         C   s   |  i  |  i | ƒ S(   sŠ  
        Obtain the object encapsulating the preferred processor of the
        given type, if it is available.

        Otherwise try all other processors in random order.

        Finally, give up if we don't have a single processor
        available.

        Call reload() first to ensure we pick up new processor
        libraries if we just got ZSynced to another Zope instance.

        (   R   t   defaultName(   R   R   (    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyR/   ¸   s    c         C   sŒ   |  i  ƒ  t |  i ƒ d j o d | i } t | ƒ ‚ nL |  i i | d ƒ \ } } | o |  i i | ƒ o | S|  i i	 ƒ  d Sd S(   s…  
        Obtain the name of the preferred processor of the
        given type, if it is available.

        Otherwise try all other processors in random order.

        Finally, give up if we don't have a single processor
        available.

        Call reload() first to ensure we pick up new processor
        libraries if we just got ZSynced to another Zope instance.
        
        i    s5   Sorry, no instances of an %s processor are available.N(   Ni    (
   R   R*   R   t   __name__t	   ExceptionR   R&   R'   t   has_keyt   keys(   R   R   t   messageR+   R   (    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyR1   È   s    

(   R2   t
   __module__t   __doc__R    R   R   R   R   R   R   R   R!   R%   R"   R0   R/   R1   (    (    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyR
   /   s   
											N(   R   R   R   R   R	   (    (   R8   t
   __cvstag__t   __date__t   __version__t   zope.interfaceR    t
   interfacesR   R   R   R   R   R
   (    (    (    sA   /srv/Zope/Products/ZopeXMLMethods/processors/ProcessorRegistry.pyt   <module>   s   "	    ¸