
    O|b                     `    d Z ddlZddlZddlmZ ddlZg dZdZ G d d      Z G d d	      Z	y)
z}
Tools for syncing combinations of local and remote directories.

*** WARNING: This is an unfinished in-development version!
    N)FTPHost)r   	LocalHostSynceri   c                   "    e Zd ZdZd Zd Zd Zy)r   zt
    Provide an API for local directories and files so we can use the same code
    as for `FTPHost` instances.
    c                     t        ||      S )zb
        Return a Python file object for file name `path`, opened in mode
        `mode`.
        )open)selfpathmodes      ,lib/python3.12/site-packages/ftputil/sync.pyr   zLocalHost.open$   s     D$    c                      y)z
        Return the time shift value (see methods `set_time_shift` and
        `time_shift` in class `FTPHost` for a definition). By definition, the
        value is zero for local file systems.
        g         )r	   s    r   
time_shiftzLocalHost.time_shift,   s     r   c                 "    t        t        |      S )N)getattros)r	   attrs     r   __getattr__zLocalHost.__getattr__4   s    r4  r   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s    
 !r   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	r   ze
    Control synchronization between combinations of local and remote
    directories and files.
    c                      || _         || _        y)a  
        Init the `FTPSyncer` instance.

        Each of `source` and `target` is either an `FTPHost` or a `LocalHost`
        object. The source and target directories, resp. have to be set with
        the `chdir` command before passing them in. The semantics is so that
        the items under the source directory will show up under the target
        directory after the synchronization (unless there's an error).
        N)_source_target)r	   sourcetargets      r   __init__zSyncer.__init__>   s     r   c                 ,   | j                   j                  j                  |      r.t        j                  j                  dj                  |            | j                   j                  j                  |      s| j                   j                  |       yy)z
        Try to create the target directory `target_dir`. If it already exists,
        don't do anything. If the directory is present but it's actually a
        file, raise a `SyncError`.
        z"target dir '{}' is actually a fileN)	r   r
   isfileftputilerror	SyncErrorformatisdirmkdir)r	   
target_dirs     r   _mkdirzSyncer._mkdirK   sr     <<##J/--))4;;JG  ||  &&z2LLz* 3r   c                 F   | j                   j                  |d      }	 | j                  j                  |d      }	 t        j                  ||t
               |j                          	 |j                          y # |j                          w xY w# |j                          w xY w)Nrbwb)length)r   r   r   shutilcopyfileobj
CHUNK_SIZEclose)r	   source_filetarget_filer   r   s        r   
_sync_filezSyncer._sync_file]   su     "";5	\\&&{D9F""66*ELLN LLNs"   B A9 B 9BB B c                 v    |j                  | j                  j                  | j                  j                        S )zo
        Return the string `path` with appropriate path separators for the
        target file system.
        )replacer   sepr   )r	   r
   s     r   _fix_sep_for_targetzSyncer._fix_sep_for_targeto   s)    
 ||DLL,,dll.>.>??r   c                    | j                  |       | j                  j                  |      D ]  \  }}}|D ]]  }| j                  j                  j	                  ||      }|j                  ||d      }| j                  |      }| j                  |       _ |D ]^  }	| j                  j                  j	                  ||	      }
|
j                  ||d      }| j                  |      }| j                  |
|       `  y)a  
        Synchronize the source and the target directory tree by updating the
        target to match the source as far as possible.

        Current limitations:
        - _don't_ delete items which are on the target path but not on the
          source path
        - files are always copied, the modification timestamps are not compared
        - all files are copied in binary mode, never in ASCII/text mode
        - incomplete error handling
           N)r*   r   walkr
   joinr7   r9   r5   )r	   
source_dirr)   dirpath	dir_names
file_namesdir_nameinner_source_dirinner_target_dir	file_namer3   r4   s               r   
_sync_treezSyncer._sync_treev   s     	J.2ll.?.?
.K 
	:*GY
% .#'<<#4#4#9#9'8#L #3#;#;J
TU#V #'#;#;<L#M ,-	.
 ( :	"ll//44WiH)11*j!L"66{C[9	:
	:r   c                 ,   | j                   j                  j                  |      }| j                  j                  j                  |      }| j                   j                  j	                  |      r| j                  ||       y| j                  ||       y)a  
        Synchronize `source_path` and `target_path` (both are strings, each
        denoting a directory or file path), i. e. update the target path so
        that it's a copy of the source path.

        This method handles both directory trees and single files.
        N)r   r
   abspathr   r"   r5   rF   )r	   source_pathtarget_paths      r   synczSyncer.sync   sl     ll''//<ll''//<<<##K0OOK5OOK5r   N)
r   r   r   r   r    r*   r5   r9   rF   rK   r   r   r   r   r   8   s&    
+$$@:26r   r   )
r   r   r/   r#   r   ftputil.error__all__r1   r   r   r   r   r   <module>rN      s:   
 
   
, 
! !4e6 e6r   