# Path: thrifty/settings.py
# DEFINITIONS = {
#     'sample_rate': Definition(
#         ['--sample-rate', '-s'],
#         setting_parsers.metric_float,
#         '2.4M',
#         "Sample rate (sps)"
#     ),
# 
#     'chip_rate': Definition(
#         ['--chip-rate', '-p'],
#         setting_parsers.metric_float,
#         '0.999707M',
#         "Rate at which the code is being transmitted (bps)"
#     ),
# 
#     'tuner_freq': Definition(
#         ['--freq', '-f'],
#         setting_parsers.metric_float,
#         '433.83M',
#         "Tuner center frequency (Hz)"
#     ),
# 
#     'tuner_gain': Definition(
#         ['--gain', '-g'],
#         float,
#         '0',
#         "Tuner gain (dB)"
#     ),
# 
#     'capture_skip': Definition(
#         ['--skip', '-k'],
#         int,
#         '1',
#         "Number of blocks to skip before starting capturing from the SDR"
#     ),
# 
#     'block_size': Definition(
#         ['--block-size', '-b'],
#         int,
#         '16384',
#         "Length of fixed-sized blocks, which should be a power of two "
#         "(samples)"
#     ),
# 
#     'block_history': Definition(
#         ['--history', '-y'],
#         int,
#         '4920',
#         "The number of samples at the end of a block that should be repeated "
#         "at the start of the next block (samples)"
#     ),
# 
#     'carrier_window': Definition(
#         ['--carrier-window', '-w'],
#         setting_parsers.freq_range,
#         '0--1',
#         "Range of frequencies or frequency bins to look for carrier"
#     ),
# 
#     'carrier_threshold': Definition(
#         ['--carrier-threshold', '-t'],
#         setting_parsers.threshold,
#         '15*snr',
#         "Threshold formula for carrier detector"
#     ),
# 
#     'corr_threshold': Definition(
#         ['--corr-threshold', '-u'],
#         setting_parsers.threshold,
#         '15*snr',
#         "Threshold formula for correlation peak detector"
#     ),
# 
#     'template': Definition(
#         ['--template', '-z'],
#         str,
#         'template.npy',
#         "Load template from a Numpy .npy file"
#     ),
# 
#     'rxid': Definition(
#         ['--rxid', '-r'],
#         int,
#         -1,
#         "Unique identifier of this receiver"
#     ),
# }
# DEFAULT_CONFIG_PATH = 'detector.cfg'
# CONFIG_COMMENT_CHAR = '#'
# CONFIG_DELIMITER = ':'
# CONFIG_DEST = 'config'
# class Error(Exception):
# class ConfigSyntaxError(Error):
# class SettingKeyError(Error):
# class Namespace(dict):
#     def __init__(self, line_no, msg):
#     def __str__(self):
#     def __init__(self, msg):
#     def __str__(self):
#     def __init__(self, dict_):
# def add_argparse_arguments(parser, keys, definitions=None):
# def load(args=None, config_file=None, definitions=None):
# def load_args(parser, keys, argv=None, definitions=None):
# def parse_kvconfig(config_file):


import argparse
import io
import pytest
from thrifty import settings
def test_argparse_simple():
    """Can generate argparse arguments."""
    parser = argparse.ArgumentParser()
    settings.add_argparse_arguments(parser, ['foo', 'bar.baz'],
                                    definitions=DEFINITIONS)
    args = vars(parser.parse_args(['-f', '12.34', '--baz=56.78']))
    assert args['foo'] == '12.34'
    assert args['bar.baz'] == '56.78'


def test_load_default_values():
    """Can load default values."""
    values = settings.load(None, None, DEFINITIONS)
    assert len(values) == 2
    assert values['foo'] == float(DEFAULT_FOO)
    assert values['bar.baz'] == float(DEFAULT_BAZ)


def test_load_config():
    """Can load settings from config file."""
    config = io.BytesIO("bar.baz:   1234.56")
    values = settings.load(None, config, DEFINITIONS)
    assert values['foo'] == float(DEFAULT_FOO)
    assert values['bar.baz'] == 1234.56


def test_load_syntax_error():
    """Throw ConfigSyntaxError if config's syntax is invalid."""
    config = io.BytesIO("foobar")
    with pytest.raises(settings.ConfigSyntaxError):

# Path: thrifty/settings.py
# DEFINITIONS = {
#     'sample_rate': Definition(
#         ['--sample-rate', '-s'],
#         setting_parsers.metric_float,
#         '2.4M',
#         "Sample rate (sps)"
#     ),
# 
#     'chip_rate': Definition(
#         ['--chip-rate', '-p'],
#         setting_parsers.metric_float,
#         '0.999707M',
#         "Rate at which the code is being transmitted (bps)"
#     ),
# 
#     'tuner_freq': Definition(
#         ['--freq', '-f'],
#         setting_parsers.metric_float,
#         '433.83M',
#         "Tuner center frequency (Hz)"
#     ),
# 
#     'tuner_gain': Definition(
#         ['--gain', '-g'],
#         float,
#         '0',
#         "Tuner gain (dB)"
#     ),
# 
#     'capture_skip': Definition(
#         ['--skip', '-k'],
#         int,
#         '1',
#         "Number of blocks to skip before starting capturing from the SDR"
#     ),
# 
#     'block_size': Definition(
#         ['--block-size', '-b'],
#         int,
#         '16384',
#         "Length of fixed-sized blocks, which should be a power of two "
#         "(samples)"
#     ),
# 
#     'block_history': Definition(
#         ['--history', '-y'],
#         int,
#         '4920',
#         "The number of samples at the end of a block that should be repeated "
#         "at the start of the next block (samples)"
#     ),
# 
#     'carrier_window': Definition(
#         ['--carrier-window', '-w'],
#         setting_parsers.freq_range,
#         '0--1',
#         "Range of frequencies or frequency bins to look for carrier"
#     ),
# 
#     'carrier_threshold': Definition(
#         ['--carrier-threshold', '-t'],
#         setting_parsers.threshold,
#         '15*snr',
#         "Threshold formula for carrier detector"
#     ),
# 
#     'corr_threshold': Definition(
#         ['--corr-threshold', '-u'],
#         setting_parsers.threshold,
#         '15*snr',
#         "Threshold formula for correlation peak detector"
#     ),
# 
#     'template': Definition(
#         ['--template', '-z'],
#         str,
#         'template.npy',
#         "Load template from a Numpy .npy file"
#     ),
# 
#     'rxid': Definition(
#         ['--rxid', '-r'],
#         int,
#         -1,
#         "Unique identifier of this receiver"
#     ),
# }
# DEFAULT_CONFIG_PATH = 'detector.cfg'
# CONFIG_COMMENT_CHAR = '#'
# CONFIG_DELIMITER = ':'
# CONFIG_DEST = 'config'
# class Error(Exception):
# class ConfigSyntaxError(Error):
# class SettingKeyError(Error):
# class Namespace(dict):
#     def __init__(self, line_no, msg):
#     def __str__(self):
#     def __init__(self, msg):
#     def __str__(self):
#     def __init__(self, dict_):
# def add_argparse_arguments(parser, keys, definitions=None):
# def load(args=None, config_file=None, definitions=None):
# def load_args(parser, keys, argv=None, definitions=None):
# def parse_kvconfig(config_file):


# Path: tests/test_settings.py
import argparse
import io
import pytest
from thrifty import settings

def test_argparse_simple():
    """Can generate argparse arguments."""
    parser = argparse.ArgumentParser()
    settings.add_argparse_arguments(parser, ['foo', 'bar.baz'],
                                    definitions=DEFINITIONS)
    args = vars(parser.parse_args(['-f', '12.34', '--baz=56.78']))
    assert args['foo'] == '12.34'
    assert args['bar.baz'] == '56.78'


def test_load_default_values():
    """Can load default values."""
    values = settings.load(None, None, DEFINITIONS)
    assert len(values) == 2
    assert values['foo'] == float(DEFAULT_FOO)
    assert values['bar.baz'] == float(DEFAULT_BAZ)


def test_load_config():
    """Can load settings from config file."""
    config = io.BytesIO("bar.baz:   1234.56")
    values = settings.load(None, config, DEFINITIONS)
    assert values['foo'] == float(DEFAULT_FOO)
    assert values['bar.baz'] == 1234.56


def test_load_syntax_error():
    """Throw ConfigSyntaxError if config's syntax is invalid."""
    config = io.BytesIO("foobar")
    with pytest.raises(settings.ConfigSyntaxError):

        settings.load(None, config, DEFINITIONS)