aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/python/usrp_mpm/simulator/config.py
blob: a5f07950dda5495045eb02a11d7bd78a76e8477e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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)