From fa56e91b00ab576228a31ab55c68fdb813d4ca07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke?= Date: Sat, 29 Dec 2018 02:48:22 +0100 Subject: [PATCH] Minor update to dialog (#1911) * Do not load blank lines as dialogs. * Simplify render code slightly using random.choice * Remove import of io's open, not needed in python 3 --- mycroft/dialog/__init__.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/mycroft/dialog/__init__.py b/mycroft/dialog/__init__.py index 2506a0e59a..0be3ae3490 100644 --- a/mycroft/dialog/__init__.py +++ b/mycroft/dialog/__init__.py @@ -13,8 +13,6 @@ # limitations under the License. # import random -from io import open - import os import re from pathlib import Path @@ -48,8 +46,9 @@ class MustacheDialogRenderer(object): with open(filename, 'r') as f: for line in f: template_text = line.strip() - # Skip all lines starting with '#' - if not template_text.startswith('#'): + # Skip all lines starting with '#' and all empty lines + if (not template_text.startswith('#') and + template_text != ''): if template_name not in self.templates: self.templates[template_name] = [] @@ -86,10 +85,10 @@ class MustacheDialogRenderer(object): template_functions = self.templates.get(template_name) if index is None: - index = random.randrange(len(template_functions)) + line = random.choice(template_functions) else: - index %= len(template_functions) - line = template_functions[index] + line = template_functions[index % len(template_functions)] + # Replace {key} in line with matching values from context line = line.format(**context) return line