First draft of generate command

This commit is contained in:
Robert Miles 2019-11-03 13:20:33 -05:00
parent 672e0ec5d8
commit dd9a4b68a7
2 changed files with 48 additions and 1 deletions

37
commands/generate.py Normal file
View File

@ -0,0 +1,37 @@
import plugin, utils, tracery, titlecase
from tracery.modifiers import base_english
NAME = re.compile(r'\$([^\s]+)')
SUB = r"#\1#"
replace_name = lambda x: NAME.sub(SUB,x)
GENERATORS = [
"book_fantasy",
"book_horror",
"book_hp",
"book",
"book_rom",
"book_sf",
"char",
"idea",
"land",
"place"
]
GENERATOR_URL = "https://github.com/cwarwicker/discord-WriterBot/raw/master/assets/json/en/gen_{}.json"
def get_generator(name):
generator = utils.get(GENERATOR_URL.format(name))
out = dict()
out.update(generator["names"])
out["origin"] = [replace_name(x) for x in generator["formats"]]
ret = tracery.Grammar(out)
ret.add_modifiers(base_english)
return (lambda: ret.flatten("#origin#"))
GENERATORS = utils.LazyLoadedDict(get_generator)
@plugin.command("generate")
def generate(bot,channel,nick,generator):
res = GENERATORS[generator]()
if generator!="idea":
res = titlecase.titlecase(res)
bot.say(channel,"{}: {}".format(nick,res))

View File

@ -1,4 +1,4 @@
import plugin, subprocess, requests
import plugin, subprocess, requests, collections
def register_command_call(command,cli,use_args=False,verify_args=lambda args: args):
@plugin.command(command)
@ -12,3 +12,13 @@ def get(*args,**kwargs):
r = requests.get(*args,**kwargs)
r.raise_for_status()
return r.json()
class LazyLoadedDict(collections.UserDict):
def __init__(self,func,initialdata=dict()):
super().__init__(initialdata)
self.func = func
def __getitem__(self,key):
if key not in self.data:
self.data[key]=self.func(key)
return self.data[key]
return self.data[key]