made slap use the new TextGenerator class

This commit is contained in:
Luke Rogers 2013-09-12 22:46:43 +12:00
parent 15e7825125
commit 2be0c46b89
6 changed files with 93 additions and 75 deletions

View file

@ -5,19 +5,26 @@ TEMPLATE_RE = re.compile(r"\{(.+?)\}")
class TextGenerator(object):
def __init__(self, name, templates, default_templates, parts, variables=None):
self.name = name
def __init__(self, templates, parts, default_templates=None, variables=None):
self.templates = templates
self.default_templates = default_templates
self.parts = parts
self.variables = variables
print self.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)]
if self.default_templates:
text = self.templates[template or random.choice(self.default_templates)]
else:
text = random.choice(self.templates)
if self.variables:
for key, value in self.variables.items():
text = text.replace("{%s}" % key, value)
# get a list of all text parts we need
required_parts = TEMPLATE_RE.findall(text)