Commit 988ab988 authored by Oliver Rümpelein's avatar Oliver Rümpelein
Browse files

Added .gitignore, rewrote give plugin

 - new Syntax (all are working:
   ```
   !give --to foo,bar,baz something cool
   !give -t foo,bar,baz something cool
   !give something cool # same as !give -t <yournick> something cool
   ```
parent fc6e9284
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
from errbot import BotPlugin, botcmd
import argparse
from errbot import BotPlugin, arg_botcmd
# region > Utility functions
......@@ -13,85 +14,45 @@ def peer_account_name(msg):
class Give(BotPlugin):
"""Example: Hand out Beer"""
"""Example: Hand out Beer and other stuff."""
@arg_botcmd("-t", "--to", dest="receivers", type=str, default='')
@arg_botcmd("beer", type=str, default='', nargs=argparse.REMAINDER)
def beer(self, msg, beer, receivers):
"""Give a beer to someone or yourself"""
receiver_string = self.__make_receivers(msg, receivers)
beer_string = " ".join(beer) + " " if beer else ""
return "/me goes to the cellar and returns, carrying a {}beer for {}.".format(beer_string, receiver_string)
@arg_botcmd("-t", "--to", dest="receivers", type=str, default='')
@arg_botcmd("things", type=str, default='some secret thing', nargs=argparse.REMAINDER)
def give(self, msg, things, receivers):
"""Give 'something' to '--to' or yourself"""
receiver_string = self.__make_receivers(msg, receivers)
thing_string = " ".join(things) if things != [] else "some secret thing"
return "/me gives {} to {}.".format(thing_string, receiver_string)
@staticmethod
def __evalto(args):
"""Count number of given names, separated by 'and'
Args:
args: List of strings
Returns:
Integer with number of names
"""
num = 1
for i in range(1, len(args), 2):
if args[i] == "and":
num += 1
else:
break
return num
def __make_receivers(msg, receivers):
persons = [x.strip() for x in str.split(receivers, ',') if x != '']
num_receivers = len(persons)
receiver_string = peer_account_name(msg) if num_receivers == 0 \
else Give.__create_receiver_string(persons)
return receiver_string
@staticmethod
def __printargs(args):
"""Print args as string if given
def __create_receiver_string(names):
"""Creates a syntactically correct, printable string of the given receivers.
Args:
args: List of strings
Returns:
String for in-sentence use.
:param names: A list of names to concatenate
:return: The string mentioned above.
"""
if args:
return " " + " ".join(args) + " "
size = len(names)
if size == 0:
return 'no-one'
elif size == 1:
return names[0]
elif size == 2:
return "{} and {}".format(names[0], names[1])
else:
return " "
@staticmethod
def __printto(args, num):
"""Print user string, with users separated by 'and'.
Args:
args: List of strings
num: Numbers of names
Returns:
String for end-of-sentence-use
"""
answer_string = args[0]
for i in range(2, 2*num, 2):
answer_string += " and " + args[i]
return answer_string
@botcmd(split_args_with=None)
def beer(self, msg, args):
"""Get beer from the cellar, optional specify properties"""
return "/me goes to the cellar and returns, carrying a{}beer \
for {}.".format(self.__printargs(args),
peer_account_name(msg))
# noinspection PyUnusedLocal
@botcmd(split_args_with=None)
def beer_for(self, msg, args):
"""Get beer from the cellar, hand to someone else"""
num = self.__evalto(args)
return "/me goes to the cellar and returns, \
carrying a{}beer for {}.".format(
self.__printargs(args[2*num-1:]),
self.__printto(args, num),
)
@botcmd(split_args_with=None)
def give(self, msg, args):
"""Give 'something' to yourself"""
return "/me gives a{}to {}.".format(
self.__printargs(args),
peer_account_name(msg))
# noinspection PyUnusedLocal
@botcmd(split_args_with=None)
def give_to(self, msg, args):
"""Give 'something' to 'someone'"""
num = self.__evalto(args)
return "/me gives{}to {}.".format(
self.__printargs(args[2*num-1:]),
self.__printto(args, num)
)
return "{}, and {}".format(", ".join(names[0:-1]), names[-1])
......@@ -29,86 +29,73 @@ def dialog_test(bot, msg, exp):
class TestGive:
extra_plugin_dir = "."
def test_evalto(self, testbot):
def test_create_receiver_string(self, testbot):
plugin = get_plugin(testbot, 'Give')
expected = [1, 3]
result = [
plugin._Give__evalto(['asdil1991']),
plugin._Give__evalto(['asdil1991', 'and', 'pheerai',
'and', 'asdil12', 'cool', 'büchenbacher'])
]
expected = ["no-one", 'chip', "chip and dale", "chip, dale, and gadget"]
inputs = [[], ['chip'], ['chip', 'dale'], ['chip', 'dale', 'gadget']]
result = [plugin._Give__create_receiver_string(x) for x in inputs]
assert result == expected
def test_printargs(self, testbot):
plugin = get_plugin(testbot, 'Give')
expected = [" ", " 1 2 3 "]
result = [plugin._Give__printargs([]),
plugin._Give__printargs(["1", "2", "3"])]
assert result == expected
def test_printto(self, testbot):
plugin = get_plugin(testbot, 'Give')
expected = ['asdil1991', 'asdil1991 and pheerai and asdil12']
result = [plugin._Give__printto(["asdil1991"], 1),
plugin._Give__printto(["asdil1991", "and", "pheerai",
"and", "asdil12"], 3)]
assert expected == result
def test_beer_1(self, testbot):
def test_give(self, testbot):
dialog_test(
testbot,
'!beer',
'/me goes to the cellar and returns, carrying a beer \
for None.'
'!give',
"/me gives some secret thing to None."
)
dialog_test(
testbot,
'!beer cool büchenbacher',
'/me goes to the cellar and returns, carrying a cool \
büchenbacher beer for None.'
'!give sweet chocolate',
'/me gives sweet chocolate to None.'
)
def test_beer_for_1(self, testbot):
dialog_test(
testbot,
'!beer for asdil1991',
'/me goes to the cellar and returns, carrying a beer \
for asdil1991.'
'!give --to chip sweet chocolate',
'/me gives sweet chocolate to chip.'
)
def test_beer_for_2(self, testbot):
dialog_test(
testbot,
'!beer for asdil1991 and pheerai and asdil12 \
cool Büchenbacher',
'/me goes to the cellar and returns, carrying a \
cool Büchenbacher beer for asdil1991 and pheerai and asdil12.'
)
def test_give(self, testbot):
'!give -t chip sweet chocolate',
'/me gives sweet chocolate to chip.'
)
dialog_test(
testbot,
'!give',
'/me gives a to None.'
'!give --to chip,dale sweet chocolate',
'/me gives sweet chocolate to chip and dale.'
)
dialog_test(
testbot,
'!give sweet chocolate',
'/me gives a sweet chocolate to None.'
'!give --to chip,dale,gadget sweet chocolate',
'/me gives sweet chocolate to chip, dale, and gadget.'
)
def test_give_to(self, testbot):
# give to 1 person
def test_beer2(self, testbot):
dialog_test(
testbot,
'!beer',
"/me goes to the cellar and returns, carrying a beer for None."
)
dialog_test(
testbot,
'!beer sweet chocolate',
"/me goes to the cellar and returns, carrying a sweet chocolate beer for None."
)
dialog_test(
testbot,
'!give to asdil1991',
'/me gives to asdil1991.'
'!beer --to chip sweet chocolate',
'/me goes to the cellar and returns, carrying a sweet chocolate beer for chip.'
)
# Give to multiple persons
dialog_test(
testbot,
'!give to asdil1991 and pheerai and asdil12 \
salty gulasch',
'/me gives salty gulasch to asdil1991 and pheerai \
and asdil12.'
)
'!beer -t chip sweet chocolate',
'/me goes to the cellar and returns, carrying a sweet chocolate beer for chip.'
)
dialog_test(
testbot,
'!beer --to chip,dale sweet chocolate',
'/me goes to the cellar and returns, carrying a sweet chocolate beer for chip and dale.'
)
dialog_test(
testbot,
'!beer --to chip,dale,gadget sweet chocolate',
'/me goes to the cellar and returns, carrying a sweet chocolate beer for chip, dale, and gadget.'
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment