aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/python/usrp_mpm/simulator/config.py
diff options
context:
space:
mode:
authorSamuel O'Brien <sam.obrien@ni.com>2020-07-21 12:43:55 -0500
committerAaron Rossetto <aaron.rossetto@ni.com>2020-10-28 15:25:48 -0500
commitbd278a4b936f3e30f51d7cb9ff489f3ff7215379 (patch)
tree0001f604bf57831c8b760f7c135f134d91e950c2 /mpm/python/usrp_mpm/simulator/config.py
parentd42ddc804118b2e9120c84efd477f9f4b3f8472e (diff)
downloaduhd-bd278a4b936f3e30f51d7cb9ff489f3ff7215379.tar.gz
uhd-bd278a4b936f3e30f51d7cb9ff489f3ff7215379.tar.bz2
uhd-bd278a4b936f3e30f51d7cb9ff489f3ff7215379.zip
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 <sam.obrien@ni.com>
Diffstat (limited to 'mpm/python/usrp_mpm/simulator/config.py')
-rw-r--r--mpm/python/usrp_mpm/simulator/config.py52
1 files changed, 52 insertions, 0 deletions
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)