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/v0.1/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/v0.1/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 |
|
Wrapper class for a file descriptor that closes it exactly once, either manually or upon object destruction. |
- 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
Cmdinstance that 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.FileDesc(fd)[source]¶
Wrapper class for a file descriptor that closes it exactly once, either manually or upon object destruction.
- Parameters
fd – file descriptor
- close()[source]¶
Ensure fd is closed; operation is idempotent and will call
os.close()exactly once.