Exemplo n.º 1
0
async def api_profile() -> Union[str, Response]:
    """Read or write profile JSON directly"""
    assert core is not None
    layers = request.args.get("layers", "all")

    if request.method == "POST":
        # Ensure that JSON is valid
        profile_json = json5.loads(await request.data)
        recursive_remove(core.profile.system_json, profile_json)

        profile_path = Path(core.profile.write_path("profile.json"))
        with open(profile_path, "w") as profile_file:
            json.dump(profile_json, profile_file, indent=4)

        msg = f"Wrote profile to {profile_path}"
        logger.debug(msg)
        return msg

    if layers == "defaults":
        # Read default settings
        return jsonify(core.defaults)

    if layers == "profile":
        # Local settings only
        profile_path = Path(core.profile.read_path("profile.json"))
        return await send_file(profile_path)

    return jsonify(core.profile.json)
Exemplo n.º 2
0
def api_profile() -> Union[str, Response]:
    """Read or write profile JSON directly"""
    assert core is not None
    layers = request.args.get("layers", "all")

    if request.method == "POST":
        # Ensure that JSON is valid
        profile_json = json.loads(request.data)
        recursive_remove(core.profile.system_json, profile_json)

        profile_path = os.path.abspath(core.profile.write_path("profile.json"))
        with open(profile_path, "w") as profile_file:
            json.dump(profile_json, profile_file, indent=4)

        msg = "Wrote profile to %s" % profile_path
        logger.debug(msg)
        return msg

    if layers == "defaults":
        # Read default settings
        return jsonify(core.defaults)
    elif layers == "profile":
        # Local settings only
        profile_path = core.profile.read_path("profile.json")
        return send_file(open(profile_path, "rb"), mimetype="application/json")
    else:
        return jsonify(core.profile.json)
Exemplo n.º 3
0
def api_profile() -> Response:
    """Read or write profile JSON directly"""
    assert core is not None
    layers = request.args.get("layers", "all")

    if request.method == "POST":
        # Ensure that JSON is valid
        profile_json = json.loads(request.data)
        # from cerberus import Validator

        # schema_path = os.path.join(
        #     os.path.dirname(__file__), "rhasspy", "profile_schema.json"
        # )

        # with open(schema_path, "r") as schema_file:
        #     v = Validator(json.load(schema_file))
        #     profile_dict = json.loads(request.data)
        #     if not v.validate(profile_dict):
        #         print(json.dumps(profile_dict, indent=4))
        #         raise Exception(str(v._errors[0].info))

        if layers == "defaults":
            # Write default settings
            for profiles_dir in profiles_dirs:
                profile_path = os.path.join(profiles_dir, "defaults.json")
                try:
                    with open(profile_path, "wb") as profile_file:
                        profile_file.write(request.data)
                    break
                except:
                    pass
        else:
            # Write local profile settings
            if "rhasspy" in profile_json:
                if "default_profile" in profile_json["rhasspy"]:
                    del profile_json["rhasspy"]["default_profile"]

            recursive_remove(core.defaults, profile_json)

            profile_path = os.path.abspath(
                core.profile.write_path("profile.json"))
            with open(profile_path, "w") as profile_file:
                json.dump(profile_json, profile_file, indent=4)

        msg = "Wrote profile to %s" % profile_path
        logger.debug(msg)
        return msg

    if layers == "defaults":
        # Read default settings
        return jsonify(core.defaults)
    elif layers == "profile":
        # Local settings only
        profile_path = core.profile.read_path("profile.json")
        return send_file(open(profile_path, "rb"), mimetype="application/json")
    else:
        return jsonify(core.profile.json)