def test_icon_links(self): env = EnvironmentStub() req = Mock(hdf=HDFWrapper(), cgi_location='/trac.cgi', path_info='') # No icon set in config, so no icon links env.config.set('project', 'icon', '') Chrome(env).populate_hdf(req, None) assert 'chrome.links.icon' not in req.hdf assert 'chrome.links.shortcut icon' not in req.hdf # Relative URL for icon config option env.config.set('project', 'icon', 'trac.ico') Chrome(env).populate_hdf(req, None) self.assertEqual('/trac.cgi/chrome/common/trac.ico', req.hdf['chrome.links.icon.0.href']) self.assertEqual('/trac.cgi/chrome/common/trac.ico', req.hdf['chrome.links.shortcut icon.0.href']) # URL relative to the server root for icon config option req.hdf = HDFWrapper() env.config.set('project', 'icon', '/favicon.ico') Chrome(env).populate_hdf(req, None) self.assertEqual('/favicon.ico', req.hdf['chrome.links.icon.0.href']) self.assertEqual('/favicon.ico', req.hdf['chrome.links.shortcut icon.0.href']) # Absolute URL for icon config option req.hdf = HDFWrapper() env.config.set('project', 'icon', 'http://example.com/favicon.ico') Chrome(env).populate_hdf(req, None) self.assertEqual('http://example.com/favicon.ico', req.hdf['chrome.links.icon.0.href']) self.assertEqual('http://example.com/favicon.ico', req.hdf['chrome.links.shortcut icon.0.href'])
def test_logo(self): env = EnvironmentStub() req = Mock(hdf=HDFWrapper(), cgi_location='/trac.cgi', path_info='') # Verify that no logo data is put in the HDF if no logo is configured env.config.set('header_logo', 'src', '') Chrome(env).populate_hdf(req, None) assert 'chrome.logo.src' not in req.hdf # Test with a relative path to the logo image req.hdf = HDFWrapper() env.config.set('header_logo', 'src', 'foo.png') Chrome(env).populate_hdf(req, None) self.assertEqual('/trac.cgi/chrome/common/foo.png', req.hdf['chrome.logo.src']) # Test with a server-relative path to the logo image req.hdf = HDFWrapper() env.config.set('header_logo', 'src', '/img/foo.png') Chrome(env).populate_hdf(req, None) self.assertEqual('/img/foo.png', req.hdf['chrome.logo.src']) # Test with an absolute path to the logo image req.hdf = HDFWrapper() env.config.set('header_logo', 'src', 'http://www.example.org/foo.png') Chrome(env).populate_hdf(req, None) self.assertEqual('http://www.example.org/foo.png', req.hdf['chrome.logo.src'])
def send_project_index(req, options, env_paths=None): from trac.web.clearsilver import HDFWrapper if 'TRAC_ENV_INDEX_TEMPLATE' in options: tmpl_path, template = os.path.split(options['TRAC_ENV_INDEX_TEMPLATE']) from trac.config import default_dir req.hdf = HDFWrapper(loadpaths=[default_dir('templates'), tmpl_path]) tmpl_vars = {} if 'TRAC_TEMPLATE_VARS' in options: for pair in options['TRAC_TEMPLATE_VARS'].split(','): key, val = pair.split('=') req.hdf[key] = val else: req.hdf = HDFWrapper() template = req.hdf.parse('''<html> <head><title>Available Projects</title></head> <body><h1>Available Projects</h1><ul><?cs each:project = projects ?><li><?cs if:project.href ?> <a href="<?cs var:project.href ?>" title="<?cs var:project.description ?>"> <?cs var:project.name ?></a><?cs else ?> <small><?cs var:project.name ?>: <em>Error</em> <br /> (<?cs var:project.description ?>)</small><?cs /if ?> </li><?cs /each ?></ul></body> </html>''') if not env_paths and 'TRAC_ENV_PARENT_DIR' in options: dir = options['TRAC_ENV_PARENT_DIR'] env_paths = [os.path.join(dir, f) for f in os.listdir(dir)] href = Href(req.idx_location) try: projects = [] for env_path in env_paths: if not os.path.isdir(env_path): continue env_dir, project = os.path.split(env_path) try: env = _open_environment(env_path) proj = { 'name': env.config.get('project', 'name'), 'description': env.config.get('project', 'descr'), 'href': href(project) } except Exception, e: proj = {'name': project, 'description': str(e)} projects.append(proj) projects.sort(lambda x, y: cmp(x['name'], y['name'])) req.hdf['projects'] = projects # TODO maybe this should be 404 if index wasn't specifically requested req.display(template, response=200)
def test_nav_contributor_order(self): class TestNavigationContributor1(Component): implements(INavigationContributor) def get_active_navigation_item(self, req): return None def get_navigation_items(self, req): yield 'metanav', 'test1', 'Test 1' class TestNavigationContributor2(Component): implements(INavigationContributor) def get_active_navigation_item(self, req): return None def get_navigation_items(self, req): yield 'metanav', 'test2', 'Test 2' env = EnvironmentStub( enable=[TestNavigationContributor1, TestNavigationContributor2]) req = Mock(hdf=HDFWrapper(), path_info='/', cgi_location='/trac.cgi') chrome = Chrome(env) # Test with both items set in the order option env.config.set('trac', 'metanav', 'test2, test1') chrome.populate_hdf(req, None) node = req.hdf.getObj('chrome.nav.metanav').child() self.assertEqual('test2', node.name()) self.assertEqual('test1', node.next().name()) # Test with only test1 in the order options req.hdf = HDFWrapper() env.config.set('trac', 'metanav', 'test1') chrome.populate_hdf(req, None) node = req.hdf.getObj('chrome.nav.metanav').child() self.assertEqual('test1', node.name()) self.assertEqual('test2', node.next().name()) # Test with only test2 in the order options req.hdf = HDFWrapper() env.config.set('trac', 'metanav', 'test2') chrome.populate_hdf(req, None) node = req.hdf.getObj('chrome.nav.metanav').child() self.assertEqual('test2', node.name()) self.assertEqual('test1', node.next().name()) # Test with none in the order options (order corresponds to # registration order) req.hdf = HDFWrapper() env.config.set('trac', 'metanav', 'foo, bar') chrome.populate_hdf(req, None) node = req.hdf.getObj('chrome.nav.metanav').child() self.assertEqual('test1', node.name()) self.assertEqual('test2', node.next().name())
def test_add_link_advanced(self): req = Mock(hdf=HDFWrapper()) add_link(req, 'start', '/trac/wiki', 'Start page', 'text/html', 'home') self.assertEqual('/trac/wiki', req.hdf['chrome.links.start.0.href']) self.assertEqual('Start page', req.hdf['chrome.links.start.0.title']) self.assertEqual('text/html', req.hdf['chrome.links.start.0.type']) self.assertEqual('home', req.hdf['chrome.links.start.0.class'])
def process_request(self, req): # !!!: Wow, this is hackish. <NPK> add_stylesheet(req, 'common/css/wiki.css') wiki_hdf = HDFWrapper() wiki_hdf.readString(req.__CACHED_PAGE) req.hdf.copy('wiki', wiki_hdf.hdf) return 'wiki.cs', None
def __init__(self, env): self.env = env self.config = env.config self.db = env.get_db_cnx() self.hdf = HDFWrapper(loadpaths=[env.get_templates_dir(), self.config.get('trac', 'templates_dir')]) populate_hdf(self.hdf, env)
def send_project_index(environ, start_response, parent_dir=None, env_paths=None): req = Request(environ, start_response) loadpaths = [pkg_resources.resource_filename('trac', 'templates')] use_clearsilver = False if req.environ.get('trac.env_index_template'): tmpl_path, template = os.path.split( req.environ['trac.env_index_template']) loadpaths.insert(0, tmpl_path) use_clearsilver = template.endswith('.cs') # assume Clearsilver if use_clearsilver: req.hdf = HDFWrapper( loadpaths) # keep that for custom .cs templates else: template = 'index.html' data = { 'trac': { 'version': TRAC_VERSION, 'time': format_datetime() }, 'req': req } if req.environ.get('trac.template_vars'): for pair in req.environ['trac.template_vars'].split(','): key, val = pair.split('=') data[key] = val if use_clearsilver: req.hdf[key] = val try: href = Href(req.base_path) projects = [] for env_name, env_path in get_environments(environ).items(): try: env = open_environment(env_path, use_cache=not environ['wsgi.run_once']) proj = { 'env': env, 'name': env.project_name, 'description': env.project_description, 'href': href(env_name) } except Exception, e: proj = {'name': env_name, 'description': to_unicode(e)} projects.append(proj) projects.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower())) data['projects'] = projects if use_clearsilver: req.hdf['projects'] = projects req.display(template) loader = TemplateLoader(loadpaths, variable_lookup='lenient') tmpl = loader.load(template) stream = tmpl.generate(**data) output = stream.render('xhtml', doctype=DocType.XHTML_STRICT) req.send(output, 'text/html')
def __init__(self, env): self.env = env self.config = env.config self.db = env.get_db_cnx() loadpaths = Chrome(self.env).get_all_templates_dirs() self.hdf = HDFWrapper(loadpaths) populate_hdf(self.hdf, env)
def setUp(self): self.env = EnvironmentStub() self.ticket_module = TicketModule(self.env) self.mimeview = Mimeview(self.env) self.req = Mock(hdf=HDFWrapper(['./templates']), base_path='/trac.cgi', path_info='', href=Href('/trac.cgi'))
def send_pretty_error(e, env, req=None): """Send a "pretty" HTML error page to the client.""" import traceback import StringIO tb = StringIO.StringIO() traceback.print_exc(file=tb) if not req: from trac.web.cgi_frontend import CGIRequest from trac.web.clearsilver import HDFWrapper req = CGIRequest() req.authname = '' req.hdf = HDFWrapper() try: if not env: from trac.env import open_environment env = open_environment() env.href = Href(req.cgi_location) if env and env.log: env.log.exception(e) populate_hdf(req.hdf, env, req) if isinstance(e, TracError): req.hdf['title'] = e.title or 'Error' req.hdf['error.title'] = e.title or 'Error' req.hdf['error.type'] = 'TracError' req.hdf['error.message'] = e.message if e.show_traceback: req.hdf['error.traceback'] = escape(tb.getvalue()) req.display('error.cs', response=500) elif isinstance(e, PermissionError): req.hdf['title'] = 'Permission Denied' req.hdf['error.type'] = 'permission' req.hdf['error.action'] = e.action req.hdf['error.message'] = e req.display('error.cs', response=403) else: req.hdf['title'] = 'Oops' req.hdf['error.type'] = 'internal' req.hdf['error.message'] = escape(str(e)) req.hdf['error.traceback'] = escape(tb.getvalue()) req.display('error.cs', response=500) except RequestDone: pass except Exception, e2: if env and env.log: env.log.error('Failed to render pretty error page: %s', e2, exc_info=True) req.send_response(500) req.send_header('Content-Type', 'text/plain') req.end_headers() req.write('Oops...\n\nTrac detected an internal error:\n\n') req.write(str(e)) req.write('\n') req.write(tb.getvalue())
def test_default_links(self): env = EnvironmentStub() req = Mock(hdf=HDFWrapper(), cgi_location='/trac.cgi', path_info='') Chrome(env).populate_hdf(req, None) self.assertEqual('/trac.cgi/wiki', req.hdf['chrome.links.start.0.href']) self.assertEqual('/trac.cgi/search', req.hdf['chrome.links.search.0.href']) self.assertEqual('/trac.cgi/wiki/TracGuide', req.hdf['chrome.links.help.0.href']) self.assertEqual('/trac.cgi/chrome/common/css/trac.css', req.hdf['chrome.links.stylesheet.0.href'])
def render(self, req, mimetype, content, filename=None, rev=None): from trac.web.clearsilver import HDFWrapper content = content_to_unicode(self.env, content, mimetype) d = self._diff_to_hdf(content.splitlines(), Mimeview(self.env).tab_width) if not d: raise TracError, u'Contenu diff unifié invalide' hdf = HDFWrapper(loadpaths=[self.env.get_templates_dir(), self.config.get('trac', 'templates_dir')]) hdf['diff.files'] = d add_stylesheet(req, 'common/css/diff.css') return hdf.render(hdf.parse(self.diff_cs))
def render(self, req, mimetype, content, filename=None, rev=None): from trac.web.clearsilver import HDFWrapper tabwidth = int(self.config.get('diff', 'tab_width')) d = self._diff_to_hdf(content.splitlines(), tabwidth) if not d: raise TracError, 'Invalid unified diff content' hdf = HDFWrapper(loadpaths=[ self.env.get_templates_dir(), self.config.get('trac', 'templates_dir') ]) hdf['diff.files'] = d add_stylesheet(req, 'common/css/diff.css') return hdf.render(hdf.parse(self.diff_cs))
def test_nav_contributor_active(self): class TestNavigationContributor(Component): implements(INavigationContributor) def get_active_navigation_item(self, req): return 'test' def get_navigation_items(self, req): yield 'metanav', 'test', 'Test' env = EnvironmentStub(enable=[TestNavigationContributor]) req = Mock(hdf=HDFWrapper(), path_info='/', cgi_location='/trac.cgi') chrome = Chrome(env) chrome.populate_hdf(req, TestNavigationContributor(env)) self.assertEqual('Test', req.hdf['chrome.nav.metanav.test']) self.assertEqual('1', req.hdf['chrome.nav.metanav.test.active'])
def _test_import(self, env, filename, sheet=1): req = Request( { 'SERVER_PORT': 0, 'SERVER_NAME': 'any', 'wsgi.url_scheme': 'any', 'wsgi.input': 'any', 'REQUEST_METHOD': 'GET' }, lambda x, y: _printme) try: from trac.test import MockPerm req.perm = MockPerm() except ImportError: pass req.authname = 'testuser' req.hdf = HDFWrapper( [] ) # replace by this if you want to generate HTML: req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs()) db = env.get_db_cnx() cursor = db.cursor() _exec(cursor, "select * from enum") enums_before = cursor.fetchall() _exec(cursor, "select * from component") components_before = cursor.fetchall() #print enums_before # when testing, always use the same time so that the results are comparable #print "importing " + filename + " with tickettime " + str(ImporterTestCase.TICKET_TIME) template, content_type = ImportModule(env)._do_import( filename, sheet, req, filename, ImporterTestCase.TICKET_TIME) #sys.stdout = tempstdout #req.display(template, content_type or 'text/html') #open('/tmp/out.html', 'w').write(req.hdf.render(template, None)) _exec(cursor, "select * from ticket") tickets = cursor.fetchall() _exec(cursor, "select * from ticket_custom") tickets_custom = cursor.fetchall() _exec(cursor, "select * from ticket_change") tickets_change = cursor.fetchall() _exec(cursor, "select * from enum") enums = [f for f in set(cursor.fetchall()) - set(enums_before)] _exec(cursor, "select * from component") components = [ f for f in set(cursor.fetchall()) - set(components_before) ] pp = pprint.PrettyPrinter(indent=4) return pp.pformat( [tickets, tickets_custom, tickets_change, enums, components])
def send_project_index(environ, start_response, parent_dir=None, env_paths=None): from trac.config import default_dir req = Request(environ, start_response) loadpaths = [default_dir('templates')] if req.environ.get('trac.env_index_template'): tmpl_path, template = os.path.split( req.environ['trac.env_index_template']) loadpaths.insert(0, tmpl_path) else: template = 'index.cs' req.hdf = HDFWrapper(loadpaths) tmpl_vars = {} if req.environ.get('trac.template_vars'): for pair in req.environ['trac.template_vars'].split(','): key, val = pair.split('=') req.hdf[key] = val if parent_dir and not env_paths: env_paths = dict([(filename, os.path.join(parent_dir, filename)) for filename in os.listdir(parent_dir)]) try: href = Href(req.base_path) projects = [] for env_name, env_path in get_environments(environ).items(): try: env = _open_environment(env_path, run_once=environ['wsgi.run_once']) proj = { 'name': env.project_name, 'description': env.project_description, 'href': href(env_name) } except Exception, e: proj = {'name': env_name, 'description': to_unicode(e)} projects.append(proj) projects.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower())) req.hdf['projects'] = projects req.display(template)
def dispatch(self, req): """Find a registered handler that matches the request and let it process it. In addition, this method initializes the HDF data set and adds the web site chrome. """ req.authname = self.authenticate(req) req.perm = PermissionCache(self.env, req.authname) chrome = Chrome(self.env) req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs()) populate_hdf(req.hdf, self.env, req) newsession = req.args.has_key('newsession') req.session = Session(self.env, req, newsession) # Select the component that should handle the request chosen_handler = None default_handler = None if not req.path_info or req.path_info == '/': default_handler = self.config.get('trac', 'default_handler') for handler in self.handlers: if handler.match_request(req) or \ handler.__class__.__name__ == default_handler: chosen_handler = handler break chrome.populate_hdf(req, chosen_handler) if not chosen_handler: # FIXME: Should return '404 Not Found' to the client raise TracError, 'No handler matched request to %s' % req.path_info try: resp = chosen_handler.process_request(req) if resp: template, content_type = resp if not content_type: content_type = 'text/html' req.display(template, content_type or 'text/html') finally: # Give the session a chance to persist changes req.session.save()
def _test_preview(self, env, filename): req = Request( { 'SERVER_PORT': 0, 'SERVER_NAME': 'any', 'wsgi.url_scheme': 'any', 'wsgi.input': 'any', 'REQUEST_METHOD': 'GET' }, lambda x, y: _printme) try: from trac.test import MockPerm req.perm = MockPerm() except ImportError: pass req.authname = 'testuser' req.hdf = HDFWrapper( [] ) # replace by this if you want to generate HTML: req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs()) template, content_type = ImportModule(env)._do_preview( filename, 1, req) #sys.stdout = tempstdout #req.display(template, content_type or 'text/html') #open('/tmp/out.html', 'w').write(req.hdf.render(template, None)) return str(req.hdf) + "\n"
def test_add_stylesheet(self): req = Mock(cgi_location='/trac.cgi', hdf=HDFWrapper()) add_stylesheet(req, 'common/css/trac.css') self.assertEqual('text/css', req.hdf['chrome.links.stylesheet.0.type']) self.assertEqual('/trac.cgi/chrome/common/css/trac.css', req.hdf['chrome.links.stylesheet.0.href'])
def test_add_link_simple(self): req = Mock(hdf=HDFWrapper()) add_link(req, 'start', '/trac/wiki') self.assertEqual('/trac/wiki', req.hdf['chrome.links.start.0.href'])
def _get_hdf(self, req): hdf = HDFWrapper(loadpaths=Chrome(self.env).get_all_templates_dirs()) populate_hdf(hdf, self.env, req) return hdf
'to locate the Trac environment(s).') run_once = environ['wsgi.run_once'] env = env_error = None try: env = _open_environment(env_path, run_once=run_once) if env.base_url: environ['trac.base_url'] = env.base_url except TracError, e: env_error = e req = Request(environ, start_response) try: if not env and env_error: from trac.config import default_dir req.hdf = HDFWrapper([default_dir('templates')]) raise HTTPInternalError(env_error.message) try: try: dispatcher = RequestDispatcher(env) dispatcher.dispatch(req) except RequestDone: pass return req._response or [] finally: if not run_once: env.shutdown(threading._get_ident()) except HTTPException, e: if env: env.log.warn(e)
def dispatch(self, req): """Find a registered handler that matches the request and let it process it. In addition, this method initializes the HDF data set and adds the web site chrome. """ # FIXME: For backwards compatibility, should be removed in 0.11 self.env.href = req.href # FIXME in 0.11: self.env.abs_href = Href(self.env.base_url) self.env.abs_href = req.abs_href # Select the component that should handle the request chosen_handler = None early_error = None req.authname = 'anonymous' req.perm = NoPermissionCache() try: if not req.path_info or req.path_info == '/': chosen_handler = self.default_handler else: for handler in self.handlers: if handler.match_request(req): chosen_handler = handler break # Attach user information to the request early, so that # the IRequestFilter can see it while preprocessing if not getattr(chosen_handler, 'anonymous_request', False): try: req.authname = self.authenticate(req) req.perm = PermissionCache(self.env, req.authname) req.session = Session(self.env, req) req.form_token = self._get_form_token(req) except: req.authname = 'anonymous' req.perm = NoPermissionCache() early_error = sys.exc_info() chosen_handler = self._pre_process_request(req, chosen_handler) except: early_error = sys.exc_info() if not chosen_handler and not early_error: early_error = (HTTPNotFound('No handler matched request to %s', req.path_info), None, None) # Prepare HDF for the clearsilver template try: use_template = getattr(chosen_handler, 'use_template', True) req.hdf = None if use_template: chrome = Chrome(self.env) req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs()) populate_hdf(req.hdf, self.env, req) chrome.populate_hdf(req, chosen_handler) except: req.hdf = None # revert to sending plaintext error if not early_error: raise if early_error: try: self._post_process_request(req) except Exception, e: self.log.exception(e) raise early_error[0], early_error[1], early_error[2]
def test_htdocs_location(self): env = EnvironmentStub() req = Mock(hdf=HDFWrapper(), cgi_location='/trac.cgi', path_info='') Chrome(env).populate_hdf(req, None) self.assertEqual('/trac.cgi/chrome/common/', req.hdf['htdocs_location'])
def __init__(self, env): NotifyEmail.__init__(self, env) self.hdf = HDFWrapper(loadpaths=self.get_templates_dirs()) populate_hdf(self.hdf, env)