def _on_template_clicked(self, arg): try: with open(".\\templates\\" + self.template_entry.get_text(), 'r') as f: template = f.read() except: md = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "Unable to find specified template.") md.run() md.destroy() else: self.te = TemplateEngine(template) for block in self.te.blocks: exec """self.block_%s_label = gtk.Label('%s') self.txtbox_%s = CustomTextView('%s') self.vbox.pack_start(self.block_%s_label, expand=False) self.vbox.pack_start(self.txtbox_%s, expand=True) self.txtbox_list.append(self.txtbox_%s)""" % (block, block, block, block, block, block, block) in globals(), locals() self.preview_btn = gtk.Button("Preview") self.submit_btn = gtk.Button("Submit email") self.preview_btn.connect("clicked", self._on_preview_clicked) self.submit_btn.connect("clicked", self._on_submit_clicked) self.vbox.pack_start(self.preview_btn, expand=False) self.vbox.pack_start(self.submit_btn, expand=False) self.show_all()
def main(): temp_eng = TemplateEngine(TEMPLATEDIR, DEVICECONFDIR) from_templ_src2, uri2 = temp_eng.process_config('net_route_domains_GET') src_eng2 = SourceEngine(SOURCE_ROOTDIR, from_templ_src2, uri2) src_eng2.integrate() from_templ_src, uri =\ temp_eng.process_config('net_route_domains_route_domain_POST') src_eng = SourceEngine(SOURCE_ROOTDIR, from_templ_src, uri) src_eng.integrate()
def generate_setup(self): """Create the setup script""" template_engine = TemplateEngine() template_context = {} template_context["scripts"] = self.__setup_scripts template_context["images"] = self.__images with open(self.__filename, "w") as script_file: script_file.write( template_engine.render_template("templates/setup/setup.sh.tpl", template_context)) os.chmod( self.__filename, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR | stat.S_IWGRP)
def get(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) else: template_values = { 'user': user.email() } template_engine = TemplateEngine() self.response.out.write(template_engine.render('home.html', template_values))
def do_get(self, request): super().do_get(request) # print("self.path", self.path) # if self.path in ["blog", "blog/", "/", ""]: # self.response = Response(main.protocolVersion, States.OK) # self.response.body = replace_engine.set_top_page(main.DOCUMENT_ROOT + "/blog_top.html") # self.ext = "html" # return self.response head, tail = os.path.split(self.path) print("tail",tail) self.root, self.ext = os.path.splitext(tail) self.ext = self.ext.lstrip(".") print(self.ext) print(bool(self.ext)) if self.ext: normal = NormalController() return normal.do_get(request) if tail and not tail == "blog": print("in article") article = articles.get_articles(tail) if article: engine = TemplateEngine(article, "template") self.response = Response(main.protocolVersion, States.OK) self.response.body = engine.render() self.ext = "html" else: self.response = self.not_found() else: print("in blog_top") self.response = Response(main.protocolVersion, States.OK) self.response.body = replace_engine.set_top_page(os.path.join(main.DOCUMENT_ROOT, "blog_top.html")) self.ext = "html" # return self.response # self.response = Response(main.protocolVersion, States.Not_Found) # self.response.body = os.path.join(main.DOCUMENT_ROOT, "blog.html") # self.ext = "html" return self.response
class Test(unittest.TestCase): def setUp(self): self.engine = TemplateEngine() def test_evaluate_string_with_no_variables(self): string = "Hello" variables = {} evaluated_string = self.engine.evaluate(string, variables) self.assertEqual(evaluated_string, "Hello") def test_evaluate_string_with_single_variable(self): string = "Hello {name}" variables = {'name': 'Ben'} evaluated_string = self.engine.evaluate(string, variables) self.assertEqual(evaluated_string, "Hello Ben") def test_evaluate_string_with_multiple_variables(self): string = "Hello {fname} {sname}" variables = { 'fname': 'Ben', 'sname': 'Nuttall' } evaluated_string = self.engine.evaluate(string, variables) self.assertEqual(evaluated_string, "Hello Ben Nuttall") def test_evaluate_string_with_non_existant_variable(self): string = "Hello {user}" variables = {} method_call = self.engine.evaluate expected_exception = TemplateEngine.MissingValueException self.assertRaises(expected_exception, method_call, string, variables) def test_evaluate_string_with_double_curly_braces_around_variable(self): string = "Hello {{name}}" variables = {'name': 'Ben'} evaluated_string = self.engine.evaluate(string, variables) self.assertEqual(evaluated_string, "Hello {Ben}") def test_evaluate_same_string_twice_with_different_variables(self): string = "Hello {name}" variables = {'name': 'Ben'} evaluated_string_1 = self.engine.evaluate(string, variables) variables = {'name': 'Bill'} evaluated_string_2 = self.engine.evaluate(string, variables) self.assertEqual(evaluated_string_1, "Hello Ben") self.assertEqual(evaluated_string_2, "Hello Bill")
from template_engine import TemplateEngine if __name__ == '__main__': with open('template-responses.txt', 'r') as templates_responses: templates = [] for template in templates_responses.readlines(): templates.append(tuple(template.split('|'))) engine = TemplateEngine(templates) while True: message = input() print(engine.process_question(message))
def setUp(self): self.engine = TemplateEngine()
class CreateInterface(gtk.Window): def __init__(self): super(CreateInterface, self).__init__() self.set_title("Create new email") self.vbox = gtk.VBox(True, 2) self.hbox = gtk.HBox(False, 2) self.template_label = gtk.Label("Template: ") self.template_entry = gtk.Entry() self.template_entry.set_text(default_template) self.template_select = gtk.Button("Select") self.template_select.connect("clicked", self._on_template_clicked) self.hbox.pack_start(self.template_label, expand = False) self.hbox.pack_start(self.template_entry) self.hbox.pack_start(self.template_select, expand = False) self.vbox.pack_start(self.hbox, expand=False) self.hbox_email = gtk.HBox(False,2) self.email_label = gtk.Label("Email: ") self.email = gtk.Entry() self.hbox_email.pack_start(self.email_label, expand=False) self.hbox_email.pack_start(self.email) self.hbox_subject = gtk.HBox(False,2) self.subject_label = gtk.Label("Subject: ") self.subject = gtk.Entry() self.hbox_subject.pack_start(self.subject_label, expand=False) self.hbox_subject.pack_start(self.subject) self.vbox.pack_start(self.hbox_email) self.vbox.pack_start(self.hbox_subject) self.txtbox_list = [] self.add(self.vbox) self.show_all() def _on_template_clicked(self, arg): try: with open(".\\templates\\" + self.template_entry.get_text(), 'r') as f: template = f.read() except: md = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "Unable to find specified template.") md.run() md.destroy() else: self.te = TemplateEngine(template) for block in self.te.blocks: exec """self.block_%s_label = gtk.Label('%s') self.txtbox_%s = CustomTextView('%s') self.vbox.pack_start(self.block_%s_label, expand=False) self.vbox.pack_start(self.txtbox_%s, expand=True) self.txtbox_list.append(self.txtbox_%s)""" % (block, block, block, block, block, block, block) in globals(), locals() self.preview_btn = gtk.Button("Preview") self.submit_btn = gtk.Button("Submit email") self.preview_btn.connect("clicked", self._on_preview_clicked) self.submit_btn.connect("clicked", self._on_submit_clicked) self.vbox.pack_start(self.preview_btn, expand=False) self.vbox.pack_start(self.submit_btn, expand=False) self.show_all() def _on_preview_clicked(self, arg): attr = {} for box in self.txtbox_list: buff = box.get_buffer() start = buff.get_start_iter() end = buff.get_end_iter() attr[box.__name__] = buff.get_text(start, end) text = self.te.replace(**attr) pw = PreviewWindow(text) self.show_all() def _on_submit_clicked(self, arg): attr = {} for box in self.txtbox_list: buff = box.get_buffer() start = buff.get_start_iter() end = buff.get_end_iter() attr[box.__name__] = buff.get_text(start, end) text = self.te.replace(**attr) dbm = DBManager(db_loc) dbm.insert(self.email.get_text(), self.subject.get_text(), text, int(time.time()), self.template_entry.get_text()) self.destroy()
def get(self): template_engine = TemplateEngine() self.response.out.write(template_engine.render('main.html', []))
def get_setup(self): context = {} template_engine = TemplateEngine() setup_data = template_engine.render_template("templates/software/docker-compose-setup.sh.tpl", context) return setup_data
class CreateInterface(gtk.Window): def __init__(self): super(CreateInterface, self).__init__() self.set_title("Create new email") self.vbox = gtk.VBox(True, 2) self.hbox = gtk.HBox(False, 2) self.template_label = gtk.Label("Template: ") self.template_entry = gtk.Entry() self.template_entry.set_text(default_template) self.template_select = gtk.Button("Select") self.template_select.connect("clicked", self._on_template_clicked) self.hbox.pack_start(self.template_label, expand=False) self.hbox.pack_start(self.template_entry) self.hbox.pack_start(self.template_select, expand=False) self.vbox.pack_start(self.hbox, expand=False) self.hbox_email = gtk.HBox(False, 2) self.email_label = gtk.Label("Email: ") self.email = gtk.Entry() self.hbox_email.pack_start(self.email_label, expand=False) self.hbox_email.pack_start(self.email) self.hbox_subject = gtk.HBox(False, 2) self.subject_label = gtk.Label("Subject: ") self.subject = gtk.Entry() self.hbox_subject.pack_start(self.subject_label, expand=False) self.hbox_subject.pack_start(self.subject) self.vbox.pack_start(self.hbox_email) self.vbox.pack_start(self.hbox_subject) self.txtbox_list = [] self.add(self.vbox) self.show_all() def _on_template_clicked(self, arg): try: with open(".\\templates\\" + self.template_entry.get_text(), 'r') as f: template = f.read() except: md = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "Unable to find specified template.") md.run() md.destroy() else: self.te = TemplateEngine(template) for block in self.te.blocks: exec """self.block_%s_label = gtk.Label('%s') self.txtbox_%s = CustomTextView('%s') self.vbox.pack_start(self.block_%s_label, expand=False) self.vbox.pack_start(self.txtbox_%s, expand=True) self.txtbox_list.append(self.txtbox_%s)""" % (block, block, block, block, block, block, block) in globals(), locals() self.preview_btn = gtk.Button("Preview") self.submit_btn = gtk.Button("Submit email") self.preview_btn.connect("clicked", self._on_preview_clicked) self.submit_btn.connect("clicked", self._on_submit_clicked) self.vbox.pack_start(self.preview_btn, expand=False) self.vbox.pack_start(self.submit_btn, expand=False) self.show_all() def _on_preview_clicked(self, arg): attr = {} for box in self.txtbox_list: buff = box.get_buffer() start = buff.get_start_iter() end = buff.get_end_iter() attr[box.__name__] = buff.get_text(start, end) text = self.te.replace(**attr) pw = PreviewWindow(text) self.show_all() def _on_submit_clicked(self, arg): attr = {} for box in self.txtbox_list: buff = box.get_buffer() start = buff.get_start_iter() end = buff.get_end_iter() attr[box.__name__] = buff.get_text(start, end) text = self.te.replace(**attr) dbm = DBManager(db_loc) dbm.insert(self.email.get_text(), self.subject.get_text(), text, int(time.time()), self.template_entry.get_text()) self.destroy()