code quality

This commit is contained in:
bkfox
2023-03-13 17:47:00 +01:00
parent 934817da8a
commit 112770eddf
162 changed files with 4798 additions and 4069 deletions

View File

@ -1,7 +1,7 @@
#! /usr/bin/env python3
"""
Monitor sound files; For each program, check for:
"""Monitor sound files; For each program, check for:
- new files;
- deleted files;
- differences between files and sound;
@ -23,23 +23,22 @@ To check quality of files, call the command sound_quality_check using the
parameters given by the setting AIRCOX_SOUND_QUALITY. This script requires
Sox (and soxi).
"""
from argparse import RawTextHelpFormatter
import concurrent.futures as futures
import atexit
import concurrent.futures as futures
import logging
import os
import time
from watchdog.observers import Observer
from argparse import RawTextHelpFormatter
from django.core.management.base import BaseCommand
from watchdog.observers import Observer
from aircox import settings
from aircox.models import Program, Sound
from aircox.management.sound_file import SoundFile
from aircox.management.sound_monitor import MonitorHandler
from aircox.models import Program, Sound
logger = logging.getLogger('aircox.commands')
logger = logging.getLogger("aircox.commands")
class Command(BaseCommand):
@ -47,39 +46,42 @@ class Command(BaseCommand):
def report(self, program=None, component=None, *content):
if not component:
logger.info('%s: %s', str(program),
' '.join([str(c) for c in content]))
logger.info(
"%s: %s", str(program), " ".join([str(c) for c in content])
)
else:
logger.info('%s, %s: %s', str(program), str(component),
' '.join([str(c) for c in content]))
logger.info(
"%s, %s: %s",
str(program),
str(component),
" ".join([str(c) for c in content]),
)
def scan(self):
"""
For all programs, scan dirs
"""
logger.info('scan all programs...')
"""For all programs, scan dirs."""
logger.info("scan all programs...")
programs = Program.objects.filter()
dirs = []
for program in programs:
logger.info('#%d %s', program.id, program.title)
logger.info("#%d %s", program.id, program.title)
self.scan_for_program(
program, settings.AIRCOX_SOUND_ARCHIVES_SUBDIR,
program,
settings.AIRCOX_SOUND_ARCHIVES_SUBDIR,
type=Sound.TYPE_ARCHIVE,
)
self.scan_for_program(
program, settings.AIRCOX_SOUND_EXCERPTS_SUBDIR,
program,
settings.AIRCOX_SOUND_EXCERPTS_SUBDIR,
type=Sound.TYPE_EXCERPT,
)
dirs.append(os.path.join(program.abspath))
return dirs
def scan_for_program(self, program, subdir, **sound_kwargs):
"""
Scan a given directory that is associated to the given program, and
update sounds information.
"""
logger.info('- %s/', subdir)
"""Scan a given directory that is associated to the given program, and
update sounds information."""
logger.info("- %s/", subdir)
if not program.ensure_dir(subdir):
return
@ -97,37 +99,49 @@ class Command(BaseCommand):
sounds.append(sound_file.sound.pk)
# sounds in db & unchecked
sounds = Sound.objects.filter(file__startswith=subdir). \
exclude(pk__in=sounds)
sounds = Sound.objects.filter(file__startswith=subdir).exclude(
pk__in=sounds
)
self.check_sounds(sounds, program=program)
def check_sounds(self, qs, **sync_kwargs):
""" Only check for the sound existence or update """
"""Only check for the sound existence or update."""
# check files
for sound in qs:
if sound.check_on_file():
SoundFile(sound.file.path).sync(sound=sound, **sync_kwargs)
def monitor(self):
""" Run in monitor mode """
"""Run in monitor mode."""
with futures.ThreadPoolExecutor() as pool:
archives_handler = MonitorHandler(
settings.AIRCOX_SOUND_ARCHIVES_SUBDIR, pool,
type=Sound.TYPE_ARCHIVE)
settings.AIRCOX_SOUND_ARCHIVES_SUBDIR,
pool,
type=Sound.TYPE_ARCHIVE,
)
excerpts_handler = MonitorHandler(
settings.AIRCOX_SOUND_EXCERPTS_SUBDIR, pool,
type=Sound.TYPE_EXCERPT)
settings.AIRCOX_SOUND_EXCERPTS_SUBDIR,
pool,
type=Sound.TYPE_EXCERPT,
)
observer = Observer()
observer.schedule(archives_handler, settings.AIRCOX_PROGRAMS_DIR_ABS,
recursive=True)
observer.schedule(excerpts_handler, settings.AIRCOX_PROGRAMS_DIR_ABS,
recursive=True)
observer.schedule(
archives_handler,
settings.AIRCOX_PROGRAMS_DIR_ABS,
recursive=True,
)
observer.schedule(
excerpts_handler,
settings.AIRCOX_PROGRAMS_DIR_ABS,
recursive=True,
)
observer.start()
def leave():
observer.stop()
observer.join()
atexit.register(leave)
while True:
@ -136,25 +150,31 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.formatter_class = RawTextHelpFormatter
parser.add_argument(
'-q', '--quality_check', action='store_true',
help='Enable quality check using sound_quality_check on all '
'sounds marqued as not good'
"-q",
"--quality_check",
action="store_true",
help="Enable quality check using sound_quality_check on all "
"sounds marqued as not good",
)
parser.add_argument(
'-s', '--scan', action='store_true',
help='Scan programs directories for changes, plus check for a '
' matching diffusion on sounds that have not been yet assigned'
"-s",
"--scan",
action="store_true",
help="Scan programs directories for changes, plus check for a "
" matching diffusion on sounds that have not been yet assigned",
)
parser.add_argument(
'-m', '--monitor', action='store_true',
help='Run in monitor mode, watch for modification in the filesystem '
'and react in consequence'
"-m",
"--monitor",
action="store_true",
help="Run in monitor mode, watch for modification in the "
"filesystem and react in consequence",
)
def handle(self, *args, **options):
if options.get('scan'):
if options.get("scan"):
self.scan()
#if options.get('quality_check'):
# if options.get('quality_check'):
# self.check_quality(check=(not options.get('scan')))
if options.get('monitor'):
if options.get("monitor"):
self.monitor()