btrsync.util¶
Various general purpose utility classes and functions.
Functions
|
Generic breadth-first search iterator over nodes using childf to determine child nodes. |
|
Generic depth-first search iterator over node using childf to determine child nodes. |
|
Group items in sequence seq by keys. |
|
Index sequence seq with a series of keys. |
|
Determine whether p is a subpath, i.e., p is relative and does not go above its parent directory. |
|
Join paths a and b after removing the longest prefix of b that is also a suffix of a. |
- btrsync.util.bfs(childf, *nodes, maxdepth=None, depth_markers=False)[source]¶
Generic breadth-first search iterator over nodes using childf to determine child nodes.
- Parameters
childf – function such that
childf(node)returns a sequence of child nodesnodes – the starting set of nodes for breadth-first search
maxdepth – if not
None, stop after returning nodes at this depth (nodes are at depth 0)depth_markers – if
True, return aNoneas marker after exhausting all nodes at a particular depth
- btrsync.util.dfs(childf, node)[source]¶
Generic depth-first search iterator over node using childf to determine child nodes.
- Parameters
childf – function such that
childf(node)returns a sequence of child nodesnode – the starting node for depth-first search
- btrsync.util.group(seq, *keys)[source]¶
Group items in sequence seq by keys.
- Parameters
seq – the sequence to process
keys – a sequence of functions that take an element from seq and return a group index
- Returns
a list of dicts indexed by each key in keys, with values lists of elements from seq that share a group index
- btrsync.util.index(seq, *keys)[source]¶
Index sequence seq with a series of keys.
- Parameters
seq – the sequence to process
keys – a sequence of functions that take an element from seq and return a unique index value
- Returns
a list of dicts indexed by each key in keys, with values elements from seq
- Raises
ValueError – if two elements of seq index to the same value for any of the keys
- btrsync.util.is_subpath(p, *, path=<module 'posixpath' from '/home/docs/checkouts/readthedocs.org/user_builds/btrsync/envs/stable/lib/python3.7/posixpath.py'>)[source]¶
Determine whether p is a subpath, i.e., p is relative and does not go above its parent directory.
- Parameters
p – the path to examine
path – the path module to use (e.g.
os.path,posixpath, etc.)
- Returns
boolean whether p is a subpath
- btrsync.util.path_merge(a, b, *, root='/', path=<module 'posixpath' from '/home/docs/checkouts/readthedocs.org/user_builds/btrsync/envs/stable/lib/python3.7/posixpath.py'>)[source]¶
Join paths a and b after removing the longest prefix of b that is also a suffix of a.
- Parameters
a – the left-hand path to be merged
b – the right-hand path to be merged
root – the root directory for a beyond which one cannot go further up
path – the path module to use (e.g.
os.path,posixpath, etc.)
- Returns
the merged path
Classes
|
Convenience class for parsing shell commands to and from a |
|
Flow sourced from a locally opened file. |
|
Abstract base class for a flow of bytes that can be exposed through different interfaces. |
|
Flow sourced from the read end of a UNIX pipe. |
- class btrsync.util.Cmd(prg, args=())[source]¶
Convenience class for parsing shell commands to and from a
(program, arguments)representation.- wrap(outer, *, shellfmt=False, endmark=None)[source]¶
Return a new
Cmdthat passes self as arguments to outer.- Parameters
outer – the outer command that receives self as arguments
shellfmt – if
True, pass a shell-escaped form of self as a single last argument to outer; ifFalse,prgalong withargsare passed as individual arguments to outerendmark – if not
None, append endmark as final argument to outer, after self
- Returns
the new wrapped
Cmdinstance
- class btrsync.util.FileFlow(f)[source]¶
Flow sourced from a locally opened file.
- Parameters
f – file-like object backed by a file descriptor
- connect_fd()[source]¶
Expose flow as a raw file descriptor.
- Returns
a file-like object backed by a file descriptor
- connect_pipe()[source]¶
Expose flow as a UNIX pipe.
- Returns
a file-like object backed by the read end of a UNIX pipe
- connect_to_fd(f)¶
Direct flow to file-like object f that provides a file descriptor.
- pump()¶
Return a coroutine that handles data flow when run.
- Raises
ValueError – if called before one of the
connect_*methods
- class btrsync.util.Flow[source]¶
Abstract base class for a flow of bytes that can be exposed through different interfaces.
Flows are set up to expose a specific interface with one of the
connect_*methods. Subsequently, running the coroutine returned bypump()handles I/O and any necessary processing. A tally of transmitted bytes can be found incountifstatsisTrue.- abstract connect_fd()[source]¶
Expose flow as a raw file descriptor.
- Returns
a file-like object backed by a file descriptor
- abstract connect_pipe()[source]¶
Expose flow as a UNIX pipe.
- Returns
a file-like object backed by the read end of a UNIX pipe
- abstract connect_to_fd(f)[source]¶
Direct flow to file-like object f that provides a file descriptor.
- pump()[source]¶
Return a coroutine that handles data flow when run.
- Raises
ValueError – if called before one of the
connect_*methods
- class btrsync.util.PipeFlow(f)[source]¶
Flow sourced from the read end of a UNIX pipe.
- Parameters
f – file-like object backed by a file descriptor of the read end of the pipe
- connect_fd()[source]¶
Expose flow as a raw file descriptor.
- Returns
a file-like object backed by a file descriptor
- connect_pipe()[source]¶
Expose flow as a UNIX pipe.
- Returns
a file-like object backed by the read end of a UNIX pipe
- connect_to_fd(f)¶
Direct flow to file-like object f that provides a file descriptor.
- pump()¶
Return a coroutine that handles data flow when run.
- Raises
ValueError – if called before one of the
connect_*methods