pycyphal.application.file module
- class pycyphal.application.file.FileServer(node: Node, roots: Iterable[str | Path])[source]
Bases:
object
Exposes local filesystems via the standard RPC-services defined in
uavcan.file
. The lifetime of this instance matches the lifetime of its node.- __init__(node: Node, roots: Iterable[str | Path]) None [source]
- Parameters:
node – The node instance to initialize the file server on. It shall not be anonymous, otherwise it’s a
pycyphal.transport.OperationNotDefinedForAnonymousNodeError
.roots – All file operations will be performed in the specified directories. The first directory to match takes precedence. New files are created in the first directory.
- property roots: List[Path][source]
File operations will be performed within these root directories. The first directory to match takes precedence. New files are created in the first directory in the list. The list can be modified.
- locate(p: Path | str | Path_2_0) Tuple[Path, Path] [source]
Iterate through
roots
until a root r is found such thatr/p
exists and return(r, p)
. Otherwise, return nonexistent(roots[0], p)
. The leading slash makes no difference because we only search through the specified roots.- Raises:
FileNotFoundError
ifroots
is empty.
- class pycyphal.application.file.FileClient(local_node: Node, server_node_id: int, response_timeout: float = 3.0, priority: Priority = Priority.SLOW)[source]
Bases:
object
This class is deprecated and should not be used in new applications; instead, consider using
FileClient2
.A trivial proxy that provides a higher-level and more pythonic API on top of the standard RPC-services from
uavcan.file
. Client instances are created lazily at first request and then kept alive until this instance is closed. All remote operations raiseFileTimeoutError
on timeout.- __init__(local_node: Node, server_node_id: int, response_timeout: float = 3.0, priority: Priority = Priority.SLOW) None [source]
- Parameters:
local_node – RPC-service clients will be created on this node.
server_node_id – All requests will be sent to this node-ID.
response_timeout – Raise
FileTimeoutError
if the server does not respond in this time.priority – Transfer priority for requests (and, therefore, responses).
- property data_transfer_capacity: int[source]
A convenience constant derived from DSDL: the maximum number of bytes per read/write transfer. Larger reads/writes are non-atomic.
- async list(path: str) AsyncIterable[str] [source]
Proxy for
uavcan.file.List
. Invokes requests in series until all elements are listed.
- async get_info(path: str) Response [source]
Proxy for
uavcan.file.GetInfo
. Be sure to check the error code in the returned object.
- async copy(src: str, dst: str, overwrite: bool = False) int [source]
Proxy for
uavcan.file.Modify
.- Returns:
See
uavcan.file.Error
- async move(src: str, dst: str, overwrite: bool = False) int [source]
Proxy for
uavcan.file.Modify
.- Returns:
See
uavcan.file.Error
- async read(path: str, offset: int = 0, size: int | None = None) int | bytes [source]
Proxy for
uavcan.file.Read
.- Parameters:
path – The file to read.
offset – Read offset from the beginning of the file. Currently, it must be positive; negative offsets from the end of the file may be supported later.
size – Read requests will be stopped after the end of the file is reached or at least this many bytes are read. If None (default), the entire file will be read (this may exhaust local memory). If zero, this call is a no-op.
- Returns:
uavcan.file.Error.value
on error (e.g., no file), data on success (empty if the offset is out of bounds or the file is empty).
- async write(path: str, data: memoryview | bytes, offset: int = 0, *, truncate: bool = True) int [source]
Proxy for
uavcan.file.Write
.- Parameters:
path – The file to write.
data – The data to write at the specified offset. The number of write requests depends on the size of data.
offset – Write offset from the beginning of the file. Currently, it must be positive; negative offsets from the end of the file may be supported later.
truncate – If True, the rest of the file after
offset + len(data)
will be truncated. This is done by sending an empty write request, as prescribed by the Specification.
- Returns:
See
uavcan.file.Error
- class pycyphal.application.file.FileClient2(local_node: Node, server_node_id: int, response_timeout: float = 3.0, priority: Priority = Priority.SLOW)[source]
Bases:
object
A trivial proxy that provides a higher-level and more pythonic API on top of the standard RPC-services from
uavcan.file
. Client instances are created lazily at first request and then kept alive until this instance is closed. All remote operations raiseFileTimeoutError
on timeout.In contrast to
FileClient
,FileClient2
raises exceptions for errors reported over the network. The intent is to provide more pythonic error handling in the API. All possible exceptions are defined in this module; all of them are derived fromOSError
and also from a tag typeRemoteFileError
which can be used to easily distinguish file-related exceptions in exception handlers.- __init__(local_node: Node, server_node_id: int, response_timeout: float = 3.0, priority: Priority = Priority.SLOW) None [source]
- Parameters:
local_node – RPC-service clients will be created on this node.
server_node_id – All requests will be sent to this node-ID.
response_timeout – Raise
FileTimeoutError
if the server does not respond in this time.priority – Transfer priority for requests (and, therefore, responses).
- property data_transfer_capacity: int[source]
A convenience constant derived from DSDL: the maximum number of bytes per read/write transfer. Larger reads/writes are non-atomic.
- async list(path: str) AsyncIterable[str] [source]
Proxy for
uavcan.file.List
. Invokes requests in series until all elements are listed.
- async get_info(path: str) Response [source]
Proxy for
uavcan.file.GetInfo
.- Raises:
OSError – If the operation failed; see
uavcan.file.Error
- async remove(path: str) None [source]
Proxy for
uavcan.file.Modify
.- Raises:
OSError – If the operation failed; see
uavcan.file.Error
- async touch(path: str) None [source]
Proxy for
uavcan.file.Modify
.- Raises:
OSError – If the operation failed; see
uavcan.file.Error
- async copy(src: str, dst: str, overwrite: bool = False) None [source]
Proxy for
uavcan.file.Modify
.- Raises:
OSError – If the operation failed; see
uavcan.file.Error
- async move(src: str, dst: str, overwrite: bool = False) None [source]
Proxy for
uavcan.file.Modify
.- Raises:
OSError – If the operation failed; see
uavcan.file.Error
- async read(path: str, offset: int = 0, size: int | None = None) bytes [source]
Proxy for
uavcan.file.Read
.- Parameters:
path – The file to read.
offset – Read offset from the beginning of the file. Currently, it must be positive; negative offsets from the end of the file may be supported later.
size – Read requests will be stopped after the end of the file is reached or at least this many bytes are read. If None (default), the entire file will be read (this may exhaust local memory). If zero, this call is a no-op.
- Raises:
OSError – If the read operation failed; see
uavcan.file.Error
- Returns:
data on success (empty if the offset is out of bounds or the file is empty).
- async write(path: str, data: memoryview | bytes, offset: int = 0, *, truncate: bool = True) None [source]
Proxy for
uavcan.file.Write
.- Parameters:
path – The file to write.
data – The data to write at the specified offset. The number of write requests depends on the size of data.
offset – Write offset from the beginning of the file. Currently, it must be positive; negative offsets from the end of the file may be supported later.
truncate – If True, the rest of the file after
offset + len(data)
will be truncated. This is done by sending an empty write request, as prescribed by the Specification.
- Raises:
OSError – If the write operation failed; see
uavcan.file.Error
- class pycyphal.application.file.RemoteFileError[source]
Bases:
object
This is a tag type used to differentiate Cyphal remote file errors.
- exception pycyphal.application.file.FileTimeoutError[source]
Bases:
RemoteFileError
,NetworkTimeoutError
The specialization of the network error for file access. It inherits from
RemoteFileError
andpycyphal.application.NetworkTimeoutError
.
- exception pycyphal.application.file.RemoteFileNotFoundError(filename: str)[source]
Bases:
RemoteFileError
,FileNotFoundError
Exception type raised when a file server reports
uavcan.file.Error.NOT_FOUND
. This exception type inherits fromRemoteFileError
andFileNotFoundError
.
- exception pycyphal.application.file.RemoteIOError(filename: str)[source]
Bases:
RemoteFileError
,OSError
Exception type raised when a file server reports
uavcan.file.Error.IO_ERROR
. This exception type inherits fromRemoteFileError
andOSError
.
- exception pycyphal.application.file.RemoteAccessDeniedError(filename: str)[source]
Bases:
RemoteFileError
,PermissionError
Exception type raised when a file server reports``uavcan.file.Error.ACCESS_DENIED``. This exception type inherits from
RemoteFileError
and exc:PermissionError.
- exception pycyphal.application.file.RemoteIsDirectoryError(filename: str)[source]
Bases:
RemoteFileError
,IsADirectoryError
Exception type raised when a file server reports
uavcan.file.Error.IS_DIRECTORY
. This exception type inherits fromRemoteFileError
andIsADirectoryError
.
- exception pycyphal.application.file.RemoteInvalidValueError(filename: str)[source]
Bases:
RemoteFileError
,OSError
Exception type raised when a file server reports
uavcan.file.Error.INVALID_VALUE
. This exception type inherits fromRemoteFileError
andOSError
.
- exception pycyphal.application.file.RemoteFileTooLargeError(filename: str)[source]
Bases:
RemoteFileError
,OSError
Exception type raised when a file server reports
uavcan.file.Error.FILE_TOO_LARGE
. This exception type inherits fromRemoteFileError
andOSError
.
- exception pycyphal.application.file.RemoteOutOfSpaceError(filename: str)[source]
Bases:
RemoteFileError
,OSError
Exception type raised when a file server reports
uavcan.file.Error.OUT_OF_SPACE
. This exception type inherits fromRemoteFileError
andOSError
.
- exception pycyphal.application.file.RemoteNotSupportedError(filename: str)[source]
Bases:
RemoteFileError
,OSError
Exception type raised when a file server reports
uavcan.file.Error.NOT_SUPPORTED
. This exception type inherits fromRemoteFileError
andOSError
.
- exception pycyphal.application.file.RemoteUnknownError(filename: str)[source]
Bases:
RemoteFileError
,OSError
Exception type raised when a file server reports
uavcan.file.Error.UNKNOWN_ERROR
. This exception type inherits fromRemoteFileError
andOSError
.