mopidy/mopidy/core/history.py
Jens Luetjen e9625e9feb core: Fix #310: Persist mopidy state between runs.
Persist following properties:
mopidy.core.tracklist
    _tl_tracks
    _next_tlid
    get_consume()
    get_random()
    get_repeat()
    get_single()
mopidy.core.history
    _history
mopidy.core.playlist
    get_current_tl_track()
    get_time_position()
mopidy.core.mixer
    get_volume()

Details:
- moved json export/import write_library()/load_library() from mopidy/local to mopidy/models
- new core methods save_state(), load_state()
- save_state(), load_state() accessible via rpc
- save state to disk at stop
- load state from disk at start
- new config: core.restore_state ("off", "load", "play")

TODO:
- seek to play position does not work. Timing issue.
- use extra thread to load state from disk at start?
2015-12-27 19:28:41 +01:00

73 lines
2.0 KiB
Python

from __future__ import absolute_import, unicode_literals
import copy
import logging
import time
from mopidy import models
logger = logging.getLogger(__name__)
class HistoryController(object):
pykka_traversable = True
def __init__(self):
self._history = []
def _add_track(self, track):
"""Add track to the playback history.
Internal method for :class:`mopidy.core.PlaybackController`.
:param track: track to add
:type track: :class:`mopidy.models.Track`
"""
if not isinstance(track, models.Track):
raise TypeError('Only Track objects can be added to the history')
timestamp = int(time.time() * 1000)
name_parts = []
if track.artists:
name_parts.append(
', '.join([artist.name for artist in track.artists]))
if track.name is not None:
name_parts.append(track.name)
name = ' - '.join(name_parts)
ref = models.Ref.track(uri=track.uri, name=name)
self._history.insert(0, (timestamp, ref))
def get_length(self):
"""Get the number of tracks in the history.
:returns: the history length
:rtype: int
"""
return len(self._history)
def get_history(self):
"""Get the track history.
The timestamps are milliseconds since epoch.
:returns: the track history
:rtype: list of (timestamp, :class:`mopidy.models.Ref`) tuples
"""
return copy.copy(self._history)
def _state_export(self, data):
"""Internal method for :class:`mopidy.Core`."""
data['history'] = {}
data['history']['history'] = self._history
def _state_import(self, data, coverage):
"""Internal method for :class:`mopidy.Core`."""
if 'history' not in data:
return
if 'history' in coverage:
if 'history' in data['history']:
self._history = data['history']['history']