def render_template(template_file,data): from htmltmpl import TemplateManager, TemplateProcessor mgr = TemplateManager() template = mgr.prepare(template_file) tproc = TemplateProcessor(global_vars=1) for key in data.keys(): tproc.set(key,data[key]) print tproc.process(template)
def prepareTemplate(self): # tmanager tries to use a compiled (pickle) version of the # .tmpl file if available. The "problem" is that it internally # reads the files in a "normal way". I should use the # TemplateCompiler class in the following way: # # templateCompiler = TemplateCompiler() # return templateCompiler.compileString(dataReceivedFromKamaeliaReader) # # and serialize this class to a tmplc file # tmanager = TemplateManager() return tmanager.prepare(self.getTemplateFileName())
def status_connections(root, which): connections = honeyd.status_connections(which.lower()) if not len(connections): return "There are currently no active %s connections." % which.upper() for connection in connections: id = "%s,%s,%d,%s,%d" % (which.lower(), connection['src'], connection['sport'], connection['dst'], connection['dport']) connection['id'] = urllib.quote(id) template = TemplateManager().prepare(root + "/templates/status_connections.tmpl") tproc = TemplateProcessor(0) tproc.set("title", "Active %s Connections" % which.upper()) tproc.set( "explanation", "This table shows the currently active %s connections" % which.upper()) tproc.set("Connections", connections) content = tproc.process(template) return content
def stats_table(root): raw_stats = honeyd.stats_network() stats = [] # Convert the dictionary into something that the template manager # can understand. for key in raw_stats.keys(): minute = humanize(raw_stats[key][0], '/s') hour = humanize(raw_stats[key][1], '/s') day = humanize(raw_stats[key][2], '/s') stats.append({"name": key, "minute": minute, "hour": hour, "day": day}) template = TemplateManager().prepare(root + "/templates/status_stats.tmpl") tproc = TemplateProcessor(0) tproc.set("title", "Honeyd Statistics") tproc.set("explanation", ("This table shows current statistics collected " "by Honeyd.")) tproc.set("Stats", stats) content = tproc.process(template) return content
def html_template(self,template_file,data={}): """ replaces the variables in the template with values specified in a dictionary. uses the htmltmpl module. see the documentation for htmltmpl for more info. """ from htmltmpl import TemplateManager, TemplateProcessor mgr = TemplateManager() template = mgr.prepare("%s/%s" % (self.template_dir,template_file)) tproc = TemplateProcessor(html_escape=0) for key in self.__globals__.keys(): tproc.set(key,self.__globals__[key]) for key in data.keys(): tproc.set(key,data[key]) return tproc.process(template)
def tmpl(name, sid=None): if not name.endswith(".tmpl"): name = name + ".tmpl" name = os.path.join(tmpldir, name) template = TemplateManager().prepare(name) tproc = TemplateProcessor() if sid: tproc.set('sid', sid) tproc.set('css_path', css_path) return template, tproc
def config_ips(root): ips = honeyd.config_ips() template = TemplateManager().prepare(root + "/templates/config_ip.tmpl") tproc = TemplateProcessor(0) tproc.set("title", "Bound IP addresses") tproc.set( "explanation", "This table shows the IP addresses of the " + "currently configured virtual honeypots.") tproc.set("Ips", ips) content = tproc.process(template) return content
#!/usr/bin/env python import sys import gettext import locale sys.path.insert(0, "../..") from htmltmpl import TemplateManager, TemplateProcessor locale.setlocale(locale.LC_MESSAGES, "en_US") gettext.bindtextdomain("test", "./locale") gettext.textdomain("test") man = TemplateManager(precompile = 0, gettext = 1, debug = 1) tmpl = man.prepare("gettext.tmpl") tproc = TemplateProcessor(debug = 1) tproc.set("title", "Gettext test page") print(tproc.process(tmpl))
def summarize(): global success_counter, failure_counter, blow_counter, skip_counter global cur_name global report_entry_set, map_stats_entry_set print print 'Test Script: %s' % cur_name print 'Succeeded: %d' % success_counter print 'Failed: %d (%d blew exceptions)' \ % (failure_counter+blow_counter, blow_counter) print 'Skipped: %d' % skip_counter print # find whether we are in test root dir or one level up if os.path.exists(os.path.join("..", "SummaryReport.tmpl")): os.chdir("..") elif not os.path.exists("SummaryReport.tmpl"): raise "Could not find HTML template files." # Compile or load already precompiled template. template = TemplateManager().prepare("SummaryReport.tmpl") tproc = TemplateProcessor() # Set the title. mod = om.OpenModeller() tproc.set("om_version", mod.getVersion()) tproc.set("om_num_algs", mod.numAvailableAlgorithms()) # htmltmpl requires var name starting with uppercase (!?!) Entries = report_entry_set tproc.set("Entries", Entries) # get list of algorithms Algorithms = [] algList = mod.availableAlgorithms() for i in range(0, len(algList)): alg = {} alg["class"] = "a" alg["id"] = algList[i].id alg["name"] = algList[i].name Algorithms.append(alg) tproc.set("Algorithms", Algorithms) # write the processed template to disk summary_file = file("index.html", "w") summary_file.write(tproc.process(template)) summary_file.close() # Compile or load already precompiled template. template = TemplateManager().prepare("MapReport.tmpl") tproc = TemplateProcessor() # htmltmpl requires var name starting with uppercase (!?!) Mapstats = map_stats_entry_set tproc.set("Mapstats", Mapstats) # write the processed template to disk map_file = file("MapReport.html", "w") map_file.write(tproc.process(template)) map_file.close()
import honeyd import time import support from htmltmpl import TemplateManager, TemplateProcessor global counter self.send_response(200) self.send_header("Content-Type", "text/html") self.send_nocache() self.end_headers() # Compile or load already precompiled template. template = TemplateManager().prepare(self.root+"/templates/index.tmpl") tproc = TemplateProcessor(0) # Process commands given to us message = support.parse_query(self.query) # Set the title. tproc.set("title", "Honeyd Administration Interface") # Test try: counter += 1 except: counter = 1 greeting = ("Welcome to the Honeyd Administration Interface." "You are visitor %d.<p>") % counter
# 生成文档的title和最新更新时间列表 for file_path, category in article_category.items(): fd = open(file_path) fd.seek(0) title = fd.readline() keyword = fd.readline() uuid = fd.readline() fd.close() article_title[file_path] = title article_keyword[file_path] = keyword article_uuid[file_path] = uuid article_lastmodify[file_path] = "%d" % os.stat(file_path).st_mtime #============ 生成首页 =============# template = TemplateManager().prepare(index_html_template) tproc = TemplateProcessor(html_escape=0) # Create the 'Menuitem' loop. Menuitems = [] for category, cn_name in site_category.items(): if category in article_category.values(): menuitem = {} menuitem["menu_href"] = "/%s/index.html" % (category) menuitem["menu_name"] = cn_name Menuitems.append(menuitem) # 首页显示最新的一篇文章内容 index_article = [] # create new article list Newarticles = []
# Define some constants. DB = "test" USER = "" PASSWD = "" TABLE = """ CREATE TABLE IF NOT EXISTS Products ( id INTEGER NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, CONSTRAINT pkey_id PRIMARY KEY(id) ) """ template = TemplateManager().prepare("products.tmpl") tproc = TemplateProcessor() # Assign a string to template variable named "title". tproc.set("title", "Our products") # Connect the database. Create the table. db = MySQLdb.connect(db=DB, user=USER, passwd=PASSWD, cursorclass=MySQLdb.cursors.DictCursor) create_cur = db.cursor() create_cur.execute(TABLE) create_cur.close() # Insert some data.
from htmltmpl import TemplateManager, TemplateProcessor # Compile or load already precompiled template. template = TemplateManager().prepare("template.tmpl") tproc = TemplateProcessor() # Set the title. tproc.set("title", "Our customers") # Create the 'Customers' loop. customers = [] # First customer. customer = {} customer["name"] = "Joe Sixpack" customer["city"] = "Los Angeles" customer["new"] = 0 customers.append(customer) # Second customer. customer = {} customer["name"] = "Paul Newman" customer["city"] = "New York" customer["new"] = 1 customers.append(customer) tproc.set("Customers", customers) # Print the processed template. print(tproc.process(template))
#!/usr/bin/env python TEST = "compiled" import sys import os sys.path.insert(0, "..") from htmltmpl import TemplateManager, TemplateProcessor man = TemplateManager(precompile = 1, debug = "debug" in sys.argv) template = man.prepare(TEST + ".tmpl") tproc = TemplateProcessor(debug = "debug" in sys.argv) ####################################################### def fill(tproc): tproc.set("title", "Template world.") tproc.set("greeting", "Hello !") tproc.set("Boys", [ { "name" : "Tomas", "age" : 19 }, { "name" : "Pavel", "age" : 34 }, { "name" : "Janek", "age" : 67 }, { "name" : "Martin", "age" : 43 }, { "name" : "Viktor", "age" : 78 }, { "name" : "Marian", "age" : 90 }, { "name" : "Prokop", "age" : 23 }, { "name" : "Honzik", "age" : 46 }, { "name" : "Brudra", "age" : 64 }, { "name" : "Marek", "age" : 54 }, { "name" : "Peter", "age" : 42 },