Exemple #1
0
    def ready(self) -> None:
        if "celery" in sys.argv and strtobool(os.environ.get("RUN_MAIN", "0")):
            # if the development celery process starts,
            # have it import all modules containing celery tasks
            # this way, its autoreload is notified on changes in these modules

            for module in conf.CELERY_IMPORTS:
                assert isinstance(module, str)
                __import__(module)

            return

        # ready is called for every management command and for autoreload
        # only start raveberry when
        # in debug mode and the main application is run (not autoreload)
        # or in prod mode (run by daphne)
        start_raveberry = (strtobool(os.environ.get("RUN_MAIN", "0"))
                           if "runserver" in sys.argv else
                           sys.argv[0].endswith("daphne"))

        if conf.TESTING:
            from core.musiq import controller

            # when MopidyAPI instances are created too often,
            # mopidy runs into a "Set changed size during iteration" error.
            # Thus we initialize the interface once and not for every testcase
            controller.start()

        if start_raveberry:
            from core import tasks
            from core import redis
            from core.musiq import musiq
            from core.musiq import playback
            from core.settings import basic
            from core.settings import platforms
            from core.lights import worker

            logging.info("starting raveberry")

            redis.start()
            tasks.start()

            worker.start()
            musiq.start()
            basic.start()
            platforms.start()

            def stop_workers() -> None:
                # wake up the playback thread and stop it
                redis.put("stop_playback_loop", True)
                playback.queue_changed.set()

                # wake the buzzer thread so it exits
                playback.buzzer_stopped.set()

                # wake up the listener thread with an instruction to stop the lights worker
                redis.connection.publish("lights_settings_changed", "stop")

            atexit.register(stop_workers)
Exemple #2
0
def _handle_monochrome_request(device: str,
                               request: WSGIRequest) -> HttpResponse:
    value, response = extract_value(request.POST)
    assert device in ["ring", "strip", "wled", "screen"]
    storage.put(cast(DeviceMonochrome, f"{device}_monochrome"),
                strtobool(value))
    _notify_settings_changed(device)
    return response
Exemple #3
0
def get(key: str) -> Union[bool, int, float, str, List, Dict, Tuple]:
    """This method returns the value for the given :param key: from redis.
    Vaules of non-existing keys are set to their respective default value."""
    # values are stored as string in redis
    # cast the value to its respective type, defined by the default value, before returning it
    default = defaults[key]
    value = connection.get(key)
    if value is None:
        return default
    if type(default) is bool:
        return strtobool(value)
    if type(default) in (list, dict, tuple):
        # evaluate the stored literal
        return literal_eval(value)
    return type(default)(value)
Exemple #4
0
def set_lights_shortcut(request: WSGIRequest) -> HttpResponse:
    """Stores the current lights state and restores the previous one."""
    value, response = extract_value(request.POST)
    should_enable = strtobool(value)
    is_enabled = (storage.get("ring_program") != "Disabled"
                  or storage.get("wled_program") != "Disabled"
                  or storage.get("strip_program") != "Disabled")
    if should_enable == is_enabled:
        return HttpResponse()
    if should_enable:
        for device in ["ring", "wled", "strip"]:
            set_program(
                device,
                storage.get(cast(DeviceProgram, f"last_{device}_program")))
    else:
        for device in ["ring", "wled", "strip"]:
            set_program(device, "Disabled")
    return response
Exemple #5
0
def get(key: str) -> Union[bool, int, float, str, tuple]:
    """This method returns the value for the given :param key:.
    Values of non-existing keys are set to their respective default value."""
    # values are stored as string in the database
    # cast the value to its respective type, defined by the default value, before returning it
    default = defaults[key]
    value = models.Setting.objects.get_or_create(
        key=key, defaults={"value": str(default)}
    )[0].value
    if type(default) is str:
        return str(value)
    if type(default) is int:
        return int(value)
    if type(default) is float:
        return float(value)
    if type(default) is bool:
        return strtobool(value)
    if type(default) is tuple:
        # evaluate the stored literal
        return literal_eval(value)
    raise ValueError(f"{key} not defined")
Exemple #6
0
def set_online_suggestions(request: WSGIRequest) -> HttpResponse:
    """Enables or disables online suggestions based on the given value."""
    value, response = extract_value(request.POST)
    storage.put("online_suggestions", strtobool(value))
    return response
Exemple #7
0
def set_dynamic_embedded_stream(request: WSGIRequest) -> HttpResponse:
    """Enables or disables dynamic streaming based on the given value."""
    value, response = extract_value(request.POST)
    storage.put("dynamic_embedded_stream", strtobool(value))
    return response
Exemple #8
0
def set_embed_stream(request: WSGIRequest) -> HttpResponse:
    """Enables or disables logging of requests and play logs based on the given value."""
    value, response = extract_value(request.POST)
    storage.put("embed_stream", strtobool(value))
    return response
Exemple #9
0
def set_ip_checking(request: WSGIRequest) -> HttpResponse:
    """Enables or disables ip checking based on the given value."""
    value, response = extract_value(request.POST)
    storage.put("ip_checking", strtobool(value))
    return response
Exemple #10
0
def set_enqueue_first(request: WSGIRequest) -> HttpResponse:
    """Enables or disables the new music only mode based on the given value."""
    value, response = extract_value(request.POST)
    storage.put("enqueue_first", strtobool(value))
    return response
Exemple #11
0
def set_dynamic_resolution(request: WSGIRequest) -> HttpResponse:
    """Sets whether the resolution should be dynamically adjusted depending on the performance."""
    value, response = extract_value(request.POST)
    storage.put("dynamic_resolution", strtobool(value))
    _notify_settings_changed("base")
    return response
Exemple #12
0
"""This module configures django."""
import logging.config
import os
import pathlib
import subprocess
import sys
from typing import List

import yaml
from django.core.management.utils import get_random_secret_key

from core.util import strtobool

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEMO = strtobool(os.environ.get("DJANGO_DEMO", "0"))

try:
    with open(os.path.join(BASE_DIR, "config/secret_key.txt"),
              encoding="utf-8") as f:
        SECRET_KEY = f.read().strip()
except FileNotFoundError:
    SECRET_KEY = get_random_secret_key()
    with open(os.path.join(BASE_DIR, "config/secret_key.txt"),
              "w",
              encoding="utf-8") as f:
        f.write(SECRET_KEY)
    print("created secret key")

try:
    with open(os.path.join(BASE_DIR, "VERSION"), encoding="utf-8") as f:
Exemple #13
0
def set_youtube_enabled(request: WSGIRequest) -> HttpResponse:
    """Enables or disables youtube to be used as a song provider."""
    value, response = extract_value(request.POST)
    storage.put("youtube_enabled", strtobool(value))
    return response
Exemple #14
0
def set_jamendo_enabled(request: WSGIRequest) -> HttpResponse:
    """Enables or disables jamendo to be used as a song provider.
    Makes sure mopidy has correct jamendo configuration."""
    value, _ = extract_value(request.POST)
    return _set_extension_enabled("jamendo", strtobool(value))