VisibilityHandler #3

Merged
erth9960 merged 4 commits from visibility into master 2024-03-27 14:54:33 +01:00
5 changed files with 65 additions and 38 deletions
Showing only changes of commit 540a649967 - Show all commits

View File

@ -1,7 +1,6 @@
import multiprocessing as mp import multiprocessing as mp
from .audio import AudioHandler from .audio import AudioHandler
from .handler import Handler
from .metadata import MetadataHandler from .metadata import MetadataHandler
from .poster import PosterHandler from .poster import PosterHandler
from .slides import SlidesHandler from .slides import SlidesHandler
@ -9,6 +8,7 @@ from .subtitles_whisper_hack import SubtitlesWhisperHandler
from .subtitles_import import SubtitlesImportHandler from .subtitles_import import SubtitlesImportHandler
from .thumbnail import ThumbnailHandler from .thumbnail import ThumbnailHandler
from .transcode import TranscodeHandler from .transcode import TranscodeHandler
from .visibility import VisibilityHandler
from ..ldap import Ldap from ..ldap import Ldap
from ..utils import get_section from ..utils import get_section
@ -20,6 +20,7 @@ allHandlers = [AudioHandler,
SubtitlesWhisperHandler, SubtitlesWhisperHandler,
ThumbnailHandler, ThumbnailHandler,
TranscodeHandler, TranscodeHandler,
VisibilityHandler,
] ]
def init_handlers(collector, worker, config): def init_handlers(collector, worker, config):

View File

@ -1,32 +0,0 @@
from .handler import Handler
from ..exceptions import ValidationException
@Handler.register
class VisibilityHandler(Handler):
"""
This class handles visibility settings for streams.
"""
@classmethod
def wants(cls, jobspec, existing_package):
"""
Return True if this handler wants to process this jobspec.
Raises an exception if the job is wanted but doesn't pass validation.
In order for a job to be wanted, the field 'sources' must exist and
at least one of the source items must contain a 'enabled' field.
"""
pass
@classmethod
def _validate(cls, jobspec, existing_package):
"""
Return True if the job is valid for this handler.
A job is valid as long as at least one of the package's source items
"""
pass
def _handle(self, jobspec, existing_package, tempdir):
pass

View File

@ -1,7 +1,6 @@
import logging import logging
from os import path, remove, rename from os import path, remove, rename
from shutil import rmtree
import ffmpeg import ffmpeg
@ -169,7 +168,7 @@ class TranscodeHandler(Handler):
""" """
super()._validate(jobspec, existing_package) super()._validate(jobspec, existing_package)
if 'upload_dir' not in jobspec: if 'upload_dir' not in jobspec:
raise ValidationException(f"upload_dir missing") raise ValidationException("upload_dir missing")
for name, source in jobspec['sources'].items(): for name, source in jobspec['sources'].items():
if not path.isfile(path.join(jobspec['upload_dir'], if not path.isfile(path.join(jobspec['upload_dir'],
source['video'])): source['video'])):
@ -228,8 +227,10 @@ class TranscodeHandler(Handler):
else: else:
# Initialize a new source # Initialize a new source
# set playAudio to False by default so we don't conflict # set playAudio to False by default so we don't conflict
# with the audio handler # with the audio handler, set enabled to True so we don't
sources[name] = {'playAudio': False} # conflict with the visibility handler
sources[name] = {'playAudio': False,
'enabled': True}
# Save new source videos # Save new source videos
sources[name]['video'] = variants sources[name]['video'] = variants

View File

@ -0,0 +1,57 @@
import copy
from pathlib import Path
from .handler import Handler
from ..exceptions import ValidationException
from ..package import Package
@Handler.register
class VisibilityHandler(Handler):
"""
This class handles visibility settings for streams.
"""
@classmethod
def wants(cls, jobspec: dict, existing_package: Package) -> bool | ValidationException:
"""
Return True if this handler wants to process this jobspec.
Raises an exception if the job is wanted but doesn't pass validation.
In order for a job to be wanted, the field 'sources' must exist and
at least one of the source items must contain a 'enabled' field.
"""
if 'sources' in jobspec and any('enabled' in source for source in jobspec['sources'].values()):
return cls._validate(jobspec, existing_package)
return False
@classmethod
def _validate(cls, jobspec: dict, existing_package: Package) -> bool | ValidationException:
"""
Return True if the job is valid for this handler.
A job is valid as long as at least one of the package's source items
has its 'enabled' field set to True.
"""
sources = copy.deepcopy(existing_package.get('sources', {}))
for name, source in jobspec['sources'].items():
if name not in sources:
sources[name] = {}
if 'enabled' in source:
sources[name]['enabled'] = source['enabled']
for source in sources.values():
if 'enabled' in source:
return True
raise ValidationException("No enabled sources")
def _handle(self, jobspec: dict, existing_package: Package, tempdir: Path) -> callable:
"""
"""
def apply_func(package: Package) -> None:
sources = package.get('sources', {})
for name, source in jobspec['sources'].items():
if name not in sources:
sources[name] = {}
if 'enabled' in source:
sources[name]['enabled'] = source['enabled']
return apply_func

View File

@ -1,4 +1,3 @@
from os import chdir, path
from types import FunctionType from types import FunctionType
@ -23,6 +22,7 @@ canonical_jobspec = {
'source': str}}, 'source': str}},
'sources': {str: {'poster': str, 'sources': {str: {'poster': str,
'playAudio': bool, 'playAudio': bool,
'enabled': bool,
'video': str}}, 'video': str}},
'slides': str, 'slides': str,
'notification_url': str, 'notification_url': str,