U
    #sg(                     @   s\   d dl mZmZ ddlmZ ddlZdZd ZG dd deZ	e	 Z
G dd deZe ZdS )	   )seafserv_threaded_rpcccnet_threaded_rpc    )SearpcErrorNc                   @   s  e Zd Zdd ZdKd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dLddZdMddZdNd!d"Zd#d$ Zd%d& ZdOd(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 ZdPd4d5Zd6d7 Zd8d9 Zd:d; Zd<d= ZdQd?d@ZdAdB Z dCdD Z!dEdF Z"dGdH Z#dIdJ Z$dKdL Z%dMdN Z&dOdP Z'dQdR Z(dSdT Z)dUdV Z*dWdX Z+dRdYdZZ,dSd[d\Z-d]d^ Z.dTd_d`Z/dUdadbZ0dVdcddZ1dedf Z2dWdgdhZ3didj Z4dkdl Z5dmdn Z6dodp Z7dqdr Z8dsdt Z9dudv Z:dwdx Z;dydz Z<dXd|d}Z=dYd~dZ>dd Z?dd Z@dd ZAdd ZBdd ZCdd ZDdZddZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPd[ddZQdd ZRdd ZSd\ddZTdd ZUdd ZVdd ZWd]ddZXdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^d^ddĄZ_ddƄ Z`ddȄ Zaddʄ Zbd_dd̄Zcdd΄ ZdddЄ Zedd҄ ZfddԄ Zgddք Zhdd؄ Ziddڄ Zjdd܄ Zkddބ Zldd Zmdd Zndd Zodd Zpdd Zqdd Zrdd Zsdd Ztdd Zudd Zvdd Zwdd Zxdd Zydd Zzdd Z{dd 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d Zda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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ ZdbdAdBZdCdD ZdEdF ZdGdH ZdIdJ ZdS (c  
SeafileAPIc                 C   s   d S N selfr   r   _/opt/seafile-pro-server-build/seafile-server/seafile/lib/python3.8/site-packages/seaserv/api.py__init__   s    zSeafileAPI.__init__Tc                 C   s"   t |rdnd}t|||||S )a1  Generate token for access file/dir in fileserver

        op: the operation, can be 'view', 'download', 'download-dir', 'downloadblks',
            'upload', 'update', 'upload-blks-api', 'upload-blks-aj',
            'update-blks-api', 'update-blks-aj'

        Return: the access token in string
        r   r   )boolr   Zweb_get_access_token)r
   repo_idZobj_idopusernameZuse_onetimeZonetimer   r   r   get_fileserver_access_token   s    	z&SeafileAPI.get_fileserver_access_tokenc                 C   s
   t |S )zGet the WebAccess object

        token: the access token in string

        Return: the WebAccess object (lib/webaccess.vala)
        )r   Zweb_query_access_tokenr
   tokenr   r   r   query_fileserver_access_token'   s    z(SeafileAPI.query_fileserver_access_tokenc                 C   s
   t |S )zQuery zip progress for download-dir, download-multi
        token: obtained by get_fileserver_access_token
        Return: json formated string `{"zipped":, "total":}`, otherwise None.
        )r   query_zip_progressr   r   r   r   r   0   s    zSeafileAPI.query_zip_progressc                 C   s
   t |S r   )r   cancel_zip_taskr   r   r   r   r   7   s    zSeafileAPI.cancel_zip_taskc                 C   s   t ||S z7
        Return non-zero if True, otherwise 0.
        )r   Zis_passwd_setr
   r   r   r   r   r   is_password_set<   s    zSeafileAPI.is_password_setc                 C   s   t ||S )z<
        Return: a CryptKey object (lib/crypt.vala)
        )r   get_decrypt_keyr   r   r   r   r   B   s    zSeafileAPI.get_decrypt_keyc                 C   s   t ||||S r   )r   change_repo_passwd)r
   r   Z
old_passwdZ
new_passwduserr   r   r   r   H   s     zSeafileAPI.change_repo_passwdc                 C   s   t ||S r   )r   check_passwd)r
   r   magicr   r   r   r   K   s    zSeafileAPI.check_passwdc                 C   s   t |||S r   )r   
set_passwd)r
   r   r   passwdr   r   r   r   N   s    zSeafileAPI.set_passwdc                 C   s   t ||S r   )r   unset_passwdr
   r   r   r   r   r   r!   Q   s    zSeafileAPI.unset_passwdc                 C   s   t |||S r   )r   generate_magic_and_random_key)r
   enc_versionr   passwordr   r   r   r#   T   s    z(SeafileAPI.generate_magic_and_random_keyN   c	           	   	   C   s   t |||||||S r   )r   create_repo)	r
   namedescr   r    r$   Z
storage_idpwd_hash_algopwd_hash_paramsr   r   r   r'   Y   s    zSeafileAPI.create_repoc                 C   s   t |||||||||	|
|S r   )r   create_enc_repo)r
   r   r(   r)   r   r   Z
random_keyZsaltr$   Zpwd_hashr*   r+   r   r   r   r,   \   s    zSeafileAPI.create_enc_repoc                 C   s   t |||S 0
        Return: a list of Repo objects
        )r   get_repos_by_id_prefix)r
   Z	id_prefixstartlimitr   r   r   r0   _   s     z!SeafileAPI.get_repos_by_id_prefixc                 C   s
   t |S )z7
        Return: a Repo object (lib/repo.vala)
        )r   get_repor
   r   r   r   r   r3   e   s    zSeafileAPI.get_repoc                 C   s
   t |S r   )r   remove_repor4   r   r   r   r5   k   s    zSeafileAPI.remove_repoFc                 C   s   t ||||rdndS )z@
        Return: a list of Repo objects (lib/repo.vala)
        r   r   )r   get_repo_list)r
   r1   r2   Zorder_byZret_virt_repor   r   r   r6   n   s    zSeafileAPI.get_repo_listc                 C   s   t  S r   )r   count_reposr	   r   r   r   r7   t   s    zSeafileAPI.count_reposc                 C   s   t ||||S r   )r   	edit_repo)r
   r   r(   Zdescriptionr   r   r   r   r8   w   s    zSeafileAPI.edit_repoc                 C   s   t ||S )z0
        Return 1 if True, otherwise 0.
        )r   is_repo_owner)r
   r   r   r   r   r   r9   z   s    zSeafileAPI.is_repo_ownerc                 C   s   t ||S r   )r   set_repo_owner)r
   emailr   r   r   r   r:      s    zSeafileAPI.set_repo_ownerc                 C   s
   t |S )z.
        Return: repo owner in string
        )r   get_repo_ownerr4   r   r   r   r<      s    zSeafileAPI.get_repo_ownerc                 C   s   t ||rdnd||S )r/   r   r   )r   Zlist_owned_repos)r
   r   Zret_corruptedr1   r2   r   r   r   get_owned_repo_list   s
    
 zSeafileAPI.get_owned_repo_listc                 C   s
   t |S r   )r   search_repos_by_name)r
   r(   r   r   r   r>      s    zSeafileAPI.search_repos_by_namec                 C   s   t  S r   )r   get_orphan_repo_listr	   r   r   r   r?      s    zSeafileAPI.get_orphan_repo_listc                 C   s
   t |S r   )r   Zserver_repo_sizer4   r   r   r   get_repo_size   s    zSeafileAPI.get_repo_sizec                 C   s   t |||S r   )r   Zrevert_on_server)r
   r   	commit_idr   r   r   r   revert_repo   s    zSeafileAPI.revert_repor   c                 C   s   t ||||S )zE
        Return: a list of DiffEntry objects (lib/repo.vala)
        )r   Zget_diff)r
   r   Z
old_commitZ
new_commitZfold_dir_diffr   r   r   diff_commits   s    zSeafileAPI.diff_commitsc                 C   s   t |||S )zD
        Return: a list of Commit objects (lib/commit.vala)
        )r   get_commit_list)r
   r   offsetr2   r   r   r   rD      s    zSeafileAPI.get_commit_listc                 C   s0   zt |||}W n tk
r*   d}Y nX |S )z Get a commit. N)r   
get_commitr   )r
   r   Zrepo_versionZcmt_idretr   r   r   rF      s
    
zSeafileAPI.get_commitc                 C   s   t  S r   )r   get_system_default_repo_idr	   r   r   r   rH      s    z%SeafileAPI.get_system_default_repo_idc                 C   s
   t |S r   )r   get_org_id_by_repo_idr4   r   r   r   rI      s    z SeafileAPI.get_org_id_by_repo_idc                 C   s   t ||S r   )r   set_repo_status)r
   r   statusr   r   r   rJ      s    zSeafileAPI.set_repo_statusc                 C   s
   t |S r   )r   get_repo_statusr4   r   r   r   rL      s    zSeafileAPI.get_repo_statusc                 C   s   t ||S )z3
        Return: 0 on invalid; 1 on valid.
        )r   is_valid_filename)r
   r   filenamer   r   r   rM      s    zSeafileAPI.is_valid_filenamec                 C   s   t |||S r   )r   get_file_size)r
   store_idversionfile_idr   r   r   rO      s    zSeafileAPI.get_file_sizec                 C   s   t |||S )z
        Return the size of a dir. It needs to recursively calculate the size
        of the dir. It can cause great delay before returning. Use with caution!
        )r   get_dir_size)r
   rP   rQ   dir_idr   r   r   rS      s    zSeafileAPI.get_dir_sizec                 C   s   t ||S )z`
        Returns None if path not found. Only raise exception on parameter or IO error.
        )r   get_file_id_by_pathr
   r   pathr   r   r   rU      s    zSeafileAPI.get_file_id_by_pathc                 C   s   t |||S r   )r   get_file_id_by_commit_and_pathr
   r   rA   rW   r   r   r   rX      s    z)SeafileAPI.get_file_id_by_commit_and_pathc                 C   s   t ||S )z;
        Return: a Dirent object (lib/dirent.vala)
        )r   get_dirent_by_pathrV   r   r   r   rZ      s    zSeafileAPI.get_dirent_by_pathc                 C   s   t ||||S r   r   Zlist_file_blocksr
   r   rR   rE   r2   r   r   r   list_file_by_file_id   s    zSeafileAPI.list_file_by_file_idc                 C   s   t ||||S )zw
        list block ids of a file.
        Return: a string containing block list. Each id is seperated by '
'
        r[   r\   r   r   r   list_blocks_by_file_id   s    z!SeafileAPI.list_blocks_by_file_idc                 C   s   t ||S r   )r   get_dir_id_by_pathrV   r   r   r   r_      s    zSeafileAPI.get_dir_id_by_pathc                 C   s   t ||||S )z
        Return: a list of Dirent objects. The objects are sorted as follows:
                - Directories are always before files
                - Entries are sorted by names in ascending order
        )r   list_dir)r
   r   rT   rE   r2   r   r   r   list_dir_by_dir_id   s    zSeafileAPI.list_dir_by_dir_idc                 C   s(   t ||}|d krd S t ||||S r   )r   r_   r`   )r
   r   rW   rE   r2   rT   r   r   r   list_dir_by_path   s    zSeafileAPI.list_dir_by_pathc                 C   s*   t |||}|d krd S t ||||S r   )r   get_dir_id_by_commit_and_pathr`   )r
   r   rA   rW   rE   r2   rT   r   r   r   list_dir_by_commit_and_path   s    z&SeafileAPI.list_dir_by_commit_and_pathc                 C   s   t |||S r   )r   rc   rY   r   r   r   rc     s    z(SeafileAPI.get_dir_id_by_commit_and_pathc                 C   s   t ||||||S r   )r   list_dir_with_perm)r
   r   dir_pathrT   r   rE   r2   r   r   r   re     s    zSeafileAPI.list_dir_with_permc                 C   s   t ||||S r   )r   mkdir_with_parents)r
   r   
parent_dirrelative_pathr   r   r   r   rg   	  s    zSeafileAPI.mkdir_with_parentsc                 C   s   t ||S r   )r   get_file_count_info_by_pathrV   r   r   r   rj     s    z&SeafileAPI.get_file_count_info_by_pathc                 C   s   t  S r   )r   get_total_storager	   r   r   r   rk     s    zSeafileAPI.get_total_storagec                 C   s   t  S r   )r   get_total_file_numberr	   r   r   r   rl     s    z SeafileAPI.get_total_file_numberc                 C   s   t |||||S )zAdd a file to a directory)r   	post_file)r
   r   tmp_file_pathrh   rN   r   r   r   r   rm     s    
 zSeafileAPI.post_filec                 C   s   t ||||S r   )r   post_empty_filer
   r   rh   rN   r   r   r   r   ro     s     zSeafileAPI.post_empty_filec                 C   s   t ||||||S )zYUpdate an existing file

        head_id: the original commit id of the old file
        )r   put_file)r
   r   rn   rh   rN   r   Zhead_idr   r   r   rq      s
    
  zSeafileAPI.put_filec                 C   s   t ||||S r   )r   del_filerp   r   r   r   rr   ,  s    zSeafileAPI.del_filec                 C   s   t |||S r   )r   batch_del_files)r
   r   Z	filepathsr   r   r   r   rs   /  s    zSeafileAPI.batch_del_filesr   c
           
      C   s   t |||||||||		S r   )r   	copy_file)
r
   src_reposrc_dirsrc_filenamedst_repodst_dirdst_filenamer   need_progresssynchronousr   r   r   rt   7  s    
    zSeafileAPI.copy_filec                 C   s   t |||||||||	|

S r   )r   	move_file)r
   ru   rv   rw   rx   ry   rz   replacer   r{   r|   r   r   r   r}   =  s    
     zSeafileAPI.move_filec                 C   s
   t |S r   )r   get_copy_taskr
   Ztask_idr   r   r   r   C  s    zSeafileAPI.get_copy_taskc                 C   s
   t |S r   )r   cancel_copy_taskr   r   r   r   r   F  s    zSeafileAPI.cancel_copy_taskc                 C   s   t |||||S r   )r   rename_file)r
   r   rh   ZoldnameZnewnamer   r   r   r   r   I  s
      zSeafileAPI.rename_filec                 C   s   t ||||S )zAdd a directory)r   post_dir)r
   r   rh   dirnamer   r   r   r   r   M  s    zSeafileAPI.post_dirc                 C   s   t ||||S r   )r   revert_filer
   r   rA   rW   r   r   r   r   r   Q  s    zSeafileAPI.revert_filec                 C   s   t ||||S r   )r   
revert_dirr   r   r   r   r   T  s    zSeafileAPI.revert_dir/d   c                 C   s   t |||||S )a  
        Get list of deleted paths.

        @show_days: return deleted path in the last @show_days
        @path: return deleted files under this path. The path will be recursively traversed.
        @scan_stat: An opaque status returned by the last call. In the first call, None
                    must be passed. The last entry of the result list contains a 'scan_stat'
                    attribute. In the next call, pass in the returned 'scan_stat'.
        @limit: Advisory maximum number of commits to traverse. Sometimes more than @limit
                commits will be traversed.

        Return a list of DeletedEntry objects (lib/repo.vala).
        If no more deleted entries can be returned within the given time frame (specified by
        @show_days) or all deleted entries in the history have been returned, a list with a
        single entry will be returned. The 'scan_stat' attribute of this entry is set to
        None.
        )r   get_deleted)r
   r   Z	show_daysrW   Z	scan_statr2   r   r   r   r   W  s    zSeafileAPI.get_deletedc                 C   s   t ||||S )a  
        Get revisions of a file.

        @commit_id: start traversing from this commit
        @limit: maximum number of commits to traverse when looking for revisions

        Return a list of Commit objects (lib/commit.vala) related to the revisions.
        A few special attributes are added to the commit object:
        @rev_file_id: id of the file revision
        @rev_file_size: size of the file revision
        @rev_renamed_old_path: set if this revision is made by a rename operation.
                               It's set to the old path before rename.
        @next_start_commit: commit_id for next page. An extra commit which only contains @next_start_commit
                            will be appended to the list.
        )r   Zlist_file_revisions)r
   r   rA   rW   r2   r   r   r   get_file_revisionsk  s    zSeafileAPI.get_file_revisionsc                 C   s   t |||S )zkGet last modification time for files in a dir

        limit: the max number of commits to analyze
        )r   Zcalc_files_last_modified)r
   r   rh   r2   r   r   r   get_files_last_modified~  s     z"SeafileAPI.get_files_last_modifiedc                 C   s
   t |S )zR
        Return repo history limit in days. Returns -1 if it's unlimited.
        )r   get_repo_history_limitr4   r   r   r   r     s    z!SeafileAPI.get_repo_history_limitc                 C   s   t ||S )zN
        Set repo history limit in days. Pass -1 if set to unlimited.
        )r   set_repo_history_limit)r
   r   Zdaysr   r   r   r     s    z!SeafileAPI.set_repo_history_limitc                 C   s   t ||S r   )r   set_repo_valid_since)r
   r   Z	timestampr   r   r   r     s    zSeafileAPI.set_repo_valid_sincec                 C   s   t ||S r   )r   check_repo_blocks_missing)r
   r   Zblklistr   r   r   r     s    z$SeafileAPI.check_repo_blocks_missingc                 C   s   t ||S r   )r   get_upload_tmp_file_offset)r
   r   	file_pathr   r   r   r     s    z%SeafileAPI.get_upload_tmp_file_offsetc                 C   s   dS )zH
        Always return 0 since CE doesn't support file locking.
        r   r   r
   r   rW   r   r   r   r   check_file_lock  s    zSeafileAPI.check_file_lockc                 C   s   t ||||S r   )r   Z	add_sharer
   r   from_usernameto_username
permissionr   r   r   
share_repo  s     zSeafileAPI.share_repoc                 C   s   t |||S r   )r   remove_share)r
   r   r   r   r   r   r   r     s    zSeafileAPI.remove_sharec                 C   s   t ||||S r   )r   set_share_permissionr   r   r   r   r     s     zSeafileAPI.set_share_permission c                 C   s   t ||||||S r   )r   share_subdir_to_user)r
   r   rW   owner
share_userr   r    r   r   r   r     s
    
  zSeafileAPI.share_subdir_to_userc                 C   s   t ||||S r   )r   unshare_subdir_for_user)r
   r   rW   r   r   r   r   r   r     s    
z"SeafileAPI.unshare_subdir_for_userc                 C   s   t |||||S r   )r   !update_share_subdir_perm_for_user)r
   r   rW   r   r   r   r   r   r   r     s    
 z,SeafileAPI.update_share_subdir_perm_for_userc                 C   s   t ||||rdndS z
        If path is NULL, 'repo_id' represents for the repo we want,
        otherwise, 'repo_id' represents for the origin repo, return virtual repo
        r   r   )r   get_shared_repo_by_path)r
   r   rW   Z	shared_tois_orgr   r   r   r     s    z"SeafileAPI.get_shared_repo_by_pathc                 C   s   t |d||S )z[
        Get repo list shared by this user.
        Return: a list of Repo objects
        Z
from_emailr   Zlist_share_reposr
   r   r1   r2   r   r   r   get_share_out_repo_list  s     z"SeafileAPI.get_share_out_repo_listc                 C   s   t |d||S )z4
        Get repo list shared to this user.
        Zto_emailr   r   r   r   r   get_share_in_repo_list  s     z!SeafileAPI.get_share_in_repo_listc                 C   s   t ||S )zt
        Get user list this repo is shared to.
        Return: a list of SharedUser objects (lib/repo.vala)
        )r   list_repo_shared_tor
   	from_userr   r   r   r   r     s    zSeafileAPI.list_repo_shared_toc                 C   s   t ||rdndrdS dS )Nr   r   TF)r   repo_has_been_shared)r
   r   Zincluding_groupsr   r   r   r     s    zSeafileAPI.repo_has_been_sharedc                 C   s   t ||||S r   r   group_share_repor
   r   group_idr   r   r   r   r   r     s     zSeafileAPI.group_share_repoc                 C   s   t ||||S r   r   r   r   r   r   set_group_repo  s     zSeafileAPI.set_group_repoc                 C   s   t |||S r   r   group_unshare_repor
   r   r   r   r   r   r   r     s    zSeafileAPI.group_unshare_repoc                 C   s   t |||S r   r   r   r   r   r   unset_group_repo  s    zSeafileAPI.unset_group_repoc                 C   s:   t |}|sg S g }|dD ]}|s*q || q |S N
)r   get_shared_groups_by_reposplitappend)r
   r   	group_idsrG   r   r   r   r   get_shared_group_ids_by_repo  s    
z'SeafileAPI.get_shared_group_ids_by_repoc                 C   s   t ||S r   r   list_repo_shared_groupr   r   r   r   r     s    z!SeafileAPI.list_repo_shared_groupc                 C   s   t ||||rdndS r   )r   get_group_shared_repo_by_path)r
   r   rW   r   r   r   r   r   r     s    z(SeafileAPI.get_group_shared_repo_by_pathc                 C   s
   t |S )zO
        Return all the repos in all groups that the @user belongs to.
        )r   get_group_repos_by_user)r
   r   r   r   r   r     s    z"SeafileAPI.get_group_repos_by_userc                 C   s   t ||S r   )r   get_org_group_repos_by_user)r
   r   org_idr   r   r   r     s    z&SeafileAPI.get_org_group_repos_by_userc                 C   s   t ||S )zG
        Return: a list of SharedGroup objects (lib/repo.vala)
        r   r   r   r   r   list_repo_shared_group_by_user  s    z)SeafileAPI.list_repo_shared_group_by_userc                 C   s   t ||||||S r   )r   share_subdir_to_group)r
   r   rW   r   share_groupr   r    r   r   r   r     s
    
  z SeafileAPI.share_subdir_to_groupc                 C   s   t ||||S r   )r   unshare_subdir_for_group)r
   r   rW   r   r   r   r   r   r     s    
z#SeafileAPI.unshare_subdir_for_groupc                 C   s   t |||||S r   )r   "update_share_subdir_perm_for_group)r
   r   rW   r   r   r   r   r   r   r     s    
 z-SeafileAPI.update_share_subdir_perm_for_groupc                 C   s>   t |}|sg S g }|dD ]}|dkr.q || q |S )z3
        Return the list of group repo ids
        r   r   )r   get_group_repoidsr   r   )r
   r   Zrepo_idslr   r   r   r   r   !  s    
zSeafileAPI.get_group_repoidsc                 C   s6   g }|  |D ]"}| |}|d kr&q|| q|S r   )r   r3   r   )r
   r   rG   r   rr   r   r   get_group_repo_list/  s    
zSeafileAPI.get_group_repo_listc                 C   s
   t |S r.   )r   get_repos_by_groupr
   r   r   r   r   r   9  s    zSeafileAPI.get_repos_by_groupc                 C   s
   t |S )z`
        Get all repos a user share to any group
        Return: a list of Repo objects
        )r   get_group_repos_by_ownerr
   r   r   r   r   r   ?  s    z#SeafileAPI.get_group_repos_by_ownerc                 C   s   t ||S )z=
        Unshare all repos a user shared to a group.
        r   Zremove_repo_group)r
   r   r   r   r   r   remove_group_repos_by_ownerF  s    z&SeafileAPI.remove_group_repos_by_ownerc                 C   s   t |dS )zT
        Remove all repos under group.
        Return: 0 success; -1 failed
        Nr   r   r   r   r   remove_group_reposL  s    zSeafileAPI.remove_group_reposc                 C   s   t |||S r   )r   set_group_repo_permission)r
   r   r   r   r   r   r   r   S  s    z$SeafileAPI.set_group_repo_permissionc                 C   s   t |||S )zb
        Get all users a path is shared to.
        Return: a list of SharedUser objects.
        )r   get_shared_users_for_subdirr
   r   rW   r   r   r   r   r   W  s    z&SeafileAPI.get_shared_users_for_subdirc                 C   s   t |||S )zd
        Get all groups a path is shared to.
        Return: a list of SharedGroup objects.
        )r   get_shared_groups_for_subdirr   r   r   r   r   ^  s    z'SeafileAPI.get_shared_groups_for_subdirc           
      C   s   g }t |}|D ]}||j qt |}|s6|S g }|dD ]}|sNqD|t| qDt|}t	
|}	|	D ]}|j|krv||j qv|S r   )r   get_shared_users_by_repor   r   r   r   intjsondumpsr   get_groups_members	user_name)
r
   r   usersshared_usersr   r   idsr   json_idsgroup_usersr   r   r   r   e  s$    




z#SeafileAPI.get_shared_users_by_repoc                 C   s   t ||S r   )r   Zset_inner_pub_repo)r
   r   r   r   r   r   add_inner_pub_repo  s    zSeafileAPI.add_inner_pub_repoc                 C   s
   t |S r   )r   Zunset_inner_pub_repor4   r   r   r   remove_inner_pub_repo  s    z SeafileAPI.remove_inner_pub_repoc                 C   s   t  S z1
        Return: a list of Repo objects.
        )r   Zlist_inner_pub_reposr	   r   r   r   get_inner_pub_repo_list  s    z"SeafileAPI.get_inner_pub_repo_listc                 C   s
   t |S r   )r   list_inner_pub_repos_by_owner)r
   Z
repo_ownerr   r   r   r     s    z(SeafileAPI.list_inner_pub_repos_by_ownerc                 C   s   t  S r   )r   count_inner_pub_reposr	   r   r   r   r     s    z SeafileAPI.count_inner_pub_reposc                 C   s
   t |S r   )r   is_inner_pub_repor4   r   r   r   r     s    zSeafileAPI.is_inner_pub_repoc                 C   s   t ||S )z
        Check repo share permissions. Only check user share, group share and inner-pub
        shares.
        Return: 'r', 'rw', or None
        )r   check_permissionr"   r   r   r   r     s    zSeafileAPI.check_permissionc                 C   s   t |||S )a  
        Check both repo share permission and sub-folder access permissions.
        This function should be used when updating file/folder in a repo.
        In CE, this function is equivalent to check_permission.
        Return: 'r', 'rw', or None
        )r   check_permission_by_pathr   r   r   r   r     s    z#SeafileAPI.check_permission_by_pathc                 C   s   dS )zB
        Check if the permission of the repo is syncable.
        z{"is_syncable":true}r   )r
   r   r   	repo_permr   r   r   is_repo_syncable  s    zSeafileAPI.is_repo_syncablec                 C   s   dS )a4  
        Check if the permission of the dir is downloadable.
        {"is_downloadable": false, "undownloadable_path":"path"}
        - is_downloadable: true if the dir is downloadable, false if not.
        - undownloadable_path: the undownloadable path of the repo if the path is not downloadable.
        z{"is_downloadable":true}r   )r
   r   rf   r   r   r   r   r   is_dir_downloadable  s    zSeafileAPI.is_dir_downloadablec                 C   s   t ||S )z)Generate a token for sync a repo
        )r   generate_repo_tokenr   r   r   r   r     s    zSeafileAPI.generate_repo_tokenc                 C   s   t |||S r   )r   delete_repo_token)r
   r   r   r   r   r   r   r     s    zSeafileAPI.delete_repo_tokenc                 C   s
   t |S )z:
        Return: a list of RepoTokenInfo objects.
        )r   list_repo_tokensr4   r   r   r   r     s    zSeafileAPI.list_repo_tokensc                 C   s
   t |S r   )r   list_repo_tokens_by_emailr   r   r   r   r     s    z$SeafileAPI.list_repo_tokens_by_emailc                 C   s   t ||S r   )r   delete_repo_tokens_by_peer_id)r
   r;   Zpeer_idr   r   r   r     s    z(SeafileAPI.delete_repo_tokens_by_peer_idc                 C   s
   t |S r   )r   delete_repo_tokens_by_emailr
   r;   r   r   r   r     s    z&SeafileAPI.delete_repo_tokens_by_emailc                 C   s
   t |S )z&Get the sum of repos' size of the user)r   Zget_user_quota_usager   r   r   r   get_user_self_usage  s    zSeafileAPI.get_user_self_usagec                 C   s
   t |S r   )r   get_user_share_usager   r   r   r   r     s    zSeafileAPI.get_user_share_usagec                 C   s
   t |S )zT
        Return: -2 if quota is unlimited; otherwise it must be number > 0.
        )r   get_user_quotar   r   r   r   r     s    zSeafileAPI.get_user_quotac                 C   s   t ||S r   )r   set_user_quotar
   r   Zquotar   r   r   r     s    zSeafileAPI.set_user_quotac                 C   s   dS )Nr   r   r   r   r   get_user_share_quota  s    zSeafileAPI.get_user_share_quotac                 C   s   d S r   r   r   r   r   r   set_user_share_quota  s    zSeafileAPI.set_user_share_quotac                 C   s   t ||S r   )r   check_quota)r
   r   Zdeltar   r   r   r     s    zSeafileAPI.check_quotac                 C   s   t  S r   )r   list_user_quota_usager	   r   r   r   r     s    z SeafileAPI.list_user_quota_usagec                 C   s   t ||||||S r   )r   create_virtual_repo)r
   Zorigin_repo_idrW   Z	repo_nameZ	repo_descr   r    r   r   r   r     s    zSeafileAPI.create_virtual_repoc                 C   s
   t |S r   )r   get_virtual_repos_by_ownerr
   r   r   r   r   r     s    z%SeafileAPI.get_virtual_repos_by_ownerc                 C   s   t |||S r   )r   get_virtual_repo)r
   Zorigin_reporW   r   r   r   r   r    s    zSeafileAPI.get_virtual_repoc                 C   s   t ||S r   )r   clean_up_repo_history)r
   r   Z	keep_daysr   r   r   r    s    z SeafileAPI.clean_up_repo_historyc                 C   s   t ||S r   )r   get_trash_repo_listr
   r1   r2   r   r   r   r    s    zSeafileAPI.get_trash_repo_listc                 C   s
   t |S r   )r   del_repo_from_trashr4   r   r   r   r    s    zSeafileAPI.del_repo_from_trashc                 C   s
   t |S r   )r   restore_repo_from_trashr4   r   r   r   r    s    z"SeafileAPI.restore_repo_from_trashc                 C   s
   t |S r   )r   get_trash_repos_by_ownerr  r   r   r   r    s    z#SeafileAPI.get_trash_repos_by_ownerc                 C   s
   t |S r   )r   get_trash_repo_ownerr4   r   r   r   r	    s    zSeafileAPI.get_trash_repo_ownerc                 C   s   t  S r   )r   empty_repo_trashr	   r   r   r   r
    s    zSeafileAPI.empty_repo_trashc                 C   s
   t |S r   )r   empty_repo_trash_by_ownerr  r   r   r   r    s    z$SeafileAPI.empty_repo_trash_by_ownerc                 C   s   t ||S r   )r   get_server_config_intr
   groupkeyr   r   r   r    s    z SeafileAPI.get_server_config_intc                 C   s   t |||S r   )r   set_server_config_intr
   r  r  valuer   r   r   r    s    z SeafileAPI.set_server_config_intc                 C   s   t ||S r   )r   get_server_config_int64r  r   r   r   r  !  s    z"SeafileAPI.get_server_config_int64c                 C   s   t |||S r   )r   set_server_config_int64r  r   r   r   r  $  s    z"SeafileAPI.set_server_config_int64c                 C   s   t ||S r   )r   get_server_config_stringr  r   r   r   r  '  s    z#SeafileAPI.get_server_config_stringc                 C   s   t |||S r   )r   set_server_config_stringr  r   r   r   r  *  s    z#SeafileAPI.set_server_config_stringc                 C   s   t t||S r   )r   r   get_server_config_booleanr  r   r   r   r  -  s    z$SeafileAPI.get_server_config_booleanc                 C   s   t |rdnd}t|||S Nr   r   )r   r   set_server_config_boolean)r
   r  r  r  Zi_valuer   r   r   r  0  s    z$SeafileAPI.set_server_config_booleanc                 C   s   t ||| d S r   )r   del_org_group_repo)r
   r   r   r   r   r   r   r  4  s    zSeafileAPI.del_org_group_repoc                 C   s   g }t ||}|D ]}||j qt ||}|s:|S g }|dD ]}|sRqH|t| qHt|}	t	
|	}
|
D ]}|j|krz||j qz|S r   )r   org_get_shared_users_by_repor   r   Zget_org_groups_by_repor   r   r   r   r   r   r   )r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r  7  s$    


z'SeafileAPI.org_get_shared_users_by_repoc                 C   s
   t |S r   )r   list_org_inner_pub_reposr
   r   r   r   r   r  R  s    z#SeafileAPI.list_org_inner_pub_reposc                 C   s   t ||||rdndS r  )r   convert_repo_path)r
   r   rW   r   r   r   r   r   r  U  s    zSeafileAPI.convert_repo_pathc                 C   s   t ||S r   )r   publish_event)r
   channelZcontentr   r   r   r  X  s    zSeafileAPI.publish_eventc                 C   s
   t |S r   )r   	pop_event)r
   r   r   r   r   r!  [  s    zSeafileAPI.pop_eventc                 C   s   t ||S r   )r   search_files)r
   r   
search_strr   r   r   r"  ^  s    zSeafileAPI.search_filesc                 C   s   t |||S r   )r   search_files_by_path)r
   r   rW   r#  r   r   r   r$  a  s    zSeafileAPI.search_files_by_path)T)Nr&   NNN)NNN)r-   r-   )NF)Fr-   r-   )r   )r-   r-   )r-   r-   )r-   r-   )r-   r-   )r-   r-   )r-   r-   )r   )r   )r   Nr   )r   )F)F)F)r   )r   )r   )F)__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r!   r#   r'   r,   r0   r3   r5   r6   r7   r8   r9   r:   r<   r=   r>   r?   r@   rB   rC   rD   rF   rH   rI   rJ   rL   rM   rO   rS   rU   rX   rZ   r]   r^   r_   ra   rb   rd   rc   re   rg   rj   rk   rl   rm   ro   rq   rr   rs   rt   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   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   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   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"  r$  r   r   r   r   r      sH  	  
	


	

r   c                   @   s(  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dd Zdd Zdd Zdd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/d0 Zdd2d3Zdd4d5Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Z d@dA Z!dBdC Z"ddDdEZ#ddFdGZ$dHdI Z%ddKdLZ&ddMdNZ'ddOdPZ(dQdR Z)ddTdUZ*dVdW Z+dXdY Z,dZd[ Z-d\d] Z.d^d_ Z/d`da Z0dbdc Z1ddde Z2dfdg Z3dhdi Z4djdk Z5dldm Z6dndo Z7dpdq Z8drds Z9dtdu Z:dvdw Z;dxdy Z<dzd{ Z=d|d} Z>d~d Z?dd Z@dd ZAdd ZBdd ZCdS )CcnetAPIc                 C   s   d S r   r   r	   r   r   r   r   h  s    zCcnetAPI.__init__c                 C   s   t ||||S r   )r   add_emailuser)r
   r;   r    is_staff	is_activer   r   r   r)  l  s    zCcnetAPI.add_emailuserc                 C   s   t ||S )z
        source can be 'DB' or 'LDAP'.
        - 'DB': remove a user created in local database
        - 'LDAP': remove a user imported from LDAP
        )r   remove_emailuser)r
   sourcer;   r   r   r   r,  o  s    zCcnetAPI.remove_emailuserc                 C   s   t ||S )z
        Verify user's password on login. Can be used to verify DB and LDAP users.
        The function first verify password with LDAP, then local database.
        )r   validate_emailuser)r
   r;   r    r   r   r   r.  w  s    zCcnetAPI.validate_emailuserc                 C   s
   t |S )a}  
        Only return local database user or imported LDAP user.
        It first lookup user from local database, if not found, lookup imported
        LDAP user.
        Return: a list of EmailUser objects (ccnet/lib/ccnetobj.vala)
        The 'source' attribute of EmailUser object is set to 'LDAPImport' for LDAP
        imported user, and 'DB' for local database user.
        )r   get_emailuserr   r   r   r   r/  ~  s    	zCcnetAPI.get_emailuserc                 C   s
   t |S )zo
        The same as get_emailuser() but import the user from LDAP if it was not
        imported yet.
        )r   get_emailuser_with_importr   r   r   r   r0    s    z"CcnetAPI.get_emailuser_with_importc                 C   s
   t |S )zF
        Get a user from local database with the db index id.
        )r   get_emailuser_by_id)r
   user_idr   r   r   r1    s    zCcnetAPI.get_emailuser_by_idNc                 C   s0   |dkrd}n|dkrd}nd}t ||||S )a  
        source:
          - 'DB': return local db users
          - 'LDAPImport': return imported LDAP users
          - 'LDAP': retrieve users directly from LDAP server
        start: offset to start retrieving, -1 to start from the beginning
        limit: number of users to get, -1 to get all user from start
        is_active: True to return only active users; False to return inactive users;
                   None to return all users.
        Return: a list of EmailUser objects.
        TZactiveFZinactiver   )r   get_emailusers)r
   r-  r1   r2   r+  rK   r   r   r   r3    s    zCcnetAPI.get_emailusersc                 C   s   t ||||S )z
        Search for users whose name contains @email_patt.
        source: 'DB' for local db users; 'LDAP' for imported LDAP users.
                This function cannot search LDAP users directly in LDAP server.
        )r   search_emailusers)r
   r-  Z
email_pattr1   r2   r   r   r   r4    s    zCcnetAPI.search_emailusersc                 C   s   t |||S )zD
        Search for groups whose name contains @group_patt.
        )r   search_groups)r
   Z
group_pattr1   r2   r   r   r   r5    s    zCcnetAPI.search_groupsc                 C   s   t ||S r   )r   search_group_members)r
   r   patternr   r   r   r6    s    zCcnetAPI.search_group_membersFc                 C   s   t |rdndS r  )r   get_top_groups)r
   Zincluding_orgr   r   r   r8    s    zCcnetAPI.get_top_groupsc                 C   s
   t |S r   )r   get_child_groupsr   r   r   r   r9    s    zCcnetAPI.get_child_groupsc                 C   s
   t |S r   )r   get_descendants_groupsr   r   r   r   r:    s    zCcnetAPI.get_descendants_groupsc                 C   s
   t |S r   )r   get_ancestor_groupsr   r   r   r   r;    s    zCcnetAPI.get_ancestor_groupsc                 C   s   t |||S )zZ
        Search for users whose name contains @keyword directly from LDAP server.
        )r   search_ldapusers)r
   keywordr1   r2   r   r   r   r<    s    zCcnetAPI.search_ldapusersc                 C   s
   t |S )z
        Return the number of active users by source.
        source: 'DB' for local db users; 'LDAP' for imported LDAP users.
        )r   count_emailusersr
   r-  r   r   r   r>    s    zCcnetAPI.count_emailusersc                 C   s
   t |S )z
        Return the number of inactive users by source.
        source: 'DB' for local db users; 'LDAP' for imported LDAP users.
        )r   count_inactive_emailusersr?  r   r   r   r@    s    z"CcnetAPI.count_inactive_emailusersc                 C   s   t |||||S )a]  
        source: 'DB' for local db user; 'LDAP' for imported LDAP user.
        user_id: usually not changed.
        password: new password in plain text. Only effective for DB users.
                  If '!' is passed, the password won't be updated.
        is_staff: change superuser status
        is_active: activate or deactivate user
        )r   update_emailuser)r
   r-  r2  r%   r*  r+  r   r   r   rA    s    	zCcnetAPI.update_emailuserc                 C   s   t ||S r   )r   update_role_emailuser)r
   r;   Zroler   r   r   rB    s    zCcnetAPI.update_role_emailuserc                 C   s   t  S )z6
        Return: a list of EmailUser objects.
        )r   get_superusersr	   r   r   r   rC    s    zCcnetAPI.get_superusersc                 C   s   t ||S )z^
        @source: 'DB' or 'LDAP'
        @user_list: json '[user1, user2, user3,...]'
        )r   get_emailusers_in_list)r
   r-  Z	user_listr   r   r   rD    s    zCcnetAPI.get_emailusers_in_listc                 C   s   t ||S r   )r   update_emailuser_id)r
   Z	old_emailZ	new_emailr   r   r   rE    s    zCcnetAPI.update_emailuser_idr   c                 C   s   t ||||S )zF
        For CE, gtype is not used and should always be None.
        )r   create_group)r
   
group_namer   Zgtypeparent_group_idr   r   r   rF    s    zCcnetAPI.create_groupc                 C   s   t ||||S r   )r   create_org_group)r
   r   rG  r   rH  r   r   r   rI    s    zCcnetAPI.create_org_groupc                 C   s
   t |S )zO
        permission check should be done before calling this function.
        )r   remove_groupr   r   r   r   rJ    s    zCcnetAPI.remove_groupc                 C   s   t |||S z$
        user_name: unused.
        )r   group_add_memberr
   r   r   member_namer   r   r   rL  
  s    zCcnetAPI.group_add_memberc                 C   s   t |||S rK  )r   group_remove_memberrM  r   r   r   rO    s    zCcnetAPI.group_remove_memberc                 C   s   t ||S z?
        No effect if member_name is not in the group.
        )r   group_set_adminr
   r   rN  r   r   r   rQ    s    zCcnetAPI.group_set_adminc                 C   s   t ||S rP  )r   group_unset_adminrR  r   r   r   rS    s    zCcnetAPI.group_unset_adminc                 C   s   t ||S r   )r   set_group_name)r
   r   rG  r   r   r   rT  "  s    zCcnetAPI.set_group_namec                 C   s   t ||S r   )r   
quit_groupr
   r   r   r   r   r   rU  %  s    zCcnetAPI.quit_groupc                 C   s   t ||rdndS )zw
        Get all groups the user belongs to.
        Return: a list of Group objects (ccnet/lib/ccnetobj.vala)
        r   r   )r   
get_groups)r
   r   Zreturn_ancestorsr   r   r   rW  (  s    zCcnetAPI.get_groupsc                 C   s   t |||S )zH
        For CE, source is not used and should alwasys be None.
        )r   get_all_groups)r
   r1   r2   r-  r   r   r   rX  /  s    zCcnetAPI.get_all_groupsc                 C   s
   t |S r   )r   	get_groupr   r   r   r   rY  5  s    zCcnetAPI.get_groupr-   c                 C   s   t |||S )zN
        Return a list of GroupUser objects (ccnet/lib/ccnetobj.vala)
        )r   get_group_members)r
   r   r1   r2   r   r   r   rZ  8  s    zCcnetAPI.get_group_membersc                 C   s   t ||S )z4
        Return a list of GroupUser objects
        )r   get_members_with_prefix)r
   r   prefixr   r   r   r[  >  s    z CcnetAPI.get_members_with_prefixc                 C   s   t |||rdndS )z>
        Return non-zero value if true, 0 if not true
        r   r   )r   check_group_staff)r
   r   r   in_structurer   r   r   r]  D  s    zCcnetAPI.check_group_staffc                 C   s
   t |S r   )r   remove_group_userr   r   r   r   r_  J  s    zCcnetAPI.remove_group_userTc                 C   s   t |||rdndS )z
        Return non-zero value if true, 0 if not true
        If @in_structure is true, return whether user is in descendants groups and @group_id it self 
        r   r   )r   is_group_user)r
   r   r   r^  r   r   r   r`  M  s    zCcnetAPI.is_group_userc                 C   s   t ||S r   )r   set_group_creatorrV  r   r   r   ra  T  s    zCcnetAPI.set_group_creatorc                 C   s   t |||S r   )r   
create_org)r
   org_name
url_prefixZcreatorr   r   r   rb  X  s    zCcnetAPI.create_orgc                 C   s
   t |S r   )r   
remove_orgr  r   r   r   re  [  s    zCcnetAPI.remove_orgc                 C   s   t ||S )zQ
        Return a list of Organization objects (ccnet/lib/ccnetobj.vala)
        )r   get_all_orgsr  r   r   r   rf  ^  s    zCcnetAPI.get_all_orgsc                 C   s   t  S r   )r   
count_orgsr	   r   r   r   rg  d  s    zCcnetAPI.count_orgsc                 C   s
   t |S )z/
        Return an Organizaion object.
        )r   get_org_by_url_prefix)r
   rd  r   r   r   rh  g  s    zCcnetAPI.get_org_by_url_prefixc                 C   s
   t |S r   )r   get_org_by_idr  r   r   r   ri  m  s    zCcnetAPI.get_org_by_idc                 C   s   t |||S r   )r   add_org_user)r
   r   r;   r*  r   r   r   rj  p  s    zCcnetAPI.add_org_userc                 C   s   t ||S r   )r   remove_org_userr
   r   r;   r   r   r   rk  s  s    zCcnetAPI.remove_org_userc                 C   s
   t |S r   )r   get_orgs_by_userr   r   r   r   rm  v  s    zCcnetAPI.get_orgs_by_userc                 C   s   t |||S )z5
        Return a list of EmailUser objects.
        )r   get_org_emailusers)r
   rd  r1   r2   r   r   r   rn  y  s    zCcnetAPI.get_org_emailusersc                 C   s   t ||S r   )r   add_org_groupr
   r   r   r   r   r   ro    s    zCcnetAPI.add_org_groupc                 C   s   t ||S r   )r   remove_org_grouprp  r   r   r   rq    s    zCcnetAPI.remove_org_groupc                 C   s
   t |S r   )r   is_org_groupr   r   r   r   rr    s    zCcnetAPI.is_org_groupc                 C   s
   t |S r   )r   get_org_id_by_groupr   r   r   r   rs    s    zCcnetAPI.get_org_id_by_groupc                 C   s   t |||S )z=
        Return a list of int, each int is group id.
        )r   get_org_groups)r
   r   r1   r2   r   r   r   rt    s    zCcnetAPI.get_org_groupsc                 C   s
   t |S r   )r   get_org_top_groupsr  r   r   r   ru    s    zCcnetAPI.get_org_top_groupsc                 C   s   t ||S r   )r   org_user_existsrl  r   r   r   rv    s    zCcnetAPI.org_user_existsc                 C   s   t ||S r   )r   is_org_staffr
   r   r   r   r   r   rw    s    zCcnetAPI.is_org_staffc                 C   s   t ||S r   )r   set_org_staffrx  r   r   r   ry    s    zCcnetAPI.set_org_staffc                 C   s   t ||S r   )r   unset_org_staffrx  r   r   r   rz    s    zCcnetAPI.unset_org_staffc                 C   s   t ||S r   )r   set_org_name)r
   r   rc  r   r   r   r{    s    zCcnetAPI.set_org_namec                 C   s
   t |S r   )r   get_primary_idr   r   r   r   r|    s    zCcnetAPI.get_primary_idc                 C   s   t ||S r   )r   set_reference_id)r
   Z
primary_idZreference_idr   r   r   r}    s    zCcnetAPI.set_reference_idc                 C   s
   t |S )z8
        @group_ids: json '[id1, id2, id3,...]'
        )r   r   )r
   r   r   r   r   r     s    zCcnetAPI.get_groups_members)N)F)Nr   )r   )F)N)r-   r-   )N)F)T)Dr%  r&  r'  r   r)  r,  r.  r/  r0  r1  r3  r4  r5  r6  r8  r9  r:  r;  r<  r>  r@  rA  rB  rC  rD  rE  rF  rI  rJ  rL  rO  rQ  rS  rT  rU  rW  rX  rY  rZ  r[  r]  r_  r`  ra  rb  re  rf  rg  rh  ri  rj  rk  rm  rn  ro  rq  rr  rs  rt  ru  rv  rw  ry  rz  r{  r|  r}  r   r   r   r   r   r(  f  s   









r(  )Zservicer   r   Zpysearpcr   r   ZREPO_STATUS_NORMALZREPO_STATUS_READ_ONLYobjectr   Zseafile_apir(  Z	ccnet_apir   r   r   r   <module>   s          W  T