from configparser import ConfigParser class ChartConfig: """ Class that holds the chart section of a configuration file (for an instrument). Attributes : chart_config (Section) : the Section corresponding to the "chart" section in the given configuration file """ KEYS = ["cat", "color", "unit", "label"] def __init__(self, path): """ Parameters : path (str) : the path to the configuration file """ self.errors = {} self.variables = {} cfgp = ConfigParser(interpolation=None) cfgp.optionxform = str cfgp.read(path) section = cfgp["chart"] for key, raw_value in section.items(): if len(key.split('.')) > 1: self.errors[key] = f'illegal key: {key}' continue arguments = raw_value.split(",") keyword_mode = False config = {'cat': '*'} for i, argument in enumerate(arguments): argname, _, argvalue = argument.rpartition(':') if argname: keyword_mode = True config[argname] = argvalue else: if keyword_mode: self.errors[key] = f"positional arg after keywd arg: {key}={raw_value!r}" else: try: if argvalue: config[self.KEYS[i]] = argvalue except Exception as e: self.errors[key] = f"{e!r} in {key}={raw_value}" self.variables[key] = config def get_variable_parameter_config(self, key): """ Gets the configuration of the given key in the configuration file (chart section). Parameters : key (str) : the key to look for in the chart section ([.]) Returns : {"cat":(str), "color":(str), "unit":(str)} | None : a dictionnary representing the different options for the given key in the chart section. The different options are in this dict if they are found in the chart section for the given key. Returns None if the key is not in the chart section, or if there is a syntax problem for the given key. """ return self.variables.get(key)