Source code for core.utils.parse
# core/utils/parse.py
#
#
""" utils package. """
__copyright__ = "Copyright 2015, B.H.J Thate"
## IMPORT
import logging
## PARSE
[docs]def txt_parse(*args, **kwargs):
from core.thing import Thing
from core.errors import error, WrongArgument
bot = args[0]
txt = args[1]
o = Thing()
try: o.cc = args[2]
except IndexError: o.cc = bot.cc
o.txt = txt
o.rest = ""
o.cmnd = ""
o.args = []
o.is_default = False
o.wanted = Thing()
o.not_wanted = Thing()
o.switch = Thing()
splitted = o.txt.split()
c = 0
for word in splitted:
if not c:
if word[0] == o.cc:
word = word[1:]
if bot.find_cmnd(word): o.cmnd = word
elif o.cc == "none":
if bot.find_cmnd(word): o.cmnd = word
c += 1
continue
if word.startswith("+"):
try: o.karma = int(word)
except ValueError: o.karma = 0
#if "http" in word: o.args.append(word) ; o.rest += " " + word ; continue
try:
key, value = word.split("=")
pre = key[0]
#if pre != "+": raise WrongArgument(word)
#else: key = key[1:]
op = key[-1]
post = value[-1]
last = word[-1]
if key == "i":
try: o.index = int(value)
except: pass
if post == "-": value = value[:-1]
if key.startswith("!"): key = key[1:] ; o.switch[key] = value ; continue
if op == "-": key = key[:-1] ; o.not_wanted[key] = value
else: o.wanted[key] = value
#if op == "-": continue
if post == "-" : continue
o.args.append(key)
#o.rest += " " + key
except ValueError as ex:
o.args.append(word)
o.rest += " " + word
o.rest = o.rest.strip()
return o