Prefs

Prefs set configurable parameters used throughout PVP.

See prefs._DEFAULTS for description of all available parameters

Prefs are stored in a .json file, by default located at ~/pvp/prefs.json . Prefs can be manually changed by editing this file (when the system is not running, when the system is running use prefs.set_pref() ).

When any module in pvp is first imported, the prefs.init() function is called that

  • Makes any directories listed in prefs._DIRECTORIES

  • Declares all prefs as their default values from prefs._DEFAULTS to ensure they are always defined

  • Loads the existing prefs.json file and updates values from their defaults

Prefs can be gotten and set from anywhere in the system with prefs.get_pref() and prefs.set_pref() . Prefs are stored in a multiprocessing.Manager dictionary which makes these methods both thread- and process-safe. Whenever a pref is set, the prefs.json file is updated to reflect the new value, so preferences are durable between runtimes.

Additional prefs should be added by adding an entry in the prefs._DEFAULTS dict rather than hardcoding them elsewhere in the program.

Data:

LOADED

flag to indicate whether prefs have been loaded (and thus set_pref() should write to disk).

_DEFAULTS

Declare all available parameters and set default values.

_DIRECTORIES

Directories to ensure are created and added to prefs.

_LOCK

Locks access to prefs_fn

_LOGGER

A logging.Logger to log pref init and setting events

_PREFS

The dict created by prefs._PREF_MANAGER to store prefs.

_PREF_MANAGER

The multiprocessing.Manager that stores prefs during system operation

Classes:

c_bool

Functions:

get_pref([key])

Get global configuration value

init()

Initialize prefs.

load_prefs(prefs_fn)

Load prefs from a .json prefs file, combining (and overwriting) any existing prefs, and then saves.

make_dirs()

ensures _DIRECTORIES are created and added to prefs.

save_prefs([prefs_fn])

Dumps loaded prefs to PREFS_FN.

set_pref(key, val)

Sets a pref in the manager and, if prefs.LOADED is True, calls prefs.save_prefs()

pvp.common.prefs._PREF_MANAGER = <multiprocessing.managers.SyncManager object>

The multiprocessing.Manager that stores prefs during system operation

pvp.common.prefs._PREFS = <DictProxy object, typeid 'dict'>

The dict created by prefs._PREF_MANAGER to store prefs.

pvp.common.prefs._LOGGER = <Logger pvp.common.prefs (WARNING)>

A logging.Logger to log pref init and setting events

pvp.common.prefs._LOCK = <Lock(owner=None)>

Locks access to prefs_fn

Type

mp.Lock

pvp.common.prefs._DIRECTORIES = {'DATA_DIR': '/home/docs/pvp/logs', 'LOG_DIR': '/home/docs/pvp/logs', 'VENT_DIR': '/home/docs/pvp'}

Directories to ensure are created and added to prefs.

  • VENT_DIR: ~/pvp - base directory for user storage

  • LOG_DIR: ~/pvp/logs - for storage of event and alarm logs

  • DATA_DIR: ~/pvp/data - for storage of waveform data

pvp.common.prefs.LOADED = <Synchronized wrapper for c_bool(True)>

flag to indicate whether prefs have been loaded (and thus set_pref() should write to disk).

uses a multiprocessing.Value to be thread and process safe.

Type

bool

pvp.common.prefs._DEFAULTS = {'BREATH_DETECTION': True, 'BREATH_PRESSURE_DROP': 4, 'CONTROLLER_LOOPS_UNTIL_UPDATE': 1, 'CONTROLLER_LOOP_UPDATE_TIME': 0.0, 'CONTROLLER_LOOP_UPDATE_TIME_SIMULATOR': 0.005, 'CONTROLLER_MAX_FLOW': 10, 'CONTROLLER_MAX_PRESSURE': 100, 'CONTROLLER_MAX_STUCK_SENSOR': 5, 'CONTROLLER_RINGBUFFER_SIZE': 100, 'COUGH_DURATION': 0.1, 'ENABLE_DIALOGS': True, 'ENABLE_WARNINGS': True, 'GUI_STATE_FN': 'gui_state.json', 'GUI_UPDATE_TIME': 0.05, 'HEARTBEAT_TIMEOUT': 0.02, 'LOGGING_MAX_BYTES': 2147483648, 'LOGGING_MAX_FILES': 5, 'LOGLEVEL': 'WARNING', 'OXYGEN_READ_FREQUENCY': 2, 'PREFS_FN': None, 'TIMEOUT': 0.05, 'TIME_FIRST_START': None}

Declare all available parameters and set default values. If no default, set as None.

  • PREFS_FN - absolute path to the prefs file

  • TIME_FIRST_START - time when the program has been started for the first time

  • VENT_DIR: ~/pvp - base directory for user storage

  • LOG_DIR: ~/pvp/logs - for storage of event and alarm logs

  • DATA_DIR: ~/pvp/data - for storage of waveform data

  • LOGGING_MAX_BYTES : the total storage space for all loggers – each logger gets LOGGING_MAX_BYTES/len(loggers) space (2GB by default)

  • LOGGING_MAX_FILES : number of files to split each logger’s logs across (default: 5)

  • LOGLEVEL: One of ('DEBUG', 'INFO', 'WARNING', 'EXCEPTION') that sets the minimum log level that is printed and written to disk

  • TIMEOUT: timeout used for timeout decorators on time-sensitive operations (in seconds, default 0.05)

  • HEARTBEAT_TIMEOUT: Time between heartbeats between GUI and controller after which contact is assumed to be lost (in seconds, default 0.02)

  • GUI_STATE_FN: Filename of gui control state file, relative to VENT_DIR (default: gui_state.json)

  • GUI_UPDATE_TIME: Time between calls of PVP_Gui.update_gui() (in seconds, default: 0.05)

  • ENABLE_DIALOGS: Enable all GUI dialogs – set as False when testing on virtual frame buffer that doesn’t support them (default: True and should stay that way)

  • ENABLE_WARNINGS: Enable user warnings and value change confirmations (default: True)

  • CONTROLLER_MAX_FLOW: Maximum flow, above which the controller considers a sensor error (default: 10)

  • CONTROLLER_MAX_PRESSURE: Maximum pressure, above which the controller considers a sensor error (default: 100)

  • CONTROLLER_MAX_STUCK_SENSOR: Max amount of time (in s) before considering a sensor stuck (default: 0.2)

  • CONTROLLER_LOOP_UPDATE_TIME: Amount of time to sleep in between controller update times when using ControlModuleDevice (default: 0.0)

  • CONTROLLER_LOOP_UPDATE_TIME_SIMULATOR: Amount of time to sleep in between controller updates when using ControlModuleSimulator (default: 0.005)

  • CONTROLLER_LOOPS_UNTIL_UPDATE: Number of controller loops in between updating its externally-available COPY attributes retrieved by ControlModuleBase.get_sensor() et al

  • CONTROLLER_RINGBUFFER_SIZE: Maximum number of breath cycle records to be kept in memory (default: 100)

  • COUGH_DURATION: Amount of time the high-pressure alarm limit can be exceeded and considered a cough (in seconds, default: 0.1)

  • BREATH_PRESSURE_DROP: Amount pressure can drop below set PEEP before being considered an autonomous breath when in breath detection mode

  • BREATH_DETECTION: Whether the controller should detect autonomous breaths in order to reset ventilation cycles (default: True)

pvp.common.prefs.set_pref(key: str, val)[source]

Sets a pref in the manager and, if prefs.LOADED is True, calls prefs.save_prefs()

Parameters
  • key (str) – Name of pref key

  • val – Value to set

pvp.common.prefs.get_pref(key: str = None)[source]

Get global configuration value

Parameters

key (str, None) – get configuration value with specific key . if None , return all config values.

pvp.common.prefs.load_prefs(prefs_fn: str)[source]

Load prefs from a .json prefs file, combining (and overwriting) any existing prefs, and then saves.

Called on pvp import by prefs.init()

Also initializes prefs._LOGGER

Note

once this function is called, set_pref() will update the prefs file on disk. So if load_prefs() is called again at any point it should not change prefs.

Parameters

prefs_fn (str) – path of prefs.json

pvp.common.prefs.save_prefs(prefs_fn: str = None)[source]

Dumps loaded prefs to PREFS_FN.

Parameters

prefs_fn (str) – Location to dump prefs. if None, use existing PREFS_FN

pvp.common.prefs.make_dirs()[source]

ensures _DIRECTORIES are created and added to prefs.

pvp.common.prefs.init()[source]

Initialize prefs. Called in pvp.__init__.py to ensure prefs are initialized before anything else.