Configure#
- class QInstrument.lib.Configure.Configure(datadir=None, configdir=None)[source]#
Bases:
QObjectSave 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 underdatadir.- 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:
- 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:
- 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:
- 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 toconfigname(). 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), readsobj.settings.
- Return type:
- restore(obj)[source]#
Restore obj’s settings from its JSON configuration file.
Reads the JSON file at
configname()and assigns the result toobj.settings. Logs a warning and leaves obj unchanged if the file does not exist or cannot be parsed.- Parameters:
obj (object) – Object with a
settingsproperty whose setter accepts adict[str, bool | int | float | str].- Return type:
- 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. ReturnsNoneif the file does not exist or cannot be parsed, without logging a warning.