utils
Non-implementation-specific syncing utilities
Operation
Bases: Enum
The recognized sync operations
Notes
There's no UPDATE
operation because so far this class isn't used by
anything that can perform a delta update on a file
Source code in enderchest/sync/utils.py
abspath_from_uri(uri)
Extract and unquote the path component of a URI to turn it into an
unambiguous absolute pathlib.Path
h/t https://stackoverflow.com/a/61922504
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri |
ParseResult
|
The parsed URI to extract the path from |
required |
Returns:
Type | Description |
---|---|
Path
|
The path part of the URI as a Path |
Source code in enderchest/sync/utils.py
diff(source_files, destination_files)
Compute the "diff" between the source and destination, enumerating all the operations that should be performed so that the destination matches the source
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_files |
list of (Path, stat_result) tuples
|
The files and file attributes at the source |
required |
destination_files |
list of (Path, stat_result) tuples
|
The files and file attributes at the destination |
required |
Returns:
Type | Description |
---|---|
Generator of (Path, stat_result, Operation) tuples
|
The files, their attributes and the operations that should be performed on each file |
Notes
- The order of paths returned will match the order provided by the
source_files
except for the deletions, which will all come at the end and will be sorted from longest to shortest path (so that individual files are marked for deletion before their parent folders). - The attributes of each path will correspond to the source attributes for creations and replacements and to the destination attributes for the deletions
Source code in enderchest/sync/utils.py
filter_contents(contents, exclude, prefix=None)
Apply an exclusion filter to a list of files
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contents |
list of (Path, ...) tuples
|
The contents to filter |
required |
exclude |
list of str
|
The patterns to exclude |
required |
prefix |
Path
|
If the contents are iterating over a subdirectory, providing the directory
as the |
None
|
Yields:
Type | Description |
---|---|
(Path, ...) tuples
|
The elements of the provided list, omitting the ones to be excluded |
Source code in enderchest/sync/utils.py
generate_sync_report(content_diff, depth=2)
Compile a high-level summary of the outcome of the diff
method
and report it to the logging.INFO level
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content_diff |
list of (Path, Operation) tuples
|
The files and the operations that are to be performed on each file, as
generated by the |
required |
depth |
int
|
How many directories to go down from the root to generate the summary. Default is 2 (just report on top-level files and folders within the source folder). |
2
|
Returns:
Type | Description |
---|---|
None
|
|
Source code in enderchest/sync/utils.py
get_default_netloc()
Compile a netloc from environment variables, etc.
Returns:
Type | Description |
---|---|
str
|
The default netloc, which is {user}@{hostname} |
is_identical(object_one, object_two)
Determine if two objects are identical (meaning: skip when syncing)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
object_one |
stat_result or similar
|
The first object to compare |
required |
object_two |
stat_result or similar
|
The second object to compare |
required |
Returns:
Type | Description |
---|---|
bool
|
False if the objects are conclusively different, True otherwise. |
Notes
As most implementations of the SFTP protocol do not include the check-file extension, this method is limited in what it can compare. Use with caution.
Source code in enderchest/sync/utils.py
render_remote(alias, uri)
Render a remote to a descriptive string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alias |
str
|
The name of the remote |
required |
uri |
ParseResult
|
The parsed URI for the remote |
required |
Returns:
Type | Description |
---|---|
str
|
|
Source code in enderchest/sync/utils.py
uri_to_ssh(uri)
Convert a URI to an SSH address
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri |
ParseResult
|
The URI to convert |
required |
Returns:
Type | Description |
---|---|
str
|
The SSH-format address |