mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
fiddle with config syntax, add disabled new plugin for quiz
This commit is contained in:
10
config.py
10
config.py
@@ -13,6 +13,8 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from fasteners import interprocess_locked
|
from fasteners import interprocess_locked
|
||||||
from configobj import ConfigObj
|
from configobj import ConfigObj
|
||||||
from validate import Validator
|
from validate import Validator
|
||||||
@@ -99,3 +101,11 @@ def runtimeconf_deepget(key, default=None):
|
|||||||
if value is None:
|
if value is None:
|
||||||
break
|
break
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def plugin_config(name):
|
||||||
|
cfg = runtimeconf_deepget('plugins.{}'.format(name), {})
|
||||||
|
yield cfg
|
||||||
|
runtime_config_store['plugins'][name] = cfg
|
||||||
|
runtimeconf_persist()
|
||||||
|
|||||||
@@ -851,8 +851,8 @@ def isdown(argv, **args):
|
|||||||
|
|
||||||
@pluginfunction('poll', 'create a poll', ptypes_COMMAND)
|
@pluginfunction('poll', 'create a poll', ptypes_COMMAND)
|
||||||
def poll(argv, **args):
|
def poll(argv, **args):
|
||||||
pollcfg = config.runtimeconf_deepget('plugins.poll')
|
with config.plugin_config('poll') as pollcfg:
|
||||||
current_poll = pollcfg.get('subject') if pollcfg else None
|
current_poll = pollcfg.get('subject')
|
||||||
|
|
||||||
if not argv:
|
if not argv:
|
||||||
# return poll info
|
# return poll info
|
||||||
@@ -895,17 +895,46 @@ def poll(argv, **args):
|
|||||||
else:
|
else:
|
||||||
if current_poll:
|
if current_poll:
|
||||||
return {'msg': 'a poll is already running ({})'.format(current_poll)}
|
return {'msg': 'a poll is already running ({})'.format(current_poll)}
|
||||||
if pollcfg is None:
|
|
||||||
pollcfg = dict()
|
|
||||||
# create an item for each option
|
# create an item for each option
|
||||||
pollcfg['subject'] = subject
|
pollcfg['subject'] = subject
|
||||||
pollcfg.update({k: [] for k in choices})
|
pollcfg.update({k: [] for k in choices})
|
||||||
return {'msg': 'created the poll.'}
|
return {'msg': 'created the poll.'}
|
||||||
|
|
||||||
config.runtime_config_store['plugins']['poll'] = pollcfg
|
|
||||||
config.runtimeconf_persist()
|
|
||||||
|
|
||||||
|
@pluginfunction('vote', 'alias for poll', ptypes_COMMAND, enabled=False)
|
||||||
@pluginfunction('vote', 'alias for poll', ptypes_COMMAND)
|
|
||||||
def vote(argv, **args):
|
def vote(argv, **args):
|
||||||
return poll(argv, **args)
|
return poll(argv, **args)
|
||||||
|
|
||||||
|
|
||||||
|
@pluginfunction('quiz', 'play quiz', ptypes_COMMAND, enabled=False)
|
||||||
|
def quiz(argv, **args):
|
||||||
|
usage = """quiz mode usage: "quiz start", "quiz stop", "quiz answer", "quiz skip";
|
||||||
|
if the quiz mode is active, sentences are parsed and compared against the answer.
|
||||||
|
"""
|
||||||
|
questions = [
|
||||||
|
'die antwort auf alle fragen?',
|
||||||
|
'keks',
|
||||||
|
'Welcher Stern ist der Erde am nächsten?',
|
||||||
|
'Sonne'
|
||||||
|
]
|
||||||
|
if not argv:
|
||||||
|
return usage
|
||||||
|
|
||||||
|
with config.plugin_config('quiz') as quizcfg:
|
||||||
|
if quizcfg is None:
|
||||||
|
quizcfg = dict()
|
||||||
|
|
||||||
|
if argv[0] == 'start':
|
||||||
|
# select a random question
|
||||||
|
used_ids = quizcfg['used_ids']
|
||||||
|
q_index = -1
|
||||||
|
while q_index == -1:
|
||||||
|
rand = random.choice(range(0, len(questions)-2, 2))
|
||||||
|
if rand not in used_ids:
|
||||||
|
q_index = rand
|
||||||
|
|
||||||
|
quizcfg['active_id'] = q_index
|
||||||
|
quizcfg['used_ids'] = used_ids + []
|
||||||
|
|
||||||
|
return {'msg': ['Q: {}'.format(questions[q_index]),
|
||||||
|
'A: {}'.format(questions[q_index+1]), ]}
|
||||||
|
|||||||
Reference in New Issue
Block a user