m
d{>c           @   s  d  Z  d d d !Z d d d !Z d d d !Z d k l Z d k Z d k Z d k Z d k	 Z	 d	 k
 l Z d k
 Z
 d k Z d
 k l Z d k l Z d k l Z d k l Z d k l Z d k l Z d k l Z d k l Z d k l Z d k l Z d k  l! Z! l" Z" l# Z# d Z$ d Z% d Z& d Z' d Z( d d d d e) e) e) d  Z* e d e+    Z, d d d d e) d  Z- d e e e f d     YZ. e
 i/ e.  d S(    s   
$RCSfile: ExternalFile.py,v $

Implements ExternalFile class as a Zope product
ExternalFile encapsulates a file in the filesystem for
use as a Zope object.

Author: <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
Release: 1.2
s	   $Name:  $i   is   $Date: 2003/04/21 02:16:10 $s   $Revision: 1.80 $i
   (   s   StringION(   s   MessageDialog(   s   DateTime(   s   rfc1123_date(   s   ClassSecurityInfo(   s   PageTemplateFile(   s   ZopePageTemplate(   s
   SimpleItem(   s   CatalogAware(   s   PropertyManager(   s   IExternalFile(   s	   copy_file(   s   getMimeTypeLists   getMimeTypeFromFiles   getDisplayTypeFromMimeTypet   Views   Change External Filess
   FTP accesss   Search ZCatalogs   Access contents informationt    c	   	      C   s+   |  i d i | | | | | |  |  | S(   s   
    This is a convenience factory method for creating an instance of
    ExternalFile.  It returns the object created, and may therefore be
    more convenient than the manage_add() method it calls.  It is
    used by the unit testing programs.
    t   ExternalFileN(	   t   foldert   manage_addProductt   addExternalFilet   idt   titlet   descriptiont   target_filepatht   basedirt   upload_file(	   R   R   R   R   R	   R
   R   t   REQUESTt   RESPONSE(    (    tH   /home2/kennisonb/webapps/zope/Zope/Products/ExternalFile/ExternalFile.pyt   addInstance:   s     
s   www/create.ptc         C   sz   | p t d   n t i i | |  } | o t	 | |  n |  i | t | | | |   |  i |  i   d S(   s  

    Factory method to actually create an instance of ExternalFile.
    ExternalFile.  This method assumes all parameters are correct (it
    does no error checking).  It is called from CreationDialog.py once
    all of the confirmation and error checking steps have been taken.
    
    You should call this method directly if you are creating an
    instance of ExternalFile programmatically and have 'vetted' all of
    your parameters for correctness.

    s!   Required fields must not be blankN(   R   t	   Exceptiont   ost   patht   joinR
   R	   t   fully_resolved_target_filepathR   t	   copy_fileR   t
   _setObjectR   R   R   t   _getObt   reindex_object(   R   R   R   R   R	   R
   R   R   (    (    R   R   O   s     
R   c        	   B   s  t  Z d  Z d Z e Z d d d d d g Z h  d d <d	 d
 <d d <h  d d <d	 d
 <d d <h  d d <d	 d
 <d d <h  d d <d	 d <d d <d d <h  d d <d	 d <d d <d d <f Z h  d d <d d <d dS <h  d d <d d <d dT <f e i	 i	 i
 e i i i
 Z
 e   Z e i   d    Z e i d  d!   Z e i 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-  e d.  Z e i e d/  d d0  Z e i e d  e d1 e    Z  e i e d  e d2 e    Z! e i e d3  e d4 e    Z" e i d5  d6   Z# e i d7  d8   Z$ e i e% d9  e i& e% d:  d;   Z' e i e% d<  e d=  Z( e i e% d>  e e d?  Z) h  d@ dU <dC dV <dE dW <dF dX <Z* dG Z+ dD Z, e i e dH  d d e dI dJ dK e e dL  Z- e i e dM  dN   Z. e i e/ dO  e Z0 e i e1 dP  e dQ  Z2 dR   Z3 RS(Y   sN   
    ExternalFile encapsulates a file in the filesystem as a Zope object.
    s   External Filet   DTMLDocumentt
   DTMLMethodt   Filet   Imaget   PageTemplateR   R   t   typet   stringt   modet   wR   t   filepathR   t   content_typet	   selectiont   select_variablet   getMimeTypest   behave_liket   behave_like_listt   labelt   Editt   actiont   manage_editFormt   helpR   s   edit.stxR    t   manage_views   view.stxc         C   s   | |  _  | |  _ | |  _ | |  _ t |  _ t |  |  _ t |  i  } | d j o d |  _
 n$ | d j o d |  _
 n
 d |  _
 d S(   s;   
        Initialize a new instance of ExternalFile
        t   imageR   t   binaryR   R   N(   R   t   selfR   R   R"   t   __version__t   getMimeTypeFromFileR#   t   getDisplayTypeFromMimeTypet   displaytypeR'   (   R1   R   R   R   R"   R5   (    (    R   t   __init__   s     					c         C   s   t    S(   s$   return list of available mime types.N(   t   getMimeTypeList(   R1   (    (    R   R&      s     t   getSelfc         C   s   |  i d S(   s+   Return this object. For use in DTML scriptsi    N(   R1   t   aq_chain(   R1   (    (    R   R8      s     t   getContentTypec         C   s   |  i S(   s!   Returns the mimetype of the file.N(   R1   R#   (   R1   (    (    R   R:      s     t   getDisplayTypec         C   s   t  |  i  S(   s   
        Returns 'ascii', 'image', or 'binary' according to the
        mime_type_map defaults to 'binary' if no match is found.
        N(   R4   R1   R#   (   R1   (    (    R   R;      s     t   getFilepathc         C   s   t  i i |  i  S(   s   
        Return the full pathname of the external file to which we are
        referring.  The REQUEST parameter encapsulates information
        about the environment which can be used by subclasses
        overriding this method.
        N(   R   R   t   normpathR1   R"   (   R1   R   (    (    R   R<      s     t   getFileModTimec         C   s   |  i |  } t i i |  S(   sf  
        Return the last modification date of the external file to
        which we are referring.  The value is returned as number of
        seconds since the epoch in UTC (see python time module).  The
        REQUEST parameter encapsulates information about the
        environment which can be used by subclasses overriding this
        method.
        N(   R1   R<   R   R   R   t   getmtime(   R1   R   R   (    (    R   R>      s     c         C   s2   |  i |  } |  i | j o |  i Sn | Sd S(   s  
        Return the last modification date of the external file to
        which we are referring, or of our metadata which is stored
        internally in the ZODB, whichever is more recent (greater).
        The value is returned as number of seconds since the epoch in
        UTC (see python time module).  The REQUEST parameter
        encapsulates information about the environment which can be
        used by subclasses overriding this method.
        N(   R1   R>   R   t   tempTimet   _p_mtime(   R1   R   R@   (    (    R   t
   getModTime   s
    	 t   getContentsc         C   sS   y) t    } |  i |  } t | |  Wn t j
 o } | Sn X| i	   Sd S(   sI  
        Return the contents of this external file as a string.  The
        REQUEST parameter encapsulates information about the
        environment which can be used by subclasses overriding this
        method.  **The RESPONSE parameter is there for compatibility
        with other products such as ExternalEditor.**
        N(
   t   StringIOt   dataR1   R<   R   R   R   t   IOErrort   valuet   getvalue(   R1   R   R   RG   R   RE   (    (    R   RC      s     			t   setContentsc         C   s0   t  |  } t | |  i |   |  i   d S(   s   
        Overwrite the previous contents of this file with the
        newcontents string.  The REQUEST parameter encapsulates
        information about the environment which can be used by
        subclasses overriding this method.
        N(   RD   t   newcontentsRE   R   R1   R<   R   R   (   R1   RJ   R   RE   (    (    R   RI     s     s   www/edit.pts   www/view.ptt   fileinfos   www/fileinfo.ptt   __len__c         C   s6   t  i i |  i  o t  i |  i  d Sn d Sd S(   s,  
        return length of object.  This is invoked by the Python
        builtin len(), which in turn is invoked by 'if foo:' where foo
        is an instance of this class.  Unless this method exists and
        returns a non-zero value, this locution will yield unexpected
        results.
        i   i   N(   R   R   t   existsR1   R"   t   stat(   R1   (    (    R   RL   -  s     t   get_sizec         C   s
   t  |   S(   so   
        Return the length of the underlying file.  This method is
        REQUIRED or FTP won't work.
        N(   t   lenR1   (   R1   (    (    R   RO   ;  s     t
   index_htmlt	   Anonymousc         C   s   |  i d |  S(   s   
        Render the document/method/Image/File/etc appropriately.  Note
        that this method is called for ExternalFile when it is
        emulating a DTMLMethod, even though a DTMLMethod itself never
        calls index_html.
        N(   R1   t   __call__t   NoneR   (   R1   R   (    (    R   RQ   F  s     t   __str__c         C   s   |  i d |  S(   s   get contents as a stringN(   R1   RS   RT   R   (   R1   R   (    (    R   RU   P  s     RS   c   	      K   sv  |  i d j p |  i d j o | o | d } yl | i d |  i  | i d |  i |   | i d |  i    | i d d  t |  i	 |  |  Wn t
 j
 o } | Sn Xd	 Sqry3 t   } |  i	 |  } t | |  | i   SWqrt
 j
 o } | SqrXn_|  i d
 j op t i i |  i |   } |  i | _ |  i | _ |  i | _ | d	 j o
 |  } n | i |  | |  Sn |  i d j op t i i |  i |   } |  i | _ |  i | _ |  i | _ | d	 j o
 |  } n | i |  | |  Sn_ t |  i |  i |  |  i  } | i |  i  | d	 j o
 |  } n | i |  | |  Sd	 S(   sw   
        Render the document given a client object, REQUEST mapping,
        Response, and key word arguments.
        R   R   R   s   Content-Types   Last-Modifieds   Content-Lengths   Accept-Rangest   noneNR   R   (   R1   R'   R   t   respt	   setHeaderR#   RB   RO   R   R<   RF   RG   RD   RE   R   RH   t   OFSR   RC   t	   resultObjR   R   t   clientRT   t   __of__R   t   ZopePageTemplatet   pt_setTitle(	   R1   R[   R   t   kwRZ   RG   R   RE   RW   (    (    R   RS   U  sV      
		

	
t   Narrowerii    t   Wideri   t   Tallert   Shorteri(   t   manage_edits   Cancel Changest   80t   40c	      	   C   s9  | d j oI | |  _ | |  _ |  i | |  d t   i   } |  i
 d |  Sn| d j o) d } |  i
 d |  i |  d |  Sn| d j o d } d	 } | o8 | i o. | i   } | p d
 | i } q d } n | oF |  i | |  d t   i   } |  i
 d |  i |  d |  Sq5| i d d  t d d d | d d  Sn |  i | \ } } t |  i t i |  |  }
 t |  i t i |  |  } t d  d i   } | d }	 |	 i" d t# |
  d d d | |	 i" d t# |  d d d | |  i
 d | d |
 d | d d  Sd S(   s   
        Called in response to edit.dtml form submit, to save changes,
        cancel changes, replace the contents of the file with uploaded
        contents, or change the size of the edit textarea form
        control.
        s   Save Changess   Content changed. (%s)t   manage_tabs_messages   Cancel Changess   Content changes cancelled.t   externalfile_contents   Upload Filei    s,   Please specify a a non-empty file to upload.sP   Uploaded file %s is empty.
                    Please specify a non-empty file.
i   s   
s   <br/>R   t   ERRORt   messageR+   R,   t   GMTim  R   t   edit_textarea_colsR   t   /t   expirest   edit_textarea_rowss   Size changed.N($   t   SUBMITR   R1   R   RI   Rh   R   t   DateTimet   ISORj   R,   RC   t   validR   t   filenamet   readt   upload_contentst   replacet   MessageDialogt   _size_changest   colDeltat   rowDeltat   maxt   _minColsR   t   atoiRl   t   colst   _minRowsRo   t   rowst   rfc822t   eRW   t	   setCookiet   str(   R1   R   R   Rh   Rp   Rl   Ro   R   R   RW   R   Rj   R   Rv   Rs   R{   Rz   R   (    (    R   Rd     sN     		
	
""t   PUTc         C   sA   |  i | |  | d } |  i | i   |  | i d  | S(   s   Handle HTTP PUT requestst   BODYFILEi   N(   R1   t	   dav__initR   R   t   instreamRI   Ru   t	   setStatus(   R1   R   R   R   (    (    R   R     s     
t   manage_FTPgett   PrincipiaSearchSourcec         C   s,   |  i   d j o |  i |  Sn d Sd S(   s2   Support for full text search via ZCatalog indexingt   asciiR   N(   R1   R;   RC   R   (   R1   R   (    (    R   R     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.  See 'schema migration'
        discussion in README.txt

        N(    (   R1   (    (    R   t   repair  s    (   s   ExternalFiles   edit.stx(   s   ExternalFiles   view.stx(   ii    (   i   i    (   i    i   (   i    i(4   t   __name__t
   __module__t   __doc__t	   meta_typet   IExternalFilet   __implements__R(   t   _propertiesRY   t   PropertyManagert   manage_optionst
   SimpleItemt   ClassSecurityInfot	   _securityt   declareObjectPublicR6   t   declarePublicR&   R8   t   declareProtectedt	   PERM_VIEWR:   R;   R<   R>   RB   RT   RC   t	   PERM_EDITRI   t   PageTemplateFilet   globalsR,   R.   RK   RL   RO   t   PERM_CONTENTt   setPermissionDefaultRQ   RU   RS   Ry   R}   R   Rd   R   t   PERM_FTPR   t   PERM_SEARCHR   R   (    (    (    R   R   m   sp    
\	
														A*!B		(0   R   t
   __cvstag__t   __date__R2   t	   cStringIORD   t	   mimetypesR   t   os.pathR   t   GlobalsRx   RY   t   DateTime.DateTimeRq   t   webdav.commont   rfc1123_datet   AccessControlR   t'   Products.PageTemplates.PageTemplateFileR   t'   Products.PageTemplates.ZopePageTemplateR]   t   OFS.SimpleItemR   t"   Products.ZCatalog.CatalogAwarenesst   CatalogAwaret   OFS.PropertyManagerR   R   t	   FileUtilsR   t   ProductPropertiesR7   R3   R4   R   R   R   R   R   RT   R   R   t   manage_addExternalFileFormR   R   t   InitializeClass(    R   R   R7   R   R3   R   R   R   R   R   R   R2   R   R   R   R   R   RY   R]   Rq   Rx   R   RD   R   R   R   R   R   R4   R   R   R   (    (    R   t   ?
   s<   $		 