Tweaked namegen.py a bit
This commit is contained in:
parent
8d840723d4
commit
06f14ee59a
1 changed files with 16 additions and 11 deletions
|
@ -6,11 +6,13 @@ import json, random, re, os
|
||||||
TEMPLATE_RE = re.compile(r"\{(.+?)\}")
|
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 NameGenerator(data["name"], data["templates"],
|
||||||
data["default_templates"], data["parts"])
|
data["default_templates"], data["parts"])
|
||||||
|
|
||||||
|
|
||||||
class NameGenerator(object):
|
class NameGenerator(object):
|
||||||
def __init__(self, name, templates, default_templates, parts):
|
def __init__(self, name, templates, default_templates, parts):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -20,7 +22,7 @@ class NameGenerator(object):
|
||||||
|
|
||||||
def generate_name(self, template=None):
|
def generate_name(self, template=None):
|
||||||
"""
|
"""
|
||||||
Generates one name using the specified templates.
|
Generates one name using the specified templates.
|
||||||
If no templates are specified, use a random template from the default_templates list.
|
If no templates are specified, use a random template from the default_templates list.
|
||||||
"""
|
"""
|
||||||
template = self.templates[template or random.choice(self.default_templates)]
|
template = self.templates[template or random.choice(self.default_templates)]
|
||||||
|
@ -36,14 +38,12 @@ class NameGenerator(object):
|
||||||
|
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
|
||||||
def generate_names(self, amount, template=None):
|
def generate_names(self, amount, template=None):
|
||||||
names = []
|
names = []
|
||||||
for i in xrange(amount):
|
for i in xrange(amount):
|
||||||
names.append(self.generate_name())
|
names.append(self.generate_name())
|
||||||
return names
|
return names
|
||||||
|
|
||||||
|
|
||||||
def get_template(self, template):
|
def get_template(self, template):
|
||||||
return self.templates[template]
|
return self.templates[template]
|
||||||
|
|
||||||
|
@ -53,27 +53,29 @@ 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 and split up the input
|
# clean up the input
|
||||||
args = inp.strip().lower().split()
|
inp = inp.strip().lower()
|
||||||
|
|
||||||
# get a list of available name generators
|
# get a list of available name generators
|
||||||
files = os.listdir(GEN_DIR)
|
files = os.listdir(GEN_DIR)
|
||||||
all_modules = []
|
all_modules = []
|
||||||
for i in files:
|
for i in files:
|
||||||
all_modules.append(os.path.splitext(i)[0])
|
if os.path.splitext(i)[1] == ".json":
|
||||||
|
all_modules.append(os.path.splitext(i)[0])
|
||||||
|
all_modules.sort()
|
||||||
|
|
||||||
# command to return a list of all available generators
|
# command to return a list of all available generators
|
||||||
if args[0] == "list":
|
if inp == "list":
|
||||||
message = "Available generators: "
|
message = "Available generators: "
|
||||||
message += get_text_list(all_modules, 'and')
|
message += get_text_list(all_modules, 'and')
|
||||||
notice(message)
|
notice(message)
|
||||||
return
|
return
|
||||||
|
|
||||||
if inp:
|
if inp:
|
||||||
selected_module = args[0]
|
selected_module = inp.split()[0]
|
||||||
else:
|
else:
|
||||||
# make some generic fantasy names
|
# make some generic fantasy names
|
||||||
selected_module = ["fantasy"]
|
selected_module = "fantasy"
|
||||||
|
|
||||||
# check if the selected module is valid
|
# check if the selected module is valid
|
||||||
if not selected_module in all_modules:
|
if not selected_module in all_modules:
|
||||||
|
@ -81,10 +83,13 @@ def namegen(inp, notice=None):
|
||||||
|
|
||||||
# load the name generator
|
# load the name generator
|
||||||
with open(os.path.join(GEN_DIR, "{}.json".format(selected_module))) as f:
|
with open(os.path.join(GEN_DIR, "{}.json".format(selected_module))) as f:
|
||||||
generator = get_generator(f.read())
|
try:
|
||||||
|
generator = get_generator(f.read())
|
||||||
|
except ValueError as 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_names(10)
|
||||||
|
|
||||||
# and finally return the final message :D
|
# and finally return the final message :D
|
||||||
return "Some names to ponder: %s." % get_text_list(name_list, 'and')
|
return "Some names to ponder: {}.".format(get_text_list(name_list, 'and'))
|
||||||
|
|
Reference in a new issue