You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
5.4 KiB
Python
183 lines
5.4 KiB
Python
import click
|
|
|
|
from ffx.model.track import Track
|
|
|
|
from .track_type import TrackType
|
|
|
|
from .track_disposition import TrackDisposition
|
|
from .iso_language import IsoLanguage
|
|
|
|
from .track_type import TrackType
|
|
|
|
|
|
class TrackController():
|
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
self.Session = self.context['database_session'] # convenience
|
|
|
|
|
|
def addTrack(self, trackDescriptor):
|
|
|
|
try:
|
|
s = self.Session()
|
|
|
|
track = Track(pattern_id = int(trackDescriptor['pattern_id']),
|
|
|
|
track_type = int(trackDescriptor['type'].value),
|
|
|
|
sub_index = int(trackDescriptor['sub_index']),
|
|
|
|
language = str(trackDescriptor['language'].threeLetter()),
|
|
|
|
title = str(trackDescriptor['title']),
|
|
|
|
disposition_flags = int(TrackDisposition.toFlags(trackDescriptor['disposition_list'])))
|
|
|
|
s.add(track)
|
|
s.commit()
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"TrackController.addTrack(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def updateTrack(self, trackId, trackDescriptor):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Track).filter(Track.id == int(trackId))
|
|
|
|
if q.count():
|
|
|
|
track = q.first()
|
|
|
|
track.sub_index = int(trackDescriptor['sub_index'])
|
|
track.language = str(trackDescriptor['language'].threeLetter())
|
|
track.title = str(trackDescriptor['title'])
|
|
track.disposition_flags = int(TrackDisposition.toFlags(trackDescriptor['disposition_list']))
|
|
|
|
s.commit()
|
|
|
|
return True
|
|
|
|
else:
|
|
return False
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"TrackController.addTrack(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def findAllTracks(self, patternId):
|
|
|
|
try:
|
|
s = self.Session()
|
|
|
|
trackDescriptors = {}
|
|
trackDescriptors[TrackType.AUDIO.label()] = []
|
|
trackDescriptors[TrackType.SUBTITLE.label()] = []
|
|
|
|
q_audio = s.query(Track).filter(Track.pattern_id == int(patternId), Track.track_type == TrackType.AUDIO.value)
|
|
|
|
for audioTrack in q_audio.all():
|
|
trackDescriptors[TrackType.AUDIO.label()].append(audioTrack.id)
|
|
|
|
q_subtitle = s.query(Track).filter(Track.pattern_id == int(patternId), Track.track_type == TrackType.SUBTITLE.value)
|
|
for subtitleTrack in q_subtitle.all():
|
|
trackDescriptors[TrackType.SUBTITLE.label()].append(subtitleTrack.id)
|
|
|
|
|
|
return trackDescriptors
|
|
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"TrackController.findAllTracks(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def findTrack(self, patternId, trackType : TrackType, subIndex):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Track).filter(Track.pattern_id == int(patternId), Track.track_type == trackType.value, Track.sub_index == int(subIndex))
|
|
|
|
if q.count():
|
|
track = q.first()
|
|
return int(track.id)
|
|
else:
|
|
return None
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"TrackController.findTrack(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def getTrackDict(self, track):
|
|
trackDescriptor = {}
|
|
trackDescriptor['id'] = int(track.id)
|
|
trackDescriptor['pattern_id'] = int(track.pattern_id)
|
|
trackDescriptor['type'] = TrackType(track.track_type)
|
|
trackDescriptor['sub_index'] = int(track.sub_index)
|
|
trackDescriptor['language'] = IsoLanguage.findThreeLetter(track.language)
|
|
trackDescriptor['title'] = str(track.title)
|
|
trackDescriptor['disposition_list'] = TrackDisposition.toList(track.disposition_flags)
|
|
return trackDescriptor
|
|
|
|
|
|
def getTrackDescriptor(self, trackId):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Track).filter(Track.id == int(trackId))
|
|
|
|
if q.count():
|
|
track = q.first()
|
|
return self.getTrackDict(track)
|
|
else:
|
|
return {}
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"TrackController.getTrackDescriptor(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def deleteTrack(self, trackId):
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Track).filter(Track.id == int(trackId))
|
|
|
|
if q.count():
|
|
|
|
trackDescriptor = self.getTrackDict(q.first())
|
|
|
|
|
|
q_siblings = s.query(Track).filter(Track.pattern_id == int(trackDescriptor['pattern_id']), Track.track_type == trackDescriptor['type'].value).order_by(Track.sub_index)
|
|
|
|
subIndex = 0
|
|
for track in q_siblings.all():
|
|
|
|
if track.sub_index == trackDescriptor['sub_index']:
|
|
s.delete(track)
|
|
else:
|
|
track.sub_index = subIndex
|
|
subIndex += 1
|
|
|
|
s.commit()
|
|
return True
|
|
|
|
|
|
return False
|
|
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"TrackController.deleteTrack(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|