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)] 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 class Config: SECRET_KEY = os.environ.get('SECRET_KEY') or \ ''.join(random_choices(string.ascii_letters, k=20)) OUT_DIR = os.environ.get('OUT_DIR') or '/var/tmp/prolefeeder-test-out' MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180 DEBUG = False 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)