From bd278a4b936f3e30f51d7cb9ff489f3ff7215379 Mon Sep 17 00:00:00 2001 From: Samuel O'Brien Date: Tue, 21 Jul 2020 12:43:55 -0500 Subject: sim: Support Configuration Files This commit adds support for configuration files to the simulator. As of now, these files only change the source and sink of data samples, but they are easily extensible to add more configuration to the simulator. Signed-off-by: Samuel O'Brien --- mpm/python/usrp_mpm/simulator/config.py | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 mpm/python/usrp_mpm/simulator/config.py (limited to 'mpm/python/usrp_mpm/simulator/config.py') diff --git a/mpm/python/usrp_mpm/simulator/config.py b/mpm/python/usrp_mpm/simulator/config.py new file mode 100644 index 000000000..a5f07950d --- /dev/null +++ b/mpm/python/usrp_mpm/simulator/config.py @@ -0,0 +1,52 @@ +# +# Copyright 2020 Ettus Research, a National Instruments Brand +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +""" +This module contains the logic to read configuration files from the +filesystem and parse configuration data from them. This data includes +anything from the serial number of the radio to the type of hardware +it identifies itself as. +""" + +import configparser +from .sample_source import sinks, sources, NullSamples + +class Config: + """This class represents a configuration file for the usrp simulator. + This file should conform to the .ini format defined by the + configparser module + + It should have a [sample.source] section and a [sample.sink] section. + Each section should have a 'class' key, which gives the name of the + Source/Sink class to instanitate (see the decorators in + sample_source.py). The other key value pairs in the section are + passed to the source/sink constructor as strings through **kwargs + """ + def __init__(self, source_gen, sink_gen): + self.source_gen = source_gen + self.sink_gen = sink_gen + + @classmethod + def from_path(cls, path): + """Parse a config .ini file from a path""" + parser = configparser.ConfigParser() + parser.read(path) + source_gen = Config._read_sample_section(parser['sample.source'], sources) + sink_gen = Config._read_sample_section(parser['sample.sink'], sinks) + return cls(source_gen, sink_gen) + + @staticmethod + def _read_sample_section(section, lookup): + args = dict(section) + class_name = args.pop("class") + constructor = lookup[class_name] + def section_gen(): + return constructor(**args) + return section_gen + + @classmethod + def default(cls): + """Return a default config""" + return cls(NullSamples, NullSamples) -- cgit v1.2.3