test
This commit is contained in:
parent
46c38d01a8
commit
43c93a9007
@ -26,3 +26,4 @@ sopel.plugins =
|
|||||||
SpiceBotPrerun = sopel_SpiceBot_Core_Prerun
|
SpiceBotPrerun = sopel_SpiceBot_Core_Prerun
|
||||||
SpiceBotStartup = sopel_SpiceBot_Core_Startup
|
SpiceBotStartup = sopel_SpiceBot_Core_Startup
|
||||||
SpiceBotRuntimeCommands = sopel_SpiceBot_Runtime_Commands
|
SpiceBotRuntimeCommands = sopel_SpiceBot_Runtime_Commands
|
||||||
|
spicemanip = spicemanip
|
||||||
|
|||||||
@ -2,6 +2,8 @@ import functools
|
|||||||
|
|
||||||
from sopel_SpiceBot_Core_1 import sb
|
from sopel_SpiceBot_Core_1 import sb
|
||||||
|
|
||||||
|
from spicemanip import spicemanip
|
||||||
|
|
||||||
|
|
||||||
def prerun_command():
|
def prerun_command():
|
||||||
|
|
||||||
@ -27,11 +29,34 @@ def prerun_nickname_command():
|
|||||||
@functools.wraps(function)
|
@functools.wraps(function)
|
||||||
def internal_prerun(bot, trigger, *args, **kwargs):
|
def internal_prerun(bot, trigger, *args, **kwargs):
|
||||||
|
|
||||||
bot.say(str(id(trigger)))
|
trigger_command_type = str("nickname")
|
||||||
|
|
||||||
sb.osd("test", trigger.sender)
|
# Primary command used for trigger, and a list of all words
|
||||||
|
trigger_args, trigger_command, trigger_prefix = make_trigger_args(trigger.args[1], trigger_command_type)
|
||||||
|
|
||||||
|
for x in [trigger_args, trigger_command, trigger_prefix]:
|
||||||
|
bot.say(x)
|
||||||
|
|
||||||
function(bot, trigger, *args, **kwargs)
|
function(bot, trigger, *args, **kwargs)
|
||||||
|
|
||||||
return internal_prerun
|
return internal_prerun
|
||||||
return actual_decorator
|
return actual_decorator
|
||||||
|
|
||||||
|
|
||||||
|
def make_trigger_args(triggerargs_one, trigger_command_type='module'):
|
||||||
|
trigger_args = spicemanip(triggerargs_one, 'create')
|
||||||
|
if trigger_command_type in ['nickname']:
|
||||||
|
trigger_prefix = None
|
||||||
|
# if trigger_prefix.isupper() or trigger_prefix.islower():
|
||||||
|
# trigger_prefix = None
|
||||||
|
trigger_command = spicemanip(trigger_args, 2).lower()
|
||||||
|
trigger_args = spicemanip(trigger_args, '3+', 'list')
|
||||||
|
elif trigger_command_type in ['action']:
|
||||||
|
trigger_prefix = None
|
||||||
|
trigger_command = spicemanip(trigger_args, 1).lower()
|
||||||
|
trigger_args = spicemanip(trigger_args, '2+', 'list')
|
||||||
|
else:
|
||||||
|
trigger_prefix = spicemanip(trigger_args, 1).lower()[0]
|
||||||
|
trigger_command = spicemanip(trigger_args, 1).lower()[1:]
|
||||||
|
trigger_args = spicemanip(trigger_args, '2+', 'list')
|
||||||
|
return trigger_args, trigger_command, trigger_prefix
|
||||||
|
|||||||
@ -15,3 +15,13 @@ def sb_nickname_command(bot, trigger):
|
|||||||
bot.say("%s" % sb.versions.dict)
|
bot.say("%s" % sb.versions.dict)
|
||||||
|
|
||||||
sb.osd("test", trigger.sender)
|
sb.osd("test", trigger.sender)
|
||||||
|
|
||||||
|
|
||||||
|
@prerun_nickname_command()
|
||||||
|
@plugin.nickname_command('far')
|
||||||
|
def sb_fart_command(bot, trigger):
|
||||||
|
bot.say("Testing the bot")
|
||||||
|
bot.say("Attributes: %s" % [x for x in dir(sb) if not x.startswith("__")])
|
||||||
|
bot.say("%s" % sb.versions.dict)
|
||||||
|
|
||||||
|
sb.osd("test", trigger.sender)
|
||||||
|
|||||||
334
spicemanip/__init__.py
Normal file
334
spicemanip/__init__.py
Normal file
@ -0,0 +1,334 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""Top-level package for spicemanip."""
|
||||||
|
|
||||||
|
__author__ = """Sam Zick"""
|
||||||
|
__email__ = 'sam@deathbybandaid.net'
|
||||||
|
__version__ = '0.1.8'
|
||||||
|
|
||||||
|
import random
|
||||||
|
import collections
|
||||||
|
|
||||||
|
# TODO 'this*that' or '1*that' replace either all strings matching, or an index value
|
||||||
|
# TODO reverse sort z.sort(reverse = True)
|
||||||
|
# list.extend adds lists to eachother
|
||||||
|
|
||||||
|
|
||||||
|
class Spicemanip():
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __call__(self, inputs, outputtask, output_type='default'):
|
||||||
|
|
||||||
|
mainoutputtask, suboutputtask = None, None
|
||||||
|
|
||||||
|
# Input needs to be a list, but don't split a word into letters
|
||||||
|
if not inputs:
|
||||||
|
inputs = []
|
||||||
|
if isinstance(inputs, collections.abc.KeysView):
|
||||||
|
inputs = list(inputs)
|
||||||
|
elif isinstance(inputs, dict):
|
||||||
|
inputs = list(inputs.keys())
|
||||||
|
if not isinstance(inputs, list):
|
||||||
|
inputs = list(inputs.split(" "))
|
||||||
|
inputs = [x for x in inputs if x and x not in ['', ' ']]
|
||||||
|
inputs = [inputspart.strip() for inputspart in inputs]
|
||||||
|
|
||||||
|
# Create return
|
||||||
|
if outputtask == 'create':
|
||||||
|
return inputs
|
||||||
|
|
||||||
|
# Make temparray to preserve original order
|
||||||
|
temparray = []
|
||||||
|
for inputpart in inputs:
|
||||||
|
temparray.append(inputpart)
|
||||||
|
inputs = temparray
|
||||||
|
|
||||||
|
# Convert outputtask to standard
|
||||||
|
if outputtask in [0, 'complete']:
|
||||||
|
outputtask = 'string'
|
||||||
|
elif outputtask == 'index':
|
||||||
|
mainoutputtask = inputs[1]
|
||||||
|
suboutputtask = inputs[2]
|
||||||
|
inputs = inputs[0]
|
||||||
|
elif str(outputtask).isdigit():
|
||||||
|
mainoutputtask, outputtask = int(outputtask), 'number'
|
||||||
|
elif "^" in str(outputtask):
|
||||||
|
mainoutputtask = str(outputtask).split("^", 1)[0]
|
||||||
|
suboutputtask = str(outputtask).split("^", 1)[1]
|
||||||
|
outputtask = 'rangebetween'
|
||||||
|
if int(suboutputtask) < int(mainoutputtask):
|
||||||
|
mainoutputtask, suboutputtask = suboutputtask, mainoutputtask
|
||||||
|
elif str(outputtask).startswith("split_"):
|
||||||
|
mainoutputtask = str(outputtask).replace("split_", "")
|
||||||
|
outputtask = 'split'
|
||||||
|
elif str(outputtask).endswith(tuple(["!", "+", "-", "<", ">"])):
|
||||||
|
mainoutputtask = str(outputtask)
|
||||||
|
if str(outputtask).endswith("!"):
|
||||||
|
outputtask = 'exclude'
|
||||||
|
if str(outputtask).endswith("+"):
|
||||||
|
outputtask = 'incrange_plus'
|
||||||
|
if str(outputtask).endswith("-"):
|
||||||
|
outputtask = 'incrange_minus'
|
||||||
|
if str(outputtask).endswith(">"):
|
||||||
|
outputtask = 'excrange_plus'
|
||||||
|
if str(outputtask).endswith("<"):
|
||||||
|
outputtask = 'excrange_minus'
|
||||||
|
for r in (("!", ""), ("+", ""), ("-", ""), ("<", ""), (">", "")):
|
||||||
|
mainoutputtask = mainoutputtask.replace(*r)
|
||||||
|
if mainoutputtask == 'last':
|
||||||
|
mainoutputtask = len(inputs)
|
||||||
|
|
||||||
|
if outputtask == 'string':
|
||||||
|
returnvalue = inputs
|
||||||
|
else:
|
||||||
|
returnvalue = eval(
|
||||||
|
'self.' + outputtask +
|
||||||
|
'(inputs, outputtask, mainoutputtask, suboutputtask)')
|
||||||
|
|
||||||
|
# default return if not specified
|
||||||
|
if output_type == 'default':
|
||||||
|
if outputtask in [
|
||||||
|
'string', 'number', 'rangebetween', 'exclude', 'random',
|
||||||
|
'incrange_plus', 'incrange_minus', 'excrange_plus',
|
||||||
|
'excrange_minus'
|
||||||
|
]:
|
||||||
|
output_type = 'string'
|
||||||
|
elif outputtask in ['count']:
|
||||||
|
output_type = 'dict'
|
||||||
|
|
||||||
|
# verify output is correct
|
||||||
|
if output_type == 'return':
|
||||||
|
return returnvalue
|
||||||
|
if output_type == 'string':
|
||||||
|
if isinstance(returnvalue, list):
|
||||||
|
returnvalue = ' '.join(returnvalue)
|
||||||
|
elif output_type in ['list', 'array']:
|
||||||
|
if not isinstance(returnvalue, list):
|
||||||
|
returnvalue = list(returnvalue.split(" "))
|
||||||
|
returnvalue = [x for x in returnvalue if x and x not in ['', ' ']]
|
||||||
|
returnvalue = [inputspart.strip() for inputspart in returnvalue]
|
||||||
|
return returnvalue
|
||||||
|
|
||||||
|
# compare 2 lists, based on the location of an index item, passthrough needs to be [indexitem, arraytoindex, arraytocompare]
|
||||||
|
def index(self, indexitem, outputtask, arraytoindex, arraytocompare):
|
||||||
|
item = ''
|
||||||
|
for x, y in zip(arraytoindex, arraytocompare):
|
||||||
|
if x == indexitem:
|
||||||
|
item = y
|
||||||
|
return item
|
||||||
|
|
||||||
|
# split list by string
|
||||||
|
def split(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
split_array = []
|
||||||
|
restring = ' '.join(inputs)
|
||||||
|
if mainoutputtask not in inputs:
|
||||||
|
split_array = [restring]
|
||||||
|
else:
|
||||||
|
split_array = restring.split(mainoutputtask)
|
||||||
|
split_array = [x for x in split_array if x and x not in ['', ' ']]
|
||||||
|
split_array = [inputspart.strip() for inputspart in split_array]
|
||||||
|
if split_array == []:
|
||||||
|
split_array = [[]]
|
||||||
|
return split_array
|
||||||
|
|
||||||
|
# dedupe list
|
||||||
|
def dedupe(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
newlist = []
|
||||||
|
for inputspart in inputs:
|
||||||
|
if inputspart not in newlist:
|
||||||
|
newlist.append(inputspart)
|
||||||
|
return newlist
|
||||||
|
|
||||||
|
# Sort list
|
||||||
|
def sort(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
return sorted(inputs)
|
||||||
|
|
||||||
|
# reverse sort list
|
||||||
|
def rsort(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
return sorted(inputs)[::-1]
|
||||||
|
|
||||||
|
# count items in list, return dictionary
|
||||||
|
def count(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
returndict = dict()
|
||||||
|
if not len(inputs):
|
||||||
|
return returndict
|
||||||
|
uniqueinputitems, uniquecount = [], []
|
||||||
|
for inputspart in inputs:
|
||||||
|
if inputspart not in uniqueinputitems:
|
||||||
|
uniqueinputitems.append(inputspart)
|
||||||
|
for uniqueinputspart in uniqueinputitems:
|
||||||
|
count = 0
|
||||||
|
for ele in inputs:
|
||||||
|
if (ele == uniqueinputspart):
|
||||||
|
count += 1
|
||||||
|
uniquecount.append(count)
|
||||||
|
for inputsitem, unumber in zip(uniqueinputitems, uniquecount):
|
||||||
|
returndict[inputsitem] = unumber
|
||||||
|
return returndict
|
||||||
|
|
||||||
|
# random item from list
|
||||||
|
def random(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
randomselectlist = []
|
||||||
|
for temppart in inputs:
|
||||||
|
randomselectlist.append(temppart)
|
||||||
|
while len(randomselectlist) > 1:
|
||||||
|
random.shuffle(randomselectlist)
|
||||||
|
randomselect = randomselectlist[random.randint(
|
||||||
|
0,
|
||||||
|
len(randomselectlist) - 1)]
|
||||||
|
randomselectlist.remove(randomselect)
|
||||||
|
randomselect = randomselectlist[0]
|
||||||
|
return randomselect
|
||||||
|
|
||||||
|
# remove random item from list
|
||||||
|
def exrandom(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return []
|
||||||
|
randremove = self.random(inputs, outputtask, mainoutputtask, suboutputtask)
|
||||||
|
inputs.remove(randremove)
|
||||||
|
return inputs
|
||||||
|
|
||||||
|
# Convert list into lowercase
|
||||||
|
def lower(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return [inputspart.lower() for inputspart in inputs]
|
||||||
|
|
||||||
|
# Convert list to uppercase
|
||||||
|
def upper(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return [inputspart.upper() for inputspart in inputs]
|
||||||
|
|
||||||
|
# Convert list to uppercase
|
||||||
|
def title(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return [inputspart.title() for inputspart in inputs]
|
||||||
|
|
||||||
|
# Reverse List Order
|
||||||
|
def reverse(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return []
|
||||||
|
return inputs[::-1]
|
||||||
|
|
||||||
|
# comma seperated list
|
||||||
|
def list(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return ', '.join(str(x) for x in inputs)
|
||||||
|
|
||||||
|
def list_nospace(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return ','.join(str(x) for x in inputs)
|
||||||
|
|
||||||
|
# comma seperated list with and
|
||||||
|
def andlist(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
if len(inputs) < 2:
|
||||||
|
return ' '.join(inputs)
|
||||||
|
lastentry = str("and " + str(inputs[len(inputs) - 1]))
|
||||||
|
del inputs[-1]
|
||||||
|
inputs.append(lastentry)
|
||||||
|
if len(inputs) == 2:
|
||||||
|
return ' '.join(inputs)
|
||||||
|
return ', '.join(str(x) for x in inputs)
|
||||||
|
|
||||||
|
# comma seperated list with or
|
||||||
|
def orlist(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
if len(inputs) < 2:
|
||||||
|
return ' '.join(inputs)
|
||||||
|
lastentry = str("or " + str(inputs[len(inputs) - 1]))
|
||||||
|
del inputs[-1]
|
||||||
|
inputs.append(lastentry)
|
||||||
|
if len(inputs) == 2:
|
||||||
|
return ' '.join(inputs)
|
||||||
|
return ', '.join(str(x) for x in inputs)
|
||||||
|
|
||||||
|
# exclude number
|
||||||
|
def exclude(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
del inputs[int(mainoutputtask) - 1]
|
||||||
|
return ' '.join(inputs)
|
||||||
|
|
||||||
|
# Convert list to string
|
||||||
|
def string(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return ' '.join(inputs)
|
||||||
|
|
||||||
|
# Get number item from list
|
||||||
|
def number(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
elif int(mainoutputtask) > len(inputs) or int(mainoutputtask) < 0:
|
||||||
|
return ''
|
||||||
|
else:
|
||||||
|
return inputs[int(mainoutputtask) - 1]
|
||||||
|
|
||||||
|
# Get Last item from list
|
||||||
|
def last(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return inputs[len(inputs) - 1]
|
||||||
|
|
||||||
|
# range between items in list
|
||||||
|
def rangebetween(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
if not str(mainoutputtask).isdigit() or not str(
|
||||||
|
suboutputtask).isdigit():
|
||||||
|
return ''
|
||||||
|
mainoutputtask, suboutputtask = int(mainoutputtask), int(suboutputtask)
|
||||||
|
if suboutputtask == mainoutputtask:
|
||||||
|
return self.number(inputs, outputtask, mainoutputtask, suboutputtask)
|
||||||
|
if suboutputtask < mainoutputtask:
|
||||||
|
return []
|
||||||
|
if mainoutputtask < 0:
|
||||||
|
mainoutputtask = 1
|
||||||
|
if suboutputtask > len(inputs):
|
||||||
|
suboutputtask = len(inputs)
|
||||||
|
newlist = []
|
||||||
|
for i in range(mainoutputtask, suboutputtask + 1):
|
||||||
|
newlist.append(
|
||||||
|
str(
|
||||||
|
self.number(inputs, outputtask, i, suboutputtask)))
|
||||||
|
if newlist == []:
|
||||||
|
return ''
|
||||||
|
return ' '.join(newlist)
|
||||||
|
|
||||||
|
# Forward Range includes index number
|
||||||
|
def incrange_plus(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return self.rangebetween(inputs, outputtask, int(mainoutputtask), len(inputs))
|
||||||
|
|
||||||
|
# Reverse Range includes index number
|
||||||
|
def incrange_minus(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return self.rangebetween(inputs, outputtask, 1, int(mainoutputtask))
|
||||||
|
|
||||||
|
# Forward Range excludes index number
|
||||||
|
def excrange_plus(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return self.rangebetween(inputs, outputtask, int(mainoutputtask) + 1, len(inputs))
|
||||||
|
|
||||||
|
# Reverse Range excludes index number
|
||||||
|
def excrange_minus(self, inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
|
if not len(inputs):
|
||||||
|
return ''
|
||||||
|
return self.rangebetween(inputs, outputtask, 1, int(mainoutputtask) - 1)
|
||||||
|
|
||||||
|
|
||||||
|
spicemanip = Spicemanip()
|
||||||
Loading…
Reference in New Issue
Block a user