prolefeeder/config.py.example

60 lines
1.9 KiB
Text
Raw Normal View History

2018-11-12 23:21:15 +01:00
import os
import random
import string
from datetime import datetime, timedelta
def random_choices(population, k):
"""Emulate random.choices for Python < 3.6"""
return [random.choice(population) for _ in range(k)]
2018-11-11 14:48:21 +01:00
def mp3_sources(data_dir, pattern, kbits):
def start_time_for_source_fn(fn):
return datetime.strptime(fn, pattern)
def length_for_source_fn(fn):
size = os.stat(os.path.join(data_dir, fn)).st_size
length = timedelta(minutes=size / (1000*kbits/8) / 60)
return length
def get_sources():
# Get a sorted list of all source files with start time and length
sources = []
for fn in os.listdir(data_dir):
try:
start_time = start_time_for_source_fn(fn)
length = length_for_source_fn(fn)
sources.append({'fn': os.path.join(data_dir, fn), 'start_time': start_time, 'length': length})
except ValueError:
pass
return sources
return get_sources
2018-11-11 14:48:21 +01:00
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or \
''.join(random_choices(string.ascii_letters, k=20))
2019-03-06 20:19:10 +01:00
OUT_DIR = os.environ.get('OUT_DIR') or '/var/tmp/prolefeeder-test-out'
2018-11-11 14:48:21 +01:00
MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180
2018-11-30 18:25:56 +01:00
DEBUG = False
2019-03-06 20:19:10 +01:00
if not os.path.exists(OUT_DIR):
raise RuntimeError("OUT_DIR does not exist")
# Here's the interesting part: configuring the source.
#
# We need to return a function that returns a list of dicts of source files with metadata, e.g.
# [ {"fn": "/path/to/foo-20:01.mp3", "start_time": datetime(...), "length": timedelta(hours=1)} ]
data_dir = os.environ.get('DATA_DIR') or '/var/tmp/prolefeeder-test-data'
kbits = os.environ.get('KBITS') or 128
pattern = 'qfhi-%Y%m%d-%H%M.mp3'
if not os.path.exists(data_dir):
raise RuntimeError("DATA_DIR does not exist")
SOURCES = mp3_sources(data_dir, pattern, kbits)