Ë
    "ïàj¾  ã                   óö   — d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZmZmZ ddlmZ d	Z e	j"                  e«      Z G d
„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Zy)a7  
Implementation of a WebDAV provider that provides a very basic, read-only
resource layer emulation of a MongoDB database.

Usage: add the following entries to wsgidav.conf::

    from wsgidav.samples.mongo_dav_provider import MongoResourceProvider
    mongo_dav_opts = {}
    addShare("mongo", MongoResourceProvider(mongo_dav_opts))

Valid options are (sample shows defaults)::

    opts = {"host": "localhost",       # MongoDB server
            "port": 27017,             # MongoDB port
            # This options are used with `mongod --auth`
            # The user must be created in the admin db with
            # > use admin
            # > db.addUser(user_name, password)
            "user": None,              # Authenticate with this user
            "pwd": None,               # ... and password
            }

é    )ÚStringIO)ÚpformatN)ÚObjectId)Úutil)ÚDAVCollectionÚDAVNonCollectionÚDAVProvider)Újoin_uriÚreStructuredTextc                   ó.   ‡ — e Zd ZdZˆ fd„Zd„ Zd„ Zˆ xZS )ÚConnectionCollectionz+Root collection, lists all mongo databases.c                 ó\   •— t         ‰| �  ||«       | j                  j                  | _        y ©N)ÚsuperÚ__init__ÚproviderÚconn©ÚselfÚpathÚenvironÚ	__class__s      €úQ/srv/dav/.venv/lib/python3.12/site-packages/wsgidav/samples/mongo_dav_provider.pyr   zConnectionCollection.__init__0   s#   ø€ Ü‰Ñ˜˜wÔ'Ø—M‘M×&Ñ&ˆ�	ó    c                 óz   — | j                   j                  «       D �cg c]  }|j                  d«      ‘Œ c}S c c}w ©NÚutf8)r   Údatabase_namesÚencode©r   Únames     r   Úget_member_namesz%ConnectionCollection.get_member_names4   s,   € Ø04·	±	×0HÑ0HÓ0JÖK¨�—‘˜FÕ#ÒKÐKùÒKó   �8c                 óV   — t        t        | j                  |«      | j                  «      S r   )ÚDbCollectionr
   r   r   r    s     r   Ú
get_memberzConnectionCollection.get_member7   s   € ÜœH T§Y¡Y°Ó5°t·|±|ÓDÐDr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r&   Ú__classcell__©r   s   @r   r   r   -   s   ø„ Ù5ô'òLöEr   r   c                   ó4   ‡ — e Zd ZdZˆ fd„Zd„ Zd„ Zd„ Zˆ xZS )r%   z+Mongo database, contains mongo collections.c                 ó˜   •— t         ‰| �  ||«       | j                  j                  | _        | j                  | j                     | _        y r   )r   r   r   r   r!   Údbr   s      €r   r   zDbCollection.__init__>   s7   ø€ Ü‰Ñ˜˜wÔ'Ø—M‘M×&Ñ&ˆŒ	Ø—)‘)˜DŸI™IÑ&ˆ�r   c                 ó
   — ddiS )NÚtypezMongo database© ©r   s    r   Úget_display_infozDbCollection.get_display_infoC   ó   € ØÐ(Ð)Ð)r   c                 óz   — | j                   j                  «       D �cg c]  }|j                  d«      ‘Œ c}S c c}w r   )r/   Úcollection_namesr   r    s     r   r"   zDbCollection.get_member_namesF   s,   € Ø04·±×0HÑ0HÓ0JÖK¨�—‘˜FÕ#ÒKÐKùÒKr#   c                 óv   — | j                   |   }t        t        | j                  |«      | j                  |«      S r   )r/   ÚCollCollectionr
   r   r   )r   r!   Úcolls      r   r&   zDbCollection.get_memberI   s-   € Ø�w‰w�t‰}ˆÜœh t§y¡y°$Ó7¸¿¹ÀtÓLÐLr   ©	r'   r(   r)   r*   r   r4   r"   r&   r+   r,   s   @r   r%   r%   ;   s   ø„ Ù5ô'ò
*òLöMr   r%   c                   ó4   ‡ — e Zd ZdZˆ fd„Zd„ Zd„ Zd„ Zˆ xZS )r9   z,Mongo collections, contains mongo documents.c                 ój   •— t         ‰| �  ||«       | j                  j                  | _        || _        y r   )r   r   r   r   r:   )r   r   r   r:   r   s       €r   r   zCollCollection.__init__Q   s*   ø€ Ü‰Ñ˜˜wÔ'Ø—M‘M×&Ñ&ˆŒ	Øˆ�	r   c                 ó
   — ddiS )Nr1   zMongo collectionr2   r3   s    r   r4   zCollCollection.get_display_infoV   s   € ØÐ*Ð+Ð+r   c                 ó–   — g }| j                   j                  «       D ])  }|j                  t        j                  |d   «      «       Œ+ |S )NÚ_id)r:   ÚfindÚappendr   Úto_str)r   ÚresÚdocs      r   r"   zCollCollection.get_member_namesY   s?   € ØˆØ—9‘9—>‘>Ó#ò 	0ˆCØ�J‰J”t—{‘{ 3 u¡:Ó.Õ/ð	0àˆ
r   c                 ó    — | j                   j                  t        |«      «      }t        t	        | j
                  |«      | j                  |«      S r   )r:   Úfind_oner   ÚDocResourcer
   r   r   )r   r!   rE   s      r   r&   zCollCollection.get_member_   s8   € Ø�i‰i× Ñ ¤¨$£Ó0ˆÜœ8 D§I¡I¨tÓ4°d·l±lÀCÓHÐHr   r;   r,   s   @r   r9   r9   N   s   ø„ Ù6ôò
,òöIr   r9   c                   ó@   ‡ — e Zd ZdZˆ fd„Zd„ Zd„ Zd„ Zd„ Zd„ Z	ˆ xZ
S )rH   z2Mongo document, returned as virtual text resource.c                 ó4   •— t         ‰| �  ||«       || _        y r   )r   r   rE   )r   r   r   rE   r   s       €r   r   zDocResource.__init__g   s   ø€ Ü‰Ñ˜˜wÔ'Øˆ�r   c                 ól   — dt        | j                  «      z   dz   }t        |j                  d«      «      S )Nz<pre>z</pre>r   )r   rE   r   r   )r   Úhtmls     r   Úget_contentzDocResource.get_contentk   s.   € Øœ §¡Ó*Ñ*¨XÑ5ˆÜ˜Ÿ™ FÓ+Ó,Ð,r   c                 óP   — t        | j                  «       j                  «       «      S r   )ÚlenrM   Úreadr3   s    r   Úget_content_lengthzDocResource.get_content_lengtho   s   € Ü�4×#Ñ#Ó%×*Ñ*Ó,Ó-Ð-r   c                  ó   — y)Nz	text/htmlr2   r3   s    r   Úget_content_typezDocResource.get_content_typer   s   € Ør   c                 ó0  — | j                   }|j                  d«      r|d   j                  d«      S |j                  d«      r|d   j                  d«      S |j                  d«      rt        j                  |d   «      S t        j                  |d   «      S )NÚ_titler   Útitler@   Úkey)rE   Úgetr   r   rC   )r   rE   s     r   Úget_display_namezDocResource.get_display_nameu   s~   € Ø�h‰hˆØ�7‰7�8ÔØ�x‘=×'Ñ'¨Ó/Ð/Ø�W‰W�WÔØ�w‘<×&Ñ& vÓ.Ð.Ø�W‰W�UŒ^Ü—;‘;˜s 5™zÓ*Ð*Ü�{‰{˜3˜u™:Ó&Ð&r   c                 ó
   — ddiS )Nr1   zMongo documentr2   r3   s    r   r4   zDocResource.get_display_info   r5   r   )r'   r(   r)   r*   r   rM   rQ   rS   rY   r4   r+   r,   s   @r   rH   rH   d   s$   ø„ Ù<ôò-ò.òò'ö*r   rH   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚMongoResourceProviderz-DAV provider that serves a MongoDB structure.c                 óH  •— t         ‰| �  «        || _        t        j                  |j                  d«      |j                  d«      «      | _        |j                  d«      r | j                  d   }|j                  |j                  d«      |j                  d«      «      }|s*t        d|j                  ›d|j                  d«      ›�«      ‚t        j                  d|j                  ›d	|j                  d«      ›d
�«       t        j                  d| j                  z  «       y )NÚhostÚportÚuserÚadminÚpwdzFailed to logon to db z	 as user zLogged on to mongo db 'z' as user 'ú'z%MongoResourceProvider connected to %s)r   r   ÚoptionsÚpymongoÚ
ConnectionrX   r   ÚauthenticateÚRuntimeErrorr!   Ú_loggerÚinfo)r   rd   r/   rD   r   s       €r   r   zMongoResourceProvider.__init__‰   sÛ   ø€ Ü‰ÑÔØˆŒÜ×&Ñ& w§{¡{°6Ó':¸G¿K¹KÈÓ<OÓPˆŒ	Ø�;‰;�vÔà—‘˜7Ñ#ˆBØ—/‘/ '§+¡+¨fÓ"5°w·{±{À5Ó7IÓJˆCÙÝ"à—w“w §¡¨FÔ 3ð5óð ô �LŠLà—7“7˜GŸK™K¨Õ/ð1ôô 	�‰Ð<¸t¿y¹yÑHÕIr   c                 ó˜   — t         j                  d|z  «       | xj                  dz  c_        t        d|«      }|j	                  d|«      S )zYReturn DAVResource object for path.

        See DAVProvider.get_resource_inst()
        zget_resource_inst('%s')é   ú/)ri   rj   Ú_count_get_resource_instr   Úresolve)r   r   r   Úroots       r   Úget_resource_instz'MongoResourceProvider.get_resource_instœ   sD   € ô
 	�‰Ð.°Ñ5Ô6Ø×%Ò%¨Ñ*Õ%Ü# C¨Ó1ˆØ�|‰|˜C Ó&Ð&r   )r'   r(   r)   r*   r   rq   r+   r,   s   @r   r\   r\   †   s   ø„ Ù7ôJö&'r   r\   )r*   Úior   Úpprintr   re   Úbson.objectidr   Úwsgidavr   Úwsgidav.dav_providerr   r   r	   Úwsgidav.utilr
   Ú__docformat__Úget_module_loggerr'   ri   r   r%   r9   rH   r\   r2   r   r   ú<module>rz      s   ðñõ0 Ý ã Ý "å ß MÑ MÝ !à"€à
 ˆ$×
 Ñ
  Ó
*€ôE˜=ô EôM�=ô Mô&I�]ô Iô,*Ð"ô *ôD'˜Kõ 'r   