47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from configparser import ConfigParser
|
|
import logging
|
|
|
|
|
|
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.variables = {}
|
|
cfgp = ConfigParser(interpolation=None)
|
|
cfgp.optionxform = str
|
|
cfgp.read(path)
|
|
try:
|
|
section = cfgp["chart"]
|
|
except KeyError:
|
|
return
|
|
for key, raw_value in section.items():
|
|
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:
|
|
logging.error('positional arg after keywd arg: %s=%r', key, raw_value)
|
|
else:
|
|
try:
|
|
if argvalue:
|
|
config[self.KEYS[i]] = argvalue
|
|
except Exception as e:
|
|
logging.error('%r in %s=%r', e, key, raw_value)
|
|
self.variables[key] = config
|