def process_request(self, req): pieces = [item for item in req.path_info.split('/tracmath') if item] if pieces: pieces = [item for item in pieces[0].split('/') if item] if pieces: name = pieces[0] img_path = os.path.join(self.cacheDirectory, name) return req.send_file(img_path, mimeview.get_mimetype(img_path)) return
def process_request(self, req): errmsg = self._load_config() if errmsg: return self._show_err(errmsg) pieces = [item for item in req.path_info.split('/tracmath') if item] if pieces: pieces = [item for item in pieces[0].split('/') if item] if pieces: name = pieces[0] img_path = os.path.join(self.cache_dir, name) return req.send_file(img_path, mimeview.get_mimetype(img_path)) return
def get_icon_data(self, req): icon = {} icon_src = icon_abs_src = self.env.project_icon if icon_src: if not icon_src.startswith('/') and icon_src.find('://') == -1: if '/' in icon_src: icon_abs_src = req.abs_href.chrome(icon_src) icon_src = req.href.chrome(icon_src) else: icon_abs_src = req.abs_href.chrome('common', icon_src) icon_src = req.href.chrome('common', icon_src) mimetype = get_mimetype(icon_src) icon = {'src': icon_src, 'abs_src': icon_abs_src, 'mimetype': mimetype} return icon
def process_request(self, req): # check and load the configuration trouble, msg = self.load_config() if trouble: return msg.getvalue() pieces = [item for item in req.path_info.split('/gantt') if len(item)] if len(pieces): pieces = [item for item in pieces[0].split('/') if len(item)] if len(pieces): name = pieces[0] img_path = os.path.join(self.cache_dir, name) return req.send_file(img_path, mimeview.get_mimetype(img_path)) return
def process_request(self, req): prefix = req.args['prefix'] filename = req.args['filename'] dirs = [] for provider in self.template_providers: for dir in [os.path.normpath(dir[1]) for dir in provider.get_htdocs_dirs() if dir[0] == prefix]: dirs.append(dir) path = os.path.normpath(os.path.join(dir, filename)) assert os.path.commonprefix([dir, path]) == dir if os.path.isfile(path): req.send_file(path, get_mimetype(path)) self.log.warning('File %s not found in any of %s', filename, dirs) raise HTTPNotFound('File %s not found', filename)
def process_request(self, req): prefix = req.args['prefix'] filename = req.args['filename'] dirs = [] for provider in self.template_providers: for dir in [os.path.normpath(dir[1]) for dir in provider.get_htdocs_dirs() if dir[0] == prefix]: dirs.append(dir) path = os.path.normpath(os.path.join(dir, filename)) assert os.path.commonprefix([dir, path]) == dir if os.path.isfile(path): req.send_file(path, mimeview.get_mimetype(path)) self.log.warning('File %s not found in any of %s', filename, dirs) raise HTTPNotFound(u'Fichier %s non trouvé', filename)
def source( req, argument_text, env, aswiki =False): from trac.versioncontrol.web_ui.util import get_existing_node, get_path_rev_line ## XXX above .util is shaded by trac.util? path,rev,line = get_path_rev_line( argument_text) from trac.mimeview import Mimeview, get_mimetype, content_to_unicode repos = env.get_repository() # req.authname) node = get_existing_node( env, repos, path, rev) content = node.get_content().read() if aswiki: mimetype = 'text/x-trac-wiki' r = wiki_to_html( content_to_unicode( env, content, mimetype), env=env, req=req) else: mimetype = get_mimetype( os.path.basename( path) ) or 'text/plain ' mimeview = Mimeview( env) #charset = mimeview.get_charset( content, mimetype) #node.content_type? #content = util.to_utf8( content, charset ) r = mimeview.render( req, mimetype, content) return r, None
def populate_hdf(self, req, handler): """Add chrome-related data to the HDF.""" # Provided for template customization req.hdf['HTTP.PathInfo'] = req.path_info href = Href(req.base_path) req.hdf['chrome.href'] = href.chrome() htdocs_location = self.htdocs_location or href.chrome('common') req.hdf['htdocs_location'] = htdocs_location.rstrip('/') + '/' # HTML <head> links add_link(req, 'start', req.href.wiki()) add_link(req, 'search', req.href.search()) add_link(req, 'help', req.href.wiki('TracGuide')) add_stylesheet(req, 'common/css/trac.css') add_script(req, 'common/js/trac.js') icon = self.env.project_icon if icon: if not icon.startswith('/') and icon.find('://') == -1: if '/' in icon: icon = href.chrome(icon) else: icon = href.chrome('common', icon) mimetype = mimeview.get_mimetype(icon) add_link(req, 'icon', icon, mimetype=mimetype) add_link(req, 'shortcut icon', icon, mimetype=mimetype) # Logo image logo_src = self.logo_src if logo_src: logo_src_abs = logo_src.startswith('http://') or \ logo_src.startswith('https://') if not logo_src.startswith('/') and not logo_src_abs: if '/' in logo_src: logo_src = href.chrome(logo_src) else: logo_src = href.chrome('common', logo_src) width = self.logo_width > -1 and self.logo_width height = self.logo_height > -1 and self.logo_height req.hdf['chrome.logo'] = { 'link': self.logo_link, 'src': logo_src, 'src_abs': logo_src_abs, 'alt': self.logo_alt, 'width': width, 'height': height } else: req.hdf['chrome.logo.link'] = self.logo_link # Navigation links navigation = {} active = None for contributor in self.navigation_contributors: for category, name, text in contributor.get_navigation_items(req): navigation.setdefault(category, {})[name] = text if contributor is handler: active = contributor.get_active_navigation_item(req) for category, items in [(k, v.items()) for k, v in navigation.items()]: category_order = category + '_order' if hasattr(self, category_order): order = getattr(self, category_order) def navcmp(x, y): if x[0] not in order: return int(y[0] in order) if y[0] not in order: return -int(x[0] in order) return cmp(order.index(x[0]), order.index(y[0])) items.sort(navcmp) for name, text in items: req.hdf['chrome.nav.%s.%s' % (category, name)] = text if name == active: req.hdf['chrome.nav.%s.%s.active' % (category, name)] = 1
def _render_file(self, req, repos, node, rev=None): req.perm.assert_permission('FILE_VIEW') changeset = repos.get_changeset(node.rev) req.hdf['file'] = { 'rev': node.rev, 'changeset_href': util.escape(self.env.href.changeset(node.rev)), 'date': time.strftime('%x %X', time.localtime(changeset.date)), 'age': util.pretty_timedelta(changeset.date), 'author': changeset.author or 'anonymous', 'message': wiki_to_html(changeset.message or '--', self.env, req, escape_newlines=True) } mime_type = node.content_type if not mime_type or mime_type == 'application/octet-stream': mime_type = get_mimetype(node.name) or mime_type or 'text/plain' # We don't have to guess if the charset is specified in the # svn:mime-type property ctpos = mime_type.find('charset=') if ctpos >= 0: charset = mime_type[ctpos + 8:] else: charset = None format = req.args.get('format') if format in ['raw', 'txt']: req.send_response(200) req.send_header('Content-Type', format == 'txt' and 'text/plain' or mime_type) req.send_header('Content-Length', node.content_length) req.send_header('Last-Modified', util.http_date(node.last_modified)) req.end_headers() content = node.get_content() while 1: chunk = content.read(CHUNK_SIZE) if not chunk: raise RequestDone req.write(chunk) else: # Generate HTML preview max_preview_size = int( self.config.get('mimeviewer', 'max_preview_size', '262144')) content = node.get_content().read(max_preview_size) max_size_reached = len(content) == max_preview_size if not charset: charset = detect_unicode(content) or \ self.config.get('trac', 'default_charset') if not is_binary(content): content = util.to_utf8(content, charset) if mime_type != 'text/plain': plain_href = self.env.href.browser(node.path, rev=rev and node.rev, format='txt') add_link(req, 'alternate', plain_href, 'Plain Text', 'text/plain') if max_size_reached: req.hdf['file.max_file_size_reached'] = 1 req.hdf['file.max_file_size'] = max_preview_size preview = ' ' else: preview = Mimeview(self.env).render(req, mime_type, content, node.name, node.rev, annotations=['lineno']) req.hdf['file.preview'] = preview raw_href = self.env.href.browser(node.path, rev=rev and node.rev, format='raw') req.hdf['file.raw_href'] = util.escape(raw_href) add_link(req, 'alternate', raw_href, 'Original Format', mime_type) add_stylesheet(req, 'common/css/code.css')
def process_request(self, req): req.perm.assert_permission("NARCISSUS_VIEW") img = req.args.get("img", None) if img: self._load_config() img_path = os.path.join(self.cache_dir, img) return req.send_file(img_path, mimeview.get_mimetype(img_path)) # print>>sys.stderr, 'start request' add_stylesheet(req, "nar/css/narcissus.css") add_script(req, "nar/js/narcissus.js") req.hdf["trac.href.main"] = self.env.href.narcissus() req.hdf["trac.href.configure"] = self.env.href.configure() req.hdf["trac.href.user_guide"] = self.env.href.user_guide() req.hdf["main"] = "yes" req.hdf["config"] = "no" req.hdf["user_guide"] = "no" req.hdf["error.exists"] = "no" self.db = self.env.get_db_cnx() self._settings = NarcissusSettings(self.db) # Ensure Narciussus has been configured to incluude some group members if not self._settings.members: req.hdf[ "msg" ] = """No group members have been selected for visualisation. Please add group members using the configuration page.""" return "narcissus.cs", None # Parse the from date and adjust the timestamp to the last second of # the day (taken from Timeline.py, (c) Edgewall Software) t = time.localtime() if req.args.has_key("from"): try: t = time.strptime(req.args.get("from"), "%x") except: pass fromdate = time.mktime((t[0], t[1], t[2], 23, 59, 59, t[6], t[7], t[8])) try: daysback = max(0, int(req.args.get("daysback", ""))) except ValueError: daysback = 14 # Default value of one fortnight req.hdf["date.end"] = fromdate req.hdf["date.from"] = format_date(fromdate) req.hdf["date.daysback"] = daysback self._update_data(req) self._create_legend(req) trouble, msg = self._get_font() if trouble: req.hdf["error.exists"] = "yes" req.hdf["error.msg"] = msg.getvalue() else: view = req.args.get("view", "group") req.hdf["view"] = view if view == "group": self._draw_group(req) elif view == "project": self._draw_project(req), elif view == "ticket": self._draw_ticket(req) # print>>sys.stderr, 'return request' return "narcissus.cs", None
def process_request(self, req): req.perm.assert_permission('NARCISSUS_VIEW') img = req.args.get('img', None) if img: self._load_config() img_path = os.path.join(self.cache_dir, img) return req.send_file(img_path, mimeview.get_mimetype(img_path)) params = {} params['page'] = 'narcissus' params['href_narcissus'] = self.env.href.narcissus() params['href_configure'] = self.env.href.narcissus('configure') params['href_user_guide'] = self.env.href.narcissus('user_guide') params['error'] = None params['msg'] = "" self.db = self.env.get_db_cnx() self._settings = NarcissusSettings(self.db) # Ensure Narciussus has been configured to incluude some group members if not self._settings.members: params[ 'msg'] = '''No group members have been selected for visualisation. Please add group members using the configuration page.''' return 'narcissus.xhtml', params, None # Parse the from date and adjust the timestamp to the last second of # the day (taken from Timeline.py, (c) Edgewall Software) t = time.localtime() if req.args.has_key('from'): try: t = time.strptime(req.args.get('from'), '%x') except: pass fromdate = time.mktime( (t[0], t[1], t[2], 23, 59, 59, t[6], t[7], t[8])) try: daysback = max(0, int(req.args.get('daysback', ''))) except ValueError: daysback = 14 # Default value of one fortnight params['date_end'] = fromdate params['date_from'] = format_date(fromdate) params['date_daysback'] = daysback self._update_data(req) self._create_legend(req, params) trouble, msg = self._get_font() if trouble: params['error'] = msg.getvalue() else: view = req.args.get('view', 'group') params['view'] = view if view == 'group': self._draw_group(req, params) elif view == 'project': self._draw_project(req, params), elif view == 'ticket': self._draw_ticket(req, params) #print>>sys.stderr, 'return request' return 'narcissus.xhtml', params, None
def populate_hdf(self, req, handler): """Add chrome-related data to the HDF.""" # Provided for template customization req.hdf['HTTP.PathInfo'] = req.path_info href = Href(req.cgi_location) req.hdf['chrome.href'] = href.chrome() htdocs_location = self.config.get('trac', 'htdocs_location', href.chrome('common')) req.hdf['htdocs_location'] = htdocs_location.rstrip('/') + '/' # HTML <head> links add_link(req, 'start', self.env.href.wiki()) add_link(req, 'search', self.env.href.search()) add_link(req, 'help', self.env.href.wiki('TracGuide')) add_stylesheet(req, 'common/css/trac.css') icon = self.config.get('project', 'icon') if icon: if not icon.startswith('/') and icon.find('://') == -1: if '/' in icon: icon = href.chrome(icon) else: icon = href.chrome('common', icon) mimetype = mimeview.get_mimetype(icon) add_link(req, 'icon', icon, mimetype=mimetype) add_link(req, 'shortcut icon', icon, mimetype=mimetype) # Logo image logo_link = self.config.get('header_logo', 'link') logo_src = self.config.get('header_logo', 'src') if logo_src: logo_src_abs = logo_src.startswith('http://') or \ logo_src.startswith('https://') if not logo_src.startswith('/') and not logo_src_abs: if '/' in logo_src: logo_src = href.chrome(logo_src) else: logo_src = href.chrome('common', logo_src) req.hdf['chrome.logo'] = { 'link': util.escape(logo_link), 'src': util.escape(logo_src), 'src_abs': util.escape(logo_src_abs), 'alt': util.escape(self.config.get('header_logo', 'alt')), 'width': self.config.get('header_logo', 'width', ''), 'height': self.config.get('header_logo', 'height', '') } else: req.hdf['chrome.logo.link'] = util.escape(logo_link) # Navigation links navigation = {} active = None for contributor in self.navigation_contributors: for category, name, text in contributor.get_navigation_items(req): navigation.setdefault(category, {})[name] = text if contributor is handler: active = contributor.get_active_navigation_item(req) for category, items in [(k, v.items()) for k, v in navigation.items()]: order = [ x.strip() for x in self.config.get('trac', category).split(',') ] def navcmp(x, y): if x[0] not in order: return int(y[0] in order) if y[0] not in order: return -int(x[0] in order) return cmp(order.index(x[0]), order.index(y[0])) items.sort(navcmp) for name, text in items: req.hdf['chrome.nav.%s.%s' % (category, name)] = text if name == active: req.hdf['chrome.nav.%s.%s.active' % (category, name)] = 1
def process_request(self, req): req.perm.assert_permission('NARCISSUS_VIEW') img = req.args.get('img', None) if img: self._load_config() img_path = os.path.join(self.cache_dir, img) return req.send_file(img_path, mimeview.get_mimetype(img_path)) ### add_stylesheet(req, 'nar/css/narcissus.css') ### add_script(req, 'nar/js/narcissus.js') params = {} params['page'] = 'narcissus' params['href_narcissus'] = self.env.href.narcissus() params['href_configure'] = self.env.href.narcissus('configure') params['href_user_guide'] = self.env.href.narcissus('user_guide') params['error'] = None params['msg'] = "" self.db = self.env.get_db_cnx() self._settings = NarcissusSettings(self.db) # Ensure Narciussus has been configured to incluude some group members if not self._settings.members: params['msg'] = '''No group members have been selected for visualisation. Please add group members using the configuration page.''' return 'narcissus.xhtml', params, None # Parse the from date and adjust the timestamp to the last second of # the day (taken from Timeline.py, (c) Edgewall Software) t = time.localtime() if req.args.has_key('from'): try: t = time.strptime(req.args.get('from'), '%x') except: pass fromdate = time.mktime((t[0], t[1], t[2], 23, 59, 59, t[6], t[7], t[8])) try: daysback = max(0, int(req.args.get('daysback', ''))) except ValueError: daysback = 14 # Default value of one fortnight params['date_end'] = fromdate params['date_from'] = format_date(fromdate) params['date_daysback'] = daysback self._update_data(req) self._create_legend(req, params) trouble, msg = self._get_font() if trouble: params['error'] = msg.getvalue() else: view = req.args.get('view', 'group') params['view'] = view if view == 'group': self._draw_group(req, params) elif view == 'project': self._draw_project(req, params), elif view == 'ticket': self._draw_ticket(req, params) #print>>sys.stderr, 'return request' return 'narcissus.xhtml', params, None
def dispatch_request(environ, start_response): """Main entry point for the Trac web interface. @param environ: the WSGI environment dict @param start_response: the WSGI callback for starting the response """ if 'mod_python.options' in environ: options = environ['mod_python.options'] environ.setdefault('trac.env_path', options.get('TracEnv')) environ.setdefault('trac.env_parent_dir', options.get('TracEnvParentDir')) environ.setdefault('trac.env_index_template', options.get('TracEnvIndexTemplate')) environ.setdefault('trac.template_vars', options.get('TracTemplateVars')) environ.setdefault('trac.locale', options.get('TracLocale')) if 'TracUriRoot' in options: # Special handling of SCRIPT_NAME/PATH_INFO for mod_python, which # tends to get confused for whatever reason root_uri = options['TracUriRoot'].rstrip('/') request_uri = environ['REQUEST_URI'].split('?', 1)[0] if not request_uri.startswith(root_uri): raise ValueError('TracUriRoot set to %s but request URL ' 'is %s' % (root_uri, request_uri)) environ['SCRIPT_NAME'] = root_uri environ['PATH_INFO'] = urllib.unquote(request_uri[len(root_uri):]) else: environ.setdefault('trac.env_path', os.getenv('TRAC_ENV')) environ.setdefault('trac.env_parent_dir', os.getenv('TRAC_ENV_PARENT_DIR')) environ.setdefault('trac.env_index_template', os.getenv('TRAC_ENV_INDEX_TEMPLATE')) environ.setdefault('trac.template_vars', os.getenv('TRAC_TEMPLATE_VARS')) environ.setdefault('trac.locale', '') locale.setlocale(locale.LC_ALL, environ['trac.locale']) env_parent_dir = environ.get('trac.env_parent_dir') env_paths = environ.get('trac.env_paths', []) import dircache paths = dircache.listdir(env_parent_dir)[:] dircache.annotate(env_parent_dir, paths) env_paths += [os.path.join(env_parent_dir, project) \ for project in paths if project[-1] == '/' and project != '.egg-cache/'] # Determine the environment env_path = environ.get('trac.env_path') req = Request(environ, start_response) if not env_path: if env_parent_dir or env_paths: try: # The first component of the path is the base name of the # environment path_info = environ.get('PATH_INFO', '').lstrip('/').split('/') env_name = path_info.pop(0) if env_name == '': env_name = 'all' if env_name == 'all': try: # go on... #req = Request(environ, start_response) dispatcher = TramRequestDispatcher(get_allenv(environ, req)) dispatcher.dispatch(environ, start_response, env_parent_dir, env_paths, req) except RequestDone, e: return req._response or [] #except Exception, e: #raise Exception(e) if env_name == 'chrome': env_name = 'all' path_info.insert(0,'chrome') # here we send tram chrome, denoted by using the chrome/tram/ parent possible_tram_chrome = '/'.join(path_info) if (possible_tram_chrome.startswith('chrome/tram/')): file = possible_tram_chrome[12:] path = os.path.dirname(__file__) + "/htdocs/" + file if (os.path.isfile(path)): #req = Request(environ, start_response) try: req.send_file(path, mimeview.get_mimetype(path)) except RequestDone, e: return req._response or []; else: raise HTTPNotFound('File %s not found', file) if not env_name: # We need to be in the 'all' environment for the auth cookies etc #req = Request(environ, start_response) req.redirect(req.abs_href() + '/all') except HTTPException, e: #req = Request(environ, start_response) prepare_request(req, environ) loadpaths = [] loadpaths.insert(0, os.path.dirname(__file__) + "/templates/") data = { 'title': e.reason or 'Error', 'type': 'TracError', #'message': e.message 'message': e.reason or 'Error' } try: req.send_error(sys.exc_info(), status=e.code, env=Environment(env_path), data=data) except RequestDone, e: return []