Moved generation logic out of namegen.py
This commit is contained in:
parent
8e545b3a31
commit
15e7825125
2 changed files with 47 additions and 47 deletions
|
@ -1,59 +1,21 @@
|
||||||
# Plugin by Lukeroge
|
# Plugin by Lukeroge
|
||||||
import json
|
from util import hook, text, textgen
|
||||||
import random
|
import json, os
|
||||||
import re
|
|
||||||
import os
|
|
||||||
from util import hook
|
|
||||||
from util.text import get_text_list
|
|
||||||
|
|
||||||
|
|
||||||
TEMPLATE_RE = re.compile(r"\{(.+?)\}")
|
|
||||||
GEN_DIR = "./plugins/data/name_files/"
|
GEN_DIR = "./plugins/data/name_files/"
|
||||||
|
|
||||||
|
|
||||||
def get_generator(_json):
|
def get_generator(_json):
|
||||||
data = json.loads(_json)
|
data = json.loads(_json)
|
||||||
return NameGenerator(data["name"], data["templates"],
|
return textgen.TextGenerator(data["name"], data["templates"],
|
||||||
data["default_templates"], data["parts"])
|
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.
|
|
||||||
"""
|
|
||||||
name = self.templates[template or random.choice(self.default_templates)]
|
|
||||||
|
|
||||||
# get a list of all name parts we need
|
|
||||||
name_parts = TEMPLATE_RE.findall(name)
|
|
||||||
|
|
||||||
for name_part in name_parts:
|
|
||||||
part = random.choice(self.parts[name_part])
|
|
||||||
name = name.replace("{" + name_part + "}", part)
|
|
||||||
|
|
||||||
return name
|
|
||||||
|
|
||||||
def generate_names(self, amount):
|
|
||||||
names = []
|
|
||||||
for i in xrange(amount):
|
|
||||||
names.append(self.generate_name())
|
|
||||||
return names
|
|
||||||
|
|
||||||
def get_template(self, template):
|
|
||||||
return self.templates[template]
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command(autohelp=False)
|
@hook.command(autohelp=False)
|
||||||
def namegen(inp, notice=None):
|
def namegen(inp, notice=None):
|
||||||
"""namegen [generator] -- Generates some names using the chosen generator.
|
"namegen [generator] -- Generates some names using the chosen generator. " \
|
||||||
'namegen list' will display a list of all generators."""
|
"'namegen list' will display a list of all generators."
|
||||||
|
|
||||||
# clean up the input
|
# clean up the input
|
||||||
inp = inp.strip().lower()
|
inp = inp.strip().lower()
|
||||||
|
@ -69,7 +31,7 @@ def namegen(inp, notice=None):
|
||||||
# command to return a list of all available generators
|
# command to return a list of all available generators
|
||||||
if inp == "list":
|
if inp == "list":
|
||||||
message = "Available generators: "
|
message = "Available generators: "
|
||||||
message += get_text_list(all_modules, 'and')
|
message += text.get_text_list(all_modules, 'and')
|
||||||
notice(message)
|
notice(message)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -91,7 +53,7 @@ def namegen(inp, notice=None):
|
||||||
return "Unable to read name file: {}".format(error)
|
return "Unable to read name file: {}".format(error)
|
||||||
|
|
||||||
# time to generate some names
|
# time to generate some names
|
||||||
name_list = generator.generate_names(10)
|
name_list = generator.generate_strings(10)
|
||||||
|
|
||||||
# and finally return the final message :D
|
# and finally return the final message :D
|
||||||
return "Some names to ponder: {}.".format(get_text_list(name_list, 'and'))
|
return "Some names to ponder: {}.".format(text.get_text_list(name_list, 'and'))
|
||||||
|
|
38
plugins/util/textgen.py
Normal file
38
plugins/util/textgen.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import re
|
||||||
|
import random
|
||||||
|
|
||||||
|
TEMPLATE_RE = re.compile(r"\{(.+?)\}")
|
||||||
|
|
||||||
|
|
||||||
|
class TextGenerator(object):
|
||||||
|
def __init__(self, name, templates, default_templates, parts, variables=None):
|
||||||
|
self.name = name
|
||||||
|
self.templates = templates
|
||||||
|
self.default_templates = default_templates
|
||||||
|
self.parts = parts
|
||||||
|
self.variables = variables
|
||||||
|
|
||||||
|
def generate_string(self, template=None):
|
||||||
|
"""
|
||||||
|
Generates one string using the specified templates.
|
||||||
|
If no templates are specified, use a random template from the default_templates list.
|
||||||
|
"""
|
||||||
|
text = self.templates[template or random.choice(self.default_templates)]
|
||||||
|
|
||||||
|
# get a list of all text parts we need
|
||||||
|
required_parts = TEMPLATE_RE.findall(text)
|
||||||
|
|
||||||
|
for required_part in required_parts:
|
||||||
|
part = random.choice(self.parts[required_part])
|
||||||
|
text = text.replace("{%s}" % required_part, part)
|
||||||
|
|
||||||
|
return text
|
||||||
|
|
||||||
|
def generate_strings(self, amount, template=None):
|
||||||
|
strings = []
|
||||||
|
for i in xrange(amount):
|
||||||
|
strings.append(self.generate_string())
|
||||||
|
return strings
|
||||||
|
|
||||||
|
def get_template(self, template):
|
||||||
|
return self.templates[template]
|
Reference in a new issue