def get_tree(request, args): ref_name = args[0] path = args[1:] ref = repo.refs[ref_name] rows = [] tree = ref.commit.tree for p in path: tree = tree[p] if isinstance(tree, git.Blob): body = html.pre(tree.data_stream.read()) else: rows.append(html.tr(html.th("Name"), html.th("Size"), html.th("Type"))) if len(args) > 1: rows.append( html.tr(html.td(html.a("..", href="/" + "/".join(["tree"] + args[:1]))), html.td(), html.td("[DIR]")) ) for d in tree.trees: link = html.td(html.a(d.name + "/", href="/" + "/".join(["tree"] + args + [d.name]))) rows.append(html.tr(link, html.td(), html.td("[DIR]"))) for blob in tree.blobs: link = html.td(html.a(blob.name, href="/" + "/".join(["tree"] + args + [blob.name]))) size = html.td(bytes_to_human(blob.size)) rows.append(html.tr(link, size, html.td(blob.mime_type))) body = html.table(*rows, **{"class": "list"}) return html_page("Tree {} /{}".format(ref_name, "/".join(path)), html.div(body))
def get_review(request, args): [hexsha] = args review = db.get("open_reviews", hexsha) patch = html.a("patch", href=html.absolute("patch", hexsha)) buttons = html.div(patch) header = html.div(review_to_html_summary((hexsha, review)), html.hr(), buttons) diff = review_to_diff(review) return html_page( "Review {}".format(hexsha[0:12]), header, *map(diff_to_html, diff), onload="initComments('{}');".format(hexsha) )
def gen_pager(messages, position): total_pages = ((len(messages) + MESSAGES_PER_PAGE - 1) / MESSAGES_PER_PAGE) this_page = position / MESSAGES_PER_PAGE start_page = min(max(this_page - (MAX_PAGES / 2), 0), total_pages - MAX_PAGES) num_pages = min(total_pages, MAX_PAGES) result = [] for page in range(start_page, start_page + num_pages): label = "%i" % (page + 1) if this_page == page: result.append(label) else: result.append(page_link(label, page * MESSAGES_PER_PAGE)) if start_page > 0: result.insert(0, "...") result.insert(0, page_link("« First", 0)) if start_page + num_pages < total_pages: result.append("...") result.append(page_link("Last »", (total_pages - 1) * MESSAGES_PER_PAGE)) return html.div(*(" ".join(result)), style="float: right; width: 70%; text-align: right;")
def as_actions(items): """ returns actions >>> as_actions(['New']) '<div class="actions"><ul><li><a class="action" id="new-action" href="/new">New</a></li></ul></div>' >>> as_actions(['New','Delete']) '<div class="actions"><ul><li><a class="action" id="delete-action" href="/delete">Delete</a></li><li><a class="action" id="new-action" href="/new">New</a></li></ul></div>' """ if not items: return '' result = [] for item in reversed(items): if hasattr(item, '__iter__'): if len(item) == 2: text, url = item else: #TODO: review to see if this is even useful text = item[0] url = '/'.join([''] + route + [item[1]]) else: text = item url = '/'.join([''] + route + [id_for(item)]) result.append('<a class="action" id="%s-action" href="%s">%s</a>' % (id_for(text), url, text)) return div(ul(result), Class='actions')
def expander(name, inner): control = html.span("►", id="control-%s" % name, onclick='expand("%s");' % name) div = html.div(inner, id="inner-%s" % name, style="display: none;") return control + div
def gen_pager(messages, position): total_pages = ((len(messages) + MESSAGES_PER_PAGE - 1) / MESSAGES_PER_PAGE) this_page = position / MESSAGES_PER_PAGE start_page = min(max(this_page - (MAX_PAGES / 2), 0), total_pages - MAX_PAGES) num_pages = min(total_pages, MAX_PAGES) result = [] for page in range(start_page, start_page + num_pages): label = "%i" % (page + 1) if this_page == page: result.append(label) else: result.append(page_link(label, page * MESSAGES_PER_PAGE)) if start_page > 0: result.insert(0, "...") result.insert(0, page_link("« First", 0)) if start_page + num_pages < total_pages: result.append("...") result.append( page_link("Last »", (total_pages - 1) * MESSAGES_PER_PAGE)) return html.div(*(" ".join(result)), style="float: right; width: 70%; text-align: right;")
def get_refs(request, args): def get_ref(ref): commits = html.a("commits", href=html.absolute("commits", ref.name)) tree = html.a("tree", href=html.absolute("tree", ref.name)) return html.tr(html.td(ref.name), html.td(commits), html.td(tree)) return html_page("Refs", html.div(html.table(*map(get_ref, repo.refs), **{"class": "list"})))
def as_actions(items): """ returns actions >>> as_actions(['New']) '<div class="actions"><ul><li><a class="action" id="new-action" href="/new">New</a></li></ul></div>' >>> as_actions(['New','Delete']) '<div class="actions"><ul><li><a class="action" id="delete-action" href="/delete">Delete</a></li><li><a class="action" id="new-action" href="/new">New</a></li></ul></div>' """ if not items: return "" result = [] for item in reversed(items): if hasattr(item, "__iter__"): if len(item) == 2: text, url = item else: # TODO: review to see if this is even useful text = item[0] url = "/".join([""] + route + [item[1]]) else: text = item url = "/".join([""] + route + [id_for(item)]) result.append('<a class="action" id="%s-action" href="%s">%s</a>' % (id_for(text), url, text)) return div(ul(result), Class="actions")
def plain_text(post): """Print plain text of post.""" id = post["data"]["id"] text = post["data"]["selftext"] # Format text so that it is (usually?) four groups wide. # This makes it 256 bits per line. plaintext = html.div(html.tt(html.escape(text)), style="width: 38em;") return expander("plaintext-%s" % id, plaintext)
def get_commit(request, args): [commit_id] = args commit = repo.commit(commit_id) parent = commit.parents[0] diff = parent.diff(commit, None, True) header = html.div(html.a(commit.hexsha[0:12], href=html.absolute("commit", commit.hexsha)), " ", commit.summary) diffs = map(diff_to_html, diff) return html_page("Commit {}".format(commit.hexsha[0:12]), header, *diffs)
def testSelected(self) : sel = Selector(1) xs = [sel(x,lambda x : 99) for x in [0,1,2]] self.assertEquals(xs,[0,99,2]) sel = Selector(3) xs = [sel(x, lambda x : div(x)) for x in [0,1,2,3,4,5,6]] self.assertEquals(xs,[0,1,2,"<div>3</div>",4,5,6])
def test(self) : self.assertEquals(breaks("hello world"),"\nhello world\n") self.assertEquals(p('hello world'),"<p>hello world</p>") self.assertEquals(div('hello world'),"<div>hello world</div>") self.assertEquals(div({'class':'myclass','id':'myd'},'hello world'), """<div class="myclass" id="myd">hello world</div>""") self.assertEquals(div('a','b'),'<div>ab</div>') self.assertEquals(p(),'<p/>') self.assertEquals(html( head(), body( h2("Header"), p('para1'), p('para2') )), """<html><head/><body><h2>Header</h2><p>para1</p><p>para2</p></body></html>""")
def list_messages(): # Which messages to show? messages = list(reversed(sorted(db.keys()))) if "start" in form: start = int(form["start"].value) else: start = 0 page_messages = messages[start:start + MESSAGES_PER_PAGE] pager = gen_pager(messages, start) return html.body( pager, html.h1("a858 auto-analysis"), credits(), html.div(*map(lambda key: format_post(db[key]), page_messages)), pager)
def format_post(post): id = post["data"]["id"] title = post["data"]["title"] url = post["data"]["url"] def formatted(x): name, callback = x return "%s: %s" % (name, callback(post)) items = map(formatted, decoders) + [html.a("Permalink", href="/?id=%s" % id)] return html.div(html.a(name=id), html.h3(html.a(html.escape(title), href=url)), html.ul(*items), id="post-%s" % id)
def list_messages(): # Which messages to show? messages = list(reversed(sorted(db.keys()))) if "start" in form: start = int(form["start"].value) else: start = 0 page_messages = messages[start:start + MESSAGES_PER_PAGE] pager = gen_pager(messages, start) return html.body( pager, html.h1("a858 auto-analysis"), html.div(*map(lambda key: format_post(db[key]), page_messages)), pager )
def get_commits(request, args): ref_name = "/".join(args) rows = [] for commit in repo.iter_commits(ref_name, max_count=config["displayed_commits"]): check = html.input(type="checkbox", name=commit.hexsha) rows.append(html.tr(html.td(check, " ", *commit_to_html(commit)))) create_review = html.input(value="Create Review", type="submit") reset = html.input(value="Reset", type="reset") body = html.form( create_review, reset, html.hr(), html.table(*rows, **{"class": "list"}), method="post", action=html.absolute("review", "create"), ) return html_page("Commits {}".format(ref_name), html.div(body))
def format_post(post): id = post["data"]["id"] title = post["data"]["title"] url = post["data"]["url"] def formatted(x): name, callback = x return "%s: %s" % (name, callback(post)) items = map(formatted, decoders) + [ html.a("Permalink", href="/?id=%s" % id) ] return html.div(html.a(name=id), html.h3(html.a(html.escape(title), href=url)), html.ul(*items), id="post-%s" % id)
def diff_to_html(diff): def parse_segment_header(header): m = re.match("^@@ -(\d+),(\d+) \+(\d+),(\d+) @@", header) return (int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))) def line_to_html(left, right, content, classes): left_td = ( html.td() if left == 0 else html.td(str(left), **{"class": "h", "id": "{}L{}".format(diff.a_blob, left)}) ) right_td = ( html.td() if right == 0 else html.td(str(right), **{"class": "h", "id": "{}R{}".format(diff.b_blob, right)}) ) return html.tr(left_td, right_td, html.td(content), **{"class": classes}) def italize_control_char(line): return (html.span(line[0], **{"class": "h"}), line[1:]) rows = [] lines = diff.diff.split("\n") moved_from = lines.pop(0) rows.append(line_to_html(0, 0, moved_from, "h r")) moved_to = lines.pop(0) rows.append(line_to_html(0, 0, moved_to, "h a")) for line in lines: if len(line) == 0: continue elif line[0] == "+": rows.append(line_to_html(0, right_line, italize_control_char(line), "c a")) right_line += 1 elif line[0] == "-": rows.append(line_to_html(left_line, 0, italize_control_char(line), "c r")) left_line += 1 elif line[0] == "@": (left_line, _, right_line, _) = parse_segment_header(line) rows.append(line_to_html(0, 0, line, "h")) continue elif line[0] == " ": rows.append(line_to_html(left_line, right_line, line, "c")) right_line += 1 left_line += 1 else: raise Exception() return html.div(html.table(*rows, **{"class": "diff"}))
def gen_pager(messages, position): num_pages = (len(messages) + MESSAGES_PER_PAGE - 1) / MESSAGES_PER_PAGE this_page = position / MESSAGES_PER_PAGE result = [] for i in range(num_pages): label = "%i" % (i + 1) if this_page == i: result.append(label) else: if i == 0: url = "/" else: url = "/?start=%i" % (i * MESSAGES_PER_PAGE) result.append(html.a(label, href=url)) return html.div("Page: ", *(" ".join(result)), style="float: right; width: 50%; text-align: right;")
def review_to_html_summary(r): hexsha, review = r commits = html.ul(*map(lambda c: html.li(*commit_to_html(repo.commit(c))), review["includedCommits"])) return html.div(html.h1(html.a(hexsha[0:12], href=html.absolute("review", hexsha))), commits, **{"class": "review"})
def main_page(state): global page startPage("Highly Experimental RIF Demonstration Page") page << h.h2("Highly Experimental RIF Demonstration Page") page << h.p("This page currently only does translations between RIF XML and RIF PS, but the idea is to have various non-RIF languages supported as well") #for k in state.keys(): # page << h.p(`k`, '=', `state[k]`) form = h.form(method="GET", class_="f") form << h.h3("Step 1: Select Input Processor") select_input_processor(form, state) form << h.h3("Step 2: Provide Input") select_input(form, state) form << h.h3("Step 3: (Optional) Select transform or analysis plugins") select_middle(form, state) analysis_div = h.div() page << analysis_div form << h.h3("Step 4: Select Output Processor") select_output_processor(form, state) form << h.h3("Step 5: Begin Processing") form << h.br() output_div = h.div() output_done = run(output_div, state, analysis_div) page << form page << output_div if output_done: form << h.input(type="submit", name="action", value="Update Output Below") else: form << h.input(type="submit", name="action", value="Generate Output Below") #form << h.Raw(" ") #form << h.Raw(" ") #form << h.Raw(" ") #form << h.Raw(" ") #form << h.Raw(" ") #form << h.input(type="submit", name="action", value="Generate Output on New Page") if 0: page << h.h3('Translates to...') input = input.replace("\r\n", "\n") action=args.getfirst("action") if action: (notes, output) = translate(input, action) else: notes = "select a processing option" output = "" if notes: page << h.h4('Processor Message:') page << h.pre(notes, style="padding:0.5em; border: 2px solid red;") if output: page << h.pre(output, style="padding:0.5em; border: 2px solid black;") else: page << h.p("-- No Output --") page << h.hr() page << h.p("This page/software was developed by [email protected]. It's too buggy right now to use. Please don't even bother to report bugs.") print page
doc = iproc.parse(input_text) except error.SyntaxError, e: err = "" err += e.message + "\n" err += e.illustrate_position() outdiv << h.pre("Error\n"+err, style="padding: 1em; border:2px solid red;") return False for p in plugin.get_plugins(["transform","analysis"], state): if isinstance(p, plugin.TransformPlugin): doc = p.transform(doc) elif isinstance(p, plugin.AnalysisPlugin): report = p.analyze(doc) d = h.div() d << h.p("Report from %s plugin:" % p.id) d << h.pre(report, style="padding:1em; border:1px solid black;") middiv << d else: raise RuntimeError try: (oproc,) = plugin.get_plugins(["output"], state) except ValueError: outdiv << h.p('No output processor selected.') return False buffer = StringIO() oproc.serialize(doc, buffer)