forked from rc/aircox
write sound monitor tests
This commit is contained in:
@ -24,7 +24,7 @@ parameters given by the setting SOUND_QUALITY. This script requires
|
||||
Sox (and soxi).
|
||||
"""
|
||||
import atexit
|
||||
import concurrent.futures as futures
|
||||
from concurrent import futures
|
||||
import logging
|
||||
import time
|
||||
import os
|
||||
@ -142,9 +142,9 @@ class MonitorHandler(PatternMatchingEventHandler):
|
||||
"""
|
||||
|
||||
pool = None
|
||||
jobs = {}
|
||||
jobs = None
|
||||
|
||||
def __init__(self, subdir, pool, **sync_kw):
|
||||
def __init__(self, subdir, pool, jobs=None, **sync_kw):
|
||||
"""
|
||||
:param str subdir: sub-directory in program dirs to monitor \
|
||||
(SOUND_ARCHIVES_SUBDIR or SOUND_EXCERPTS_SUBDIR);
|
||||
@ -154,6 +154,7 @@ class MonitorHandler(PatternMatchingEventHandler):
|
||||
"""
|
||||
self.subdir = subdir
|
||||
self.pool = pool
|
||||
self.jobs = jobs or {}
|
||||
self.sync_kw = sync_kw
|
||||
|
||||
patterns = [
|
||||
@ -199,29 +200,27 @@ class MonitorHandler(PatternMatchingEventHandler):
|
||||
|
||||
class SoundMonitor:
|
||||
"""Monitor for filesystem changes in order to synchronise database and
|
||||
analyse files."""
|
||||
analyse files of a provided program."""
|
||||
|
||||
def report(self, program=None, component=None, logger=logging, *content):
|
||||
if not component:
|
||||
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]),
|
||||
)
|
||||
def report(self, program=None, component=None, *content, logger=logging):
|
||||
content = " ".join([str(c) for c in content])
|
||||
logger.info(
|
||||
f"{program}: {content}"
|
||||
if not component
|
||||
else f"{program}, {component}: {content}"
|
||||
)
|
||||
|
||||
def scan(self, logger=logging):
|
||||
"""For all programs, scan dirs."""
|
||||
"""For all programs, scan dirs.
|
||||
|
||||
Return scanned directories.
|
||||
"""
|
||||
logger.info("scan all programs...")
|
||||
programs = Program.objects.filter()
|
||||
|
||||
dirs = []
|
||||
for program in programs:
|
||||
logger.info("#%d %s", program.id, program.title)
|
||||
logger.info(f"#{program.id} {program.title}")
|
||||
self.scan_for_program(
|
||||
program,
|
||||
settings.SOUND_ARCHIVES_SUBDIR,
|
||||
@ -234,7 +233,7 @@ class SoundMonitor:
|
||||
logger=logger,
|
||||
type=Sound.TYPE_EXCERPT,
|
||||
)
|
||||
dirs.append(os.path.join(program.abspath))
|
||||
dirs.append(program.abspath)
|
||||
return dirs
|
||||
|
||||
def scan_for_program(
|
||||
@ -272,7 +271,12 @@ class SoundMonitor:
|
||||
if sound.check_on_file():
|
||||
SoundFile(sound.file.path).sync(sound=sound, **sync_kwargs)
|
||||
|
||||
_running = False
|
||||
|
||||
def monitor(self, logger=logging):
|
||||
if self._running:
|
||||
raise RuntimeError("already running")
|
||||
|
||||
"""Run in monitor mode."""
|
||||
with futures.ThreadPoolExecutor() as pool:
|
||||
archives_handler = MonitorHandler(
|
||||
@ -307,5 +311,13 @@ class SoundMonitor:
|
||||
|
||||
atexit.register(leave)
|
||||
|
||||
while True:
|
||||
self._running = True
|
||||
while self._running:
|
||||
time.sleep(1)
|
||||
|
||||
leave()
|
||||
atexit.unregister(leave)
|
||||
|
||||
def stop(self):
|
||||
"""Stop monitor() loop."""
|
||||
self._running = False
|
||||
|
Reference in New Issue
Block a user