Skip to content

Commit

Permalink
[InfoBarGenerics] Make ResumePoints into a class
Browse files Browse the repository at this point in the history
  • Loading branch information
Huevos committed Oct 31, 2024
1 parent cef0000 commit a423761
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 100 deletions.
13 changes: 4 additions & 9 deletions lib/python/Components/MovieList.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def getInfoString(self, serviceref, w):


def lastPlayPosFromCache(ref):
from Screens.InfoBarGenerics import resumePointCache
return resumePointCache.get(ref.toString(), None)
from Screens.InfoBarGenerics import resumePointsInstance
return resumePointsInstance.resumePointCache.get(ref.toString(), None)


def moviePlayState(cutsFileName, ref, length):
Expand Down Expand Up @@ -161,8 +161,8 @@ def moviePlayState(cutsFileName, ref, length):
def resetMoviePlayState(cutsFileName, ref=None):
try:
if ref is not None:
from Screens.InfoBarGenerics import delResumePoint
delResumePoint(ref)
from Screens.InfoBarGenerics import resumePointsInstance
resumePointsInstance.delResumePoint(ref)
f = open(cutsFileName, 'rb')
cutlist = []
while True:
Expand Down Expand Up @@ -266,7 +266,6 @@ def __init__(self, root, sort_type=None, descr_state=None, allowCollections=Fals
self.iconTrash = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "icons/trashcan.png"))
self.runningTimers = {}
self.updateRecordings()
self.updatePlayPosCache()

def get_playInBackground(self):
return self._playInBackground
Expand Down Expand Up @@ -298,10 +297,6 @@ def set_playInForeground(self, value):

playInForeground = property(get_playInForeground, set_playInForeground)

def updatePlayPosCache(self):
from Screens.InfoBarGenerics import updateresumePointCache
updateresumePointCache()

def updateRecordings(self, timer=None):
if timer is not None:
if timer.justplay:
Expand Down
4 changes: 2 additions & 2 deletions lib/python/Screens/EpgSelectionBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ def zap(self):
currentService = self.session.nav.getCurrentlyPlayingServiceOrGroup()
if currentService and currentService.isPlayback():
# in movie playback, so store the resume point before zapping
from Screens.InfoBarGenerics import setResumePoint
setResumePoint(self.session)
from Screens.InfoBarGenerics import resumePointsInstance
resumePointsInstance.setResumePoint(self.session)
self.zapSelectedService(True)
self.refreshTimer.start(1)
if not self.currch or self.currch == self.prevch:
Expand Down
10 changes: 5 additions & 5 deletions lib/python/Screens/InfoBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
InfoBarServiceNotifications, InfoBarPVRState, InfoBarCueSheetSupport, InfoBarBuffer, \
InfoBarSummarySupport, InfoBarMoviePlayerSummarySupport, InfoBarTimeshiftState, InfoBarTeletextPlugin, InfoBarExtensions, \
InfoBarSubtitleSupport, InfoBarPiP, InfoBarPlugins, InfoBarServiceErrorPopupSupport, InfoBarJobman, InfoBarZoom, \
InfoBarHdmi, setResumePoint, delResumePoint # noqa: E402
InfoBarHdmi, resumePointsInstance # noqa: E402
from Screens.ButtonSetup import InfoBarButtonSetup # noqa: E402

profile("LOAD:InitBar_Components")
Expand Down Expand Up @@ -253,7 +253,7 @@ def ensureClosed(nextService=None):
# Only try to set a resumepoint if currently playing something
ref = player.session.nav.getCurrentlyPlayingServiceOrGroup()
if ref is not None and ref.isPlayback():
setResumePoint(player.session)
resumePointsInstance.setResumePoint(player.session)
if nextService:
player.lastservice = nextService
player.close()
Expand Down Expand Up @@ -354,7 +354,7 @@ def handleLeave(self, how):
self.leavePlayerConfirmed([True, how])

def leavePlayer(self):
setResumePoint(self.session)
resumePointsInstance.setResumePoint(self.session)
self.handleLeave(config.usage.on_movie_stop.value)

def leavePlayerOnExit(self):
Expand All @@ -374,7 +374,7 @@ def leavePlayerOnExit(self):

def leavePlayerOnExitCallback(self, answer):
if answer:
setResumePoint(self.session)
resumePointsInstance.setResumePoint(self.session)
self.handleLeave("quit")

def hidePipOnExitCallback(self, answer):
Expand Down Expand Up @@ -474,7 +474,7 @@ def doEofInternal(self, playing):
return
ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
if ref:
delResumePoint(ref)
resumePointsInstance.delResumePoint(ref)
self.handleLeave(config.usage.on_movie_eof.value)

def up(self):
Expand Down
151 changes: 82 additions & 69 deletions lib/python/Screens/InfoBarGenerics.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,84 +81,97 @@ def isMoviePlayerInfoBar(self):
return self.__class__.__name__ == "MoviePlayer"


def setResumePoint(session):
global resumePointCache, resumePointCacheLast
service = session.nav.getCurrentService()
ref = session.nav.getCurrentlyPlayingServiceOrGroup()
if (service is not None) and (ref is not None): # and (ref.type != 1):
# ref type 1 has its own memory...
seek = service.seek()
if seek:
pos = seek.getPlayPosition()
if not pos[0]:
key = ref.toString()
lru = int(time())
sl = seek.getLength()
if sl:
sl = sl[1]
else:
sl = None
resumePointCache[key] = [lru, pos[1], sl]
for k, v in list(resumePointCache.items()):
if v[0] < lru:
candidate = k
filepath = ospath.realpath(candidate.split(':')[-1])
mountpoint = findMountPoint(filepath)
if ospath.ismount(mountpoint) and not ospath.exists(filepath):
del resumePointCache[candidate]
saveResumePoints()


def delResumePoint(ref):
global resumePointCache, resumePointCacheLast
try:
del resumePointCache[ref.toString()]
except KeyError:
pass
saveResumePoints()


def getResumePoint(session):
global resumePointCache
ref = session.nav.getCurrentlyPlayingServiceOrGroup()
if (ref is not None) and (ref.type != 1):
try:
entry = resumePointCache[ref.toString()]
class ResumePoints():
def __init__(self):
self.resumePointFile = "/etc/enigma2/resumepoints.pkl"
self.resumePointCache = {}
self.loadResumePoints()
self.cacheCleanTimer = eTimer()
self.cacheCleanTimer.callback.append(self.cleanCache)
self.cleanCache() # get rid of stale entries on reboot

def loadResumePoints(self):
self.resumePointCache.clear()
if fileExists(self.resumePointFile):
with open(self.resumePointFile, "rb") as f:
self.resumePointCache.update(pickle_load(f))

def saveResumePoints(self):
with open(self.resumePointFile, "wb") as f:
pickle_dump(self.resumePointCache, f, pickle_HIGHEST_PROTOCOL)

def delResumePoint(self, ref):
if (sref := ref.toString()) in self.resumePointCache:
del self.resumePointCache[sref]
self.saveResumePoints()

def cleanCache(self):
changed = False
now = int(time())
self.cacheCleanTimer.stop()
for sref, v in list(self.resumePointCache.items()):
if "%3a//" in sref: # resume point is stream
if now > v[0] + 7 * 24 * 60 * 60: # keep stream resume points maximum one week
del self.resumePointCache[sref]
changed = True
else:
filepath = ospath.realpath(sref.split(':')[-1])
mountpoint = findMountPoint(filepath)
if ospath.ismount(mountpoint) and not ospath.exists(filepath):
del self.resumePointCache[sref]
changed = True
if changed:
self.saveResumePoints()
self.cacheCleanTimer.startLongTimer(24 * 60 * 60) # clean up daily

def setResumePoint(self, session):
service = session.nav.getCurrentService()
ref = session.nav.getCurrentlyPlayingServiceOrGroup()
if service is not None and ref is not None: # and (ref.type != 1):
# ref type 1 has its own memory...
seek = service.seek()
if seek:
pos = seek.getPlayPosition()
if not pos[0]:
sref = ref.toString()
sl = x[1] if (x := seek.getLength()) else None
self.resumePointCache[sref] = [int(time()), pos[1], sl]
self.saveResumePoints()

def getResumePoint(self, session):
ref = session.nav.getCurrentlyPlayingServiceOrGroup()
if (ref is not None) and (ref.type != 1) and (sref := ref.toString()) in self.resumePointCache:
entry = self.resumePointCache[sref]
entry[0] = int(time()) # update LRU timestamp
return entry[1]
except KeyError:
return None


def saveResumePoints():
global resumePointCache, resumePointCacheLast
try:
f = open('/etc/enigma2/resumepoints.pkl', 'wb')
pickle_dump(resumePointCache, f, pickle_HIGHEST_PROTOCOL)
f.close()
except Exception as ex:
print("[InfoBarGenerics] Failed to write resumepoints:%s" % ex)
resumePointCacheLast = int(time())
resumePointsInstance = ResumePoints()


def loadResumePoints():
try:
file = open('/etc/enigma2/resumepoints.pkl', 'rb')
PickleFile = pickle_load(file)
file.close()
return PickleFile
except Exception as ex:
print("[InfoBarGenerics] Failed to load resumepoints:%s" % ex)
return {}
# Start: temporary legacy resume point code
def setResumePoint(session): # legacy code
resumePointsInstance.setResumePoint(session)


def delResumePoint(ref): # legacy code
resumePointsInstance.delResumePoint(ref)


def getResumePoint(session): # legacy code
return resumePointsInstance.getResumePoint(session)


def saveResumePoints(): # legacy code
resumePointsInstance.saveResumePoints()

def updateresumePointCache():
global resumePointCache
resumePointCache = loadResumePoints()

def loadResumePoints(): # legacy code
resumePointsInstance.loadResumePoints()

resumePointCache = loadResumePoints()
resumePointCacheLast = int(time())
resumePointCache = resumePointsInstance.resumePointCache # legacy code
resumePointCacheLast = 0 # legacy code
# End: temporary legacy resume point code


class whitelist:
Expand Down Expand Up @@ -3995,7 +4008,7 @@ def __serviceStarted(self):
last = pts
break
else:
last = getResumePoint(self.session)
last = resumePointsInstance.getResumePoint(self.session)
if last is None:
return
# only resume if at least 10 seconds ahead, or <10 seconds before the end.
Expand Down
24 changes: 12 additions & 12 deletions lib/python/Screens/MovieSelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,8 +1255,8 @@ def startPreview(self):
from Screens.InfoBar import MoviePlayer
MoviePlayerInstance = MoviePlayer.instance
if MoviePlayerInstance is not None:
from Screens.InfoBarGenerics import setResumePoint
setResumePoint(MoviePlayer.instance.session)
from Screens.InfoBarGenerics import resumePointsInstance
resumePointsInstance.setResumePoint(MoviePlayer.instance.session)
self.session.nav.stopService()
if playInBackground != current:
# come back to play the new one
Expand All @@ -1267,8 +1267,8 @@ def startPreview(self):
from Screens.InfoBar import MoviePlayer
MoviePlayerInstance = MoviePlayer.instance
if MoviePlayerInstance is not None:
from Screens.InfoBarGenerics import setResumePoint
setResumePoint(MoviePlayer.instance.session)
from Screens.InfoBarGenerics import resumePointsInstance
resumePointsInstance.setResumePoint(MoviePlayer.instance.session)
self.session.nav.stopService()
if playInForeground != current:
self.callLater(self.preview)
Expand All @@ -1293,8 +1293,8 @@ def playbackStop(self):
from Screens.InfoBar import MoviePlayer
MoviePlayerInstance = MoviePlayer.instance
if MoviePlayerInstance is not None:
from Screens.InfoBarGenerics import setResumePoint
setResumePoint(MoviePlayer.instance.session)
from Screens.InfoBarGenerics import resumePointsInstance
resumePointsInstance.setResumePoint(MoviePlayer.instance.session)
self.session.nav.stopService()
if config.movielist.show_live_tv_in_movielist.value:
self.LivePlayTimer.start(100)
Expand All @@ -1304,8 +1304,8 @@ def playbackStop(self):
from Screens.InfoBar import MoviePlayer
MoviePlayerInstance = MoviePlayer.instance
if MoviePlayerInstance is not None:
from Screens.InfoBarGenerics import setResumePoint
setResumePoint(MoviePlayer.instance.session)
from Screens.InfoBarGenerics import resumePointsInstance
resumePointsInstance.setResumePoint(MoviePlayer.instance.session)
self.closeMoviePlayerOnExit = True
self.session.nav.stopService()
if config.movielist.show_live_tv_in_movielist.value:
Expand Down Expand Up @@ -2364,8 +2364,8 @@ def __deleteListConfirmed(self, delList, confirmed):
continue
try:
moveServiceFiles(itemRef, trash)
from Screens.InfoBarGenerics import delResumePoint
delResumePoint(itemRef)
from Screens.InfoBarGenerics import resumePointsInstance
resumePointsInstance.delResumePoint(itemRef)
deletedList.append(itemRef)
except Exception as ex:
print("[MovieSelection] Couldn't move to trash '%s'. %s" % (path, ex))
Expand Down Expand Up @@ -2408,8 +2408,8 @@ def __permanentDeleteListConfirmed(self, delList, confirmed):
else:
if offline.deleteFromDisk(0):
raise Exception("Offline delete failed")
from Screens.InfoBarGenerics import delResumePoint
delResumePoint(itemRef)
from Screens.InfoBarGenerics import resumePointsInstance
resumePointsInstance.delResumePoint(itemRef)
deletedList.append(itemRef)
except Exception as ex:
print("[MovieSelection] Couldn't delete '%s'. %s" % (path, ex))
Expand Down
3 changes: 0 additions & 3 deletions lib/python/StartEnigma.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,6 @@ def runNextScreen(session, screensToRun, *result):
session.nav.shutdown()
profile("configfile.save")
configfile.save()
if not VuRecovery:
from Screens import InfoBarGenerics
InfoBarGenerics.saveResumePoints()
return 0


Expand Down

0 comments on commit a423761

Please sign in to comment.