28
28
__author__ = "G-Anime"
29
29
__license__ = "Eiffel Version 2"
31
31
__contributors__= "Mathieu Charron, Martin Samson"
33
33
import os,sys,datetime,json,logging,time
35
# Instanciate the logging
36
module_logger = logging.getLogger("hyberia.playlist")
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")
50
46
if videoInfoBackend == None:
51
print ("CRITICAL: HVIB not set")
47
self.logger.critical("HVIB not set")
53
48
raise PlayListVIBNotSetException()
55
50
#Look for HVIB_API_VERSION attr
57
52
getattr(videoInfoBackend, "HVIB_API_VERSION")
58
53
except AttributeError:
59
print ("CRITICAL: Non-HVIB compliant videoInfoBackend.")
54
self.logger.critical("Non-HVIB compliant videoInfoBackend.")
61
55
raise PlayListVIBNotSetException()
63
57
#Verify it matches required version
64
58
if not videoInfoBackend.HVIB_API_VERSION >= 1:
65
print ("CRITICAL: Incompatible HVIB")
59
self.logger.critical("CRITICAL: Incompatible HVIB")
67
60
raise PlayListVIBNotSetException()
69
62
self.__videoInfoBackend = videoInfoBackend
98
91
def load(self, playListFile):
99
92
'''Load the playlist file into memory'''
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,))
105
95
#Verify the playlist file exists
106
96
if not os.path.exists(playListFile):
112
102
playListStruct = json.load(open(playListFile, "r"))
113
103
except Exception as 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()
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()
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()
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()
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()
141
129
for dateBlock in playListStruct["blocks"]:
143
131
prev_block_id = 0
146
134
blockStruct = playListStruct["blocks"][dateBlock][timeBlock]
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))
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()
164
152
for part in blockStruct['parts']:
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()
170
158
resource = playListStruct['resources'][part]
175
163
block['totalRunTime'] += part['duration']
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()
181
169
self._playList.append(blockId)
182
170
self._blocks[blockId] = block
184
print ("INFO: Loaded " + str(len(self._playList)) + " blocks.")
172
self.logger.debug(" Loaded %s blocks." % len(self._playList))
185
173
self._playList.sort()
187
175
def getCurrentBlock(self):
197
185
curBlock = blockId
198
186
return self._blocks[curBlock]
201
if __name__ == "__main__":
202
print "##### DEBUG ######"
204
m = mkvutils.MkvUtils()
206
p.load('../cfg/playlist.json')
207
print "get", p.getCurrentBlock()