dm_config_parser

Config parser module.

DMConfigParser

DMConfigParser()

Class to parse a configuration file for device connections and runtime options.

CONFIG_FILE_PATH_ENV_VARIABLE class-attribute instance-attribute

CONFIG_FILE_PATH_ENV_VARIABLE = 'TM_DEVICES_CONFIG'

The name of the environment variable to check for a custom config file path.

This environment variable is checked to see if there is a user-provided path to a custom config file that contains a list of devices and hostnames/IP addresses.

DEFAULT_CONFIG_FILE_PATH class-attribute instance-attribute

DEFAULT_CONFIG_FILE_PATH = './tm_devices.yaml'

The default location for the config file.

If present, the file should contain a list of devices and hostnames/IP addresses. See the configuration documentation for the config file syntax.

DEVICES_ENV_VARIABLE class-attribute instance-attribute

DEVICES_ENV_VARIABLE = 'TM_DEVICES'

The name of the environment variable which can contain a list of device configurations.

Multiple devices are separated by “~~~”, each device contains a comma-separated list of device parameters to use when connecting to the device. See the configuration documentation for the device configuration environment variable syntax.

FileType class-attribute instance-attribute

FileType = ConfigFileType

A convenience enumeration listing the valid config file types.

OPTIONS_ENV_VARIABLE class-attribute instance-attribute

OPTIONS_ENV_VARIABLE = 'TM_OPTIONS'

The name of the environment variable which can contain a comma-separated list of options.

See the configuration documentation for the options environment variable syntax.

defined_config_file_path property

defined_config_file_path: str

Filepath of the config file.

Prioritizes the path contained in the environment variable defined by DMConfigParser.CONFIG_FILE_PATH_ENV_VARIABLE with a fallback to the default path defined by DMConfigParser.DEFAULT_CONFIG_FILE_PATH.

devices property

Return the device dict as a read-only object.

options property

options: DMConfigOptions

Return the config options.

add_device

add_device(
    *,
    device_type: DeviceTypes | str,
    address: str,
    connection_type: ConnectionTypes | str = TCPIP,
    alias: str | None = None,
    lan_port: int | None = None,
    lan_device_endpoint: str | None = None,
    serial_config: SerialConfig | None = None,
    device_driver: str | None = None,
    gpib_board_number: int | None = None
) -> tuple[str, DeviceConfigEntry]

Add a new device configuration entry.

Parameters:
  • device_type (DeviceTypes | str) –

    The specific device type defined in the config entry.

  • address (str) –

    The address used to connect to the device: TCPIP: IP address or the hostname. SOCKET: IP address or the hostname (must define lan_port field). REST_API: IP address or the hostname (must define lan_port field). SERIAL/ASRL: serial COM port number. USB: use expression format f”{model}-{serial_number}” (ex: “MSO24-ABC0123”).

  • connection_type (ConnectionTypes | str, default: TCPIP ) –

    The specific type of connection defined in the config entry.

  • alias (str | None, default: None ) –

    An optional key/name used to retrieve this device from the DeviceManager.

  • lan_port (int | None, default: None ) –

    The port number to connect on, used for SOCKET/REST_API connections.

  • lan_device_endpoint (str | None, default: None ) –

    The LAN device endpoint to connect on, used for TCPIP connections. The default is ‘inst0’.

  • serial_config (SerialConfig | None, default: None ) –

    A dataclass for holding serial connection info.

  • device_driver (str | None, default: None ) –

    A string indicating the specific Python device driver to use.

  • gpib_board_number (int | None, default: None ) –

    The GPIB board number (also referred to as a controller), only used for GPIB connections. The default is 0.

Returns:
Raises:
  • ValueError

    invalid config options, trouble creating (or a duplicate) device entry.

load_config_file

load_config_file(config_file_path: str | PathLike[str] | None = None) -> None

Load in the config file located at the given path.

This method will update any changed options and add any newly defined devices.

Parameters:
  • config_file_path (str | PathLike[str] | None, default: None ) –

    The path to the config file to load.

remove_device

remove_device(device_name: str) -> None

Remove a device from the configuration.

Parameters:
  • device_name (str) –

    The name of the device to remove from the internal dictionary.

to_config_file_text

to_config_file_text(file_type: ConfigFileType = YAML) -> str

Return string representation of instance as config file contents.

Parameters:
  • file_type (ConfigFileType, default: YAML ) –

    The type of file to create, yaml or toml.

update_options

update_options(new_options: DMConfigOptions) -> None

Update the current configuration options.

This ensures that any time new options are loaded in only explicitly defined options will be updated.

Parameters:
  • new_options (DMConfigOptions) –

    The new options to merge with the current options.

write_config_to_file

write_config_to_file(config_file_path: str | PathLike[str] | None = None) -> str

Write the current configuration to a config file.

This method will overwrite any existing config file with the current devices and options. If no custom path is provided, it will write to the file defined by DMConfigParser.defined_config_file_path.

Parameters:
  • config_file_path (str | PathLike[str] | None, default: None ) –

    The path to the config file. If ends in “.toml” will create toml file.

Returns:
  • str

    Path to the written file.