Пример #1
0
def get_machine_id():
    global MACHINE_ID
    if MACHINE_ID:
        return MACHINE_ID

    # determine MACHINE_ID from config files
    configs_map = {}
    # TODO check if this distinction is needed - config.CONFIG_FILE_PATH already handles tmp vs home folder
    config_file_tmp = get_config_file_tempdir()
    config_file_home = get_config_file_homedir()
    for config_file in (config_file_home, config_file_tmp):
        if config_file:
            local_configs = configs_map[config_file] = config.load_config_file(
                config_file=config_file
            )
            if "machine_id" in local_configs:
                MACHINE_ID = local_configs["machine_id"]
                break

    # if we can neither find NOR create the config files, fall back to process id
    if not configs_map:
        return PROCESS_ID

    # assign default id if empty
    if not MACHINE_ID:
        MACHINE_ID = short_uid()

    # update MACHINE_ID in all config files
    for config_file, configs in configs_map.items():
        configs["machine_id"] = MACHINE_ID
        save_file(config_file, json.dumps(configs))

    return MACHINE_ID
Пример #2
0
def get_machine_id() -> str:
    machine_id = None
    # determine machine_id from config files
    configs_map = {}
    # TODO check if this distinction is needed - config.CONFIG_FILE_PATH already handles tmp vs home folder
    config_file_tmp = get_config_file_tempdir()
    config_file_home = get_config_file_homedir()
    for config_file in (config_file_home, config_file_tmp):
        if config_file:
            local_configs = configs_map[config_file] = config.load_config_file(
                config_file=config_file)
            if "machine_id" in local_configs:
                machine_id = local_configs["machine_id"]
                break

    # if we can neither find NOR create the config files, fall back to process id
    if not configs_map:
        return get_session_id()

    # assign default id if empty
    if not machine_id:
        machine_id = common.short_uid()

    # update machine_id in all config files
    for config_file, configs in configs_map.items():
        configs["machine_id"] = machine_id
        common.save_file(config_file, json.dumps(configs))

    return machine_id
Пример #3
0
    def test_creates_file_if_not_exists(self, tmp_path):
        tmp_file = tmp_path / "config.json"
        assert not tmp_file.exists()

        cfg = config.load_config_file(str(tmp_file))

        assert tmp_file.exists()
        assert len(cfg) == 0
Пример #4
0
    def test_illegal_config_content_fails_silently(self, tmp_path):
        test_config = '{"foo: "bar"}'  # json syntax error

        tmp_file = tmp_path / "config.json"
        tmp_file.write_text(test_config)

        cfg = config.load_config_file(str(tmp_file))

        assert cfg is not None
        assert len(cfg) == 0
Пример #5
0
    def test_with_existing_file(self, tmp_path):
        test_config = '{"foo": "bar", "why": 42}'

        tmp_file = tmp_path / "config.json"
        tmp_file.write_text(test_config)

        cfg = config.load_config_file(str(tmp_file))

        assert len(cfg) == 2
        assert "foo" in cfg
        assert "why" in cfg
        assert cfg["foo"] == "bar"
        assert cfg["why"] == 42