def jinja_messages_from_strings(obj): """Converts all strings in a module or object into jinja templates""" env = jinja.Environment() for message in dir(obj): if type(getattr(obj, message)) == types.StringType and message[0] != "_": setattr(obj, message, env.from_string(getattr(obj, message)))
def __init__(self, address, port, name): self.address = address self.port = port self.name = name j_env = jin.Environment(loader=jin.FileSystemLoader("templates/")) self.widget_t = j_env.get_template("widget.html")
def do_the_thing(key, secret): print "grabbing user" user_id, user_name = get_user_id(key, secret) print "user:"******"grabbing page %d of read books" % page content = get_read_books(key, secret, user_id, page, per_page) tree = ET.fromstring(content) for review in tree.findall("./reviews/review"): bookid = review.find("book/id").text bookids.append(bookid) rating = review.find("rating").text ratings.append(rating) num_grabbed = len(bookids) total_books += num_grabbed if num_grabbed == 0: break workids = get_works_for_books(bookids) for workid, rating in zip(workids, ratings): r = works_with_ratings.get(workid) rn = int(rating) if r: if rn > r: works_with_ratings[workid] = rn else: works_with_ratings[workid] = rn workids = set(works_with_ratings.keys()) seriesids = set() bar = progressbar.ProgressBar(widgets=[ "Looking which series books are in", progressbar.widgets.SimpleProgress( format=" [%(value)d/%(max_value)d] "), progressbar.Bar(), ' (', progressbar.ETA(), ') ', ]) for workid in bar(workids): seriesids.update(get_series_for_work(workid)) series = {} bar = progressbar.ProgressBar(widgets=[ "Grabbing series info", progressbar.widgets.SimpleProgress( format=" [%(value)d/%(max_value)d] "), progressbar.Bar(), ' (', progressbar.ETA(), ') ', ]) for seriesid in bar(seriesids): sinfo = get_series_info(seriesid) if sinfo: series[seriesid] = sinfo results = [] for st in series.values(): s = Series() tbd = [] s.sid = st.find("./id").text s.title = st.find("./title").text.strip() s.description = st.find("./description").text.strip() authors = defaultdict(int) for work in st.findall("./series_works/series_work"): workid = work.find("./work/id").text book = Book() book.pos = work.find("./user_position").text book.rating = works_with_ratings.get(workid) if not book.rating: book.rating = 0 if book.pos is None: book.pos = "N/A" book.bookid = work.find("./work/best_book/id").text book.title = work.find("./work/best_book/title").text book.author = work.find("./work/best_book/author/name").text book.image_url = work.find( "./work/best_book/image_url").text.strip() authors[book.author] += 1 if workid in workids: s.read_books.append(book) else: tbd.append(book) s.authors = ", ".join(sorted(authors, key=authors.get, reverse=True)) if len(s.read_books) == 1: rating = s.read_books[0].rating if rating > 0 and rating < 3: continue read = set() for book in s.read_books: posset = pos_to_set(book.pos) if posset: read.update(posset) for book in tbd: posset = pos_to_set(book.pos) if posset and posset <= read: s.unneeded_books.append(book) else: s.unread_books.append(book) results.append(s) results.sort(key=lambda x: (x.authors.split(", ")[0].split(" ")[-1], x.authors, x.title)) env = jinja.Environment(loader=jinja.FileSystemLoader('./templates')) tmpl = env.get_template("output.html") fd, path = tempfile.mkstemp(prefix="completeseries", suffix=".html") with os.fdopen(fd, "w") as f: html = tmpl.render(results=results, user_name=user_name) f.write(html.encode("utf-8")) webbrowser.open(path)
-- Bobby Moretti (2007-07-18): initial version -- Timothy Clemans and Mike Hansen (2008-10-27): major update """ ############################################################################# # Copyright (C) 2007 William Stein <*****@*****.**> # Distributed under the terms of the GNU General Public License (GPL) # The full text of the GPL is available at: # http://www.gnu.org/licenses/ ############################################################################# import jinja import sage.misc.misc from sage.version import version TEMPLATE_PATH = sage.misc.misc.SAGE_ROOT + '/devel/sage/sage/server/notebook/templates' env = jinja.Environment(loader=jinja.FileSystemLoader(TEMPLATE_PATH)) def contained_in(container): """ Returns a function which takes in an environment, context, and value and returns True if that value is in the container and False otherwise. This is registered and used as a test in the templates. EXAMPLES: sage: from sage.server.notebook.template import contained_in sage: f = contained_in([1,2,3]) sage: f(None, None, 2) True sage: f(None, None, 4) False
import jinja import csv import sys reload(sys) sys.setdefaultencoding('utf-8') env = jinja.Environment() env.loader = jinja.FileSystemLoader("./") template = env.get_template("watchmen_conf") #rdr= csv.reader( open(sys.argv[1], "r" ) ) rdr = csv.reader(open(sys.argv[1], "r"), delimiter='#') csv_data = [row for row in rdr] output = template.render(data=csv_data) with open('watchmen_conf.js', 'w') as f: f.write(output)