def test_pickle(self): xml = XML('<li>Foo</li>') buf = StringIO() pickle.dump(xml, buf, 2) buf.seek(0) xml = pickle.load(buf) self.assertEquals('<li>Foo</li>', xml.render())
def _test(self, expected_id, result): expected = self._expected(expected_id).render(encoding='utf-8') result = XML(result.render(encoding='utf-8')).render(encoding='utf-8') expected, result = expected.splitlines(), result.splitlines() for exp, res in zip(expected, result): self.assertEquals(exp, res) self.assertEquals(len(expected), len(result))
def test_pickle(self): xml = XML('<li>Foo</li>') buf = BytesIO() pickle.dump(xml, buf, 2) buf.seek(0) xml = pickle.load(buf) self.assertEquals('<li>Foo</li>', xml.render(encoding=None))
def list_security_issues(self): ''' Fills in issues and topissues with security issues information. ''' issues = glob.glob('templates/security/PMASA-*') issues.sort(key=lambda x: int(x[24:29]) * 100 - int(x[30:])) for issue in issues: xmldata = XML(open(issue, 'r').read()) name = os.path.basename(issue) self.data['issues'].append({ 'name': name, 'link': '%ssecurity/%s' % (BASE_URL, self.get_outname(name)), 'fulllink': '%s%ssecurity/%s' % ( SERVER, BASE_URL, self.get_outname(name) ), 'summary': str(xmldata.select( 'def[@function="announcement_summary"]/text()' )), 'date': helper.date.DateTime.parse(str(xmldata.select( 'def[@function="announcement_date"]/text()' ))), 'cves': str(xmldata.select( 'def[@function="announcement_cve"]/text()' )).split(' '), 'versions': str(xmldata.select( 'def[@function="announcement_affected"]/text()' )), }) self.data['topissues'] = self.data['issues'][:TOP_ISSUES]
def extract_href(fragment): if isinstance(fragment, (Element, Fragment)): stream = tag.body(fragment).generate() else : self.log.debug("Extracting href from %s", fragment) stream = XML('<body>%s</body>' % (unicode(fragment),)) for x in stream.select('a[@href]') : if x[0] == 'START' : return x[1][1].get('href')
def generate_sitemap(self): ''' Generates list of pages with titles. ''' self.data['sitemap'] = [] self.data['sitemapxml'] = [] helper.log.dbg('Generating sitemap:') for root, dirs, files in os.walk(TEMPLATES): if '.svn' in dirs: dirs.remove('.svn') # don't visit .svn directories if '.git' in dirs: dirs.remove('.git') # don't visit .git directories files.sort() root_dir = root[len(TEMPLATES):].strip('/') if len(root_dir) > 0: root_dir += '/' for filename in files: name, ext = os.path.splitext(filename) if ext != '.tpl' and name[:6] != 'PMASA-': continue if name[0] in ['_', '.']: continue if filename in ['index.xml.tpl', 'sitemap.xml.tpl', '404.tpl']: continue helper.log.dbg('- %s' % filename) xmldata = XML(open(os.path.join(root, filename), 'r').read()) title = str(xmldata.select( 'def[@function="page_title"]/text()' )) title = title.strip() if len(title) == 0: title = str(xmldata.select( 'def[@function="announcement_id"]/text()' )) title = title.strip() if len(title) == 0: title = 'Index' link = root_dir + self.get_outname(name) sitemap = { 'link': link, 'loc': '%s%s%s' % (SERVER, BASE_URL, link), 'title': title } if name[:6] != 'PMASA-': self.data['sitemap'].append(sitemap) sitemap.update(self.get_sitemap_data(root_dir + name)) self.data['sitemapxml'].append(sitemap) for link in data.sitemap.ENTRIES: sitemap = { 'loc': SERVER + link, } sitemap.update(self.get_sitemap_data(link)) self.data['sitemapxml'].append(sitemap)
def test_style_escaping_with_namespace(self): text = """<style xmlns="http://www.w3.org/1999/xhtml">/*<![CDATA[*/ html > body { display: none; } /*]]>*/</style>""" output = XML(text).render(XHTMLSerializer, encoding=None) self.assertEqual(text, output)
def test_predicate_true_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[true()]') self.assertEqual('<foo>bar</foo>', path.select(xml).render())
def test_render_utf8(self): xml = XML('<li>Über uns</li>') self.assertEqual(u'<li>Über uns</li>'.encode('utf-8'), xml.render(encoding='utf-8'))
def test_predicate_starts_with_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[starts-with(name(), "f")]') self.assertEqual('<foo>bar</foo>', path.select(xml).render()) path = Path('*[starts-with(name(), "b")]') self.assertEqual('', path.select(xml).render())
def test_predicate_substring_before_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[substring-before(name(), "oo")="f"]') self.assertEqual('<foo>bar</foo>', path.select(xml).render())
def test_xml_space(self): text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>' output = XML(text).render(HTMLSerializer, encoding=None) self.assertEqual('<foo> Do not mess \n\n with me </foo>', output)
def test_predicate_normalize_space_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[normalize-space(" foo bar ")="foo bar"]') self.assertEqual('<foo>bar</foo>', path.select(xml).render())
def test_render_output_stream_utf8(self): xml = XML("<li>Über uns</li>") strio = BytesIO() self.assertEqual(None, xml.render(encoding="utf-8", out=strio)) self.assertEqual(u"<li>Über uns</li>".encode("utf-8"), strio.getvalue())
def test_render_ascii(self): xml = XML("<li>Über uns</li>") self.assertEqual(u"<li>Über uns</li>".encode("ascii"), xml.render(encoding="ascii"))
def test_render_output_stream_utf8(self): xml = XML('<li>Über uns</li>') strio = cStringIO() self.assertEqual(None, xml.render(out=strio)) self.assertEqual('<li>Über uns</li>', strio.getvalue())
def test_render_output_stream_unicode(self): xml = XML('<li>Über uns</li>') strio = StringIO() self.assertEqual(None, xml.render(encoding=None, out=strio)) self.assertEqual(u'<li>Über uns</li>', strio.getvalue())
def test_render_ascii(self): xml = XML('<li>Über uns</li>') self.assertEqual('<li>Über uns</li>', xml.render(encoding='ascii'))
def test_render_unicode(self): xml = XML('<li>Über uns</li>') self.assertEqual(u'<li>Über uns</li>', xml.render(encoding=None))
def test_render_utf8(self): xml = XML('<li>Über uns</li>') self.assertEqual('<li>Über uns</li>', xml.render())
def test_ignorable_space(self): text = '<foo> Mess \n\n\n with me! </foo>' output = XML(text).render(XMLSerializer, encoding=None) self.assertEqual('<foo> Mess\n with me! </foo>', output)
def test_textarea_whitespace(self): content = '\nHey there. \n\n I am indented.\n' stream = XML('<textarea name="foo">%s</textarea>' % content) output = stream.render(HTMLSerializer, encoding=None) self.assertEqual('<textarea name="foo">%s</textarea>' % content, output)
def test_pre_whitespace(self): content = '\nHey <em>there</em>. \n\n I am indented.\n' stream = XML('<pre>%s</pre>' % content) output = stream.render(HTMLSerializer, encoding=None) self.assertEqual('<pre>%s</pre>' % content, output)
def test_render_utf8(self): xml = XML("<li>Über uns</li>") self.assertEqual(u"<li>Über uns</li>".encode("utf-8"), xml.render(encoding="utf-8"))
def test_predicate_position(self): xml = XML('<root><foo id="a1"/><foo id="a2"/><foo id="a3"/></root>') path = Path('*[2]') self.assertEqual('<foo id="a2"/>', path.select(xml).render())
def test_predicate_round_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[round("4.4")=4]') self.assertEqual('<foo>bar</foo>', path.select(xml).render()) path = Path('*[round("4.6")=5]') self.assertEqual('<foo>bar</foo>', path.select(xml).render())
def test_attrwildcard_with_namespace(self): xml = XML('<root xmlns:f="FOO"><foo f:bar="baz"/></root>') path = Path('foo[@f:*]') self.assertEqual('<foo xmlns:ns1="FOO" ns1:bar="baz"/>', path.select(xml, namespaces={'f': 'FOO'}).render())
def test_predicate_string_length_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[string-length(name())=3]') self.assertEqual('<foo>bar</foo>', path.select(xml).render())
def test_render_unicode(self): xml = XML("<li>Über uns</li>") self.assertEqual(u"<li>Über uns</li>", xml.render()) self.assertEqual(u"<li>Über uns</li>", xml.render(encoding=None))
def test_predicate_translate_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[translate(name(), "fo", "ba")="baa"]') self.assertEqual('<foo>bar</foo>', path.select(xml).render())
def test_script_escaping_with_namespace(self): text = """<script xmlns="http://www.w3.org/1999/xhtml">/*<![CDATA[*/ if (1 < 2) { alert("Doh"); } /*]]>*/</script>""" output = XML(text).render(XHTMLSerializer, encoding=None) self.assertEqual(text, output)
def test_predicate_variable(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[name()=$bar]') variables = {'bar': 'foo'} self.assertEqual('<foo>bar</foo>', path.select(xml, variables=variables).render())
def test_render_output_stream_utf8(self): xml = XML('<li>Über uns</li>') strio = BytesIO() self.assertEqual(None, xml.render(encoding='utf-8', out=strio)) self.assertEqual(u'<li>Über uns</li>'.encode('utf-8'), strio.getvalue())
def test_predicate_position_and_attr(self): xml = XML('<root><foo/><foo id="a1"/><foo id="a2"/></root>') path = Path('*[1][@id]') self.assertEqual('', path.select(xml).render()) path = Path('*[2][@id]') self.assertEqual('<foo id="a1"/>', path.select(xml).render())
def test_predicate_namespace(self): xml = XML('<root><foo xmlns="NS"/><bar/></root>') path = Path('*[namespace-uri()="NS"]') self.assertEqual('<foo xmlns="NS"/>', path.select(xml).render())
def test_parse_stream(self): stream = XML('<root> ${var} $var</root>') tmpl = MarkupTemplate(stream) self.assertEqual('<root> 42 42</root>', str(tmpl.generate(var=42)))
def test_predicate_not_name(self): xml = XML('<root><foo/><bar/></root>') path = Path('*[not(name()="foo")]') self.assertEqual('<bar/>', path.select(xml).render())
def test_script_escaping(self): text = """<script>/*<![CDATA[*/ if (1 < 2) { alert("Doh"); } /*]]>*/</script>""" output = XML(text).render(XHTMLSerializer, encoding=None) self.assertEqual(text, output)
def test_predicate_attr_less_than(self): xml = XML('<root><item priority="3"/></root>') path = Path('item[@priority<3]') self.assertEqual('', path.select(xml).render()) path = Path('item[@priority<4]') self.assertEqual('<item priority="3"/>', path.select(xml).render())
def test_style_escaping(self): text = """<style>/*<![CDATA[*/ html > body { display: none; } /*]]>*/</style>""" output = XML(text).render(XHTMLSerializer, encoding=None) self.assertEqual(text, output)
def test_predicate_attr_and(self): xml = XML('<root><item/><item important="very"/></root>') path = Path('item[@important and @important="very"]') self.assertEqual('<item important="very"/>', path.select(xml).render()) path = Path('item[@important and @important="notso"]') self.assertEqual('', path.select(xml).render())
def test_xhtml_namespace_prefix(self): text = """<div xmlns="http://www.w3.org/1999/xhtml"> <strong>Hello</strong> </div>""" output = XML(text).render(XHTMLSerializer, encoding=None) self.assertEqual(text, output)
def test_predicate_attr_or(self): xml = XML('<root><item/><item important="very"/></root>') path = Path('item[@urgent or @important]') self.assertEqual('<item important="very"/>', path.select(xml).render()) path = Path('item[@urgent or @notso]') self.assertEqual('', path.select(xml).render())
def test_xml_lang_nodup(self): text = '<p lang="en" xml:lang="en">English text</p>' output = XML(text).render(HTMLSerializer, encoding=None) self.assertEqual('<p lang="en">English text</p>', output)
def test_predicate_contains_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[contains(name(), "oo")]') self.assertEqual('<foo>bar</foo>', path.select(xml).render())
def test_predicate_floor_function(self): xml = XML('<root><foo>bar</foo></root>') path = Path('*[floor("4.5")=4]') self.assertEqual('<foo>bar</foo>', path.select(xml).render())
def test_empty_script(self): text = '<script src="foo.js" />' output = XML(text).render(HTMLSerializer, encoding=None) self.assertEqual('<script src="foo.js"></script>', output)
def expand_macro(self, formatter, name, content): self.log.info('Begin expand_macro for req: ' + repr(content)) largs, kwargs = parse_args(content) if len(largs) == 0: raise TracError("File name to include is required parameter!") orig_file_name = file_name = largs[0] global multirepos if not multirepos: repos = self.env.get_repository(formatter.req.authname) else: if (orig_file_name[0] == '/'): orig_file_name = orig_file_name[1:] splitpath = file_name.split('/') if (file_name[0] == '/'): reponame = splitpath[1] else: reponame = splitpath[0] repos = self.env.get_repository(reponame) if (repos): l = len(reponame) if (file_name[0] == '/'): file_name = file_name[1:] file_name = file_name[l:] else: repos = self.env.get_repository() rev = kwargs.get('rev', None) if kwargs.has_key('header'): header = kwargs.get('header') # user specified header else: href = '../browser/%s%s' % (orig_file_name, make_rev_str(rev)) header = tag.a(file_name, href=href) if not header: header = u'\xa0' # default value from trac.mimeview.api.py # TODO - 'content' is default from mimeview.api.py, but it picks # up text-align: center, which probably isn't the best thing if # we are adding a file name in the header. There isn't an obvious # replacement in the delivered CSS to pick over this for now though header_class = kwargs.get('header_class', 'content') src = repos.get_node(file_name, rev).get_content().read() context = formatter.context # put these into context object so annotator sees them context.file_name = file_name context.rev = rev context.startline = 1 # we generally include line numbers in the output, unless it has been # explicitly requested otherwise. 0, no, false, none will suppress line_numbers = kwargs.get('line_numbers', None) if line_numbers is None: line_numbers = True else: try: line_numbers = int(line_numbers) except: negatory = ('no', 'false', 'none') line_numbers = str(line_numbers).lower() not in negatory # lines added up front to "trick" renderer when rendering partial render_prepend = [] start, end = kwargs.get('start', None), kwargs.get('end', None) if start or end: src, start, end = self._handle_partial(src, start, end) context.startline = start if start > 2 and file_name.endswith('.php'): render_prepend = [ '#!/usr/bin/php -f', '<?' ] if render_prepend: src = '\n'.join(render_prepend) + '\n' + src # ensure accurate start number after this gets stripped context.startline = start - len(render_prepend) mimetype = kwargs.get('mimetype', None) url = None # render method doesn't seem to use this mv = Mimeview(self.env) annotations = line_numbers and ['givenlineno'] or None src = mv.render(formatter.context, mimetype, src, file_name, url, annotations) if line_numbers: # handle the case where only one line of code was included # and we get back an XHTML string if not hasattr(src, 'generate'): from genshi.input import XML src = XML(src) # the _render_source method will always set the CSS class # of the annotator to it's name; there isn't an easy way # to override that. We could create our own CSS class for # givenlineno that mimics lineno, but it's cleaner to just # tweak the output here by running the genshi stream from # src through a transformer that will make the change xpath1 = 'thead/tr/th[@class="givenlineno"]' xpath2 = 'thead/tr/th[2]' # last() not supported by Genshi? xpath3 = 'thead/tr/th[2]/text()' # TODO - does genshi require a QName here? Seems to work w/o it src = src.generate() | Transformer(xpath1).attr('class', 'lineno') \ | Transformer(xpath2).attr('class', header_class) \ | Transformer(xpath3).replace(header) if render_prepend: # TODO - is there a better of stripping lines here? for i in xrange(len(render_prepend)): src = src | Transformer('tbody/tr[1]').remove() return src