def _ajaxstream(filename, selectors, **args): """Creates a genshi.Stream object containing XML response data suitable for the JS AJAX functions in ajax.js. It requires the mod_python request object, the filename of the proper template, the XPath selector string (or list of strings) of all elements that need to be updated (currently, these elements need to have an id attribute!), and any named variables the template may need. If one of the named variables is called "errormsg", <div id="errormessage"> is automatically selected. Call example: stream = _ajaxstream("index.html", '//*[@id="content"]', errormsg="Fehlertext", headline="Welcome") output = stream.render("xhtml") An output data example would be: <div xmlns="http://www.w3.org/1999/xhtml"> <div id="errormessage"> Fehlertext </div> <div id="content"> <h2>Welcome</h2> </div> </div> """ main = list() s = list(_mainstream(filename, **args)) try: if args.has_key('errormsg'): main.append(Stream(s).select("//*[@id='errormessage']")) except: pass if selectors is None: selectors = list() if isinstance(selectors, str): try: main.append(Stream(s).select(selectors)) except Exception as e: logging.debug(e) elif isinstance(selectors, list): for sel in selectors: try: main.append(Stream(s).select(sel)) except Exception as e: logging.debug(e) else: logging.debug("_ajaxstream(): selectors is not list nor str") frame = XML('<div xmlns="http://www.w3.org/1999/xhtml"><insert/></div>') return Stream(_insert(frame, u'insert', *main))
def flatland_filter(stream, context): """flatland_filter(stream, context) -> stream Filter a stream through FlatlandFilter """ return Stream(FlatlandFilter()(stream, context))
def apply_to(self, tag, attrs, stream, context, node): attrs, proceed, forced = self.pop_toggle(attrs, context) filters = context.get(CTX_FILTERS, ()) if not (proceed and filters): return tag, attrs, stream for filter_expr in filters: if callable(filter_expr): fn = filter_expr else: try: fn = Expression(filter_expr).evaluate(context) except: log.error( "Failed to parse filter expression %r" % filter_expr, ) raise want = getattr(fn, 'tags', None) if want is None or tag.localname not in want: continue tag, attrs, stream = fn(tag, attrs, stream, context, node) if isinstance(stream, basestring): stream = Stream([(TEXT, stream, (None, -1, -1))]) return tag, attrs, stream
def generate(self, tokens): pos = (None, -1, -1) span = QName('span') class_ = QName('class') def _generate(): for c, text in self._chunk(tokens): if c: attrs = Attrs([(class_, c)]) yield START, (span, attrs), pos yield TEXT, text, pos yield END, span, pos else: yield TEXT, text, pos return Stream(_generate())
def test_no_reports(self): req = MockRequest() config = Mock(name='trunk', min_rev_time=lambda env: 0, max_rev_time=lambda env: 1000, path='tmp/') build = Build(self.env, config='trunk', platform=1, rev=123, rev_time=42) build.insert() step = BuildStep(self.env, build=build.id, name='foo', status=BuildStep.SUCCESS) step.insert() summarizer = PyLintSummarizer(self.env) template, data = summarizer.render_summary(req, config, build, step, 'lint') self.assertEqual('bitten_summary_lint.html', template) self.assertEqual([], data['data']) self.assertEqual( { 'category': { 'convention': 0, 'refactor': 0, 'warning': 0, 'error': 0 }, 'files': 0, 'lines': 0, 'type': {} }, data['totals']) stream = Chrome(self.env).render_template(req, template, {'data': data}, 'text/html', fragment=True) stream = Stream(list(stream)) for i, category in enumerate( ("Convention", "Refactor", "Warning", "Error", "Totals")): text = stream.select('//tbody[@class="totals"]//td[%d]/text()' % (i + 1)).render() self.assertEqual( '0', text, "Expected total for %r to have " "value '0' but got %r" % (category, text))
def end(self, start, end, stream, context, history): kind, (tag, attrs), pos = start attrs -= H_SELECTED value = attrs.get(H_VALUE, None) if value is not None: if value == self.value: attrs |= ((H_SELECTED, u'selected'), ) else: children = list(stream) value = u'' for ck, cd, cp in children: if ck is TEXT: value += cd stream = Stream(children) if value.strip() == self.value.strip(): attrs |= ((H_SELECTED, u'selected'), ) start = kind, (tag, attrs), pos return start, end, stream
def _get_diff_stream(self, a_html, b_html): # note: parsed HTMLs are placed in <DOCUMENT_FRAGMENT> element, skip this fake-root return Stream(self._get_differ(a_html, b_html).get_result()[1:-1])
def emit(self): """Emit the document represented by self.root DOM tree.""" return Stream(self.emit_node(self.root))
def _group_lines(stream): space_re = re.compile('(?P<spaces> (?: +))|^(?P<tag><\w+.*?>)?( )') def pad_spaces(match): m = match.group('spaces') if m: div, mod = divmod(len(m), 2) return div * u'\xa0 ' + mod * u'\xa0' return (match.group('tag') or '') + u'\xa0' def _generate(): stack = [] def _reverse(): for event in reversed(stack): if event[0] is START: yield END, event[1][0], event[2] else: yield END_NS, event[1][0], event[2] for kind, data, pos in stream: if kind is TEXT: lines = data.split('\n') if lines: # First element for e in stack: yield e yield kind, lines.pop(0), pos for e in _reverse(): yield e # Subsequent ones, prefix with \n for line in lines: yield TEXT, '\n', pos for e in stack: yield e yield kind, line, pos for e in _reverse(): yield e else: if kind is START or kind is START_NS: stack.append((kind, data, pos)) elif kind is END or kind is END_NS: stack.pop() else: yield kind, data, pos buf = [] # Fix the \n at EOF. if not isinstance(stream, list): stream = list(stream) found_text = False for i in range(len(stream) - 1, -1, -1): if stream[i][0] is TEXT: e = stream[i] # One chance to strip a \n if not found_text and e[1].endswith('\n'): stream[i] = (e[0], e[1][:-1], e[2]) if len(e[1]): found_text = True break if not found_text: raise StopIteration for kind, data, pos in _generate(): if kind is TEXT and data == '\n': yield Stream(buf[:]) del buf[:] else: if kind is TEXT: data = space_re.sub(pad_spaces, data) buf.append((kind, data, pos)) if buf: yield Stream(buf[:])
def set_stream_value(self, stream, text): stream = stream_is_empty(stream) if stream is None: return Stream([(TEXT, text, (None, -1, -1))]) else: return stream
def test_basic_report(self): req = MockRequest() config = Mock(name='trunk', min_rev_time=lambda env: 0, max_rev_time=lambda env: 1000, path='tmp/') build = Build(self.env, config='trunk', platform=1, rev=123, rev_time=42) build.insert() step = BuildStep(self.env, build=build.id, name='foo', status=BuildStep.SUCCESS) step.insert() report = Report(self.env, build=build.id, step='foo', category='lint') for line, category in enumerate([ 'convention', 'warning', 'error', 'refactor', 'warning', 'error', 'refactor', 'error', 'refactor', 'refactor' ]): item = {'category': category, 'file': 'foo.py', 'type': 'unknown'} if line % 3 == 0: item['line'] = line if line % 2 == 0: item['msg'] = 'error' report.items.append(item) report.insert() summarizer = PyLintSummarizer(self.env) template, data = summarizer.render_summary(req, config, build, step, 'lint') self.assertEqual('bitten_summary_lint.html', template) self.assertEqual([{ 'category': { 'warning': 2, 'error': 3, 'refactor': 4, 'convention': 1 }, 'catnames': ['warning', 'error', 'refactor', 'convention'], 'lines': 10, 'href': '/browser/tmp/foo.py', 'file': 'foo.py', 'type': { 'unknown': 10 }, 'details': sorted([(i % 3 != 0 and '??' or i, 'unknown', i % 2 != 0 and '-' or 'error') for i in range(10)]) }], data['data']) self.assertEqual( { 'category': { 'convention': 1, 'refactor': 4, 'warning': 2, 'error': 3 }, 'files': 1, 'lines': 10, 'type': { 'unknown': 10 } }, data['totals']) stream = Chrome(self.env).render_template(req, template, {'data': data}, 'text/html', fragment=True) stream = Stream(list(stream)) file_text = stream.select( '//tr[@class="file failed"]/th/a/text()').render() self.assertEqual("foo.py", file_text) for i, (category, cnt) in enumerate([("Convention", 1), ("Refactor", 4), ("Warning", 2), ("Error", 3), ("Totals", 10)]): text = stream.select('//tbody[@class="totals"]//td[%d]/text()' % (i + 1)).render().strip() self.assertEqual( str(cnt), text, "Expected total for %r to have " "value '%d' but got %r" % (category, cnt, text)) text_file = stream.select('//table/tbody[1]//td[%d]/text()' % (i + 1)).render().strip() self.assertEqual( str(cnt), text_file, "Expected category %r for " "foo.py to have value '%d' but got %r" % (category, cnt, text_file))