New audiogen: script

closes #9
This commit is contained in:
boyska 2016-07-18 12:27:21 +02:00
parent 52cf40b780
commit 57b7586c0a
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
4 changed files with 73 additions and 1 deletions

View file

@ -0,0 +1,21 @@
from flask_wtf import Form
from wtforms import StringField, validators, SubmitField
class ScriptAudioForm(Form):
nick = StringField(u'Audio nick', validators=[validators.required()],
description='A simple name to recognize this audio')
name = StringField(u'Name', validators=[validators.required()],
description='filename (NOT path) of the script')
args = StringField(u'Arguments',
description='arguments, separated by spaces')
submit = SubmitField(u'Submit')
def scriptaudio_receive(form):
return {
'kind': 'script',
'nick': form.nick.data,
'name': form.name.data,
'args': form.args.data
}

View file

@ -0,0 +1,47 @@
import logging
import os
import subprocess
from config import get_conf
log = logging.getLogger('audioscript')
def generate(spec):
'''
Recognized arguments (fields in spec):
- name [mandatory] script name
- args [default=empty] arguments, space-separated
'''
conf = get_conf()
spec.setdefault('args', '')
args = spec['args'].split()
for attr in ('name', ):
if attr not in spec:
raise ValueError("Malformed audiospec: missing '%s'" % attr)
scriptpath = os.path.join(conf['SCRIPTS_PATH'], spec['name'])
if not os.path.exists(scriptpath):
raise ValueError("Script %s not found", spec['name'])
if not os.access(scriptpath, os.R_OK | os.X_OK):
raise ValueError("Insufficient privileges for script %s" % scriptpath)
if os.stat(scriptpath).st_uid != os.getuid():
raise ValueError("Script %s owned by %d, should be owned by %d"
% (spec['name'], os.stat(scriptpath).st_uid,
os.getuid()))
try:
log.info('Going to run {}'.format([scriptpath] + args))
out = subprocess.check_output([scriptpath] + args,
env=dict(
HOME=os.environ['HOME'],
PATH=os.environ['PATH'],
MPD_HOST=conf['MPD_HOST'],
MPD_PORT=str(conf['MPD_PORT'])),
cwd='/')
except subprocess.CalledProcessError as exc:
log.error("Error %d when running script %s" %
(exc.returncode, spec['name']))
return []
out = [p for p in out.split('\n') if p]
logging.debug('Script %s produced %d files' % (spec['name'], len(out)))
return out

View file

@ -18,6 +18,7 @@ def get_conf(prefix='LARIGIRA_'):
conf['MPD_PORT'] = int(os.getenv('MPD_PORT', '6600'))
conf['CACHING_TIME'] = 10
conf['DB_URI'] = os.path.join(conf_dir, 'db.json')
conf['SCRIPTS_PATH'] = os.path.join(conf_dir, 'scripts')
conf['BOOTSTRAP_SERVE_LOCAL'] = True
conf['SECRET_KEY'] = 'Please replace me!'
conf['MPD_WAIT_START'] = True

View file

@ -58,7 +58,8 @@ setup(name='larigira',
'larigira.audiogenerators': [
'mpd = larigira.audiogen_mpdrandom:generate_by_artist',
'static = larigira.audiogen_static:generate',
'randomdir = larigira.audiogen_randomdir:generate'
'randomdir = larigira.audiogen_randomdir:generate',
'script = larigira.audiogen_script:generate',
],
'larigira.timegenerators': [
'frequency = larigira.timegen_every:FrequencyAlarm',
@ -72,9 +73,11 @@ setup(name='larigira',
],
'larigira.audioform_create': [
'static = larigira.audioform_static:StaticAudioForm',
'script = larigira.audioform_script:ScriptAudioForm',
],
'larigira.audioform_receive': [
'static = larigira.audioform_static:staticaudio_receive',
'script = larigira.audioform_script:scriptaudio_receive',
],
}
)