mode module
Low-level classes for mode handling.
For the core concepts of mode handling see the control module.
Controls should use the high-level Mode methods of Control.
Classes
- class ctlbase.mode.ModeDirHelper
Static functions for dealing with mode directories, i.e. directories where modes are tracked.
- static find(dirName, isUseCallerName=True, isUseScriptName=True)
Tries to find a mode directory with the given parameters.
An eligible mode directory must be writable to the current user.
To avoid the runtime overhead of auto-discovery, provide an absolute path in
dirName.- Parameters:
dirName¶ (str | True) – The mode directory name. It may include a relative or absolute path specification. True for auto-discovery.
isUseCallerName¶ (bool) – If True, the caller’s normalized module name is used as a directory name, as returned by
getName(). Only relevant ifdirNameis True.isUseScriptName¶ (bool) – If True, the current script’s normalized module name is used as a directory name, based on
sys.argv[0]. Only relevant ifdirNameis True.isUseCallerNametakes precedence.
- Returns:
The absolute real path of the directory if found.
- Return type:
- Raises:
FileNotFoundError – If the directory could not be found or is not writable for the current user.
- class ctlbase.mode.FileModeHandler(dirPath=None)
A mode handler using text files for persistence.
- __init__(dirPath=None)
Creates a new mode handler operating in a specific directory.
- activatePending()
Activates the pending mode by renaming the pending mode file with the most current modification time.
Also removes the files of expired pending modes.
- Raises:
RuntimeError – If there is no pending mode that could be activated.
- Returns:
The activate mode.
- Return type:
- getActive()
Retrieves the active mode by reading the active mode file.
- Returns:
The active mode if set, otherwise
none(str).- Return type:
- getPending()
Retrieves the pending mode by identifying and reading the pending mode file with the most current modification time.
Also removes the files of expired pending modes.
- Returns:
The pending mode if set, otherwise
none(str).- Return type:
- setActive(mode, group=None)
Sets the active mode by creating an active mode file or overwriting an existing one.
If
groupis not None, the group ownership is only set if the current user is the owner of the mode directory and file, respectively. Otherwise, the ownership is left unmodified.- Parameters:
- Raises:
ValueError – If the given group does not exist.
- Returns:
True if the mode was set, False if it was unset.
- Return type:
- setPending(mode, timeout_s=-1, group=None)
Sets the pending mode by creating a new pending mode file.
If
groupis not None, the group ownership is only set if the current user is the owner of the mode directory and file, respectively. Otherwise, the ownership is left unmodified.- Parameters:
mode¶ (str) – The mode to set. Passing an empty str or
noneis equivalent to callingunsetPending().timeout_s¶ (int | None) – The mode timeout in seconds. If
-1, the value ofTIMEOUT_DEFAULT_Sis assumed. Passing0is equivalent to callingunsetPending().group¶ (str | None) – The group ownership to be set on the mode directory and file. Accepts a group name or numerical group ID (gid).
- Raises:
ValueError – If the given group does not exist.
- Returns:
True if the mode was set, False if it was unset.
- Return type:
- unsetActive()
Unsets the active mode by deleting the active mode file if it exists.
Removes the mode directory if no files are left.
- unsetPending()
Unsets the pending mode by deleting all existing pending mode files.
Removes the mode directory altogether if no files are left.
Constants and defaults
- ctlbase.mode.TIMEOUT_DEFAULT_S = 30
Default timeout for a pending mode in seconds.
- ctlbase.mode.BASE_DIR_PATH_DEFAULT = '/tmp'
Default base directory under which mode directories are created.