This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/namegen.py

88 lines
2.6 KiB
Python
Raw Normal View History

# Plugin by Lukeroge
2012-04-02 18:17:55 +02:00
from util import hook
from util.text import get_text_list
2012-10-11 06:47:27 +02:00
import json, random, re, os
TEMPLATE_RE = re.compile(r"\{(.+?)\}")
GEN_DIR = "./plugins/data/name_files/"
def get_generator(_json):
data = json.loads(_json)
return NameGenerator(data["name"], data["templates"],
data["default_templates"], data["parts"])
class NameGenerator(object):
def __init__(self, name, templates, default_templates, parts):
self.name = name
self.templates = templates
self.default_templates = default_templates
self.parts = parts
def generate_name(self, template=None):
"""
Generates one name using the specified templates.
If no templates are specified, use a random template from the default_templates list.
"""
template = self.templates[template or random.choice(self.default_templates)]
# get a list of all name parts we need
name_parts = TEMPLATE_RE.findall(template)
name = template
for name_part in name_parts:
part = random.choice(self.parts[name_part])
name = name.replace("{%s}" % name_part, part)
return name
def generate_names(self, amount, template=None):
names = []
for i in xrange(amount):
names.append(self.generate_name())
return names
def get_template(self, template):
return self.templates[template]
2011-11-22 02:36:12 +01:00
2012-04-02 18:17:55 +02:00
2012-04-23 11:38:21 +02:00
@hook.command(autohelp=False)
def namegen(inp, notice=None):
2012-10-11 06:47:27 +02:00
"namegen [generator] -- Generates some names using the chosen generator. " \
"'namegen list' will display a list of all generators."
2011-11-22 02:36:12 +01:00
2012-10-11 06:47:27 +02:00
# get a list of available name generators
files = os.listdir(GEN_DIR)
all_modules = []
for i in files:
all_modules.append(os.path.splitext(i)[0])
2011-11-22 02:36:12 +01:00
2012-10-11 06:47:27 +02:00
# command to return a list of all available generators
2011-11-22 02:36:12 +01:00
if inp == "list":
2012-10-11 06:47:27 +02:00
message = "Available generators: "
2012-04-23 11:38:21 +02:00
message += get_text_list(all_modules, 'and')
notice(message)
2011-11-22 02:45:30 +01:00
return
2012-02-02 14:05:11 +01:00
2012-04-23 11:38:21 +02:00
if inp:
2012-10-11 06:47:27 +02:00
selected_module = inp.split(' ')[0]
2012-04-23 11:38:21 +02:00
else:
# make some generic fantasy names
2012-10-11 06:47:27 +02:00
selected_module = ["fantasy"]
2012-10-11 06:47:27 +02:00
# check if the selected module is valid
if not selected_module in all_modules:
return "Invalid name generator :("
2011-11-22 02:36:12 +01:00
2012-10-11 06:47:27 +02:00
# load the name generator
with open(os.path.join(GEN_DIR, "{}.json".format(selected_module))) as f:
generator = get_generator(f.read())
2011-11-22 02:36:12 +01:00
2012-10-11 06:47:27 +02:00
# time to generate some names
name_list = generator.generate_names(10)
2012-04-23 11:38:21 +02:00
# and finally return the final message :D
return "Some names to ponder: %s." % get_text_list(name_list, 'and')