-
Notifications
You must be signed in to change notification settings - Fork 3
/
StateActor.py
73 lines (56 loc) · 2.05 KB
/
StateActor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json, logging
from TickActor import TickActor
STATE_FILE = 'data/state.json'
class StateActor(TickActor):
def __init__(self, mpdActor, sleepSeconds):
super(StateActor, self).__init__(sleepSeconds)
self.mpdActor = mpdActor
def doTick(self):
current = self.mpdActor.getCurrentSong().get()
if current == None:
return
state = self.loadState()
state["current"] = current
try:
state["played"][current["name"]] = current["elapsed"]
except KeyError:
state["played"] = {}
state["played"][current["name"]] = current["elapsed"]
self.saveState(state)
def playFromLastState(self, name, fromStart=False):
if name == self.getCurrent():
fromStart = True
elapsed = 0
if not fromStart:
elapsed = self.getElapsed(name)
self.mpdActor.playByNameFrom(name, elapsed)
def playLast(self, relativeElapsed=0):
name = self.getCurrent()
if name != None:
elapsed = self.getElapsed(name) + relativeElapsed
if elapsed < 0:
elapsed = 0
self.mpdActor.playByNameFrom(name, elapsed)
def getCurrent(self):
state = self.loadState()
try:
return state["current"]["name"]
except KeyError:
logging.getLogger('zbap').info('No info of currently played file. Probably a fresh start.')
return None
def getElapsed(self, name):
state = self.loadState()
try:
return state["played"][name]
except KeyError:
logging.getLogger('zbap').info('No info of elapsed seconds of file %s. Playing from start.' % name)
return 0
def loadState(self):
try:
with open(STATE_FILE, 'r') as stateFile:
return json.load(stateFile)
except (IOError, ValueError) as e:
return {}
def saveState(self, state):
with open(STATE_FILE, 'w') as stateFile:
json.dump(state, stateFile)