Minimise code duplication for isPlayable functions.
authorStephen R. van den Berg <srb@cuci.nl>
Thu, 31 Jan 2013 19:34:39 +0000 (20:34 +0100)
committerLittlesat <littlesat99@yahoo.com>
Fri, 1 Feb 2013 18:57:47 +0000 (19:57 +0100)
Pull together several similar routines in places sprinkled throughout
the code and centralise them in one place to minimise code duplication.

ServiceReference.py
lib/python/Components/Converter/ServiceName.py
lib/python/Components/Converter/ServiceOrbitalPosition.py
lib/python/Components/Sources/ServiceEvent.py
lib/python/Screens/InfoBarGenerics.py

index 926acc0..f83b7a7 100644 (file)
@@ -1,4 +1,6 @@
-from enigma import eServiceReference, eServiceCenter
+from enigma import eServiceReference, eServiceCenter, \
+ getBestPlayableServiceReference
+import NavigationInstance
 
 class ServiceReference(eServiceReference):
        def __init__(self, ref, reftype = eServiceReference.idInvalid, flags = 0, path = ''):
@@ -34,4 +36,27 @@ class ServiceReference(eServiceReference):
 
        def isRecordable(self):
                ref = self.ref
-               return ref.flags & eServiceReference.isGroup or (ref.type == eServiceReference.idDVB or ref.type == eServiceReference.idDVB + 0x100)
\ No newline at end of file
+               return ref.flags & eServiceReference.isGroup or (ref.type == eServiceReference.idDVB or ref.type == eServiceReference.idDVB + 0x100)
+
+def getPlayingref(ref):
+       playingref = None
+       if NavigationInstance.instance:
+               playingref = NavigationInstance.instance.\
+                       getCurrentlyPlayingServiceReference()
+       if not playingref:
+               playingref = eServiceReference()
+       return playingref
+
+def isPlayableForCur(ref):
+       info = eServiceCenter.getInstance().info(ref)
+       return not not (info and info.isPlayable(ref, getPlayingref(ref)))
+
+def resolveAlternate(ref):
+       nref = None
+       if ref.flags & eServiceReference.isGroup:
+               nref \
+                = getBestPlayableServiceReference(ref, getPlayingref(ref))
+               if not nref:
+                       nref = getBestPlayableServiceReference(ref,
+                        eServiceReference(), True)
+       return nref
index fa61134..4255750 100644 (file)
@@ -1,8 +1,8 @@
 # -*- coding: utf-8 -*-
 from Components.Converter.Converter import Converter
-from enigma import iServiceInformation, iPlayableService, iPlayableServicePtr,\
- eServiceReference, getBestPlayableServiceReference
+from enigma import iServiceInformation, iPlayableService, iPlayableServicePtr
 from Components.Element import cached
+from ServiceReference import resolveAlternate
 
 class ServiceName(Converter, object):
        NAME = 0
@@ -27,27 +27,23 @@ class ServiceName(Converter, object):
                else: # reference
                        info = service and self.source.info
                        ref = service
-               if info is None:
+               if not info:
                        return ""
                if self.type == self.NAME:
                        name = ref and info.getName(ref)
-                       if name is None:
+                       if not name:
                                name = info.getName()
                        return name.replace('\xc2\x86', '').replace('\xc2\x87', '')
                elif self.type == self.PROVIDER:
                        return info.getInfoString(iServiceInformation.sProvider)
                elif self.type == self.REFERENCE:
-                       if ref is None:
-                         return \
-                          info.getInfoString(iServiceInformation.sServiceref)
-                       if ref.flags & eServiceReference.isGroup:
-                         nref = getBestPlayableServiceReference(ref, \
-                          eServiceReference())
-                         if nref is None:
-                           nref = getBestPlayableServiceReference(ref, \
-                            eServiceReference(), True)
-                         if not (nref is None):
-                           ref = nref
+                       if not ref:
+                               return \
+                                info.getInfoString( \
+                                 iServiceInformation.sServiceref)
+                       nref = resolveAlternate(ref)
+                       if nref:
+                               ref = nref
                        return ref.toString()
 
        text = property(getText)
index 4e1504b..89e9796 100644 (file)
@@ -1,7 +1,8 @@
 # -*- coding: utf-8 -*-
 from Components.Converter.Converter import Converter
 from enigma import iServiceInformation, iPlayableService, iPlayableServicePtr,\
- eServiceReference, getBestPlayableServiceReference, eServiceCenter
+ eServiceCenter
+from ServiceReference import resolveAlternate
  
 from Components.Element import cached
 
@@ -25,18 +26,13 @@ class ServiceOrbitalPosition(Converter, object):
                else: # reference
                        info = service and self.source.info
                        ref = service
-               if info is None:
+               if not info:
                        return ""
                if ref:
-                       if ref.flags & eServiceReference.isGroup:
-                         nref = getBestPlayableServiceReference(ref, \
-                          eServiceReference())
-                         if nref is None:
-                           nref = getBestPlayableServiceReference(ref, \
-                            eServiceReference(), True)
-                         if not (nref is None):
-                           ref = nref
-                           info = eServiceCenter.getInstance().info(ref)
+                       nref = resolveAlternate(ref)
+                       if nref:
+                               ref = nref
+                               info = eServiceCenter.getInstance().info(ref)
                        transponder_info = info.getInfoObject(ref, iServiceInformation.sTransponderData)
                else:
                        transponder_info = info.getInfoObject(iServiceInformation.sTransponderData)
index 8a0a66a..06f4d46 100644 (file)
@@ -1,5 +1,5 @@
 from Components.Element import cached
-from enigma import eServiceCenter, eServiceReference as Ref
+from enigma import eServiceCenter
 from Source import Source
 
 class ServiceEvent(Source, object):
index 76a5522..d2548e2 100644 (file)
@@ -27,7 +27,7 @@ from Screens.SubtitleDisplay import SubtitleDisplay
 from Screens.RdsDisplay import RdsInfoDisplay, RassInteractive
 from Screens.TimeDateInput import TimeDateInput
 from Screens.UnhandledKey import UnhandledKey
-from ServiceReference import ServiceReference
+from ServiceReference import ServiceReference, isPlayableForCur
 
 from Tools import Notifications
 from Tools.Directories import fileExists
@@ -500,7 +500,7 @@ class InfoBarChannelSelection:
                                                        self.servicelist.prevBouquet()
                                        self.servicelist.moveUp()
                                        cur = self.servicelist.getCurrentSelection()
-                                       if cur and (cur.toString() == prev or self.isPlayable(cur)):
+                                       if cur and (cur.toString() == prev or isPlayableForCur(cur)):
                                                        break
                else:
                        self.servicelist.moveUp()
@@ -517,22 +517,12 @@ class InfoBarChannelSelection:
                                        else:
                                                self.servicelist.moveDown()
                                        cur = self.servicelist.getCurrentSelection()
-                                       if cur and (cur.toString() == prev or self.isPlayable(cur)):
+                                       if cur and (cur.toString() == prev or isPlayableForCur(cur)):
                                                        break
                else:
                        self.servicelist.moveDown()
                self.servicelist.zap(enable_pipzap = True)
 
-       def isPlayable(self, ref):
-               if not (ref.flags & eServiceReference.isMarker):
-                       cur_running = self.session.nav.getCurrentlyPlayingServiceReference()
-                       if not cur_running:
-                               cur_running = eServiceReference()
-                       info = eServiceCenter.getInstance().info(ref)
-                       if info and info.isPlayable(ref, cur_running):
-                               return True
-               return False
-
 class InfoBarMenu:
        """ Handles a menu action, to open the (main) menu """
        def __init__(self):