nightl
parent
eaee3b34da
commit
1c9f67e47a
@ -0,0 +1,41 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from ffx.model.show import Base, Show
|
||||||
|
from ffx.model.pattern import Pattern
|
||||||
|
from ffx.model.track import Track
|
||||||
|
|
||||||
|
from ffx.model.media_tag import MediaTag
|
||||||
|
from ffx.model.track_tag import TrackTag
|
||||||
|
|
||||||
|
|
||||||
|
def databaseContext():
|
||||||
|
|
||||||
|
databaseContext = {}
|
||||||
|
|
||||||
|
# Initialize DB
|
||||||
|
homeDir = os.path.expanduser("~")
|
||||||
|
ffxVarDir = os.path.join(homeDir, '.local', 'var', 'ffx')
|
||||||
|
if not os.path.exists(ffxVarDir):
|
||||||
|
os.makedirs(ffxVarDir)
|
||||||
|
|
||||||
|
databaseContext['url'] = f"sqlite:///{os.path.join(ffxVarDir, 'ffx.db')}"
|
||||||
|
databaseContext['engine'] = create_engine(databaseContext['url'])
|
||||||
|
databaseContext['session'] = sessionmaker(bind=databaseContext['engine'])
|
||||||
|
|
||||||
|
Base.metadata.create_all(databaseContext['engine'])
|
||||||
|
|
||||||
|
# isSyncronuous = False
|
||||||
|
# while not isSyncronuous:
|
||||||
|
# while True:
|
||||||
|
# try:
|
||||||
|
# with databaseContext['database_engine'].connect() as connection:
|
||||||
|
# connection.execute(sqlalchemy.text('PRAGMA foreign_keys=ON;'))
|
||||||
|
# #isSyncronuous = True
|
||||||
|
# break
|
||||||
|
# except sqlite3.OperationalError:
|
||||||
|
# time.sleep(0.1)
|
||||||
|
|
||||||
|
return databaseContext
|
@ -0,0 +1,193 @@
|
|||||||
|
import click
|
||||||
|
|
||||||
|
from ffx.model.track import Track
|
||||||
|
|
||||||
|
from .track_type import TrackType
|
||||||
|
|
||||||
|
from .track_disposition import TrackDisposition
|
||||||
|
from .iso_language import IsoLanguage
|
||||||
|
|
||||||
|
from ffx.model.media_tag import MediaTag
|
||||||
|
from ffx.model.track_tag import TrackTag
|
||||||
|
|
||||||
|
|
||||||
|
class TagController():
|
||||||
|
|
||||||
|
def __init__(self, context):
|
||||||
|
|
||||||
|
self.context = context
|
||||||
|
self.Session = self.context['database']['session'] # convenience
|
||||||
|
|
||||||
|
|
||||||
|
def addMediaTag(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 addTrackTag(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.index())
|
||||||
|
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.index())
|
||||||
|
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 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)
|
||||||
|
return track.getDescriptor()
|
||||||
|
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()
|
Loading…
Reference in New Issue