
    Íf)                         d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	 d Z
d Z G d de          Zej        fd	Zej        fd
Zd ZddZedd            ZdS )z8functions and classes that support the Connection object    )contextmanagerN)S_IMODES_ISDIRS_ISREGc                  ~    t           j                            t           j                            ddd                    S )z:return a proper path to ssh's known_host file for the user~z.sshknown_hosts)ospath
expanduserjoin     P/var/www/ffwdstore.be/apps/py-env/lib/python3.11/site-packages/pysftp/helpers.pyr	   r	   
   s*    7bgll3FFGGGr   c           	      ~    t          t          t          t          |                               dd                   S )ay  SFTAttributes st_mode returns an stat type that shows more than what
    can be set.  Trim off those bits and convert to an int representation.
    if you want an object that was `chmod 711` to return a value of 711, use
    this function

    :param int val: the value of an st_mode attr returned by SFTPAttributes

    :returns int: integer representation of octal mode

    N)intstroctr   )vals    r   st_mode_to_intr      s1     s3ws||$$%%bcc*+++r   c                       e Zd ZdZd Zd Zd Zd Zed             Z	e	j
        d             Z	ed             Zej
        d	             Zed
             Zej
        d             ZdS )WTCallbacksz1an object to house the callbacks, used internallyc                 0    g | _         g | _        g | _        dS )zset instance varsN)_flist_dlist_ulistselfs    r   __init__zWTCallbacks.__init__   s    r   c                 :    | j                             |           dS )zecalled for regular files, appends pathname to .flist

        :param str pathname: file path
        N)r   appendr   pathnames     r   file_cbzWTCallbacks.file_cb%        
 	8$$$$$r   c                 :    | j                             |           dS )zhcalled for directories, appends pathname to .dlist

        :param str pathname: directory path
        N)r   r"   r#   s     r   dir_cbzWTCallbacks.dir_cb,   r&   r   c                 :    | j                             |           dS )ztcalled for unknown file types, appends pathname to .ulist

        :param str pathname: unknown entity path
        N)r   r"   r#   s     r   unk_cbzWTCallbacks.unk_cb3   r&   r   c                 *    t          | j                  S )zreturn a sorted list of files currently traversed

        :getter: returns the list
        :setter: sets the list
        :type: list
        )sortedr   r   s    r   flistzWTCallbacks.flist:        dk"""r   c                     || _         dS )zsetter for _flist N)r   r   r   s     r   r-   zWTCallbacks.flistD        r   c                 *    t          | j                  S )zreturn a sorted list of directories currently traversed

        :getter: returns the list
        :setter: sets the list
        :type: list
        )r,   r   r   s    r   dlistzWTCallbacks.dlistI   r.   r   c                     || _         dS )zsetter for _dlist N)r   r0   s     r   r3   zWTCallbacks.dlistS   r1   r   c                 *    t          | j                  S )zreturn a sorted list of unknown entities currently traversed

        :getter: returns the list
        :setter: sets the list
        :type: list
        )r,   r   r   s    r   ulistzWTCallbacks.ulistX   r.   r   c                     || _         dS )zsetter for _ulist N)r   r0   s     r   r6   zWTCallbacks.ulistb   r1   r   N)__name__
__module____qualname____doc__r    r%   r(   r*   propertyr-   setterr3   r6   r   r   r   r   r      s        ;;  % % %% % %% % % # # X# \  \ # # X# \  \ # # X# \  \  r   r   c              #      K   d}| d         |k    r|}d}|                      |          }|r%|d         r||d         z   |d<   n||d         z   |d<   |D ](}t          j                            ||          }|r|V  )dS )zgenerator to iterate over a file path forwards

    :param str thepath: the path to navigate forwards
    :param str sep: *Default: os.sep* - the path separator to use

    :returns: (iter)able of strings

     r      Nsplitr
   r   r   )thepathsepprecurpathpartsparts         r   path_advancerI   h   s       CqzSGMM#E
 &8 	&U1X~E!HHU1X~E!H  ',,w-- 	MMM r   c              #      K   d}| d         |k    r|}|                      |          }|r:t          j        j        | r|t          j        j        | V  |dd         }|8dS dS )zgenerator to iterate over a file path in reverse

    :param str thepath: the path to retreat over
    :param str sep: *Default: os.sep* - the path separator to use

    :returns: (iter)able of strings

    r?   r   NrA   )rC   rD   rE   rG   s       r   path_retreatrL      s       CqzSMM#E
 7< 	7Cu!5!56666crc
      r   c                     |d         t           j        t          j        fv rd|z   }t          j                            | |          S )a  when copying or moving a directory structure, you need to re-parent the
    oldpath.  When using os.path.join to calculate this new path, the
    appearance of a / root path at the beginning of oldpath, supplants the
    newparent and we don't want this to happen, so we need to make the oldpath
    root appear as a child of the newparent.

    :param: str newparent: the new parent location for oldpath (target)
    :param str oldpath: the path being adopted by newparent (source)

    :returns: (str) resulting adoptive path
    r   .)	posixpathrD   ntpathr
   r   r   )	newparentoldpaths     r   reparentrS      s;     qzimVZ000-7<<	7+++r   Tc                 P   t          j        |           D ]}t           j                            | |          }t          j        |          j        }t          |          r  ||           |rt          ||||           jt          |          r ||            ||           dS )a  on the local file system, recursively descend, depth first, the
    directory tree rooted at localpath, calling discreet callback functions
    for each regular file, directory and unknown file type.

    :param str localpath:
        root of remote directory to descend, use '.' to start at
        :attr:`.pwd`
    :param callable fcallback:
        callback function to invoke for a regular file.
        (form: ``func(str)``)
    :param callable dcallback:
        callback function to invoke for a directory. (form: ``func(str)``)
    :param callable ucallback:
        callback function to invoke for an unknown file type.
        (form: ``func(str)``)
    :param bool recurse: *Default: True* -  should it recurse

    :returns: None

    :raises: OSError, if localpath doesn't exist

    N)	r
   listdirr   r   statst_moder   walktreer   )	localpath	fcallback	dcallback	ucallbackrecurseentryr$   modes           r   rX   rX      s    0 I&&    7<<	511wx  (4== 	 Ih D9iCCCT]] 	 Ih Ih   r   c              #      K   	 t          j                    }| t          j        |            dV  t          j        |           dS # t          j        |           w xY w)a(  context manager that can change to a optionally specified local
    directory and restores the old pwd on exit.

    :param str|None localpath: *Default: None* -
        local path to temporarily make the current directory
    :returns: None
    :raises: OSError, if local path doesn't exist
    N)r
   getcwdchdir)rY   original_paths     r   cdrd      sa       	 HY
s   -A A)T)N)r;   
contextlibr   rP   rO   r
   rV   r   r   r   r	   r   objectr   rD   rI   rL   rS   rX   rd   r   r   r   <module>rg      s1   > > % % % % % %      				 * * * * * * * * * *H H H
, , ,H H H H H& H H HV !f    4 !f    &, , ,$&  &  &  & R            r   