~registry/hyberia/trunk

« back to all changes in this revision

Viewing changes to hyberia/playlist.py

  • Committer: Mathieu Charron
  • Date: 2010-10-06 01:38:41 UTC
  • mfrom: (60.1.2)
  • Revision ID: git-v1:5867ee3184348c69075af3e2178643f9068d42fd
Beta of 0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
__author__ = "G-Anime"
29
29
__license__ = "Eiffel Version 2"
30
 
__version__ = "0.3.2"
 
30
__version__ = "0.4"
31
31
__contributors__= "Mathieu Charron, Martin Samson"
32
32
 
33
33
import os,sys,datetime,json,logging,time
34
34
 
35
 
# Instanciate the logging
36
 
module_logger = logging.getLogger("hyberia.playlist")
37
 
 
38
35
class PlayListNotFoundException(Exception): pass
39
36
class PlayListImportErrorException(Exception): pass
40
37
class PlayListFileNotFoundException(Exception): pass
44
41
    def __init__(self, videoInfoBackend = None):
45
42
        '''videoInfoBackend Hyberia VIB API compatible backend class instance'''
46
43
        # Instanciate the logger
47
 
        self.logger = logging.getLogger("hyberia.playlist.Playlist")
48
 
        self.logger.info("Creating instance")
 
44
        self.logger = logging.getLogger("hyberia.playlist")
49
45
 
50
46
        if videoInfoBackend == None:
51
 
            print ("CRITICAL: HVIB not set")
52
 
            print("")
 
47
            self.logger.critical("HVIB not set")
53
48
            raise PlayListVIBNotSetException()
54
49
 
55
50
        #Look for HVIB_API_VERSION attr
56
51
        try:
57
52
            getattr(videoInfoBackend, "HVIB_API_VERSION")
58
53
        except AttributeError:
59
 
            print ("CRITICAL: Non-HVIB compliant videoInfoBackend.")
60
 
            print("")
 
54
            self.logger.critical("Non-HVIB compliant videoInfoBackend.")
61
55
            raise PlayListVIBNotSetException()
62
56
 
63
57
        #Verify it matches required version
64
58
        if not videoInfoBackend.HVIB_API_VERSION >= 1:
65
 
            print ("CRITICAL: Incompatible HVIB")
66
 
            print("")
 
59
            self.logger.critical("CRITICAL: Incompatible HVIB")
67
60
            raise PlayListVIBNotSetException()
68
61
 
69
62
        self.__videoInfoBackend = videoInfoBackend
97
90
 
98
91
    def load(self, playListFile):
99
92
        '''Load the playlist file into memory'''
100
 
 
101
 
        # Get the playlist file
102
 
        playListFile = playListFile + "/playlist.json"
103
 
        self.logger.debug("Loading playlist from %s" % (playListFile,))
 
93
        self.logger.debug("Loading playlist from %s ." % (playListFile,))
104
94
 
105
95
        #Verify the playlist file exists
106
96
        if not os.path.exists(playListFile):
111
101
        try:
112
102
            playListStruct = json.load(open(playListFile, "r"))
113
103
        except Exception as e:
114
 
            print(e)
115
 
            print("This is a parsing error. Strings in json must be delimited with \" instead of ' ")
 
104
            self.logger.warning("This is a parsing error. Strings in json must be delimited with \" instead of ' .")
 
105
            self.logger.critical(e)
116
106
            raise PlayListImportErrorException()
117
107
 
118
108
        #Verify that some data exists
119
109
        for elem in ["blocks","resources"]:
120
110
            if not elem in playListStruct:
121
 
                print(elem + " not found")
 
111
                self.logger.warning("%s not found." % elem)
122
112
                raise PlayListImportErrorException()
123
113
 
124
114
 
125
115
        #Parse the resources
126
116
        for resource in playListStruct['resources']:
127
117
            if not 'file' in playListStruct['resources'][resource]:
128
 
                print("CRITICAL: Resource "+ str(resource) +" has no file attribute");
 
118
                self.logger.critical("Resource %s has no file attribute" % resource);
129
119
                raise PlayListImportErrorException()
130
120
 
131
121
            if not 'file' in playListStruct['resources'][resource]:
132
 
                print("CRITICAL: Resource "+ resource +" does not have a file attribute!")
 
122
                self.logger.critical("Resource %s does not have a file attribute!" % resource)
133
123
                raise PlayListImportErrorException()
134
124
 
135
125
            if not os.path.exists(playListStruct['resources'][resource]['file']):
136
 
                print("CRITICAL: Resource " + str(resource) + " file ("+ playListStruct['resources'][resource]['file'] +") does not exists")
 
126
                self.logger.critical("Resource %s file (%s) does not exists" % (resource,playListStruct['resources'][resource]['file']))
137
127
                raise PlayListImportErrorException()
138
 
 
139
 
 
140
 
 
 
128
                
141
129
        for dateBlock in playListStruct["blocks"]:
142
130
 
143
131
            prev_block_id = 0
146
134
                blockStruct = playListStruct["blocks"][dateBlock][timeBlock]
147
135
 
148
136
                if len(blockStruct['parts']) == 0:
149
 
                    print("WARNING: Skipping block " + str(timeBlock) + " on " + str(dateBlock) + ". No parts to play")
 
137
                    self.logger.warning("Skipping block %s on %s. No parts to play", (timeBlock , dateBlock))
150
138
                    continue
151
139
 
152
140
                for item in ['name','description', 'parts']:
153
141
                    if not item in blockStruct:
154
 
                        print ("CRITICAL: Block " + timeBlock +" on " + dateBlock +" does not have a " + item + "!")
 
142
                        self.logger.critical("Block %s on %s does not have a %s!" %s (timeBlock,dateBlock,item))
155
143
                        raise PlayListImportErrorException()
156
144
 
157
145
 
164
152
                for part in blockStruct['parts']:
165
153
 
166
154
                    if not part in playListStruct['resources']:
167
 
                        print("CRITICAL: Part " + str(part) + " has no resource file!")
 
155
                        self.logger.critical("Part %s has no resource file!" % part)
168
156
                        raise PlayListImportErrorException()
169
157
 
170
158
                    resource = playListStruct['resources'][part]
175
163
                    block['totalRunTime'] += part['duration']
176
164
 
177
165
                if prev_block_id > blockId:
178
 
                    print("Critical: Block duration overlapping at " + str(blockId))
 
166
                    self.logger.critical("Block duration overlapping at %s !" % blockId)
179
167
                    raise PlayListImportErrorException()
180
168
 
181
169
                self._playList.append(blockId)
182
170
                self._blocks[blockId] = block
183
171
 
184
 
        print ("INFO: Loaded " + str(len(self._playList)) + " blocks.")
 
172
        self.logger.debug(" Loaded %s blocks." % len(self._playList))
185
173
        self._playList.sort()
186
174
 
187
175
    def getCurrentBlock(self):
197
185
                curBlock = blockId
198
186
            return self._blocks[curBlock]
199
187
        return None
200
 
 
201
 
if __name__ == "__main__":
202
 
    print "##### DEBUG ######"
203
 
    import mkvutils
204
 
    m = mkvutils.MkvUtils()
205
 
    p = PlayList(m)
206
 
    p.load('../cfg/playlist.json')
207
 
    print "get", p.getCurrentBlock()