Configure#

class QInstrument.lib.Configure.Configure(datadir=None, configdir=None)[source]#

Bases: QObject

Save and restore instrument configuration to and from JSON files.

Configuration files are named after the class of the target object and stored under configdir. Timestamped data filenames are generated under datadir.

Parameters:
  • datadir (str | None) – Directory for timestamped data files. Default: ~/data/.

  • configdir (str | None) – Directory for JSON configuration files. Default: ~/.QInstrument/.

datadir#

Resolved path to the data directory.

Type:

Path

configdir#

Resolved path to the configuration directory.

Type:

Path

timestamp()[source]#

Return a string representing the current date and time.

Return type:

str

Returns:

str – Timestamp formatted as _YYYYMonDD_HHMMSS (e.g. _2024Jan15_143022).

filename(prefix='QInstrument', suffix='')[source]#

Return a timestamped filename under datadir.

Parameters:
  • prefix (str) – String prepended to the filename. Default: 'QInstrument'.

  • suffix (str) – String appended after the timestamp. Default: ''.

Return type:

str

Returns:

str – Absolute path string of the form <datadir>/<prefix><timestamp><suffix>.

configname(obj)[source]#

Return the path to the JSON configuration file for obj.

The filename is derived from obj’s class name, so all instances of the same class share one configuration file.

Parameters:

obj (object) – Object whose class name determines the configuration filename.

Return type:

str

Returns:

str – Absolute path string of the form <configdir>/<ClassName>.json.

save(obj, settings=None)[source]#

Save obj’s settings to its JSON configuration file.

Reads obj.settings (a dict) and serializes it to configname(). Does nothing when the settings dict is empty. An explicit settings dict may be supplied to avoid reading from obj directly — useful when obj lives in a worker thread.

Parameters:
  • obj (object) – Object whose class name determines the configuration filename.

  • settings (dict or None, optional) – Settings to save. When None (default), reads obj.settings.

Return type:

None

restore(obj)[source]#

Restore obj’s settings from its JSON configuration file.

Reads the JSON file at configname() and assigns the result to obj.settings. Logs a warning and leaves obj unchanged if the file does not exist or cannot be parsed.

Parameters:

obj (object) – Object with a settings property whose setter accepts a dict[str, bool | int | float | str].

Return type:

None

read(obj)[source]#

Read and return obj’s saved configuration without applying it.

Reads the JSON file at configname() and returns its contents as a dict. Returns None if the file does not exist or cannot be parsed, without logging a warning.

Parameters:

obj (object) – Object whose class name determines the configuration filename.

Return type:

dict | None

Returns:

dict or None – Saved configuration dict, or None if unavailable.

query_save(obj)[source]#

Prompt the user and save obj’s settings if confirmed.

Opens a modal dialog asking whether to save the current configuration. Calls save() if the user clicks Yes.

Parameters:

obj (object) – Object to save if the user confirms.

Return type:

None