def getPageNear(): conf = config.loadConfig("../../conf/example_config.json") dataImpl = inject.inject.getDataImpl() sessionImpl = inject.inject.getSessionImpl() user = dataImpl.getPerson( 1) # todo: change with sessionImpl.getSession(cookie).getId() user_households = map(lambda x: dataImpl.getHousehold(x), dataImpl.getHouseholdIdsFromPerson(1)) user_household = user_households[0] households = dataImpl.getHouseholds() sorted_households = orderByDistance(user_household, households, 10) sorted_household_residents = [] sorted_household_distances = [] for i in range(0, len(sorted_households)): sorted_household_residents.append( map( lambda x: dataImpl.getPerson(x), dataImpl.getPersonIdsFromHousehold( int(sorted_households[i]["id"])))) sorted_household_distances.append("{0:.2f}".format( lonLatDistance(float(sorted_households[i]["latitude"]), float(sorted_households[i]["longitude"]), float(user_household["latitude"]), float(user_household["longitude"])))) carpools = [] for i in range(0, len(sorted_households)): carpools.append((sorted_households[i], sorted_household_residents[i], sorted_household_distances[i])) t = Templite(filename=path.join(conf["projectRoot"], "src", "html", "near.templite")) return t.render(user=user, user_households=user_households, user_household_selected=0, carpools=carpools, num_results=10)
def m(username): f = open("home.tpl", "r") html = str(f.read()) f.close() print "Content-Type: text/html\r\n\r\n" t = Templite(html) print t.render( username=username, login_link=LINK_HEADERS.LOGIN_LINK, home_link=LINK_HEADERS.HOME_LINK, transaction_link=LINK_HEADERS.TRANSACTION_LINK, deposit_link=LINK_HEADERS.DEPOSIT_LINK, upload_link=LINK_HEADERS.UPLOAD_LINK, dropdown_link=LINK_HEADERS.DROPDOWN_LINK, portfolio_link=LINK_HEADERS.PORTFOLIO_LINK, active_stocks_percentchange_link=LINK_HEADERS. ACTIVE_STOCKS_PERCENTCHANGE_LINK, create_user_link=LINK_HEADERS.CREATE_USER_LINK, update_user_link=LINK_HEADERS.UPDATE_USER_LINK, active_stocks_volumechange_link=LINK_HEADERS. ACTIVE_STOCKS_VOLUMECHANGE_LINK, update_password_link=LINK_HEADERS.UPDATE_PASSWORD_LINK, stock_symbol_link=LINK_HEADERS.STOCK_LINK, display_algorithms_link=LINK_HEADERS.DISPLAY_ALGORITHMS_LINK, execute_algo_link=LINK_HEADERS.EXECUTE_ALGORITHMS_LINK, get_active_algorithms_link=LINK_HEADERS.GET_ACTIVE_ALGORITHMS_LINK, filter_link=LINK_HEADERS.FILTER_DROPDOWN_LINK, algorithm_graph_link=LINK_HEADERS.ALGORITHM_GRAPH_LINK, algorithm_graph_dropdown_link=LINK_HEADERS. ALGORITHM_GRAPH_DROPDOWN_LINK, trade_info_script_link=LINK_HEADERS.TRADE_INFO_SCRIPT_LINK, algorithm_profit_graph_link=LINK_HEADERS.ALGORITHM_PROFIT_GRAPH_LINK)
def m(username): f = open("home.tpl", "r") html = str(f.read()) f.close() print "Content-Type: text/html\r\n\r\n" t = Templite(html) print t.render( username=username, login_link=LINK_HEADERS.LOGIN_LINK, home_link=LINK_HEADERS.HOME_LINK, transaction_link=LINK_HEADERS.TRANSACTION_LINK, deposit_link=LINK_HEADERS.DEPOSIT_LINK, upload_link=LINK_HEADERS.UPLOAD_LINK, dropdown_link=LINK_HEADERS.DROPDOWN_LINK, portfolio_link=LINK_HEADERS.PORTFOLIO_LINK, active_stocks_percentchange_link=LINK_HEADERS.ACTIVE_STOCKS_PERCENTCHANGE_LINK, create_user_link=LINK_HEADERS.CREATE_USER_LINK, update_user_link=LINK_HEADERS.UPDATE_USER_LINK, active_stocks_volumechange_link=LINK_HEADERS.ACTIVE_STOCKS_VOLUMECHANGE_LINK, update_password_link=LINK_HEADERS.UPDATE_PASSWORD_LINK, stock_symbol_link=LINK_HEADERS.STOCK_LINK, display_algorithms_link=LINK_HEADERS.DISPLAY_ALGORITHMS_LINK, execute_algo_link=LINK_HEADERS.EXECUTE_ALGORITHMS_LINK, get_active_algorithms_link=LINK_HEADERS.GET_ACTIVE_ALGORITHMS_LINK, filter_link=LINK_HEADERS.FILTER_DROPDOWN_LINK, algorithm_graph_link=LINK_HEADERS.ALGORITHM_GRAPH_LINK, algorithm_graph_dropdown_link=LINK_HEADERS.ALGORITHM_GRAPH_DROPDOWN_LINK, trade_info_script_link=LINK_HEADERS.TRADE_INFO_SCRIPT_LINK, algorithm_profit_graph_link=LINK_HEADERS.ALGORITHM_PROFIT_GRAPH_LINK, )
def generate(argv): if len(argv) != 3: print('Usage: ./generate.py [seed] [output_file]') sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) description = '' with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('unicode_escape') template = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), '14_angr_shared_library_so.c.templite'), 'r').read() c_code = Templite(template).render(description=description) with tempfile.NamedTemporaryFile(delete=False, suffix='.c', mode='w') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -I' + os.path.dirname(os.path.realpath(__file__)) + ' -fno-stack-protector -fpic -m32 -c -o 14_angr_shared_library.o ' + temp.name) os.system('gcc -shared -m32 -o ' + os.path.join('/'.join(output_file.split('/')[0:-1]), 'lib' + output_file.split('/')[-1] + '.so') + ' 14_angr_shared_library.o') os.system('rm 14_angr_shared_library.o') os.system('chmod -x ' + os.path.join('/'.join(output_file.split('/')[0:-1]), 'lib' + output_file.split('/')[-1] + '.so')) template = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), '14_angr_shared_library.c.templite'), 'r').read() c_code = Templite(template).render() with tempfile.NamedTemporaryFile(delete=False, suffix='.c', mode='w') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -m32 -I . -L ' + '/'.join(output_file.split('/')[0:-1]) + ' -o ' + output_file + ' ' + temp.name + ' -l' + output_file.split('/')[-1])
def index(): """Creates a OPDS catalog from the book database""" template = Templite(TPL) books = [] models.initDB() for book in models.Book.query.order_by("title").all(): epub_fn = None pdf_fn = None mobi_fn = None exts = book.available_formats() print(("EXTS:", exts)) if '.epub' in exts: epub_fn = "/book/%s.epub"%book.id if '.mobi' in exts: epub_fn = "/book/%s.mobi"%book.id if '.pdf' in exts: epub_fn = "/book/%s.pdf"%book.id books.append([ book.title, book.id, ','.join([a.name for a in book.authors]), book.comments, epub_fn, pdf_fn, mobi_fn, ]) response.content_type='application/atom+xml' return template.render(books = books)
def test_passthrough(self): # Strings without variables are passed through unchanged. self.assertEqual(Templite("Hello").render(), "Hello") self.assertEqual( Templite("Hello, 20% fun time!").render(), "Hello, 20% fun time!" )
def test_reusability(self): # A single Templite can be used more than once with different data. globs = { 'upper': lambda x: x.upper(), 'punct': '!', } template = Templite("This is {{name|upper}}{{punct}}", globs) self.assertEqual(template.render({'name':'Ned'}), "This is NED!") self.assertEqual(template.render({'name':'Ben'}), "This is BEN!")
def test_reusability(self): # A single Templite can be used more than once with different data. globs = { 'upper': lambda x: x.upper(), 'punct': '!', } template = Templite("This is {{name|upper}}{{punct}}", globs) self.assertEqual(template.render({'name': 'Ned'}), "This is NED!") self.assertEqual(template.render({'name': 'Ben'}), "This is BEN!")
def load_data(self, book_id): self.book = models.Book.get_by(id=book_id) if not self.book: # Called with invalid book ID print("Wrong book ID") tplfile = codecs.open(os.path.join(os.path.dirname(__file__), 'templates', 'about_the_book.tmpl'), 'r', 'utf-8') self.template = Templite(tplfile.read()) tplfile.close() t1 = time.time() html = self.template.render( title=self.book.title, authors=self.book.authors, identifiers=[[identifier.key, identifier.value] for identifier in self.book.identifiers], files=[fname.file_name for fname in self.book.files], tags=[tag.name for tag in self.book.tags], thumb=QtCore.QUrl.fromLocalFile(self.book.cover()).toString(), quotes=['Sample Quote #1', 'Sample Quote #2'], ) print(("Rendered in: %s seconds" % (time.time() - t1))) self.about_web_view.page().mainFrame().setHtml(html)
def generate(argv): if len(argv) != 3: print('Usage: ./generate.py [seed] [output_file]') sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) rodata_tail_modifier = 0x14 rodata_parts = ''.join([ chr(random.randint(ord('A'), ord('Z'))) for _ in range(3) ] + [ chr(random.randint(ord('A') - rodata_tail_modifier, ord('Z') - rodata_tail_modifier)) ]) rodata_address = '0x' + binascii.hexlify(rodata_parts.encode('utf8')).decode('utf8') description = '' with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('unicode_escape') template = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), '15_angr_arbitrary_read.c.templite'), 'r').read() c_code = Templite(template).render(description=description) with tempfile.NamedTemporaryFile(delete=False, suffix='.c', mode='w') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -m32 -fno-stack-protector -Wl,--section-start=.rodata=' + rodata_address + ' -o ' + output_file + ' ' + temp.name)
def generate(argv): if len(argv) != 3: print 'Usage: pypy generate.py [seed] [output_file]' sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) rodata_tail_modifier = 0x2e rodata_parts = ''.join([ chr(random.randint(ord('A'), ord('Z'))) for _ in xrange(3) ] + [ chr(random.randint(ord('A') - rodata_tail_modifier, ord('Z') - rodata_tail_modifier)) ]) rodata_address = '0x' + rodata_parts.encode('hex') userdef_charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' userdef = ''.join(random.choice(userdef_charset) for _ in range(8)) description = '' with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('string_escape').replace('\"', '\\\"') template = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), '16_angr_arbitrary_write.c.templite'), 'r').read() c_code = Templite(template).render(description=description, userdef=userdef) with tempfile.NamedTemporaryFile(delete=False, suffix='.c') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -m32 -fno-stack-protector -Wl,--section-start=.data=' + rodata_address + ' -o ' + output_file + ' ' + temp.name)
def generate(argv): if len(argv) != 3: print('Usage: ./generate.py [seed] [output_file]') sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) description = '' with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('unicode_escape') padding0 = random.randint(0, 2**26) padding1 = random.randint(0, 2**26) template = open( os.path.join(os.path.dirname(os.path.realpath(__file__)), '05_angr_symbolic_memory.c.templite'), 'r').read() c_code = Templite(template).render(description=description, padding0=padding0, padding1=padding1) with tempfile.NamedTemporaryFile(delete=False, suffix='.c', mode='w') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -m32 -o ' + output_file + ' ' + temp.name)
def generate(argv): if len(argv) != 3: print 'Usage: pypy generate.py [seed] [output_file]' sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) text_tail_modifier0 = 0x10 text_tail_modifier1 = 0x01 text_parts = ''.join([ chr(random.randint(ord('A'), ord('Z'))) for _ in xrange(2) ] + [ chr(random.randint(ord('A') - text_tail_modifier1, ord('Z') - text_tail_modifier1)) ] + [ chr(random.randint(ord('A') - text_tail_modifier0, ord('Z') - text_tail_modifier0)) ]) text_address = '0x' + text_parts.encode('hex') description = '' with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('string_escape').replace('\"', '\\\"') template = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), '17_angr_arbitrary_jump.c.templite'), 'r').read() c_code = Templite(template).render(description=description) with tempfile.NamedTemporaryFile(delete=False, suffix='.c') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -fno-stack-protector -Wl,--section-start=.text=' + text_address + ' -m32 -o ' + output_file + ' ' + temp.name)
def render(self): """ Render markdown documents to HTML page containing "timeline" from template. :return: HTML timeline """ article_list = [] # List containing all article information for md_article in self.markdown: html_doc = self.converter.convert(md_article) # Convert markdown article to HTML article_dict = { "title": self.converter.Meta['title'], # Metadata got from markdown "author": self.converter.Meta['author'], "url": "%s.html" %os.path.splitext(os.path.basename(self.converter.Meta['filename'][0]))[0], "content": html_doc # HTML article } article_list.append(article_dict) render_dict = { # Set data for template "lang": self.setting_access.get_blog_lang, "blog_name": self.setting_access.get_blog_name, "description": self.setting_access.get_blog_description, "article_list": article_list # List of article data } return str(Templite(self.template).render(**render_dict))
def generate(argv): if len(argv) != 3: print 'Usage: pypy generate.py [seed] [output_file]' sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) description = '' with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('string_escape').replace( '\"', '\\\"') template = open( os.path.join(os.path.dirname(os.path.realpath(__file__)), '02_angr_find_condition.c.templite'), 'r').read() c_code = Templite(template).render(description=description) with tempfile.NamedTemporaryFile(delete=False, suffix='.c') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -m32 -o ' + output_file + ' ' + temp.name)
def generate(argv): if len(argv) != 3: print('Usage: ./generate.py [seed] [output_file]') sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) description = '' with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('unicode_escape') template = open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'xx_angr_segfault.c.templite'), 'r').read() c_code = Templite(template).render(description=description) with tempfile.NamedTemporaryFile(delete=False, suffix='.c', mode='w') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -fno-pie -no-pie -m32 -fno-stack-protector -o ' + output_file + ' ' + temp.name)
def generate(argv): if len(argv) != 3: print('Usage: ./generate.py [seed] [output_file]') sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) description = '' with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('unicode_escape') userdef_charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' userdef1 = ''.join(random.choice(userdef_charset) for _ in range(8)) template = open( os.path.join(os.path.dirname(os.path.realpath(__file__)), '07_angr_symbolic_file.c.templite'), 'r').read() c_code = Templite(template).render(description=description, userdef1=userdef1) with tempfile.NamedTemporaryFile(delete=False, suffix='.c', mode='w') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -m32 -o ' + output_file + ' ' + temp.name)
def generate(argv): if len(argv) != 3: print('Usage: ./generate.py [seed] [output_file]') sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) description = '' with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('unicode_escape') random_list = [random.choice([True, False]) for _ in range(64)] template = open( os.path.join(os.path.dirname(os.path.realpath(__file__)), '01_angr_avoid.c.templite'), 'r').read() c_code = Templite(template).render(description=description, random_list=random_list) with tempfile.NamedTemporaryFile(delete=False, suffix='.c', mode='w') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -m32 -o ' + output_file + ' ' + temp.name)
def generate(argv): if len(argv) != 3: print 'Usage: pypy generate.py [seed] [output_file]' sys.exit() seed = argv[1] output_file = argv[2] random.seed(seed) description = '' with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.txt'), 'r') as desc_file: description = desc_file.read().encode('string_escape').replace( '\"', '\\\"') userdef_charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' userdef = ''.join([random.choice(userdef_charset) for _ in range(16)]) template = open( os.path.join(os.path.dirname(os.path.realpath(__file__)), '10_angr_simprocedures.c.templite'), 'r').read() c_code = Templite(template).render(description=description, userdef=userdef) with tempfile.NamedTemporaryFile(delete=False, suffix='.c') as temp: temp.write(c_code) temp.seek(0) os.system('gcc -m32 -o ' + output_file + ' ' + temp.name)
class HtmlReporter(object): """HTML reporting.""" def __init__(self): #TO-DO: Handle when template not available self.source_tmpl = Templite(open("htmlfiles/pyfile.html").read(), globals()) def html_file(self, pyfile, modname, linecounts, flowarray, plottype, imagetype='svg'): """Generate a HTML file for one source file. """ source = open(pyfile).read() htmldir = 'htmlfiles' lines = [] plotvalues = [] for lineno, line in enumerate(source_token_lines(source)): lineno += 1 line_class = [] annotate_html = "" annotate_title = "" if lineno in linecounts: linecount = linecounts[lineno] else: linecount = 0 plotvalues.append(linecount) # Build the HTML for the line html = [] for tok_type, tok_text in line: if tok_type == "ws": html.append(escape(tok_text)) else: tok_html = escape(tok_text) or ' ' html.append( "<span class='%s'>%s</span>" % (tok_type, tok_html) ) lines.append({ 'html': ''.join(html), 'number': lineno, 'count': linecount, 'class': ' '.join(line_class) or "pln", 'annotate': annotate_html, 'annotate_title': annotate_title, }) imagefile = modname + '.' + imagetype plotLineCounts(plotvalues,os.path.join(htmldir, imagefile),lineno, imagetype,plottype) algflow = flowarray # Write the HTML page for this file. html_filename = os.path.join(htmldir, os.path.splitext(os.path.basename(pyfile))[0] + ".html") html = spaceless(self.source_tmpl.render(locals())) fhtml = open(html_filename, 'w') fhtml.write(html) fhtml.close() return html_filename
def index_file(self): """Write the index.html file for this report.""" index_tmpl = Templite(data("htmlfiles/index.html"), globals()) files = self.files total_stm = sum([f['stm'] for f in files]) total_run = sum([f['run'] for f in files]) total_exc = sum([f['exc'] for f in files]) if total_stm: total_cov = 100.0 * total_run / total_stm else: total_cov = 100.0 fhtml = open(os.path.join(self.directory, "index.html"), "w") fhtml.write(index_tmpl.render(locals())) fhtml.close()
def try_render(self, text, ctx=None, result=None): """Render `text` through `ctx`, and it had better be `result`. Result defaults to None so we can shorten the calls where we expect an exception and never get to the result comparison. """ actual = Templite(text).render(ctx or {}) if result: self.assertEqual(actual, result)
def setWidget (self, widget): tplfile = os.path.join( os.path.abspath( os.path.dirname(__file__)),'category.tmpl') tplfile = codecs.open(tplfile,'r','utf-8') self.template = Templite(tplfile.read()) tplfile.close() self.widget = widget
def _write_pac(self): files = os.listdir('templates') templates = [] for file in files: if file.endswith('.tpl.pac'): templates.append(file) dir = self._config['pac-directory'].strip('\'"') if not dir.endswith(os.sep): dir = dir + os.sep if not os.path.exists(dir): os.makedirs(dir) self._logger.info("directory `%s` does not exist, create it", dir) for template_file in templates: template = self._fetch("templates"+os.sep+template_file).replace("\r\n", "\n") t = Templite(template) path = dir + template_file.replace('.tpl', '') open(path, 'w').write(t.render(self._vars).replace("\\\n","")) self._logger.info("write pac file `%s` successfully", path)
def getPageNear(): conf = config.loadConfig("../../conf/example_config.json") dataImpl = inject.inject.getDataImpl() sessionImpl = inject.inject.getSessionImpl() user = dataImpl.getPerson(1) # todo: change with sessionImpl.getSession(cookie).getId() user_households = map(lambda x: dataImpl.getHousehold(x), dataImpl.getHouseholdIdsFromPerson(1)) user_household = user_households[0] households = dataImpl.getHouseholds() sorted_households = orderByDistance(user_household, households, 10) sorted_household_residents = [] sorted_household_distances = [] for i in range(0, len(sorted_households)): sorted_household_residents.append(map(lambda x: dataImpl.getPerson(x), dataImpl.getPersonIdsFromHousehold(int(sorted_households[i]["id"])))) sorted_household_distances.append("{0:.2f}".format(lonLatDistance(float(sorted_households[i]["latitude"]), float(sorted_households[i]["longitude"]), float(user_household["latitude"]), float(user_household["longitude"])))) carpools = [] for i in range(0, len(sorted_households)): carpools.append((sorted_households[i], sorted_household_residents[i], sorted_household_distances[i])) t = Templite(filename=path.join(conf["projectRoot"], "src", "html", "near.templite")) return t.render(user=user, user_households=user_households, user_household_selected=0, carpools=carpools, num_results=10)
class AboutBook(QtGui.QWidget): def __init__(self, book_id=None, *args): QtGui.QWidget.__init__(self, *args) uifile = ui.path('about_book.ui') uic.loadUi(uifile, self) self.ui = self self.about_web_view = WebView() self.verticalLayout.insertWidget(0, self.about_web_view) self.about_web_view.show() self.about_web_view.settings().setAttribute( QtWebKit.QWebSettings.JavascriptEnabled, True) self.about_web_view.settings().setAttribute( QtWebKit.QWebSettings.JavascriptCanOpenWindows, True) self.about_web_view.settings().setAttribute( QtWebKit.QWebSettings.JavascriptCanAccessClipboard, True) self.about_web_view.page().setLinkDelegationPolicy( self.about_web_view.page().DelegateAllLinks) # self.about_web_view.linkClicked.connect(self.openLink) # StyleSheet #self.about_web_view.settings().setUserStyleSheetUrl( #QtCore.QUrl.fromLocalFile( #os.path.join(os.path.dirname(__file__), #'about_book.css'))) if book_id is not None: self.load_data(book_id) def load_data(self, book_id): self.book = models.Book.get_by(id=book_id) if not self.book: # Called with invalid book ID print("Wrong book ID") tplfile = codecs.open(os.path.join(os.path.dirname(__file__), 'templates', 'about_the_book.tmpl'), 'r', 'utf-8') self.template = Templite(tplfile.read()) tplfile.close() t1 = time.time() html = self.template.render( title=self.book.title, authors=self.book.authors, identifiers=[[identifier.key, identifier.value] for identifier in self.book.identifiers], files=[fname.file_name for fname in self.book.files], tags=[tag.name for tag in self.book.tags], thumb=QtCore.QUrl.fromLocalFile(self.book.cover()).toString(), quotes=['Sample Quote #1', 'Sample Quote #2'], ) print(("Rendered in: %s seconds" % (time.time() - t1))) self.about_web_view.page().mainFrame().setHtml(html)
def try_render(self, text, ctx=None, result=None): """Render `text` through `ctx`, and it had better be `result`. Result defaults to None so we can shorten the calls where we expect an exception and never get to the result comparison. """ actual = Templite(text).render(ctx or {}) # If result is None, then an exception should have prevented us getting # to here. assert result is not None self.assertEqual(actual, result)
def on_export(self, event): global export_dir, outputs for output, contents in outputs.items(): print("saving", output) dlg = wx.FileDialog( self, message="Choose a file", defaultDir=export_dir, defaultFile=output, style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT ) if dlg.ShowModal() == wx.ID_OK: export_dir = os.path.dirname(dlg.GetPath()) filename = dlg.GetPath() template = Templite(contents) def cbool(bool): return str(bool).lower() def comment(bool): return "" if bool else "//" with open(filename, "w") as fh: fh.write(template.render(vstore.instance, cbool=cbool, comment=comment))
def render(self): """ Render markdown document to HTML page from template :return: HTML article """ html_doc = self.converter.convert(self.markdown) # Convert markdown article to HTML render_dict = { # Set data for template "lang": self.setting_access.get_blog_lang, "blog_name": self.setting_access.get_blog_name, "description": self.setting_access.get_blog_description, "article_title": self.converter.Meta['title'], # Metadata got from markdown article "article_author": self.converter.Meta['author'], "article": html_doc # HTML article } return str(Templite(self.template).render(**render_dict))
# -*- coding: utf-8 -*- from templite import Templite if __name__ == '__main__': template = '''\ <h1>Hello {{name|upper}}!</h1> {% for topic in topics %} <p>You are interested in {{topic}}.</p> {% endfor %} ''' templite = Templite(template, {'upper': str.upper}) text = templite.render({ 'name': 'Ned', 'topics': ['Python', 'Geometry', 'Juggling'] }) print(text)
output_filename = data['output'] if opts['template'] != None: template_filename = opts['template'] else: template_filename = root+"/"+data['template'] # check the IN/OUT file check_filename(opts["input"]) check_filename(template_filename) check_output(output_filename) # if the input is : # a directory, use the recursive option # a file, parse it directly if(os.path.isdir(opts["input"])): handle_dir(opts["input"], opts["recursive"], comments_list) elif(os.path.isfile(opts["input"])): parse_file(opts["input"], comments_list) # use the template file file_template = Templite(None, template_filename) new_content = file_template.render(comments=comments_list, sctr_path=root) # time to write down report_file = open(output_filename, "w") report_file.write(new_content) report_file.close()
... ${emit("hi")}$ ... ... tralala ${if x > 7: ... say_hello("big x")}$ lala ... ... $\{this is escaped starting delimiter ... ... ${emit("this }\$ is an escaped ending delimiter")}$ ... ... ${# this is a python comment }$ ... ... </body> ... </html> ... """ >>> >>> t = Templite(demo) >>> print t(x = 8) <html> <body> <table> <tr><td> hello 0<br> </tr></td> <tr><td> hello 1<br> </tr></td> <tr><td> hello 2<br> </tr></td> <tr><td> hello 3<br> </tr></td> <tr><td> hello 4<br> </tr></td> <tr><td> hello 5<br> </tr></td> <tr><td> hello 6<br> </tr></td> <tr><td> hello 7<br> </tr></td>
#!/usr/bin/python from templite import Templite import Cookie, LINK_HEADERS f = open("login.tpl", "r") html = str(f.read()) f.close() t = Templite(html) print "Content-Type: text/html\r\n\r\n" print t.render(validate_login_link=LINK_HEADERS.VALIDATE_LOGIN_LINK)
def render(vals, template): t = Templite(template) return t.render(**vals)
def convert(self, oeb_book, output_path, input_plugin, opts, log): from lxml import etree from calibre.utils import zipfile from templite import Templite from urllib import unquote from calibre.ebooks.html.meta import EasyMeta # read template files if opts.template_html_index is not None: template_html_index_data = open(opts.template_html_index, 'rb').read() else: template_html_index_data = P('templates/html_export_default_index.tmpl', data=True) if opts.template_html is not None: template_html_data = open(opts.template_html, 'rb').read() else: template_html_data = P('templates/html_export_default.tmpl', data=True) if opts.template_css is not None: template_css_data = open(opts.template_css, 'rb').read() else: template_css_data = P('templates/html_export_default.css', data=True) template_html_index_data = template_html_index_data.decode('utf-8') template_html_data = template_html_data.decode('utf-8') template_css_data = template_css_data.decode('utf-8') self.log = log self.opts = opts meta = EasyMeta(oeb_book.metadata) tempdir = os.path.realpath(PersistentTemporaryDirectory()) output_file = os.path.join(tempdir, basename(re.sub(r'\.zip', '', output_path)+'.html')) output_dir = re.sub(r'\.html', '', output_file)+'_files' if not exists(output_dir): os.makedirs(output_dir) css_path = output_dir+os.sep+'calibreHtmlOutBasicCss.css' with open(css_path, 'wb') as f: f.write(template_css_data.encode('utf-8')) with open(output_file, 'wb') as f: html_toc = self.generate_html_toc(oeb_book, output_file, output_dir) templite = Templite(template_html_index_data) nextLink = oeb_book.spine[0].href nextLink = relpath(output_dir+os.sep+nextLink, dirname(output_file)) cssLink = relpath(abspath(css_path), dirname(output_file)) tocUrl = relpath(output_file, dirname(output_file)) t = templite.render(has_toc=bool(oeb_book.toc.count()), toc=html_toc, meta=meta, nextLink=nextLink, tocUrl=tocUrl, cssLink=cssLink, firstContentPageLink=nextLink) if isinstance(t, unicode): t = t.encode('utf-8') f.write(t) with CurrentDir(output_dir): for item in oeb_book.manifest: path = abspath(unquote(item.href)) dir = dirname(path) if not exists(dir): os.makedirs(dir) if item.spine_position is not None: with open(path, 'wb') as f: pass else: with open(path, 'wb') as f: f.write(str(item)) item.unload_data_from_memory(memory=path) for item in oeb_book.spine: path = abspath(unquote(item.href)) dir = dirname(path) root = item.data.getroottree() # get & clean HTML <HEAD>-data head = root.xpath('//h:head', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0] head_content = etree.tostring(head, pretty_print=True, encoding='utf-8') head_content = re.sub(r'\<\/?head.*\>', '', head_content) head_content = re.sub(re.compile(r'\<style.*\/style\>', re.M|re.S), '', head_content) head_content = re.sub(r'<(title)([^>]*)/>', r'<\1\2></\1>', head_content) # get & clean HTML <BODY>-data body = root.xpath('//h:body', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0] ebook_content = etree.tostring(body, pretty_print=True, encoding='utf-8') ebook_content = re.sub(r'\<\/?body.*\>', '', ebook_content) ebook_content = re.sub(r'<(div|a|span)([^>]*)/>', r'<\1\2></\1>', ebook_content) # generate link to next page if item.spine_position+1 < len(oeb_book.spine): nextLink = oeb_book.spine[item.spine_position+1].href nextLink = relpath(abspath(nextLink), dir) else: nextLink = None # generate link to previous page if item.spine_position > 0: prevLink = oeb_book.spine[item.spine_position-1].href prevLink = relpath(abspath(prevLink), dir) else: prevLink = None cssLink = relpath(abspath(css_path), dir) tocUrl = relpath(output_file, dir) firstContentPageLink = oeb_book.spine[0].href # render template templite = Templite(template_html_data) toc = lambda: self.generate_html_toc(oeb_book, path, output_dir) t = templite.render(ebookContent=ebook_content, prevLink=prevLink, nextLink=nextLink, has_toc=bool(oeb_book.toc.count()), toc=toc, tocUrl=tocUrl, head_content=head_content, meta=meta, cssLink=cssLink, firstContentPageLink=firstContentPageLink) # write html to file with open(path, 'wb') as f: f.write(t) item.unload_data_from_memory(memory=path) zfile = zipfile.ZipFile(output_path, "w") zfile.add_dir(output_dir, basename(output_dir)) zfile.write(output_file, basename(output_file), zipfile.ZIP_DEFLATED) if opts.extract_to: if os.path.exists(opts.extract_to): shutil.rmtree(opts.extract_to) os.makedirs(opts.extract_to) zfile.extractall(opts.extract_to) self.log('Zip file extracted to', opts.extract_to) zfile.close() # cleanup temp dir shutil.rmtree(tempdir)
print("ERROR: Cannot find template " + template_path + " Please ensure you have used the correct template file") sys.exit(1) # OPEN THE TEMPLATE DOCUMENT content = "" with open(template_path, "r") as file: content = file.read() return content if __name__ == "__main__": git_args = ['git', 'log', '--pretty=format:%h;%an;%cd : %s'] args = parseArguments() if args.gitdir: git_args.insert(1, '--git-dir') git_args.insert(2, args.gitdir) logs = ExcuteGitLog(git_args) commits = ParseLogs(logs) content = GetTemplate(args.template) t = Templite(content) result = t(commitlist=commits) print(result) if args.output: with open(args.output, "wb") as f: f.write(result.encode("utf-8"))
sys.stderr.write('Usage: /usr/bin/python run VERSION\n') sys.exit(1) font = fontforge.font() font.encoding = 'UnicodeFull' font.version = VERSION font.weight = 'Regular' font.fontname = 'octicons' font.familyname = 'octicons' font.fullname = 'octicons' font.em = 96 font.ascent = 84 font.descent = 12 cwd = path.dirname(__file__) tpl = Templite(open(path.join(cwd, 'template.css'), 'r').read()) glyphs = [] # Get list of svg files dirpath = path.join(cwd, "svg") files = [] for filename in listdir(dirpath): filepath = path.join(dirpath, filename) if not path.isfile(filepath): continue (name, ext) = path.splitext(filename) if ext != '.svg': continue
class Catalog(BookStore): title = "ManyBooks.net: Free and Public Domain Books" itemText = "ManyBooks.net" def __init__(self): print("INIT: ManyBooks.net") BookStore.__init__(self) self.widget = None self.w = None self.cache = {} def setWidget (self, widget): tplfile = os.path.join( os.path.abspath( os.path.dirname(__file__)),'category.tmpl') tplfile = codecs.open(tplfile,'r','utf-8') self.template = Templite(tplfile.read()) tplfile.close() self.widget = widget def operate(self): "Show the store" if not self.widget: print("Call setWidget first") return self.widget.title.setText(self.title) if not self.w: uifile = os.path.join( os.path.abspath( os.path.dirname(__file__)),'store.ui') self.w = uic.loadUi(uifile) self.pageNumber = self.widget.stack.addWidget(self.w) self.crumbs=[] self.openUrl(QtCore.QUrl('http://manybooks.net/opds/')) self.w.store_web.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateExternalLinks) self.w.store_web.page().linkClicked.connect(self.openUrl) self.w.crumbs.linkActivated.connect(self.openUrl) self.w.store_web.loadStarted.connect(self.loadStarted) self.w.store_web.loadProgress.connect(self.loadProgress) self.w.store_web.loadFinished.connect(self.loadFinished) self.w.store_web.page().mainFrame().javaScriptWindowObjectCleared.connect(self.addJSObject) self.widget.stack.setCurrentIndex(self.pageNumber) showGrid = operate showList = operate def addJSObject(self): print("DEBUG: JS Window Object Cleared") self.w.store_web.page().mainFrame().addToJavaScriptWindowObject(QtCore.QString('catalog'), self) def search (self, terms): url = "http://manybooks.net/opds/search.php?"+urllib.parse.urlencode(dict(q=terms)) self.crumbs=[self.crumbs[0],["Search: %s"%terms, url]] self.openUrl(QtCore.QUrl(url)) @QtCore.pyqtSlot(QString) @QtCore.pyqtSlot(QtCore.QUrl) def openUrl(self, url): print(("openURL:", url)) if isinstance(url, QtCore.QUrl): url = url.toString() url = str(url) if not url.startswith('http'): url=urllib.parse.urljoin('http://manybooks.net/opds/',url) extension = url.split('.')[-1] if extension in EBOOK_EXTENSIONS: # It's a book, get metadata, file and download # Metadata is cached cache = self.cache[self.get_url_key(url)] title = cache['title'] _author = cache['author'] book_id = cache['id'] cover_url = cache['cover'][0] self.setStatusMessage.emit("Downloading: "+title) book = Book.get_by(title = title) book_tid = url.split('/')[-2] if not book: ident_urn = Identifier(key="ManyBooks.net_ID", value=book_id) ident_tid = Identifier(key="ManyBooks.net_TID", value=book_tid) author = Author.get_by (name = _author) if not author: author = Author(name = _author) book = Book ( title = title, authors = [author], identifiers = [ident_urn, ident_tid], ) session.commit() # Get the file fname = os.path.abspath(os.path.join("ebooks", str(book.id) + '.' + extension)) book.fetch_file(url, extension) if cover_url: book.fetch_cover(cover_url) else: self.showBranch(url) def showCrumbs(self): ctext = [] for c in self.crumbs[-4:]: ctext.append('<a href="%s">%s</a>'%(c[1],c[0])) ctext = " > ".join(ctext) self.w.crumbs.setText(ctext) def showBranch(self, url): """Trigger download of the branch, then trigger parseBranch when it's downloaded""" print(("Showing:", url)) # Disable updates to prevent flickering self.w.store_web.setUpdatesEnabled(False) self.w.store_web.page().mainFrame().load(QtCore.QUrl(url)) self.setStatusMessage.emit("Loading: "+url) self.w.store_web.page().loadFinished.connect(self.parseBranch) return def _generate_links (self, epub_url): """Takes the link for an epub file and returns the list of links to download the ebook in all the formats that are supported by ManyBooks.net""" links = [] book_tid = epub_url.split('/')[-2] for fmt in _FILE_FORMATS: if fmt == 'mnybksjar': ext = '.jar' else: ext = fmt.split(':')[2] links.append('http://manybooks.net/send/%s/%s/%s%s' % \ (fmt, book_tid, book_tid, ext)) return links @QtCore.pyqtSlot() def parseBranch(self): """Replaces the content of the web page (which is assumed to be an Atom feed from ManyBooks) with the generated HTML. """ self.w.store_web.page().loadFinished.disconnect(self.parseBranch) url = str(self.w.store_web.page().mainFrame().requestedUrl().toString()) print(("Parsing the branch:", url)) t1 = time.time() data = parse(str(self.w.store_web.page().mainFrame().toHtml()).encode('utf-8')) if not data.entries: QtGui.QMessageBox.critical(None, \ 'Failed to load ManyBooks.net', \ 'An error ocurred and the ManyBooks.net catalog could not be loaded.') nextPage = '' prevPage = '' for l in data.feed.get('links',[]): print(("LINK:", l)) if l.rel == 'next': nextPage = '<a href="%s">Next Page</a>'%l.href elif l.rel == 'prev': prevPage = '<a href="%s">Previous Page</a>'%l.href title = data.feed.get('title',data.feed.get('subtitle','###')) if '?n=' in url: # It's paginated pnum = int(url.split('=')[-1])/10+1 title = title+'(%s)'%pnum if '?n=' in self.crumbs[-1][1]: # Don't show two numbered pages in a row del(self.crumbs[-1]) crumb = [title, url] if crumb in self.crumbs: self.crumbs = self.crumbs[:self.crumbs.index(crumb)+1] else: self.crumbs.append(crumb) self.showCrumbs() # FIXME: this leaks memory forever (not very much, though) # REVIEW: I don't know where the leak was, but maybe my latest # change helped (or not). self.cache = {} books = [] links = [] for entry in data.entries: # Find acquisition links acq_links = [l.href for l in entry.get('links',[]) if l.rel=='http://opds-spec.org/acquisition'] if acq_links: cover_url = [l.href for l in entry.links if l.rel=='http://opds-spec.org/cover'] if cover_url: entry.cover_url = cover_url[0] if len(acq_links) == 1: entry.links = self._generate_links(acq_links[0]) # A book books.append(entry) for href in acq_links: key = self.get_url_key(href) self.cache[key] = { \ 'cover': cover_url, \ 'id': entry.get('id'), \ 'author': entry.author, \ 'title': entry.title \ } else: # A category links.append(entry) t1 = time.time() html = self.template.render( title = title, books = books, links = links, url = url, nextPage = nextPage, prevPage = prevPage ) print(("Rendered in: %s seconds"%(time.time()-t1))) # open('x.html','w+').write(html) self.w.store_web.setHtml(html) self.w.store_web.setUpdatesEnabled(True) def get_url_key (self, url): """Takes a book URL and extracts the product key""" return url.split('/')[-1].split('.')[0] def on_catalog_itemExpanded(self, item): if item.childCount()==0: self.addBranch(item, item.url) def on_catalog_itemActivated(self, item, column): url=item.url if url.split('/')[-1].isdigit(): # It's a book self.web.load(QtCore.QUrl(url))
def __init__(self): #TO-DO: Handle when template not available self.source_tmpl = Templite(open("htmlfiles/pyfile.html").read(), globals())
#import urllib.request # import # contents = urllib.request.urlopen("http://checkip.amazonaws.com/").read() # # # print(contents) import os import urllib2 response = urllib2.urlopen('http://checkip.amazonaws.com/') ip = response.read().strip() print("External IP: {}".format(ip)) from templite import Templite template = "templates/main.tf" t = Templite("").from_file(template) dirName = "temp" if not os.path.exists(dirName): os.mkdir(dirName) f = open("temp/main.tf", "w") f.write(t(x = 8,homeip = ip))
#!/usr/bin/python from templite import Templite import Cookie, LINK_HEADERS f = open("login.tpl","r") html = str(f.read()) f.close() t = Templite(html) print "Content-Type: text/html\r\n\r\n" print t.render(validate_login_link=LINK_HEADERS.VALIDATE_LOGIN_LINK)
antifuzz_constants_tpl_h = '\n'.join(newLines) antifuzz_tpl_c = open("antifuzz.tpl.c", "r").read() sha512_h = open("sha512.h", "r").read() + "\n" + open("sha512.c", "r").read() aes_h = open("aes.h", "r").read() + "\n" + open("aes.c", "r").read() # pre-render step (include all necessary files first) fileContent = str(open("antifuzz.tpl.h", "r").read()) fileContent = fileContent.replace("${ANTIFUZZ_CONSTANTS_TPL_H}$", antifuzz_constants_tpl_h) fileContent = fileContent.replace("${ANTIFUZZ_TPL_C}$", antifuzz_tpl_c) fileContent = fileContent.replace("${SHA512_H}$", sha512_h) fileContent = fileContent.replace("${AES_H}$", aes_h) # and now render all options fileOutput = open("antifuzz.h", "w+") t = Templite(fileContent) renderOptions = {} renderOptions['USE_ANTIFUZZ'] = int(enableantifuzz) renderOptions['NUM_FILLBITMAP'] = fillbitmapFunctions renderOptions['USE_FILLBITMAP'] = int(fillbitmapFunctions > 0) renderOptions['NUM_HEAVYWEIGHTBB'] = heavyweightbbs renderOptions['USE_HEAVYWEIGHTBB'] = int(heavyweightbbs > 0) renderOptions['USE_SIGNAL_TAMPERING'] = int(enablesignal) renderOptions['DETECT_AFL'] = int(detectafl) renderOptions['DETECT_QEMU'] = int(detectqemu) renderOptions['DETECT_PIN'] = int(detectpin) renderOptions['ENABLE_SLEEP'] = int(enablesleep) renderOptions['FOR_CGC'] = int(forcgc) renderOptions['DIFFICULTY_LEVEL'] = int(difficulty) renderOptions['SLEEP_MS'] = int(sleepms) renderOptions['SLEEP_CRASH_MS'] = int(sleepcrashms)
def __init__(self, coverage, ignore_errors=False): super(HtmlReporter, self).__init__(coverage, ignore_errors) self.directory = None self.source_tmpl = Templite(data("htmlfiles/pyfile.html"), globals()) self.files = []
class GuessDialog(QtGui.QDialog): def __init__(self, book, *args): QtGui.QDialog.__init__(self, *args) uifile = ui.path('guess.ui') uic.loadUi(uifile, self) self.ui = self self.setWindowTitle('Guess book information') self._query = None self.md = [] self.currentMD = None self.book = book self.titleText.setText(book.title or "") self.authorText.setText((', '.join([a.name for a in book.authors]) or "")) ident = models.Identifier.get_by(key='ISBN', book=book) if ident: self.isbnText.setText(ident.value) self._isbn = ident.value else: self.isbn.hide() self.isbnText.hide() self.candidates.page().setLinkDelegationPolicy( self.candidates.page().DelegateAllLinks) self.candidates.linkClicked.connect(self.updateWithCandidate) self.guesser_dict = {} self.guessers.clear() # Fill the guessers combo with appropiate names for plugin in manager.getPluginsOfCategory("Guesser"): if isPluginEnabled(plugin.name) and \ plugin.plugin_object.can_guess(self.book): self.guesser_dict[str(plugin.plugin_object.name)] = \ plugin.plugin_object self.guessers.addItem(plugin.plugin_object.name) self.guesser = self.guesser_dict[str(self.guessers.currentText())] @QtCore.pyqtSlot("QString") def on_guessers_currentIndexChanged(self, text): self.guesser = self.guesser_dict[str(text)] @QtCore.pyqtSlot() def on_guessButton_clicked(self): # Try to guess based on the reliable data query = {'title': None, 'authors': None, 'isbn': None} # self.bookList.clear() if self.title.isChecked(): query['title'] = str(self.titleText.text()) if self.author.isChecked(): query['authors'] = str(self.authorText.text()) if self.isbn.isChecked(): query['isbn'] = str(self.isbnText.text()) if query['title'] is None and \ query['authors'] is None and \ query['isbn'] is None: QtGui.QMessageBox.warning(self, 'Select something to search', 'You need to select at least one ' 'field to search') return self._query = BookMetadata(title=query['title'], thumbnail=None, date=None, subjects=None, authors=query['authors'], identifiers=[query['isbn']], description=None) if self._query: try: print((self.guesser, type(self.guesser))) self.md = self.guesser.guess(self._query) or [] except Exception as e: print(("Guesser exception: %s" % str(e))) QtGui.QMessageBox.warning(self, 'Failed to load data', str(e)) return if self.md: tpl = """ <html> <body> ${for i,candidate in enumerate(md):}$ ${ title = candidate.title thumb = candidate.thumbnail authors = candidate.authors if isinstance(authors, list): authors = u', '.join(authors) identifiers = candidate.identifiers }$ <div style="min-height: 128px; border: solid 3px lightgrey; padding: 15px; border-radius: 15px; margin: 6px; -webkit-transition: all 500ms linear;" onmouseover="this.style.border='solid 3px lightgreen'; this.style.backgroundColor='lightgreen'; document.getElementById('submit-${i}$').style.opacity=1.0;" onmouseout="this.style.border='solid 3px lightgrey'; this.style.backgroundColor='white'; document.getElementById('submit-${i}$').style.opacity=0;"> <img width="64px" style="float: left; margin-right: 4px;" src="${thumb}$"> <div style="text-align: right; margin-top: 12px;"> <b>${title}$</b><br> by ${authors}$<br> ${for identifier in identifiers:}$ <small>${identifier[0]}$:${identifier[1]}$</small><br> ${:end-for}$ <a href="/${i}$/" id="submit-${i}$" >Update</a> </form> </div> </div> ${:end-for}$ """ self.template = Templite(tpl) t1 = time.time() html = self.template.render(md=self.md) print(("Rendered in: %s seconds" % (time.time() - t1))) self.candidates.page().mainFrame().setHtml(html) else: self.candidates.page().mainFrame().setHtml( "<h3>No matches found for the selected criteria</h3>") def updateWithCandidate(self, url): cId = int(url.path()[1:-1]) self.currentMD = self.md[cId] self.accept()
class HtmlReporter(Reporter): """HTML reporting.""" def __init__(self, coverage, ignore_errors=False): super(HtmlReporter, self).__init__(coverage, ignore_errors) self.directory = None self.source_tmpl = Templite(data("htmlfiles/pyfile.html"), globals()) self.files = [] def report(self, morfs, directory, omit_prefixes=None): """Generate an HTML report for `morfs`. `morfs` is a list of modules or filenames. `directory` is where to put the HTML files. `omit_prefixes` is a list of strings, prefixes of modules to omit from the report. """ assert directory, "must provide a directory for html reporting" # Process all the files. self.report_files(self.html_file, morfs, directory, omit_prefixes) # Write the index file. self.index_file() # Create the once-per-directory files. shutil.copyfile( data_filename("htmlfiles/style.css"), os.path.join(directory, "style.css") ) shutil.copyfile( data_filename("htmlfiles/jquery-1.3.2.min.js"), os.path.join(directory, "jquery-1.3.2.min.js") ) def html_file(self, cu, statements, excluded, missing): """Generate an HTML file for one source file.""" source = cu.source_file() source_lines = source.readlines() n_lin = len(source_lines) n_stm = len(statements) n_exc = len(excluded) n_mis = len(missing) n_run = n_stm - n_mis if n_stm > 0: pc_cov = 100.0 * n_run / n_stm else: pc_cov = 100.0 # These classes determine which lines are highlighted by default. c_run = " run hide" c_exc = " exc" c_mis = " mis" lines = [] for lineno, line in enumerate(source_lines): lineno += 1 # enum is 0-based, lines are 1-based. css_class = "" if lineno in statements: css_class += " stm" if lineno not in missing and lineno not in excluded: css_class += c_run if lineno in excluded: css_class += c_exc if lineno in missing: css_class += c_mis lineinfo = { 'text': line, 'number': lineno, 'class': css_class.strip() or "pln" } lines.append(lineinfo) # Write the HTML page for this file. html_filename = cu.flat_rootname() + ".html" html_path = os.path.join(self.directory, html_filename) html = spaceless(self.source_tmpl.render(locals())) fhtml = open(html_path, 'w') fhtml.write(html) fhtml.close() # Save this file's information for the index file. self.files.append({ 'stm': n_stm, 'run': n_run, 'exc': n_exc, 'mis': n_mis, 'pc_cov': pc_cov, 'html_filename': html_filename, 'cu': cu, }) def index_file(self): """Write the index.html file for this report.""" index_tmpl = Templite(data("htmlfiles/index.html"), globals()) files = self.files total_stm = sum([f['stm'] for f in files]) total_run = sum([f['run'] for f in files]) total_exc = sum([f['exc'] for f in files]) if total_stm: total_cov = 100.0 * total_run / total_stm else: total_cov = 100.0 fhtml = open(os.path.join(self.directory, "index.html"), "w") fhtml.write(index_tmpl.render(locals())) fhtml.close()
def on_guessButton_clicked(self): # Try to guess based on the reliable data query = {'title': None, 'authors': None, 'isbn': None} # self.bookList.clear() if self.title.isChecked(): query['title'] = str(self.titleText.text()) if self.author.isChecked(): query['authors'] = str(self.authorText.text()) if self.isbn.isChecked(): query['isbn'] = str(self.isbnText.text()) if query['title'] is None and \ query['authors'] is None and \ query['isbn'] is None: QtGui.QMessageBox.warning(self, 'Select something to search', 'You need to select at least one ' 'field to search') return self._query = BookMetadata(title=query['title'], thumbnail=None, date=None, subjects=None, authors=query['authors'], identifiers=[query['isbn']], description=None) if self._query: try: print((self.guesser, type(self.guesser))) self.md = self.guesser.guess(self._query) or [] except Exception as e: print(("Guesser exception: %s" % str(e))) QtGui.QMessageBox.warning(self, 'Failed to load data', str(e)) return if self.md: tpl = """ <html> <body> ${for i,candidate in enumerate(md):}$ ${ title = candidate.title thumb = candidate.thumbnail authors = candidate.authors if isinstance(authors, list): authors = u', '.join(authors) identifiers = candidate.identifiers }$ <div style="min-height: 128px; border: solid 3px lightgrey; padding: 15px; border-radius: 15px; margin: 6px; -webkit-transition: all 500ms linear;" onmouseover="this.style.border='solid 3px lightgreen'; this.style.backgroundColor='lightgreen'; document.getElementById('submit-${i}$').style.opacity=1.0;" onmouseout="this.style.border='solid 3px lightgrey'; this.style.backgroundColor='white'; document.getElementById('submit-${i}$').style.opacity=0;"> <img width="64px" style="float: left; margin-right: 4px;" src="${thumb}$"> <div style="text-align: right; margin-top: 12px;"> <b>${title}$</b><br> by ${authors}$<br> ${for identifier in identifiers:}$ <small>${identifier[0]}$:${identifier[1]}$</small><br> ${:end-for}$ <a href="/${i}$/" id="submit-${i}$" >Update</a> </form> </div> </div> ${:end-for}$ """ self.template = Templite(tpl) t1 = time.time() html = self.template.render(md=self.md) print(("Rendered in: %s seconds" % (time.time() - t1))) self.candidates.page().mainFrame().setHtml(html) else: self.candidates.page().mainFrame().setHtml( "<h3>No matches found for the selected criteria</h3>")
def get_template(self, view_path): if view_path not in self.templates: self.templates[view_path] = Templite.from_file(view_path) return self.templates[view_path]
#!/usr/bin/env python # coding=utf-8 from templite import Templite # Make a Templite object. templite = Templite(''' <h1>Hello {{name|upper}}!</h1> {% for topic in topics %} {% if topic == "LINUX" %} <p>You are interested in {{topic|lower}}.</p> {% endif %} {% endfor %} {% for d in D %} <p>{{ d.name }} : {{ d.age }}</p> {% endfor %} ''', {'upper': str.upper}, {'lower': str.lower}, ) text = templite.render({ 'name': "J.am", 'topics': ['python', 'Geometry', 'Juggling', 'LINUX'], 'D':({'name':'zhangsan','age':24},{'name':'lisi','age':54}) })
from templite import Templite temp_text=""" <h1>Hello {{name|upper}}!</h1> <h2>Hello {{xname}}</h2> {% for topic in topics %} <p>You are interested in {{topic}}.</p> {% endfor %} {% for num in xs %} <p>{{num}}</p> {% endfor %} {% if name %} <p>name</p> {% endif %} """ templite=Templite(temp_text,{'upper': str.upper},) text=templite.render({ 'name':"Ned", 'xname':"dev", 'topics': ['Python', 'Geometry', 'Juggling'], 'xs':[1,2,3,4,5], }) if __name__=='__main__': print(text)
def display_manialink(self, player): xml = """ <?xml version="1.0" encoding="utf-8"?> <manialink version="1"> <frame> <quad posn="158.15 80 1" sizen="4 4" halign="right" valign="top" style="Icons64x64_1" substyle="StateSuggested"/> <label posn="153.15 78.7 1" sizen="100 7" halign="right" valign="top" textcolor="ffff" text="${opinion}$" textsize="1"/> </frame> <frame> <quad ${if actions:}$ action="votebox_1" ${:endif}$ posn="140.15 76 1" sizen="5 5" halign="right" valign="top" style="UIConstructionSimple_Buttons" substyle="Up"/> <quad ${if actions:}$ action="votebox_0" ${:endif}$ posn="158.15 76 1" sizen="5 5" halign="right" valign="top" style="UIConstructionSimple_Buttons" substyle="Down"/> <label posn="153.15 74.7 1" sizen="100 7" halign="right" valign="top" textcolor="ffff" text="${upvotes}$ $f00${downvotes}$$fff" textsize="1"/> </frame> ${if results:}$ <label posn="157.55 -68.5 1" sizen="100 7" halign="right" valign="top" textcolor="ffff" text="$ff9All Time$n $m$ddd${results}$" textsize="1"/> ${:endif}$ </manialink>""" print player upvotes, totalvotes = self.getvotes() ratio = (100*upvotes) /totalvotes print "%d/%d = %d" % (upvotes, totalvotes, ratio) if ratio >= 80: opinion = '$0f0adored' elif ratio >= 65: opinion = '$6F3enjoyed' elif ratio >= 50: opinion = '$CF9positive' elif ratio >= 35: opinion = '$FF0mixed' elif ratio >= 20: opinion = '$F60negative' else: opinion = '$f00hated' if player.voted: actions = False else: actions = True if player.userid in self.current_track.results: stored = self.current_track.results[player.userid] hours, minutes, seconds, milliseconds = self.getfragments(stored) results = "%2d:%02d:%2d.%d" % (hours, minutes, seconds, milliseconds) print results else: results = None t = Templite(xml) manialink = t.render(actions=True, opinion=opinion, upvotes=upvotes, downvotes=(totalvotes-upvotes), results=results) print player.login self.tm.SendDisplayManialinkPageToLogin(player.login, manialink, 0, False)
def convert(self, oeb_book, output_path, input_plugin, opts, log): from lxml import etree from calibre.utils import zipfile from templite import Templite from urllib import unquote from calibre.ebooks.html.meta import EasyMeta # read template files if opts.template_html_index is not None: template_html_index_data = open(opts.template_html_index, 'rb').read() else: template_html_index_data = P( 'templates/html_export_default_index.tmpl', data=True) if opts.template_html is not None: template_html_data = open(opts.template_html, 'rb').read() else: template_html_data = P('templates/html_export_default.tmpl', data=True) if opts.template_css is not None: template_css_data = open(opts.template_css, 'rb').read() else: template_css_data = P('templates/html_export_default.css', data=True) template_html_index_data = template_html_index_data.decode('utf-8') template_html_data = template_html_data.decode('utf-8') template_css_data = template_css_data.decode('utf-8') self.log = log self.opts = opts meta = EasyMeta(oeb_book.metadata) tempdir = os.path.realpath(PersistentTemporaryDirectory()) output_file = os.path.join( tempdir, basename(re.sub(r'\.zip', '', output_path) + '.html')) output_dir = re.sub(r'\.html', '', output_file) + '_files' if not exists(output_dir): os.makedirs(output_dir) css_path = output_dir + os.sep + 'calibreHtmlOutBasicCss.css' with open(css_path, 'wb') as f: f.write(template_css_data.encode('utf-8')) with open(output_file, 'wb') as f: html_toc = self.generate_html_toc(oeb_book, output_file, output_dir) templite = Templite(template_html_index_data) nextLink = oeb_book.spine[0].href nextLink = relpath(output_dir + os.sep + nextLink, dirname(output_file)) cssLink = relpath(abspath(css_path), dirname(output_file)) tocUrl = relpath(output_file, dirname(output_file)) t = templite.render(has_toc=bool(oeb_book.toc.count()), toc=html_toc, meta=meta, nextLink=nextLink, tocUrl=tocUrl, cssLink=cssLink, firstContentPageLink=nextLink) if isinstance(t, unicode_type): t = t.encode('utf-8') f.write(t) with CurrentDir(output_dir): for item in oeb_book.manifest: path = abspath(unquote(item.href)) dir = dirname(path) if not exists(dir): os.makedirs(dir) if item.spine_position is not None: with open(path, 'wb') as f: pass else: with open(path, 'wb') as f: f.write(str(item)) item.unload_data_from_memory(memory=path) for item in oeb_book.spine: path = abspath(unquote(item.href)) dir = dirname(path) root = item.data.getroottree() # get & clean HTML <HEAD>-data head = root.xpath( '//h:head', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0] head_content = etree.tostring(head, pretty_print=True, encoding='utf-8') head_content = re.sub(r'\<\/?head.*\>', '', head_content) head_content = re.sub( re.compile(r'\<style.*\/style\>', re.M | re.S), '', head_content) head_content = re.sub(r'<(title)([^>]*)/>', r'<\1\2></\1>', head_content) # get & clean HTML <BODY>-data body = root.xpath( '//h:body', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0] ebook_content = etree.tostring(body, pretty_print=True, encoding='utf-8') ebook_content = re.sub(r'\<\/?body.*\>', '', ebook_content) ebook_content = re.sub(r'<(div|a|span)([^>]*)/>', r'<\1\2></\1>', ebook_content) # generate link to next page if item.spine_position + 1 < len(oeb_book.spine): nextLink = oeb_book.spine[item.spine_position + 1].href nextLink = relpath(abspath(nextLink), dir) else: nextLink = None # generate link to previous page if item.spine_position > 0: prevLink = oeb_book.spine[item.spine_position - 1].href prevLink = relpath(abspath(prevLink), dir) else: prevLink = None cssLink = relpath(abspath(css_path), dir) tocUrl = relpath(output_file, dir) firstContentPageLink = oeb_book.spine[0].href # render template templite = Templite(template_html_data) toc = lambda: self.generate_html_toc(oeb_book, path, output_dir ) t = templite.render(ebookContent=ebook_content, prevLink=prevLink, nextLink=nextLink, has_toc=bool(oeb_book.toc.count()), toc=toc, tocUrl=tocUrl, head_content=head_content, meta=meta, cssLink=cssLink, firstContentPageLink=firstContentPageLink) # write html to file with open(path, 'wb') as f: f.write(t) item.unload_data_from_memory(memory=path) zfile = zipfile.ZipFile(output_path, "w") zfile.add_dir(output_dir, basename(output_dir)) zfile.write(output_file, basename(output_file), zipfile.ZIP_DEFLATED) if opts.extract_to: if os.path.exists(opts.extract_to): shutil.rmtree(opts.extract_to) os.makedirs(opts.extract_to) zfile.extractall(opts.extract_to) self.log('Zip file extracted to', opts.extract_to) zfile.close() # cleanup temp dir shutil.rmtree(tempdir)
class Catalog(BookStore): title = "FeedBooks: Free and Public Domain Books" itemText = "FeedBooks.com" def __init__(self): BookStore.__init__(self) self.w = None def setWidget (self, widget): tplfile = os.path.join( os.path.abspath( os.path.dirname(__file__)),'category.tmpl') tplfile = codecs.open(tplfile,'r','utf-8') self.template = Templite(tplfile.read()) tplfile.close() self.widget = widget def operate(self): "Show the store" if not self.widget: print("Call setWidget first") return self.widget.title.setText(self.title) if not self.w: uifile = os.path.join( os.path.abspath( os.path.dirname(__file__)),'store.ui') self.w = uic.loadUi(uifile) self.pageNumber = self.widget.stack.addWidget(self.w) self.crumbs=[] self.openUrl(QtCore.QUrl('http://www.feedbooks.com/catalog.atom')) self.w.store_web.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateExternalLinks) self.w.store_web.page().linkClicked.connect(self.openUrl) self.w.crumbs.linkActivated.connect(self.openUrl) self.w.store_web.loadStarted.connect(self.loadStarted) self.w.store_web.loadProgress.connect(self.loadProgress) self.w.store_web.loadFinished.connect(self.loadFinished) self.widget.stack.setCurrentIndex(self.pageNumber) showGrid = operate showList = operate def search (self, terms): url = "http://www.feedbooks.com/search.atom?"+urllib.parse.urlencode(dict(query=terms)) self.crumbs=[self.crumbs[0],["Search: %s"%terms, url]] self.openUrl(QtCore.QUrl(url)) def openUrl(self, url): if isinstance(url, QtCore.QUrl): url = url.toString() url = str(url) # This happens for catalogs by language if url.startswith('/'): url=urllib.parse.urljoin('http://feedbooks.com',url) extension = url.split('.')[-1] print(("Opening:",url)) if url.split('/')[-1].isdigit() or url.split('/')[-2].isdigit(): # A details page crumb = ["#%s"%url.split('/')[-1],url] if crumb in self.crumbs: self.crumbs = self.crumbs[:self.crumbs.index(crumb)+1] else: self.crumbs.append(crumb) self.showCrumbs() self.w.store_web.load(QtCore.QUrl(url)) elif extension in EBOOK_EXTENSIONS: # It's a book, get metadata, file and download book_id = url.split('/')[-1].split('.')[0] bookdata = parse("http://www.feedbooks.com/book/%s.atom"%book_id) if bookdata.status == 404: bookdata = parse("http://www.feedbooks.com/userbook/%s.atom"%book_id) bookdata = bookdata.entries[0] title = bookdata.title self.setStatusMessage.emit("Downloading: "+title) book = Book.get_by(title = title) if not book: # Let's create a lot of data tags = [] for tag in bookdata.get('tags',[]): t = Tag.get_by(name = tag.label) if not t: t = Tag(name = tag.label) tags.append(t) ident = Identifier(key="FEEDBOOKS_ID", value=book_id) author = Author.get_by (name = bookdata.author) if not author: author = Author(name = bookdata.author) book = Book ( title = title, authors = [author], tags = tags, identifiers = [ident] ) session.commit() # Get the file book.fetch_file(url, extension) book.fetch_cover("http://www.feedbooks.com/book/%s.jpg"%book_id) else: self.showBranch(url) def showCrumbs(self): ctext = [] for c in self.crumbs: ctext.append('<a href="%s">%s</a>'%(c[1],c[0])) ctext = " > ".join(ctext) self.w.crumbs.setText(ctext) def showBranch(self, url): """Trigger download of the branch, then trigger parseBranch when it's downloaded""" print(("Showing:", url)) # Disable updates to prevent flickering self.w.store_web.setUpdatesEnabled(False) self.w.store_web.page().mainFrame().load(QtCore.QUrl(url)) self.setStatusMessage.emit("Loading: "+url) self.w.store_web.page().loadFinished.connect(self.parseBranch) return @QtCore.pyqtSlot() def parseBranch(self): """Replaces the content of the web page (which is assumed to be an Atom feed from Feedbooks) with the generated HTML. """ self.w.store_web.page().loadFinished.disconnect(self.parseBranch) url = str(self.w.store_web.page().mainFrame().requestedUrl().toString()) print(("Parsing the branch:", url)) t1 = time.time() data = parse(str(self.w.store_web.page().mainFrame().toHtml()).encode('utf-8')) print(("Parsed branch in: %s seconds"%(time.time()-t1))) title = data.feed.get('title','') if 'page=' in url: # A page print("DELETING LAST CRUMB") if 'page=' in self.crumbs[-1][1]: #last one was also a page del self.crumbs[-1] if title: crumb = [title.split("|")[0].split("/")[-1].strip(), url] try: r=self.crumbs.index(crumb) self.crumbs=self.crumbs[:r+1] except ValueError: self.crumbs.append(crumb) self.showCrumbs() books = [] links = [] for entry in data.entries: # Find acquisition links acq_links = [l.href for l in entry.links if l.rel=='http://opds-spec.org/acquisition'] if acq_links: # Can be acquired books.append(entry) else: links.append(entry) totPages = int(ceil(float(data.feed.get('opensearch_totalresults', 1))/int(data.feed.get('opensearch_itemsperpage', 1)))) curPage = int(urllib.parse.parse_qs(urllib.parse.urlparse(url).query).get('page',[1])[-1]) t1 = time.time() html = self.template.render( title = title, books = books, links = links, url = url, totPages = totPages, curPage = int(curPage) ) print(("Rendered in: %s seconds"%(time.time()-t1))) # open('x.html','w+').write(html) self.w.store_web.page().mainFrame().setHtml(html) self.w.store_web.setUpdatesEnabled(True)