U
    Vc                     @   s   d Z ddlmZmZmZ ddlmZmZmZm	Z	m
Z
 ddlmZ ddlZddlZddlZddlZdZeeZddd	d
ddddgZddgZG dd deZG dd deZG dd deZG dd deZdS )a  
Abstract base class for DAV resource providers.

This module serves these purposes:

  1. Documentation of the DAVProvider interface
  2. Common base class for all DAV providers
  3. Default implementation for most functionality that a resource provider must
     deliver.

If no default implementation can be provided, then all write actions generate
FORBIDDEN errors. Read requests generate NOT_IMPLEMENTED errors.


**_DAVResource, DAVCollection, DAVNonCollection**

Represents an existing (i.e. mapped) WebDAV resource or collection.

A _DAVResource object is created by a call to the DAVProvider.

The resource may then be used to query different attributes like ``res.name``,
``res.is_collection``, ``res.get_content_length()``, and ``res.support_etag()``.

It also implements operations, that require an *existing* resource, like:
``get_preferred_path()``, ``create_collection()``, or ``get_property_value()``.

Usage::

    res = provider.get_resource_inst(path, environ)
    if res is not None:
        print(res.getName())



**DAVProvider**

A DAV provider represents a shared WebDAV system.

There is only one provider instance per share, which is created during
server start-up. After that, the dispatcher (``request_resolver.RequestResolver``)
parses the request URL and adds it to the WSGI environment, so it
can be accessed like this::

    provider = environ["wsgidav.provider"]

The main purpose of the provider is to create _DAVResource objects for URLs::

    res = provider.get_resource_inst(path, environ)


**Supporting Objects**
The DAVProvider takes two supporting objects:

propertyManager
   An object that provides storage for dead properties assigned for webDAV resources.

   PropertyManagers must provide the methods as described in
   ``wsgidav.interfaces.propertymanagerinterface``

   See prop_man.property_manager.PropertyManager for a sample implementation
   using shelve.

lockManager
   An object that provides storage for locks made on webDAV resources.

   LockManagers must provide the methods as described in
   ``wsgidav.interfaces.lockmanagerinterface``

   See lock_manager.LockManager for a sample implementation
   using shelve.

See :doc:`reference_guide` for more information about the WsgiDAV architecture.
    )compatutil	xml_tools)as_DAVErrorDAVErrorHTTP_FORBIDDENHTTP_NOT_FOUND#PRECONDITION_CODE_ProtectedProperty)etreeNreStructuredText{DAV:}creationdate{DAV:}displayname{DAV:}getcontenttype{DAV:}resourcetype{DAV:}getlastmodified{DAV:}getcontentlength{DAV:}getetagz{DAV:}getcontentlanguage{DAV:}lockdiscovery{DAV:}supportedlockc                   @   sx  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd^d-d.Zd/d0 Zd_d2d3Zd4d5 Zd`d6d7Zd8d9 Zd:d; Zd<d= Zd>d? Z d@dA Z!dBdC Z"dDdE Z#dadFdGZ$dHdI Z%dJdK Z&dLdM Z'dNdO Z(dPdQ Z)dRdS Z*dTdU Z+dVdW Z,dXdY Z-dZd[ Z.d\d] Z/d1S )b_DAVResourcea
  Represents a single existing DAV resource instance.

    A resource may be a collection (aka 'folder') or a non-collection (aka
    'file').
    _DAVResource is the common base class for the specialized classes::

        _DAVResource
          +- DAVCollection
          \- DAVNonCollection

    Instances of this class are created through the DAVProvider::

        res = provider.get_resource_inst(path, environ)
        if res and res.is_collection():
            print(res.get_display_name())

    In the example above, res will be ``None``, if the path cannot be mapped to
    an existing resource.
    The following attributes and methods are considered 'cheap'::

        res.path
        res.provider
        res.name
        res.is_collection
        res.environ

    Querying other attributes is considered 'expensive' and may be delayed until
    the first access.

        get_content_length()
        get_content_type()
        get_creation_date()
        get_display_name()
        get_etag()
        get_last_modified()
        support_ranges()

        support_etag()
        support_modified()
        support_content_length()

    These functions return ``None``, if the property is not available, or
    not supported.

    See also DAVProvider.get_resource_inst().
    c                 C   sR   t |st|dks$|ds$t|d | _|| _|| _|| _t	| j| _
d S )N /zwsgidav.provider)r   	is_nativeAssertionError
startswithproviderpathis_collectionenvironr   get_uri_namename)selfr   r   r    r"   $/opt/wsgidav/wsgidav/dav_provider.py__init__   s    
z_DAVResource.__init__c                 C   s   d | jj| jS )Nz{}({!r}))format	__class____name__r   r!   r"   r"   r#   __repr__   s    z_DAVResource.__repr__c                 C   s   | j r
dS tdS )a9  Contains the Content-Length header returned by a GET without accept
        headers.

        The getcontentlength property MUST be defined on any DAV compliant
        resource that returns the Content-Length header in response to a GET.

        This method MUST be implemented by non-collections only.
        Nr   NotImplementedErrorr(   r"   r"   r#   get_content_length   s    	z_DAVResource.get_content_lengthc                 C   s   | j r
dS tdS )a  Contains the Content-Type header returned by a GET without accept
        headers.

        This getcontenttype property MUST be defined on any DAV compliant
        resource that returns the Content-Type header in response to a GET.
        See http://www.webdav.org/specs/rfc4918.html#PROPERTY_getcontenttype

        This method MUST be implemented by non-collections only.
        Nr*   r(   r"   r"   r#   get_content_type   s    
z_DAVResource.get_content_typec                 C   s   dS )af  Records the time and date the resource was created.

        The creationdate property should be defined on all DAV compliant
        resources. If present, it contains a timestamp of the moment when the
        resource was created (i.e., the moment it had non-null state).

        This method SHOULD be implemented, especially by non-collections.
        Nr"   r(   r"   r"   r#   get_creation_date   s    	z_DAVResource.get_creation_datec                 C   s   | j s
tdS )a  Return a list of dictionaries with information for directory
        rendering.

        This default implementation return None, so the dir browser will
        traverse all members.

        This method COULD be implemented for collection resources.
        N)r   r   r(   r"   r"   r#   get_directory_info   s    	
z_DAVResource.get_directory_infoc                 C   s   | j S )a  Provides a name for the resource that is suitable for presentation to
        a user.

        The displayname property should be defined on all DAV compliant
        resources. If present, the property contains a description of the
        resource that is suitable for presentation to a user.

        This default implementation returns `name`, which is the last path
        segment.
        )r    r(   r"   r"   r#   get_display_name   s    z_DAVResource.get_display_namec                 C   sR   | j rddiS tj| jkrJ| jtjd  }t|dk rJdd|iS ddiS )zReturn additional info dictionary for displaying (optional).

        This information is not part of the DAV specification, but meant for use
        by the dir browser middleware.

        This default implementation returns ``{'type': '...'}``
        typeZ	Directory   z{}-FileZFile)r   osextsepr    splitupperlenr%   )r!   extr"   r"   r#   get_display_info   s    z_DAVResource.get_display_infoc                 C   s   dS )z
        See http://www.webdav.org/specs/rfc4918.html#PROPERTY_getetag

        This method SHOULD be implemented, especially by non-collections.
        Nr"   r(   r"   r"   r#   get_etag
  s    z_DAVResource.get_etagc                 C   s   dS )a  Contains the Last-Modified header returned by a GET method without
        accept headers.

        Return None, if this live property is not supported.

        Note that the last-modified date on a resource may reflect changes in
        any part of the state of the resource, not necessarily just a change to
        the response to the GET method. For example, a change in a property may
        cause the last-modified date to change. The getlastmodified property
        MUST be defined on any DAV compliant resource that returns the
        Last-Modified header in response to a GET.

        This method SHOULD be implemented, especially by non-collections.
        Nr"   r(   r"   r"   r#   get_last_modified  s    z_DAVResource.get_last_modifiedc                 C   s   t dS )z@Set last modified time for destPath to timeStamp on epoch-formatNr+   )r!   	dest_path
time_stampdry_runr"   r"   r#   set_last_modified#  s    z_DAVResource.set_last_modifiedc                 C   s   t dS )zReturn True, if this non-resource supports Range on GET requests.

        This method MUST be implemented by non-collections only.
        Nr=   r(   r"   r"   r#   support_ranges'  s    z_DAVResource.support_rangesc                 C   s   |   dk	S )zReturn True, if this resource supports Content-Length.

        This default implementation checks `self.get_content_length() is None`.
        N)r,   r(   r"   r"   r#   support_content_length.  s    z#_DAVResource.support_content_lengthc                 C   s   |   dk	S )z}Return True, if this resource supports ETags.

        This default implementation checks `self.get_etag() is None`.
        N)r;   r(   r"   r"   r#   support_etag5  s    z_DAVResource.support_etagc                 C   s   |   dk	S )zReturn True, if this resource supports last modified dates.

        This default implementation checks `self.get_last_modified() is None`.
        N)r<   r(   r"   r"   r#   support_modified<  s    z_DAVResource.support_modifiedc                 C   s0   | j dkrdS | jr*| j ds*| j d S | j S )a  Return preferred mapping for a resource mapping.

        Different URLs may map to the same resource, e.g.:
            '/a/b' == '/A/b' == '/a/b/'
        get_preferred_path() returns the same value for all these variants, e.g.:
            '/a/b/'   (assuming resource names considered case insensitive)

        @param path: a UTF-8 encoded, unquoted byte string.
        @return: a UTF-8 encoded, unquoted byte string.
        r   r   r   )r   r   endswithr(   r"   r"   r#   get_preferred_pathC  s
    

z_DAVResource.get_preferred_pathc                 C   s   t | jj|   S )a  Return the quoted, absolute, unique URL of a resource, relative to appRoot.

        Byte string, UTF-8 encoded, quoted.
        Starts with a '/'. Collections also have a trailing '/'.

        This is basically the same as get_preferred_path, but deals with
        'virtual locations' as well.

        e.g. '/a/b' == '/A/b' == '/bykey/123' == '/byguid/abc532'

        get_ref_url() returns the same value for all these URLs, so it can be
        used as a key for locking and persistence storage.

        DAV providers that allow virtual-mappings must override this method.

        See also comments in DEVELOPERS.txt glossary.
        )r   quoter   
share_pathrH   r(   r"   r"   r#   get_ref_urlZ  s    z_DAVResource.get_ref_urlc                 C   s&   d}t j| jj| jj |   |dS )a*  Convert path to a URL that can be passed to XML responses.

        Byte string, UTF-8 encoded, quoted.

        See http://www.webdav.org/specs/rfc4918.html#rfc.section.8.3
        We are using the path-absolute option. i.e. starting with '/'.
        URI ; See section 3.2.1 of [RFC2068]
        z/!*'(),$-_|.)safe)r   rI   r   
mount_pathrJ   rH   )r!   rL   r"   r"   r#   get_href|  s    z_DAVResource.get_hrefc                 C   s@   | j s
tg }|  D ]$}| |}|dk	s0t|| q|S )zReturn a list of direct members (_DAVResource or derived objects).

        This default implementation calls self.get_member_names() and
        self.get_member() for each of them.
        A provider COULD overwrite this for performance reasons.
        N)r   r+   get_member_names
get_memberr   append)r!   Z
memberListr    memberr"   r"   r#   get_member_list  s    
z_DAVResource.get_member_listc                 C   s   t dS )zReturn list of (direct) collection member names (UTF-8 byte strings).

        Every provider MUST provide this method for collection resources.
        Nr=   r(   r"   r"   r#   rO     s    z_DAVResource.get_member_namesTFinfinityc           	   
   C   s   |dkst g }|r"|s"||  |dkr| jr|  D ]r}|sH|   |rR|jp\|o\|j }|rp|sp|| |jr|dkr||j||||dd |r8|r8|| q8|r|r||  |S )ac  Return a list _DAVResource objects of a collection (children,
        grand-children, ...).

        This default implementation calls self.get_member_list() recursively.

        This function may also be called for non-collections (with add_self=True).

        :Parameters:
            depth_first : bool
                use <False>, to list containers before content.
                (e.g. when moving / copying branches.)
                Use <True>, to list content before containers.
                (e.g. when deleting branches.)
            depth : string
                '0' | '1' | 'infinity'
        )01rT   rU   rT   F)add_self)r   rQ   r   rS   extendget_descendants)	r!   collectionsZ	resourcesdepth_firstdepthrW   reschildZwantr"   r"   r#   rY     s8    



    
z_DAVResource.get_descendantsc                 C   s   g }| d |  dk	r$| d |  dk	rD| jr:t| d |  dk	rZ| d |  dk	rp| d |  dk	r| d |  dk	r| d | j	j
r|  s|t | j	jr|  }|| j	j|| j |S )	a  Return list of supported property names in Clark Notation.

        Note that 'allprop', despite its name, which remains for
        backward-compatibility, does not return every property, but only dead
        properties and the live properties defined in RFC4918.

        This default implementation returns a combination of:

        - Supported standard live properties in the {DAV:} namespace, if the
          related getter method returns not None.
        - {DAV:}lockdiscovery and {DAV:}supportedlock, if a lock manager is
          present
        - If a property manager is present, then a list of dead properties is
          appended

        A resource provider may override this method, to add a list of
        supported custom live property names.
        r   Nr   r   r   r   r   r   )rQ   r.   r,   r   r   r-   r<   r0   r;   r   lock_managerprevent_lockingrX   _lockPropertyNamesprop_managerrK   get_propertiesr   )r!   Z
is_allproppropNameListrefUrlr"   r"   r#   get_property_names  s.    








z_DAVResource.get_property_namesNc                 C   s  |dkst |dkr0|dks t | |dk}n|dk	s<t g }|dk}|D ]}z0|rf||df n| |}|||f W qL tk
r } z|||f W 5 d}~X Y qL tk
r } z0||t|f | jjdkrt	
dtj W 5 d}~X Y qLX qL|S )a  Return properties as list of 2-tuples (name, value).

        If mode is 'name', then None is returned for the value.

        name
            the property name in Clark notation.
        value
            may have different types, depending on the status:
            - string or unicode: for standard property values.
            - etree.Element: for complex values.
            - DAVError in case of errors.
            - None: if mode == 'name'.

        @param mode: "allprop", "name", or "named"
        @param name_list: list of property names in Clark Notation (required for mode 'named')

        This default implementation basically calls self.get_property_names() to
        get the list of names, then call self.get_property_value on each of them.
        )allpropr    named)rg   r    Nrg   r       
   )r   rf   rQ   get_property_valuer   	Exceptionr   r   verbose	traceback	print_excsysstdout)r!   mode	name_listpropListZ	namesOnlyr    valueer"   r"   r#   rc     s(    
"z_DAVResource.get_propertiesc              	   C   sV  |   }| jj}|rn|dkrn||}t|}|D ].}t|d}t|d}t|dd|d  t|d}	t|	dd|d  |d	 t|d
_|d rt	
|d }
||
 |d }|dk rd}n |d }dtt|t   }|t|d_t|d}|d t|d_| j|d }| j|| j}| }t|d}|t|d_q8|S |r|dkrt|}t|d}t|d}	t|	d t|d}t|d t|d}t|d}	t|	d t|d}t|d |S |dr|dkr6|  dk	r6t|  S |dkrV|  dk	rV|  S |dkr| jrt|}t|d  |S d!S |d"kr|  dk	rt|  S |d#kr|  dk	rt|  S |d$kr|  dk	r|  S |d%kr|  dk	r|  S tt| jj }|rJ|!||| j}|dk	rJt	
|S ttdS )&a  Return the value of a property.

        name:
            the property name in Clark notation.
        return value:
            may have different types, depending on the status:

            - string or unicode: for standard property values.
            - lxml.etree.Element: for complex values.

            If the property is not available, a DAVError is raised.

        This default implementation handles ``{DAV:}lockdiscovery`` and
        ``{DAV:}supportedlock`` using the associated lock manager.

        All other *live* properties (i.e. name starts with ``{DAV:}``) are
        delegated to the self.xxx() getters.

        Finally, other properties are considered *dead*, and are handled  by
        the associated property manager.
        r   z{DAV:}activelockz{DAV:}locktypez{}{}{DAV:}r1   z{DAV:}lockscopescoper\   z{DAV:}depthownertimeoutr   ZInfiniteexpirezSecond-z{DAV:}timeoutz{DAV:}locktokentokenz
{DAV:}hrefrootz{DAV:}lockrootr   z{DAV:}lockentryz{DAV:}exclusivez{DAV:}writez{DAV:}sharedr   Nr   r   z{DAV:}collectionr   r   r   r   r   )"rK   r   r_   Zget_url_lock_listr
   Element
SubElementr%   textr   string_to_xmlrQ   strinttimeref_url_to_pathget_resource_instr   rN   r   r.   r   get_rfc3339_timer-   r   r<   get_rfc1123_timer,   r;   r0   r   r   rb   Zget_property)r!   r    re   ZlmZactivelocklistZlockdiscoveryELlockZactivelockELZ
locktypeELZlockscopeELZownerELrz   r{   ZlocktokenELZlockPathZlockResZlockHrefZ
lockrootELZsupportedlockELZlockentryELZresourcetypeELpmru   r"   r"   r#   rk   F  s    










z_DAVResource.get_property_valuec           
      C   s  |dkst |st|tkr*tttd| jd }|dg }|	dr|t
kr||kr|dkrz| | j|j|W S  tk
r   td| j Y nX tt|	dr*| jd	d
}|di dd}|r*d|kr*d|kr | | j|j|S d|krdS d|krdS d|kr*dS | jj}|r|	ds|  }	|dkrh||	||| jS t|}||	|||| jS ttdS )a  Set a property value or remove a property.

        value == None means 'remove property'.
        Raise HTTP_FORBIDDEN if property is read-only, or not supported.

        When dry_run is True, this function should raise errors, as in a real
        run, but MUST NOT change any data.

        This default implementation

        - raises HTTP_FORBIDDEN, if trying to modify a locking property
        - raises HTTP_FORBIDDEN, if trying to modify an immutable {DAV:}
          property
        - handles Windows' Win32LastModifiedTime to set the getlastmodified
          property, if enabled
        - stores everything else as dead property, if a property manager is
          present.
        - raises HTTP_FORBIDDEN, else

        Removing a non-existing prop is NOT an error.

        Note: RFC 4918 states that {DAV:}displayname 'SHOULD NOT be protected'

        A resource provider may override this method, to update supported custom
        live properties.
        N)err_conditionzwsgidav.configmutable_live_propsrw   )r   z{DAV:}last_modifiedz2Provider does not support set_last_modified on {}.z{urn:schemas-microsoft-com:}HTTP_USER_AGENTNonehotfixesemulate_win32_lastmodFzMiniRedir/6.1ZWin32LastModifiedTimeZOSVWin32FileAttributesTZWin32CreationTimezOSV_DISABLE Win32LastAccessTime)r   is_etree_elementr   ra   r   r   r	   r   getr   _standardLivePropNamesrA   r   r   rl   _loggerwarningr%   r   rb   rK   Zremove_propertyr
   tostringZwrite_property)
r!   r    ru   r@   configZmutableLivePropsagentZ	win32_emur   re   r"   r"   r#   set_property_value  sX     








z_DAVResource.set_property_valuec                 C   s"   | j jr| j j|  | j dS )z&Remove all associated dead properties.N)r   rb   Zremove_propertiesrK   r   r!   	recursiver"   r"   r#   remove_all_properties*  s
     z"_DAVResource.remove_all_propertiesc                 C   s   dS )zReturn True, to prevent locking.

        This default implementation returns ``False``, so standard processing
        takes place: locking (and refreshing of locks) is implemented using
        the lock manager, if one is configured.
        Fr"   r(   r"   r"   r#   r`   3  s    z_DAVResource.prevent_lockingc                 C   s"   | j jdkrdS | j j|  S )zReturn True, if URI is locked.NF)r   r_   Zis_url_lockedrK   r(   r"   r"   r#   	is_locked<  s    z_DAVResource.is_lockedc                 C   s   | j jr| j j|   d S N)r   r_   Zremove_all_locks_from_urlrK   r   r"   r"   r#   remove_all_locksB  s    z_DAVResource.remove_all_locksc                 C   s   | j s
tttdS a  Create and return an empty (length-0) resource as member of self.

        Called for LOCK requests on unmapped URLs.

        Preconditions (to be ensured by caller):

          - this must be a collection
          - <self.path + name> must not exist
          - there must be no conflicting locks

        Returns a DAVResuource.

        This method MUST be implemented by all providers that support write
        access.
        This default implementation simply raises HTTP_FORBIDDEN.
        Nr   r   r   r   r!   r    r"   r"   r#   create_empty_resourceH  s    
z"_DAVResource.create_empty_resourcec                 C   s   | j s
tttdS a  Create a new collection as member of self.

        Preconditions (to be ensured by caller):

          - this must be a collection
          - <self.path + name> must not exist
          - there must be no conflicting locks

        This method MUST be implemented by all providers that support write
        access.
        This default implementation raises HTTP_FORBIDDEN.
        Nr   r   r"   r"   r#   create_collection\  s    
z_DAVResource.create_collectionc                 C   s   | j r
ttdS )a
  Open content as a stream for reading.

        Returns a file-like object / stream containing the contents of the
        resource specified.
        The calling application will close() the stream.

        This method MUST be implemented by all providers.
        Nr   r   r+   r(   r"   r"   r#   get_contentl  s    	
z_DAVResource.get_contentc                 C   s   | j r
tttdS zOpen content as a stream for writing.

        This method MUST be implemented by all providers that support write
        access.
        Nr   r!   content_typer"   r"   r#   begin_writex  s    
z_DAVResource.begin_writec                 C   s   dS )ziCalled when PUT has finished writing.

        This is only a notification. that MAY be handled.
        Nr"   r!   with_errorsr"   r"   r#   	end_write  s    z_DAVResource.end_writec                 C   s   dS )a  Handle a DELETE request natively.

        This method is called by the DELETE handler after checking for valid
        request syntax and making sure that there are no conflicting locks and
        If-headers.
        Depending on the return value, this provider can control further
        processing:

        False:
            handle_delete() did not do anything. WsgiDAV will process the request
            by calling delete() for every resource, bottom-up.
        True:
            handle_delete() has successfully performed the DELETE request.
            HTTP_NO_CONTENT will be reported to the DAV client.
        List of errors:
            handle_delete() tried to perform the delete request, but failed
            completely or partially. A list of errors is returned like
            ``[ (<ref-url>, <DAVError>), ... ]``
            These errors will be reported to the client.
        DAVError raised:
            handle_delete() refuses to perform the delete request. The DAVError
            will be reported to the client.

        An implementation may choose to apply other semantics and return True.
        For example deleting '/by_tag/cool/myres' may simply remove the 'cool'
        tag from 'my_res'.
        In this case, the resource might still be available by other URLs, so
        locks and properties are not removed.

        This default implementation returns ``False``, so standard processing
        takes place.

        Implementation of this method is OPTIONAL.
        Fr"   r(   r"   r"   r#   handle_delete  s    #z_DAVResource.handle_deletec                 C   s   | j s
ttdS )zReturn True, if delete() may be called on non-empty collections
        (see comments there).

        This method MUST be implemented for collections (not called on
        non-collections).
        Nr   r(   r"   r"   r#   support_recursive_delete  s    
z%_DAVResource.support_recursive_deletec                 C   s   t dS )a  Remove this resource (recursive).

        Preconditions (ensured by caller):

          - there are no conflicting locks or If-headers
          - if support_recursive_delete() is False, and this is a collection,
            all members have already been deleted.

        When support_recursive_delete is True, this method must be prepared to
        handle recursive deletes. This implies that child errors must be
        reported as tuple list [ (<ref-url>, <DAVError>), ... ].
        See http://www.webdav.org/specs/rfc4918.html#delete-collections

        This function

          - removes this resource
          - if this is a non-empty collection, also removes all members.
            Note that this may only occur, if support_recursive_delete is True.
          - For recursive deletes, return a list of error tuples for all failed
            resource paths.
          - removes associated direct locks
          - removes associated dead properties
          - raises HTTP_FORBIDDEN for read-only resources
          - raises HTTP_INTERNAL_ERROR on error

        This method MUST be implemented by all providers that support write
        access.
        Nr=   r(   r"   r"   r#   delete  s    z_DAVResource.deletec                 C   s   dS )a  Handle a COPY request natively.

        This method is called by the COPY handler after checking for valid
        request syntax and making sure that there are no conflicting locks and
        If-headers.
        Depending on the return value, this provider can control further
        processing:

        False:
            handle_copy() did not do anything. WsgiDAV will process the request
            by calling copy_move_single() for every resource, bottom-up.
        True:
            handle_copy() has successfully performed the COPY request.
            HTTP_NO_CONTENT/HTTP_CREATED will be reported to the DAV client.
        List of errors:
            handle_copy() tried to perform the copy request, but failed
            completely or partially. A list of errors is returned like
            ``[ (<ref-url>, <DAVError>), ... ]``
            These errors will be reported to the client.
        DAVError raised:
            handle_copy() refuses to perform the copy request. The DAVError
            will be reported to the client.

        An implementation may choose to apply other semantics and return True.
        For example copying '/by_tag/cool/myres' to '/by_tag/hot/myres' may
        simply add a 'hot' tag.
        In this case, the resource might still be available by other URLs, so
        locks and properties are not removed.

        This default implementation returns ``False``, so standard processing
        takes place.

        Implementation of this method is OPTIONAL.
        Fr"   )r!   r>   Zdepth_infinityr"   r"   r#   handle_copy  s    #z_DAVResource.handle_copyc                 C   s   t dS )a  Copy or move this resource to destPath (non-recursive).

        Preconditions (ensured by caller):

          - there must not be any conflicting locks on destination
          - overwriting is only allowed (i.e. destPath exists), when source and
            dest are of the same type ((non-)collections) and a Overwrite='T'
            was passed
          - destPath must not be a child path of this resource

        This function

          - Overwrites non-collections content, if destination exists.
          - MUST NOT copy collection members.
          - MUST NOT copy locks.
          - SHOULD copy live properties, when appropriate.
            E.g. displayname should be copied, but creationdate should be
            reset if the target did not exist before.
            See http://www.webdav.org/specs/rfc4918.html#dav.properties
          - SHOULD copy dead properties.
          - raises HTTP_FORBIDDEN for read-only providers
          - raises HTTP_INTERNAL_ERROR on error

        When is_move is True,

          - Live properties should be moved too (e.g. creationdate)
          - Non-collections must be moved, not copied
          - For collections, this function behaves like in copy-mode:
            detination collection must be created and properties are copied.
            Members are NOT created.
            The source collection MUST NOT be removed.

        This method MUST be implemented by all providers that support write
        access.
        Nr=   r!   r>   is_mover"   r"   r#   copy_move_single  s    $z_DAVResource.copy_move_singlec                 C   s   dS )a9  Handle a MOVE request natively.

        This method is called by the MOVE handler after checking for valid
        request syntax and making sure that there are no conflicting locks and
        If-headers.
        Depending on the return value, this provider can control further
        processing:

        False:
            handle_move() did not do anything. WsgiDAV will process the request
            by calling delete() and copy_move_single() for every resource,
            bottom-up.
        True:
            handle_move() has successfully performed the MOVE request.
            HTTP_NO_CONTENT/HTTP_CREATED will be reported to the DAV client.
        List of errors:
            handle_move() tried to perform the move request, but failed
            completely or partially. A list of errors is returned like
            ``[ (<ref-url>, <DAVError>), ... ]``
            These errors will be reported to the client.
        DAVError raised:
            handle_move() refuses to perform the move request. The DAVError
            will be reported to the client.

        An implementation may choose to apply other semantics and return True.
        For example moving '/by_tag/cool/myres' to '/by_tag/hot/myres' may
        simply remove the 'cool' tag from 'my_res' and add a 'hot' tag instead.
        In this case, the resource might still be available by other URLs, so
        locks and properties are not removed.

        This default implementation returns ``False``, so standard processing
        takes place.

        Implementation of this method is OPTIONAL.
        Fr"   r!   r>   r"   r"   r#   handle_move!  s    $z_DAVResource.handle_movec                 C   s   | j s
ttdS )CReturn True, if move_recursive() is available (see comments there).Nr   r   r"   r"   r#   support_recursive_moveG  s    
z#_DAVResource.support_recursive_movec                 C   s   t tdS )a  Move this resource and members to destPath.

        This method is only called, when support_recursive_move() returns True.

        MOVE is frequently used by clients to rename a file without changing its
        parent collection, so it's not appropriate to reset all live properties
        that are set at resource creation. For example, the DAV:creationdate
        property value SHOULD remain the same after a MOVE.

        Preconditions (ensured by caller):

          - there must not be any conflicting locks or If-header on source
          - there must not be any conflicting locks or If-header on destination
          - destPath must not exist
          - destPath must not be a member of this resource

        This method must be prepared to handle recursive moves. This implies
        that child errors must be reported as tuple list
        [ (<ref-url>, <DAVError>), ... ].
        See http://www.webdav.org/specs/rfc4918.html#move-collections

        This function

          - moves this resource and all members to destPath.
          - MUST NOT move associated locks.
            Instead, if the source (or children thereof) have locks, then
            these locks should be removed.
          - SHOULD maintain associated live properties, when applicable
            See http://www.webdav.org/specs/rfc4918.html#dav.properties
          - MUST maintain associated dead properties
          - raises HTTP_FORBIDDEN for read-only resources
          - raises HTTP_INTERNAL_ERROR on error

        An implementation may choose to apply other semantics.
        For example copying '/by_tag/cool/myres' to '/by_tag/new/myres' may
        simply add a 'new' tag to 'my_res'.

        This method is only called, when self.support_recursive_move() returns
        True. Otherwise, the request server implements MOVE using delete/copy.

        This method MAY be implemented in order to improve performance.
        Nr   r   r   r"   r"   r#   move_recursiveL  s    +z_DAVResource.move_recursivec                 C   s   t dS )zReturn a _DAVResource object for the path (None, if not found).

        `path_info`: is a URL relative to this object.

        DAVCollection.resolve() provides an implementation.
        Nr=   r!   script_name	path_infor"   r"   r#   resolvey  s    z_DAVResource.resolvec                 C   s   dS )zPerform custom operations on the response headers.

        This gets called before the response is started.
        It enables adding additional headers or modifying the default ones.
        Nr"   )r!   r   response_headersr"   r"   r#   finalize_headers  s    z_DAVResource.finalize_headers)TTFrT   F)N)F)N)0r'   
__module____qualname____doc__r$   r)   r,   r-   r.   r/   r0   r:   r;   r<   rA   rB   rC   rD   rE   rH   rK   rN   rS   rO   rY   rf   rc   rk   r   r   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r   v   sf   /	"	     
33
0 

[		
	%
%&&-	r   c                   @   sR   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdddZ	dd Z
dd ZdS )DAVNonCollectionz
    A DAVNonCollection is a _DAVResource, that has content (like a 'file' on
    a filesystem).

    A DAVNonCollecion is able to read and write file content.

    See also _DAVResource
    c                 C   s   t | |d| d S NFr   r$   r!   r   r   r"   r"   r#   r$     s    zDAVNonCollection.__init__c                 C   s   t dS )zReturns the byte length of the content.

        MUST be implemented.

        See also _DAVResource.get_content_length()
        Nr=   r(   r"   r"   r#   r,     s    z#DAVNonCollection.get_content_lengthc                 C   s   t dS )a?  Contains the Content-Type header returned by a GET without accept
        headers.

        This getcontenttype property MUST be defined on any DAV compliant
        resource that returns the Content-Type header in response to a GET.
        See http://www.webdav.org/specs/rfc4918.html#PROPERTY_getcontenttype
        Nr=   r(   r"   r"   r#   r-     s    z!DAVNonCollection.get_content_typec                 C   s   t dS )a  Open content as a stream for reading.

        Returns a file-like object / stream containing the contents of the
        resource specified.
        The application will close() the stream.

        This method MUST be implemented by all providers.
        Nr=   r(   r"   r"   r#   r     s    	zDAVNonCollection.get_contentc                 C   s   dS )z~Return True, if this non-resource supports Range on GET requests.

        This default implementation returns False.
        Fr"   r(   r"   r"   r#   rB     s    zDAVNonCollection.support_rangesNc                 C   s   t tdS r   r   r   r"   r"   r#   r     s    zDAVNonCollection.begin_writec                 C   s   dS )zhCalled when PUT has finished writing.

        This is only a notification that MAY be handled.
        Nr"   r   r"   r"   r#   r     s    zDAVNonCollection.end_writec                 C   s   |dkr| S dS )zReturn a _DAVResource object for the path (None, if not found).

        Since non-collection don't have members, we return None if path is not
        empty.
        rF   Nr"   r   r"   r"   r#   r     s    zDAVNonCollection.resolve)N)r'   r   r   r   r$   r,   r-   r   rB   r   r   r   r"   r"   r"   r#   r     s   		

r   c                   @   sx   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )DAVCollectiona  
    A DAVCollection is a _DAVResource, that has members (like a 'folder' on
    a filesystem).

    A DAVCollecion 'knows' its members, and how to obtain them from the backend
    storage.
    There is also optional built-in support for member caching.

    See also _DAVResource
    c                 C   s   t | |d| d S )NTr   r   r"   r"   r#   r$     s    zDAVCollection.__init__c                 C   s   d S r   r"   r(   r"   r"   r#   r,     s    z DAVCollection.get_content_lengthc                 C   s   d S r   r"   r(   r"   r"   r#   r-     s    zDAVCollection.get_content_typec                 C   s   t tdS r   r   r   r"   r"   r#   r     s    z#DAVCollection.create_empty_resourcec                 C   s   | j s
tttdS r   r   r   r"   r"   r#   r   )  s    
zDAVCollection.create_collectionc                 C   s$   | j s
t| jt| j|| jS )zReturn child resource with a given name (None, if not found).

        This method COULD be overridden by a derived class, for performance
        reasons.
        This default implementation calls self.provider.get_resource_inst().
        )r   r   r   r   r   join_urir   r   r   r"   r"   r#   rP   9  s
    
 zDAVCollection.get_memberc                 C   s   | j s
ttdS )zxReturn list of (direct) collection member names (UTF-8 byte strings).

        This method MUST be implemented.
        Nr   r(   r"   r"   r#   rO   E  s    
zDAVCollection.get_member_namesc                 C   s   dS )zReturn True, if delete() may be called on non-empty collections
        (see comments there).

        This default implementation returns False.
        Fr"   r(   r"   r"   r#   r   M  s    z&DAVCollection.support_recursive_deletec                 C   s   t tdS )zRemove this resource (possibly recursive).

        This method MUST be implemented if resource allows write access.

        See _DAVResource.delete()
        Nr   r(   r"   r"   r#   r   U  s    zDAVCollection.deletec                 C   s   t tdS )zCopy or move this resource to destPath (non-recursive).

        This method MUST be implemented if resource allows write access.

        See _DAVResource.copy_move_single()
        Nr   r   r"   r"   r#   r   ^  s    zDAVCollection.copy_move_singlec                 C   s   dS )r   Fr"   r   r"   r"   r#   r   g  s    z$DAVCollection.support_recursive_movec                 C   s   t tdS )z}Move this resource and members to destPath.

        This method MAY be implemented in order to improve performance.
        Nr   r   r"   r"   r#   r   k  s    zDAVCollection.move_recursivec                 C   sZ   |dkr| S | dstt|\}}| |}|dksB|dkrF|S |t|||S )zReturn a _DAVResource object for the path (None, if not found).

        `path_info`: is a URL relative to this object.
        rF   r   N)r   r   r   pop_pathrP   r   r   )r!   r   r   r    restr]   r"   r"   r#   r   r  s    
zDAVCollection.resolveN)r'   r   r   r   r$   r,   r-   r   r   rP   rO   r   r   r   r   r   r   r"   r"   r"   r#   r     s   '		r   c                   @   sp   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )DAVProviderzAbstract base class for DAV resource providers.

    There will be only one DAVProvider instance per share (not per request).
    c                 C   s.   d| _ d | _d | _d | _d| _d| _d| _d S )Nr      r   )rM   rJ   r_   rb   rm   _count_get_resource_instZ_count_get_resource_inst_initr(   r"   r"   r#   r$     s    zDAVProvider.__init__c                 C   s   | j jS r   )r&   r'   r(   r"   r"   r#   r)     s    zDAVProvider.__repr__c                 C   s   dS r   r"   r(   r"   r"   r#   is_readonly  s    zDAVProvider.is_readonlyc                 C   s    |dks| drt|| _dS )zSet application root for this resource provider.

        This is the value of SCRIPT_NAME, when WsgiDAVApp is called.
        rF   r   N)rG   r   rM   )r!   rM   r"   r"   r#   set_mount_path  s    zDAVProvider.set_mount_pathc                 C   sB   |dks| dst|dkr"d}|dks8|dr8t|| _dS )zSet application location for this resource provider.

        @param share_path: a UTF-8 encoded, unquoted byte string.
        r   r   rF   N)r   r   rG   rJ   )r!   rJ   r"   r"   r#   set_share_path  s
    zDAVProvider.set_share_pathc                 C   s    |rt |dstd|| _d S )Ncheck_write_permissionz8Must be compatible with wsgidav.lock_manager.LockManager)hasattrr   r_   )r!   r_   r"   r"   r#   set_lock_manager  s     zDAVProvider.set_lock_managerc                 C   s    |rt |dstd|| _d S )Ncopy_propertieszIMust be compatible with wsgidav.prop_man.property_manager.PropertyManager)r   r   rb   )r!   rb   r"   r"   r#   set_prop_manager  s     zDAVProvider.set_prop_managerc                 C   s   dt t|| jd S )zConvert a refUrl to a path, by stripping the share prefix.

        Used to calculate the <path> from a storage key by inverting get_ref_url().
        r   )r   unquoter   	lstripstrrJ   lstrip)r!   ref_urlr"   r"   r#   r     s    zDAVProvider.ref_url_to_pathc                 C   s   t dS )a  Return a _DAVResource object for path.

        Should be called only once per request and resource::

            res = provider.get_resource_inst(path, environ)
            if res and not res.is_collection:
                print(res.get_content_type())

        If <path> does not exist, None is returned.
        <environ> may be used by the provider to implement per-request caching.

        See _DAVResource for details.

        This method MUST be implemented.
        Nr=   r   r"   r"   r#   r     s    zDAVProvider.get_resource_instc                 C   s   |  ||dk	S )a  Return True, if path maps to an existing resource.

        This method should only be used, if no other information is queried
        for <path>. Otherwise a _DAVResource should be created first.

        This method SHOULD be overridden by a more efficient implementation.
        N)r   r   r"   r"   r#   exists  s    zDAVProvider.existsc                 C   s   |  ||}|o|jS )zReturn True, if path maps to an existing collection resource.

        This method should only be used, if no other information is queried
        for <path>. Otherwise a _DAVResource should be created first.
        )r   r   )r!   r   r   r]   r"   r"   r#   r     s    zDAVProvider.is_collectionc                 C   s
   |||S )zOptionally implement custom request handling.

        requestmethod = environ["REQUEST_METHOD"]
        Either

        - handle the request completely
        - do additional processing and call default_handler(environ, start_response)
        r"   )r!   r   start_responseZdefault_handlerr"   r"   r#   custom_request_handler  s    	z"DAVProvider.custom_request_handlerN)r'   r   r   r   r$   r)   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r     s   	
	r   )r   wsgidavr   r   r   wsgidav.dav_errorr   r   r   r   r	   Zwsgidav.utilr
   r4   rp   r   rn   __docformat__get_module_loggerr'   r   r   ra   objectr   r   r   r   r"   r"   r"   r#   <module>   s@   I
         O *