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,61 +851,90 @@ 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
|
||||||
if not current_poll:
|
if not current_poll:
|
||||||
return {'msg': 'no poll running.'}
|
return {'msg': 'no poll running.'}
|
||||||
|
else:
|
||||||
|
return {'msg': 'current poll: {}'.format(current_poll)}
|
||||||
|
elif len(argv) == 1:
|
||||||
|
if argv[0] == 'stop':
|
||||||
|
if not current_poll:
|
||||||
|
return {'msg': 'no poll to stop.'}
|
||||||
|
pollcfg.clear()
|
||||||
|
return {'msg': 'stopped the poll "{}"'.format(current_poll)}
|
||||||
|
elif argv[0] == 'show_raw':
|
||||||
|
if not current_poll:
|
||||||
|
return {'msg': 'no poll to show.'}
|
||||||
|
return {'msg': 'current poll (raw): {}'.format(str(pollcfg))}
|
||||||
|
elif argv[0] == 'show':
|
||||||
|
if not current_poll:
|
||||||
|
return {'msg': 'no poll to show.'}
|
||||||
|
lines = ['current poll: "{}"'.format(current_poll)]
|
||||||
|
for option, voters in pollcfg.items():
|
||||||
|
if option == 'subject':
|
||||||
|
continue
|
||||||
|
lines.append('{0: <4} {1}'.format(len(voters), option))
|
||||||
|
return {'msg': lines}
|
||||||
|
if current_poll and argv[0] in pollcfg:
|
||||||
|
user = args['reply_user']
|
||||||
|
for option, voters in pollcfg.items():
|
||||||
|
if user in voters:
|
||||||
|
pollcfg[option].remove(user)
|
||||||
|
|
||||||
|
pollcfg[argv[0]] = list(set(pollcfg[argv[0]] + [user]))
|
||||||
|
return {'msg': 'voted.'}
|
||||||
else:
|
else:
|
||||||
return {'msg': 'current poll: {}'.format(current_poll)}
|
subject = argv[0]
|
||||||
elif len(argv) == 1:
|
choices = argv[1:]
|
||||||
if argv[0] == 'stop':
|
if len(choices) == 1:
|
||||||
if not current_poll:
|
return {'msg': 'creating a poll with a single option is "alternativlos"'}
|
||||||
return {'msg': 'no poll to stop.'}
|
else:
|
||||||
pollcfg.clear()
|
if current_poll:
|
||||||
return {'msg': 'stopped the poll "{}"'.format(current_poll)}
|
return {'msg': 'a poll is already running ({})'.format(current_poll)}
|
||||||
elif argv[0] == 'show_raw':
|
# create an item for each option
|
||||||
if not current_poll:
|
pollcfg['subject'] = subject
|
||||||
return {'msg': 'no poll to show.'}
|
pollcfg.update({k: [] for k in choices})
|
||||||
return {'msg': 'current poll (raw): {}'.format(str(pollcfg))}
|
return {'msg': 'created the poll.'}
|
||||||
elif argv[0] == 'show':
|
|
||||||
if not current_poll:
|
|
||||||
return {'msg': 'no poll to show.'}
|
|
||||||
lines = ['current poll: "{}"'.format(current_poll)]
|
|
||||||
for option, voters in pollcfg.items():
|
|
||||||
if option == 'subject':
|
|
||||||
continue
|
|
||||||
lines.append('{0: <4} {1}'.format(len(voters), option))
|
|
||||||
return {'msg': lines}
|
|
||||||
if current_poll and argv[0] in pollcfg:
|
|
||||||
user = args['reply_user']
|
|
||||||
for option, voters in pollcfg.items():
|
|
||||||
if user in voters:
|
|
||||||
pollcfg[option].remove(user)
|
|
||||||
|
|
||||||
pollcfg[argv[0]] = list(set(pollcfg[argv[0]] + [user]))
|
|
||||||
return {'msg': 'voted.'}
|
|
||||||
else:
|
|
||||||
subject = argv[0]
|
|
||||||
choices = argv[1:]
|
|
||||||
if len(choices) == 1:
|
|
||||||
return {'msg': 'creating a poll with a single option is "alternativlos"'}
|
|
||||||
else:
|
|
||||||
if current_poll:
|
|
||||||
return {'msg': 'a poll is already running ({})'.format(current_poll)}
|
|
||||||
if pollcfg is None:
|
|
||||||
pollcfg = dict()
|
|
||||||
# create an item for each option
|
|
||||||
pollcfg['subject'] = subject
|
|
||||||
pollcfg.update({k: [] for k in choices})
|
|
||||||
return {'msg': 'created the poll.'}
|
|
||||||
|
|
||||||
config.runtime_config_store['plugins']['poll'] = pollcfg
|
|
||||||
config.runtimeconf_persist()
|
|
||||||
|
|
||||||
|
|
||||||
@pluginfunction('vote', 'alias for poll', ptypes_COMMAND)
|
@pluginfunction('vote', 'alias for poll', ptypes_COMMAND, enabled=False)
|
||||||
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