Example #1
0
def test_reload_yaml():  # type: ignore
    with tempfile.NamedTemporaryFile() as f:
        f.file.write(YAML.encode())
        f.file.flush()
        cfg = config_from_yaml(f.name, read_from_file=True)
        assert cfg == config_from_dict(DICT)

        f.file.seek(0)
        f.file.truncate(0)
        f.file.write(YAML2.encode())
        f.file.flush()
        cfg.reload()
        assert cfg == config_from_yaml(YAML2)
Example #2
0
def test_load_yaml_filename():  # type: ignore
    with tempfile.NamedTemporaryFile() as f:
        f.file.write(YAML.encode())
        f.file.flush()
        cfg = config_from_yaml(f.name, read_from_file=True)
    assert cfg["a1.b1.c1"] == 1
    assert cfg["a1.b1"].as_dict() == {"c1": 1, "c2": 2, "c3": 3}
    assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": True, "c3": 1.1}
    assert cfg == config_from_dict(DICT)
Example #3
0
def test_load_yaml_2():  # type: ignore
    cfg = config_from_yaml(YAML2)
    assert cfg["martin.name"] == "Martin D'vloper"
    assert cfg["martin"].as_dict() == {
        "job": "Developer",
        "name": "Martin D'vloper",
        "skills": ["python", "perl", "pascal"],
    }
    assert cfg["martin.skills"] == ["python", "perl", "pascal"]
def test_load_yaml_2():  # type: ignore
    cfg = config_from_yaml(YAML2)
    assert cfg["martin.name"] == 'Martin D\'vloper'
    assert cfg["martin"].as_dict() == {
        'job': 'Developer',
        'name': "Martin D'vloper",
        'skills': ['python', 'perl', 'pascal']
    }
    assert cfg["martin.skills"] == ['python', 'perl', 'pascal']
Example #5
0
 def load_files(
     files: typing.Dict[str, pathlib.Path]
 ) -> typing.Dict[str, typing.Dict[str, typing.Any]]:
     files_data: typing.Dict[str, typing.Dict[str, typing.Any]] = {}
     for file_tag, file in files.items():
         file_data = config.config_from_yaml(
             str(file), read_from_file=True
         ).as_attrdict()
         files_data.update({file_tag: file_data})
     return files_data
Example #6
0
def read_config():
    """Open the YAML configuration file and check the contents"""
    try:
        yaml.FullLoader.add_constructor('!secret', secret_yaml)
        yaml_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), CONFIG_YAML)
        config = config_from_yaml(data=yaml_file, read_from_file=True)
        if config:
            config = check_config(config)
        return config
    except ConfigError as e:
        raise FailedInitialization(f"ConfigError exception: {e}")
    except FailedInitialization:
        raise
    except Exception as e:
        error_message = buildYAMLExceptionString(exception=e, file=yaml_file)
        raise FailedInitialization(f"Unexpected exception: {error_message}")
Example #7
0
def load_config():
    global _conf
    home = expanduser('~')
    config_folder = f'{home}/.config/holmes'
    config_file = f'{config_folder}/config.yaml'
    if exists(config_file):
        with open(config_file) as input:
            _conf = ConfigurationSet(config_from_env('HOLMES'),
                                     config_from_yaml(input))
    else:
        DEFAULT_CONFIG = {
            'storage': 'file',
            'files': {
                'frames': f'{home}/Library/ApplicationSupport/watson/frames',
                'state': f'{home}/Library/ApplicationSupport/watson/state',
            }
        }
        _conf = ConfigurationSet(config_from_dict(DEFAULT_CONFIG))
Example #8
0
        except NewConnectionError:
            raise
        except Exception as e:
            raise InfluxDBBucketError(f"Unexpected exception in connect_bucket(): {e}")


#
# Debug code to test instandalone mode
#

testdata = [
    {'sb51': 0, 'sb71': 0, 'sb72': 0, 'site': 0, 'topic': 'ac_measurements/power'},
    {'sb51': {'a': 0, 'b': 0, 'c': 0, 'sb51': 0}, 'sb71': {'a': 0, 'b': 0, 'c': 0, 'sb71': 0},
        'sb72': {'a': 0, 'b': 0, 'c': 0, 'sb72': 0}, 'site': 0, 'topic': 'dc_measurements/power'},
    {'sb51': {'a': 10.0, 'b': 20.0, 'c': 30.0}, 'sb71': {'a': 40.0, 'b': 50.0, 'c': 60.0},
        'sb72': {'a': 70.0, 'b': 80.0, 'c': 90.0}, 'topic': 'dc_measurements/voltage'},
    {'sb51': 16777213, 'sb71': 16777213, 'sb72': 16777213, 'topic': 'status/general_operating_status'}
]

if __name__ == "__main__":
    yaml_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'multisma2.yaml')
    config = config_from_yaml(data=yaml_file, read_from_file=True)
    influxdb = InfluxDB()
    result = influxdb.start(config=config.multisma2.influxdb2)
    if not result:
        print("Something failed during initialization")
    else:
        influxdb.write_sma_sensors(testdata)
        influxdb.stop()
        print("Done")
Example #9
0
def test_fails():  # type: ignore
    with raises(ValueError):
        config_from_yaml(YAML3)
Example #10
0
def test_load_yaml():  # type: ignore
    cfg = config_from_yaml(YAML)
    assert cfg["a1.b1.c1"] == 1
    assert cfg["a1.b1"].as_dict() == {"c1": 1, "c2": 2, "c3": 3}
    assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": True, "c3": 1.1}
Example #11
0
def test_equality():  # type: ignore
    cfg = config_from_yaml(YAML)
    assert cfg == config_from_dict(DICT)
Example #12
0
def setup_config(dictionary):
    return config.ConfigurationSet(
        config.config_from_env(prefix="MLCOMMONS"),
        config.config_from_yaml(config_path(), read_from_file=True),
        config.config_from_dict(dictionary),
    )