Feat: packaging (#127)

- Add configuration files for packaging
- Precommit now uses ruff

Co-authored-by: bkfox <thomas bkfox net>
Reviewed-on: rc/aircox#127
This commit is contained in:
Thomas Kairos
2023-10-11 10:58:34 +02:00
parent 5ea092dba6
commit f7a61fe6c0
82 changed files with 332 additions and 935 deletions

View File

@ -22,9 +22,7 @@ class DiffusionMonitor:
def update(self):
episodes, diffusions = [], []
for schedule in Schedule.objects.filter(
program__active=True, initial__isnull=True
):
for schedule in Schedule.objects.filter(program__active=True, initial__isnull=True):
eps, diffs = schedule.diffusions_of_month(self.date)
if eps:
episodes += eps

View File

@ -44,9 +44,7 @@ class LogArchiver:
path = self.get_path(station, date)
# FIXME: remove binary mode
with gzip.open(path, "ab") as archive:
data = yaml.dump(
[self.serialize(line) for line in logs]
).encode("utf8")
data = yaml.dump([self.serialize(line) for line in logs]).encode("utf8")
archive.write(data)
if not keep:
@ -95,10 +93,7 @@ class LogArchiver:
return [
Log(
diffusion=rel_obj(log, "diffusion"),
sound=rel_obj(log, "sound"),
track=rel_obj(log, "track"),
**log
diffusion=rel_obj(log, "diffusion"), sound=rel_obj(log, "sound"), track=rel_obj(log, "track"), **log
)
for log in logs
]

View File

@ -50,14 +50,7 @@ class PlaylistImport:
logger.info("start reading csv " + self.path)
self.data = list(
csv.DictReader(
(
row
for row in file
if not (
row.startswith("#") or row.startswith("\ufeff#")
)
and row.strip()
),
(row for row in file if not (row.startswith("#") or row.startswith("\ufeff#")) and row.strip()),
fieldnames=settings.IMPORT_PLAYLIST_CSV_COLS,
delimiter=settings.IMPORT_PLAYLIST_CSV_DELIMITER,
quotechar=settings.IMPORT_PLAYLIST_CSV_TEXT_QUOTE,
@ -70,11 +63,7 @@ class PlaylistImport:
If save is true, save it into the database
"""
if self.track_kwargs.get("sound") is None:
logger.error(
"related track's sound is missing. Skip import of "
+ self.path
+ "."
)
logger.error("related track's sound is missing. Skip import of " + self.path + ".")
return
maps = settings.IMPORT_PLAYLIST_CSV_COLS
@ -87,17 +76,11 @@ class PlaylistImport:
return
try:
timestamp = (
int(line.get("minutes") or 0) * 60
+ int(line.get("seconds") or 0)
if has_timestamp
else None
int(line.get("minutes") or 0) * 60 + int(line.get("seconds") or 0) if has_timestamp else None
)
track, created = Track.objects.get_or_create(
title=line.get("title"),
artist=line.get("artist"),
position=index,
**self.track_kwargs
title=line.get("title"), artist=line.get("artist"), position=index, **self.track_kwargs
)
track.timestamp = timestamp
track.info = line.get("info")

View File

@ -58,14 +58,7 @@ class SoundFile:
def episode(self):
return self.sound and self.sound.episode
def sync(
self,
sound=None,
program=None,
deleted=False,
keep_deleted=False,
**kwargs
):
def sync(self, sound=None, program=None, deleted=False, keep_deleted=False, **kwargs):
"""Update related sound model and save it."""
if deleted:
return self._on_delete(self.path, keep_deleted)
@ -79,9 +72,7 @@ class SoundFile:
if sound:
created = False
else:
sound, created = Sound.objects.get_or_create(
file=self.sound_path, defaults=kwargs
)
sound, created = Sound.objects.get_or_create(file=self.sound_path, defaults=kwargs)
self.sound = sound
self.path_info = self.read_path(self.path)
@ -172,9 +163,7 @@ class SoundFile:
year, month, day = pi.get("year"), pi.get("month"), pi.get("day")
if pi.get("hour") is not None:
at = tz.datetime(
year, month, day, pi.get("hour", 0), pi.get("minute", 0)
)
at = tz.datetime(year, month, day, pi.get("hour", 0), pi.get("minute", 0))
at = tz.make_aware(at)
else:
at = date(year, month, day)
@ -210,22 +199,10 @@ class SoundFile:
if self.info and self.info.tags:
tags = self.info.tags
title, artist, album, year = tuple(
t and ", ".join(t)
for t in (
tags.get(k)
for k in ("title", "artist", "album", "year")
)
)
title = (
title
or (self.path_info and self.path_info.get("name"))
or os.path.basename(path_noext)
)
info = (
"{} ({})".format(album, year)
if album and year
else album or year or ""
t and ", ".join(t) for t in (tags.get(k) for k in ("title", "artist", "album", "year"))
)
title = title or (self.path_info and self.path_info.get("name")) or os.path.basename(path_noext)
info = "{} ({})".format(album, year) if album and year else album or year or ""
track = Track(
sound=sound,
position=int(tags.get("tracknumber", 0)),

View File

@ -155,10 +155,7 @@ class MonitorHandler(PatternMatchingEventHandler):
self.jobs = jobs or {}
self.sync_kw = sync_kw
patterns = [
"*/{}/*{}".format(self.subdir, ext)
for ext in settings.SOUND_FILE_EXT
]
patterns = ["*/{}/*{}".format(self.subdir, ext) for ext in settings.SOUND_FILE_EXT]
super().__init__(patterns=patterns, ignore_directories=True)
def on_created(self, event):
@ -202,11 +199,7 @@ class SoundMonitor:
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}"
)
logger.info(f"{program}: {content}" if not component else f"{program}, {component}: {content}")
def scan(self, logger=logging):
"""For all programs, scan dirs.
@ -234,9 +227,7 @@ class SoundMonitor:
dirs.append(program.abspath)
return dirs
def scan_for_program(
self, program, subdir, logger=logging, **sound_kwargs
):
def scan_for_program(self, program, subdir, logger=logging, **sound_kwargs):
"""Scan a given directory that is associated to the given program, and
update sounds information."""
logger.info("- %s/", subdir)
@ -257,9 +248,7 @@ class SoundMonitor:
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):

View File

@ -38,9 +38,7 @@ class SoxStats:
args += ["trim", str(at), str(length)]
args.append("stats")
p = subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# sox outputs to stderr (my god WHYYYY)
out_, out = p.communicate()
self.values = self.parse(str(out, encoding="utf-8"))
@ -94,16 +92,8 @@ class SoundStats:
position += self.sample_length
def check(self, name, min_val, max_val):
self.good = [
index
for index, stats in enumerate(self.stats)
if min_val <= stats.get(name) <= max_val
]
self.bad = [
index
for index, stats in enumerate(self.stats)
if index not in self.good
]
self.good = [index for index, stats in enumerate(self.stats) if min_val <= stats.get(name) <= max_val]
self.bad = [index for index, stats in enumerate(self.stats) if index not in self.good]
self.resume()
def resume(self):
@ -120,10 +110,6 @@ class SoundStats:
def _view(self, array):
return [
"file"
if index == 0
else "sample {} (at {} seconds)".format(
index, (index - 1) * self.sample_length
)
"file" if index == 0 else "sample {} (at {} seconds)".format(index, (index - 1) * self.sample_length)
for index in array
]