Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Add an autosave feature. State can saved frequently #6

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mopidy_autoplay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ def get_config_schema(self):
# mute = True|False|"auto")
schema['mixer.mute'] = AutoValue(config.Boolean)

# Save state on events
schema['autosave_events'] = AutoValue(config.List,optional=True)
schema['autosave_min_interval'] = AutoValue(
config.Integer, minimum=10)

return schema

def setup(self, registry):
Expand Down
5 changes: 5 additions & 0 deletions mopidy_autoplay/ext.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ playback.time_position = auto
# Mixer (volume = [0..100]; mute = on|off|true|false)
mixer.volume = auto
mixer.mute = auto

# Auto safe on events. See https://docs.mopidy.com/en/latest/api/core/#core-events for possible events
# autosave_events = track_playback_started
autosave_events =
autosave_min_interval = 60
19 changes: 15 additions & 4 deletions mopidy_autoplay/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import glob
import json
import pykka

import time
from mopidy import core
from . import Extension, Recollection

Expand All @@ -36,14 +36,15 @@ def __init__(self, config, core):
logger.debug(
"Use '%s' as statefile.",
self.statefile)

self._last_autosave = time.time()
# The frontend implementation

def on_start(self): # noqa: D401
"""Called, when the extension is started."""
logger.debug("on_start()")

state = self.read_state(self.statefile)

if state:
self.restore_state(state)

Expand All @@ -54,8 +55,18 @@ def on_stop(self): # noqa: D401
state = self.store_state()
self.write_state(state, self.statefile)

# Helper functions

def on_event(self, event, **kwargs):
logger.debug("Event %s args: %s",event,str(kwargs))
if event in self.config['autosave_events']:
now = time.time()
if (now - self._last_autosave) < self.config['autosave_min_interval']:
logger.info("Skip autosave")
else:
logger.info("Autosave ({0})".format(event))
state = self.store_state()
self.write_state(state, self.statefile)
self._last_autosave = now

def _get_config(self, state, controller, option):
"""
Return the configuration from `config` and `state`.
Expand Down