pyqt_reactive.services.flag_context_manager

Metaprogrammed context manager factory for boolean flag management.

This module provides a universal context manager for managing temporary boolean flags on objects, following the OpenHCS pattern from config_framework/context_manager.py.

Key features: 1. Single implementation handles all flag patterns 2. Automatic save/restore of previous values 3. Enum-based validation for type safety 4. Composable and nestable 5. Guaranteed cleanup even on exception

Pattern:
Instead of:

self._in_reset = True try:

# … logic

finally:

self._in_reset = False

Use:
with FlagContextManager.manage_flags(self, _in_reset=True):

# … logic

This eliminates duplicate try/finally patterns and ensures flags are always restored.

Classes

FlagContextManager()

Metaprogrammed context manager factory for boolean flag management.

ManagerFlag(*values)

Registry of valid ParameterFormManager flags.

class pyqt_reactive.services.flag_context_manager.ManagerFlag(*values)[source]

Registry of valid ParameterFormManager flags.

This enum serves as: 1. Documentation of all available flags 2. Validation for FlagContextManager 3. Type-safe flag references

Add new flags here as they’re introduced to the codebase.

IN_RESET = '_in_reset'
BLOCK_CROSS_WINDOW = '_block_cross_window_updates'
INITIAL_LOAD_COMPLETE = '_initial_load_complete'
class pyqt_reactive.services.flag_context_manager.FlagContextManager[source]

Metaprogrammed context manager factory for boolean flag management.

This class provides a universal context manager that can manage any combination of boolean flags on an object, with automatic save/restore and validation.

Examples

# Single flag: with FlagContextManager.manage_flags(self, _in_reset=True):

self._reset_parameter_impl(param_name)

# Multiple flags: with FlagContextManager.manage_flags(self, _in_reset=True, _block_cross_window_updates=True):

for param_name in param_names:

self._reset_parameter_impl(param_name)

# Convenience method for reset: with FlagContextManager.reset_context(self):

# … reset logic

VALID_FLAGS: Set[str] = {'_block_cross_window_updates', '_in_reset', '_initial_load_complete'}
static manage_flags(obj: Any, **flags: bool)[source]

Context manager that sets flags on entry and restores previous values on exit.

This is the core metaprogrammed context manager that handles all flag patterns. It saves previous values, sets new values, and guarantees restoration even on exception.

Parameters:
  • obj – Object to set flags on (typically ParameterFormManager instance)

  • **flags – Flag names and values to set (e.g., _in_reset=True)

Raises:

ValueError – If any flag name is not in VALID_FLAGS registry

Example

with FlagContextManager.manage_flags(self, _in_reset=True, _block_cross_window_updates=True):

# Both flags are True here # … logic

# Both flags restored to previous values here

static reset_context(obj: Any, block_cross_window: bool = True)[source]

Convenience context manager for reset operations.

This is a specialized version of manage_flags() for the common reset pattern. It sets _in_reset=True and optionally _block_cross_window_updates=True.

Parameters:
  • obj – Object to set flags on (typically ParameterFormManager instance)

  • block_cross_window – If True, also block cross-window updates (default: True)

Example

# Single parameter reset (don’t block cross-window): with FlagContextManager.reset_context(self, block_cross_window=False):

self._reset_parameter_impl(param_name)

# Batch reset (block cross-window): with FlagContextManager.reset_context(self):

for param_name in param_names:

self._reset_parameter_impl(param_name)

static initial_load_context(obj: Any)[source]

Convenience context manager for initial form load.

Sets _initial_load_complete=False during form building, then sets it to True on exit. This disables live updates during initial form construction.

Parameters:

obj – Object to set flags on (typically ParameterFormManager instance)

Example

with FlagContextManager.initial_load_context(self):

self.build_form()

# _initial_load_complete is now True

static is_flag_set(obj: Any, flag: ManagerFlag) bool[source]

Check if a flag is currently set to True.

Parameters:
  • obj – Object to check flag on

  • flag – ManagerFlag enum value

Returns:

True if flag is set, False otherwise

Example

if FlagContextManager.is_flag_set(self, ManagerFlag.IN_RESET):

return # Skip expensive operation during reset

static get_flag_state(obj: Any) Dict[str, bool][source]

Get current state of all registered flags.

Useful for debugging and logging.

Parameters:

obj – Object to get flag state from

Returns:

Dict mapping flag names to their current values

Example

state = FlagContextManager.get_flag_state(self) logger.debug(f”Flag state: {state}”)