Ñò
ÓÛqKc           @   s  d  Z  d d k Z d d k Z d d k Z d d k Z d d k l Z d d k l Z d d k	 l
 Z
 d d k l Z d d k l Z d d k Z d d k Z d d k Z d d k Z d d k l Z l Z d d	 k l Z l Z d d
 k l Z d d k l Z d Z d Z d Z d „  Z d d d „ Z e d e  ƒ  ƒ Z! d d e" e" d „ Z# d d d „ Z$ d e e
 e f d „  ƒ  YZ% e i& e% ƒ d S(   s,  
$RCSfile: CacheManager.py,v $

ZopeXMLMethods provides filters to apply to Zope objects for XML/XSLT
processing.  XSLTMethod associates XSLT transformers with XML
documents.  ZopeXMLMethods includes XML Method Cache Manager that is
specialized to notice changes to the XML source files and to store
cached contents in files in the filesystem, rather than the Zope
object database.

Author: Craeg Strong <cstrong@arielpartners.com>
Modified by Philipp von Weitershausen <philikon@philikon.de>

$Id: CacheManager.py,v 1.2 2003/04/29 09:22:07 philikon Exp $
iÿÿÿÿN(   t
   SimpleItem(   t   CatalogAware(   t   PropertyManager(   t
   implements(   t   ICacheManager(   t   MessageDialogt   HTML(   t   aq_chaint	   aq_parent(   t   ClassSecurityInfo(   t   PageTemplateFilet   Edits   Manage XML Method Cachet   ZopeXMLCachec          C   s3   d }  t  i d j o
 d }  n t i i |  t ƒ S(   Ns   /tmpt   win32s   c:\tmp(   t   syst   platformt   ost   patht   joint   cachePrefix(   t   spoolDirectory(    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyt   defaultCachePrefix3   s    
t    c         C   s"   |  i  d i | | | ƒ |  | S(   sÿ   
    This is a convenience factory method for creating an instance of
    CacheManager.  It returns the object created, and may therefore be
    more convenient than the addCacheManager() method it calls.  It is
    used by the unit testing programs.
    t   ZopeXMLMethods(   t   manage_addProductt   addXMLMethodCacheManager(   t   foldert   idt   titlet   description(    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyt   addInstance=   s    s   www/create_cachemgr.ptc         C   s|   y. |  i  | | | ƒ | i |  i ƒ  d ƒ WnG t j
 o; } t | ƒ } t i d d ƒ t d d d | d d ƒ SXd	 S(
   sÊ   
    Add an XML Method Cache Manager to a folder.  Called from the
    create_cachemgr.dtml GUI form in the Zope Management interface.
    It calls addXMLMethodCacheManager to actually do the work.
    s   /manage_mains   
s   <br/>R   t   Errort   messaget   actiont   manage_mainN(   R   t   redirectt   absolute_urlt	   Exceptiont   strt   mesaget   replaceR   (   t   selfR   R   R   t   REQUESTt   RESPONSEt   eR    (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyt   manage_addXMLMethodCacheManagerN   s    c         C   s7   | p t  d ƒ ‚ n |  i | t | | | ƒ ƒ d S(   s¦   
    Add an XML Method Cache Manager to a folder.  You should call
    this method directly if you are creating an instance of
    CacheManager programmatically.
    s!   Required fields must not be blankN(   R%   t
   _setObjectt   CacheManager(   R)   R   R   R   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR   a   s    R/   c           B   sG  e  Z d  Z d Z e e ƒ h d d 6d d 6d d 6h d d 6d d 6d d 6h d	 d 6d d 6d d 6f Z h d
 d 6d d 6d2 d 6f e i i i	 e i
 i
 i	 Z	 e ƒ  Z e i e d ƒ e d e ƒ  ƒ Z d „  Z e i e d ƒ d „  Z e i e d ƒ d „  Z e i e d ƒ d „  Z e i e d ƒ d „  Z e i e d ƒ d „  Z e i e d ƒ d „  Z e i e d ƒ d  „  Z e i e d! ƒ d" „  Z e i e d# ƒ d$ „  Z e i e d% ƒ d& „  Z e i e d' ƒ d( „  Z e i e d) ƒ d* „  Z d+ „  Z  d, „  Z! d- „  Z" d. „  Z# e i$ d/ ƒ d0 „  Z% d1 „  Z& RS(3   sØ   
    CacheManager caches the results of performing transformations with
    filters.  It does not inherit from OFS/CacheManager because it
    has so little in common with it.  @@ revisit this later CKS 3/2/2003
    s   XML Method Cache ManagerR   R   t   stringt   typet   wt   modeR   R   t   Cachet   labelt   manage_cacheFormR!   R   s	   cache.stxt   helps   www/edit_cachemgr.ptc         C   s4   | |  _  | |  _ | |  _ t ƒ  |  _ h  |  _ d S(   s;   
        Initialize a new instance of CacheManager
        N(   R   R   R   R   R   t
   cacheFiles(   R)   R   R   R   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyt   __init__   s
    			t   manage_setCachingOnc         C   s/   |  i  | ƒ } | i d d ƒ |  i d | ƒ S(   sI   
        ZMI method: turn caching on for all XML Method objects.
        s   
s   <br/>t   batchOperationOutput(   t   batchSetCachingOnR(   R6   (   R)   R*   R    (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR:   œ   s    t   manage_setCachingOffc         C   s/   |  i  | ƒ } | i d d ƒ |  i d | ƒ S(   sJ   
        ZMI method: turn caching off for all XML Method objects.
        s   
s   <br/>R;   (   t   batchSetCachingOffR(   R6   (   R)   R*   R    (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR=   ¥   s    t   manage_clearCachec         C   s,   |  i  ƒ  } | i d d ƒ |  i d | ƒ S(   s)   
        ZMI method: clear cache
        s   
s   <br/>R;   (   t
   clearCacheR(   R6   (   R)   R    (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR?   ®   s    t   manage_listCacheFilesc         C   sb   d } |  i  ƒ  } | o# x& | D] } | | d } q  Wn d } | i d d ƒ |  i d | ƒ S(   s.   
        ZMI method: list cache files
        s   Cache Files:

s   
s   No files in caches   <br/>R;   (   t   listCacheFilesR(   R6   (   R)   R    t   listt   file(    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRA   ·   s     t   manage_editPropertiesc         C   sˆ   xo |  i  ƒ  D]a } | d } | d j oD | i | d ƒ } |  i | ƒ p t d d d | d d d	 ƒ Sq q Wt i d
 ƒ |  | ƒ S(   s¶   
        Cover for PropertyManager.manage_editProperties() method.
        First validate cachePrefix, then pass it on to the inherited
        method for further processing
        R   R   R   R   t   ERRORR    s    is not a valid Cache PrefixR!   t   manage_propertiesFormRE   (   t   _propertyMapt   gett   isValidCachePrefixR   R/   t   inheritedAttribute(   R)   R*   t   propt   namet   value(    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRE   È   s     

R<   c         C   s=   |  i  ƒ  } d | i ƒ  d } | |  i | d | ƒ } | S(   s€   
        Turns caching on for all instances of all types of XML Methods
        within the scope of this cache manager.
        s6   Turning Caching On for all XML filter instances under s	    Folder:
t   setCachingOn(   t   getParentFoldert   getIdt   batchExecute(   R)   R*   t   parentR    (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR<   Ý   s    R>   c         C   s=   |  i  ƒ  } d | i ƒ  d } | |  i | d | ƒ } | S(   s   
        Turns caching off for all instances of all types of XML
        Methods within the scope of this cache manager.
        s7   Turning Caching Off for all XML filter instances under s	    Folder:
t   setCachingOff(   RP   RQ   RR   (   R)   R*   RS   R    (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR>   ë   s    t   cacheFileTimeStampc         C   sA   |  i  | ƒ } d } t i i | ƒ o t i i | ƒ Sd Sd S(   s—   
        Return the last modified time of the cache file for the passed in
        filter client object, or 0 if the cache file does not exist
        i    N(   t   cacheFileNameR   R   t   existst   getmtime(   R)   t   urlt   fileNamet	   cachetime(    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRU   ù   s
    t   valueFromCachec         C   s8   |  i  | ƒ } t | d ƒ } | i ƒ  } | i ƒ  | S(   s–   
        Retrieve the output from the cache for the passed in
        filter client object, or None if the cache file does not
        exist.
        t   rb(   RV   t   opent   readt   close(   R)   RY   RZ   t   cachet   result(    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR\     s
    
t   saveToCachec         C   sO   |  i  | ƒ } t | d ƒ } | i | ƒ | i ƒ  | |  i | <d |  _ d S(   sá   
        Save the contents to the cache for the passed in filter
        client object.  It will overwrite the previous contents of the
        cache file, or create a new cache file if it does not yet
        exist.
        t   wbi   N(   RV   R^   t   writeR`   R8   t
   _p_changed(   R)   RY   t   contentsRZ   Ra   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRc     s    
R@   c         C   sƒ   d } xb |  i  i ƒ  D]Q } y t i | ƒ | | d } Wq t j
 o } | d | | f } q Xq W|  i  i ƒ  | p d S(   si   
        Clear the cache.  This involves removing every cache file from
        the file system.
        R   s    successfully removed
s   %s not removed, error: %s
s   No files in cache(   R8   t   valuesR   t   removeR%   t   clear(   R)   R    RZ   RN   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR@   #  s     RB   c         C   s   |  i  i ƒ  S(   s7   
        Return the list of files in the cache
        (   R8   Rh   (   R)   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRB   3  s    c         C   s"   t  i i | ƒ } t  i i | ƒ S(   sN   
        Return 1 if this represents a valid value for a cache prefix
        (   R   R   t   dirnameRW   (   R)   RN   Rk   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRJ   =  s    c         C   sG   t  i  | ƒ \ } } } } } } t i i |  i t i d d | ƒ ƒ S(   s«   
        Algorithmically determine the leaf name of the file in which
        to store the transformed contents of the passed in XML filter
        client object.
        s   [/\\]t   _(   t   urlparseR   R   t   normpathR   t   ret   sub(   R)   RY   t   addressingSchemet   networkLocationR   t   paramst   queryt   fragment(    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRV   F  s    !c         C   s
   t  |  ƒ S(   sM   
        Returns the folder containing this instance of CacheManager
        (   R   (   R)   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRP   P  s    c         C   s¦   d } | i  | d | g d d ƒ} t i i | } d } xe | D]] } y1 | d | d } | | d | ƒ | d } WqA t j
 o }	 | d	 |	 }
 qA XqA W| S(
   sÑ   
        Recursively execute passed-in method on all objects of type
        XSLTMethod within the passed-in context (ie Zope folder).
        Returns a list of the object IDs of the affected objects.
        s   XSLT Methodt   obj_metatypest
   search_subi   s   

s   
Found XSLTMethod i    s
   ...successs   ...error: %s(   t   ZopeFindt
   XSLTMethodt   __dict__R%   (   R)   t   contextt
   methodNameR*   t   metaTypet   objectst   funcR    t   docRN   R'   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyRR   V  s    	 t   getSelfc         C   s   |  i  d S(   s+   Return this object. For use in DTML scriptsi    (   R   (   R)   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR   o  s    c         C   s   d S(   s¿   
        Repair this object.  This method is used for schema migration,
        when the class definition changes and existing instances of
        previous versions must be updated.
        N(    (   R)   (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyt   repairw  s    (   s   ZopeXMLMethodss	   cache.stx('   t   __name__t
   __module__t   __doc__t	   meta_typeR   R   t   _propertiest   OFSR   t   manage_optionsR    R	   t	   _securityt   declareProtectedt   PERM_MANAGER
   t   globalsR6   R9   t	   PERM_EDITR:   R=   R?   RA   RE   R<   R>   RU   R\   Rc   R@   RB   RJ   RV   RP   RR   t   declarePublicR   R‚   (    (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyR/   p   sT   
8														
			
			('   R…   R   Ro   R   Rm   t   OFS.SimpleItemR    t"   Products.ZCatalog.CatalogAwarenessR   t   OFS.PropertyManagerR   t   zope.interfaceR   t
   interfacesR   Ry   Rˆ   t   Globalst   AcquisitionR   R   R   R   t   AccessControlR	   t'   Products.PageTemplates.PageTemplateFileR
   RŽ   RŒ   R   R   R   R   t#   manage_addXMLMethodCacheManagerFormt   NoneR-   R   R/   t   InitializeClass(    (    (    s1   /srv/Zope/Products/ZopeXMLMethods/CacheManager.pyt   <module>   s:   0	
ÿ 