def source(self, req, args): arg = re.compile(":").split(args) if (len(arg) != 2): raise TracError('Usage: BibAdd(attachment:[path/to/]file)') realm = 'wiki' page = None file = arg[1] path_info = arg[1].split('/', 1) # greedy! split wikipath and filename if len(path_info) > 2: raise TracError('Usage: BibAdd(attachment:[path/to/]file)') elif len(path_info) == 1: file = path_info[0] page = req.args.get('page') if page is None: # TODO: clean solution page = 'WikiStart' bib = Attachment(self.env, realm, page, file) elif len(path_info) == 2: page = path_info[0] file = path_info[1] bib = Attachment(self.env, realm, page, file) file = bib.open() text = file.read() file.close() return _extract(text)
def process_request(self, req): """Process the request. For ClearSilver, return a (template_name, content_type) tuple, where `template` is the ClearSilver template to use (either a `neo_cs.CS` object, or the file name of the template), and `content_type` is the MIME type of the content. For Genshi, return a (template_name, data, content_type) tuple, where `data` is a dictionary of substitutions for the template. For both templating systems, "text/html" is assumed if `content_type` is `None`. Note that if template processing should not occur, this method can simply send the response itself and not return anything. """ # handle image setting if req.method == 'POST': self.set_default_image(req) req.redirect(req.get_header('referer') or req.href(req.path_info)) # GET default image ticket_id, size = self.ticket_id_and_size(req.path_info) image = DefaultTicketImage(self.env).default_image(ticket_id, size) assert image is not None # TODO better images = ImageTrac(self.env).images(ticket_id) attachment = Attachment(self.env, 'ticket', ticket_id, images[image][size]) mimeview = Mimeview(self.env) mimetype = mimeview.get_mimetype(attachment.filename) req.send(attachment.open().read(), mimetype)
def get_psf(self): """ parse attached "team project set" for Eclipse IDE returns a-list as [ Eclipse project name -> repository URL ] """ if self._psf is None: self._psf = {} psfResource = Resource('wiki', 'TeamProjectSet').child( 'attachment', 'projectSet.psf') if (self.compmgr[AttachmentModule].resource_exists(psfResource)): psf = Attachment(self.env, psfResource) def startElement(name, attrs): if name == 'project': attr = attrs.get('reference', "").split(',') self._psf[attr[2]] = urlparse( attr[1]).path # trim leading scheme/port reader = expat.ParserCreate() reader.StartElementHandler = startElement reader.ParseFile(psf.open()) # specify checkout dir in server subversion directory rm = RepositoryManager(self.env) repos = rm.get_all_repositories() for projectname in self._psf.keys(): path = self._psf.get(projectname) + '/.project' for reponame in repos: repo = rm.get_repository(reponame) if not repo: continue for npath in self.iter_lstrip(path): if not repo.has_node(npath, None): continue self._psf[projectname] = npath[:-9] # search .classpath here npath = npath[:-9] + '/.classpath' if not repo.has_node(npath, None): continue node = repo.get_node(npath, None) srcpathes = self.parse_classpath( node.get_content()) self._srcpathes[repo.reponame] = map( lambda x: npath[:-10] + x, srcpathes) else: # TeamProjectSet not found for repo in self.config.getlist('wiki', 'source_path', sep=';'): # expected: "svn: trunk/theproject/src/main/java trunk/theproject/src/test/java;" repo = repo.split(':') repo, srcpaths = len(repo) < 2 and ( "", repo[0] ) or repo # no leading reponame, use default repo self._srcpathes[repo] = self._srcpathes.get(repo, []) self._srcpathes[repo].extend([ s.rstrip('/') + '/' for s in srcpaths.split(' ') if s ]) return self._psf # { project_name: repository_url, ... }
def getAttachment(self, req, path): """ returns the content of an attachment. """ pagename, filename = os.path.split(path) attachment = Attachment(self.env, 'wiki', pagename, filename) req.perm(attachment.resource).require('ATTACHMENT_VIEW') return Binary(attachment.open().read())
def getAttachment(self, req, path): """ returns the content of an attachment. """ pagename, filename = posixpath.split(path) attachment = Attachment(self.env, 'wiki', pagename, filename) return xmlrpclib.Binary(attachment.open().read())
def getAttachment(self, req, path): """ returns the content of an attachment. """ pagename, filename = os.path.split(path) attachment = Attachment(self.env, "wiki", pagename, filename) req.perm(attachment.resource).require("ATTACHMENT_VIEW") return Binary(attachment.open().read())
def render_macro(self, req, name, content): # args will be null if the macro is called without parenthesis. if not content: return '' args = content.split(',') filespec = args[0] # parse filespec argument to get module and id if contained. parts = filespec.split(':') url = None if len(parts) == 3: # module:id:attachment if parts[0] in ['wiki', 'ticket']: module, id, file = parts else: raise Exception("%s module can't have attachments" % parts[0]) elif len(parts) == 2: from trac.versioncontrol.web_ui import BrowserModule try: browser_links = [ link for link, _ in BrowserModule( self.env).get_link_resolvers() ] except Exception: browser_links = [] if parts[0] in browser_links: # source:path module, file = parts rev = None if '@' in file: file, rev = file.split('@') url = req.href.browser(file, rev=rev) raw_url = req.href.browser(file, rev=rev, format='raw') desc = filespec else: # #ticket:attachment or WikiPage:attachment # FIXME: do something generic about shorthand forms... id, file = parts if id and id[0] == '#': module = 'ticket' id = id[1:] elif id == 'htdocs': raw_url = url = req.href.chrome('site', file) desc = os.path.basename(file) elif id in ('http', 'https', 'ftp'): # external URLs raw_url = url = desc = id + ':' + file else: module = 'wiki' elif len(parts) == 1: # attachment # determine current object # FIXME: should be retrieved from the formatter... # ...and the formatter should be provided to the macro file = filespec module, id = 'wiki', 'WikiStart' path_info = req.path_info.split('/', 2) if len(path_info) > 1: module = path_info[1] if len(path_info) > 2: id = path_info[2] if module not in ['wiki', 'ticket']: raise Exception('Cannot reference local attachment from here') else: raise Exception('No filespec given') if not url: # this is an attachment from trac.attachment import Attachment attachment = Attachment(self.env, module, id, file) url = attachment.href(req) raw_url = attachment.href(req, format='raw') desc = attachment.description width, height = swfsize(attachment.open()) if len(args) == 3: if args[1][0] == '*': width = int(width * float(args[1][1:])) else: width = args[1] if args[2][0] == '*': height = int(height * float(args[2][1:])) else: height = args[2] elif len(args) != 1: raise Exception('Too few arguments. (filespec, width, height)') else: if len(args) < 3: raise Exception('Too few arguments. (filespec, width, height)') else: width = args[1] height = args[2] vars = {'width': width, 'height': height, 'raw_url': raw_url} return """ <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" width="%(width)s" height="%(height)s"> <param name="movie" value="%(raw_url)s"> <param name="quality" value="low"> <param name="play" value="true"> <embed src="%(raw_url)s" quality="low" width="%(width)s" height="%(height)s" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed> </object> """ % dict(map(lambda i: (i[0], escapeHTML(str(i[1]))), vars.items()))
def getAttachment(self, req, ticket, filename): """ returns the content of an attachment. """ attachment = Attachment(self.env, 'ticket', ticket, filename) req.perm(attachment.resource).require('ATTACHMENT_VIEW') return Binary(attachment.open().read())
def expand_macro(self,formatter,name,content): args, kwargs = parse_args(content, strict=False) if len(args) != 1: raise TracError('[[Usage: BibAdd(source:file@rev) or BibAdd(attachment:wikipage/file) or BibAdd(attachment:file)]] ') whom = re.compile(":|@").split(args[0]) file = None rev = None pos = None path = None entry = None entries = None # load the file from the repository if whom[0] == 'source': if len(whom) < 2: raise TracError('[[Missing argument(s) for citing from source; Usage: BibAdd(source:file@rev)]]') elif len(whom) == 2: rev = 'latest' else: rev = whom[2] file = whom[1] repos = self.env.get_repository() bib = repos.get_node(file, rev) file = bib.get_content() string = file.read() # load the file from the wiki attachments elif whom[0] == 'attachment': if (len(whom) != 2): raise TracError('Wrong syntax for environment \'attachment\'; Usage: BibAdd(attachment:file)') pos = 'wiki' page = None file = whom[1] path_info = whom[1].split('/',1) if len(path_info) == 2: page = path_info[0] file = path_info[1] else: page = formatter.req.args.get('page') if (page == None): page = 'WikiStart' bib = Attachment(self.env,pos,page,file) file = bib.open() string = file.read() # use wiki page itself elif whom[0] == 'wiki': if (len(whom) != 2): raise TracError('Wrong syntax for environment \'wiki\'; Usage BibAdd(wiki:page)') page = WikiPage(self.env,whom[1]) if page.exists: if '{{{' in page.text and '}}}' in page.text: tmp = re.compile('{{{|}}}',2).split(page.text) string = tmp[1] else: raise TracError('No code block on page \'' + whom[1] + '\' found.') else: raise TracError('No wiki page named \'' + whom[1] + '\' found.') else: raise TracError('Unknown location \''+ whom[0] +'\'') try: # handle all data as unicode objects try: u = unicode(string,"utf-8") except TypeError: u = string entries = extract_entries(u) except UnicodeDecodeError: raise TracError("A UnicodeDecodeError occured while loading the data. Try to save the file in UTF-8 encoding.") if entries == None: raise TracError('No entries from file \''+ args[0] +'\' loaded.') bibdb = getattr(formatter, BIBDB,{}) bibdb.update(entries) setattr(formatter,BIBDB,bibdb)
def render_macro(self, req, name, content): # args will be null if the macro is called without parenthesis. if not content: return '' args = content.split(',') filespec = args[0] # parse filespec argument to get module and id if contained. parts = filespec.split(':') url = None if len(parts) == 3: # module:id:attachment if parts[0] in ['wiki', 'ticket']: module, id, file = parts else: raise Exception("%s module can't have attachments" % parts[0]) elif len(parts) == 2: from trac.versioncontrol.web_ui import BrowserModule try: browser_links = [link for link,_ in BrowserModule(self.env).get_link_resolvers()] except Exception: browser_links = [] if parts[0] in browser_links: # source:path module, file = parts rev = None if '@' in file: file, rev = file.split('@') url = req.href.browser(file, rev=rev) raw_url = req.href.browser(file, rev=rev, format='raw') desc = filespec else: # #ticket:attachment or WikiPage:attachment # FIXME: do something generic about shorthand forms... id, file = parts if id and id[0] == '#': module = 'ticket' id = id[1:] elif id == 'htdocs': raw_url = url = req.href.chrome('site', file) desc = os.path.basename(file) elif id in ('http', 'https', 'ftp'): # external URLs raw_url = url = desc = id+':'+file else: module = 'wiki' elif len(parts) == 1: # attachment # determine current object # FIXME: should be retrieved from the formatter... # ...and the formatter should be provided to the macro file = filespec module, id = 'wiki', 'WikiStart' path_info = req.path_info.split('/',2) if len(path_info) > 1: module = path_info[1] if len(path_info) > 2: id = path_info[2] if module not in ['wiki', 'ticket']: raise Exception('Cannot reference local attachment from here') else: raise Exception('No filespec given') if not url: # this is an attachment from trac.attachment import Attachment attachment = Attachment(self.env, module, id, file) url = attachment.href(req) raw_url = attachment.href(req, format='raw') desc = attachment.description width, height = swfsize(attachment.open()) if len(args) == 3: if args[1][0] == '*': width = int(width * float(args[1][1:])) else: width = args[1] if args[2][0] == '*': height = int(height * float(args[2][1:])) else: height = args[2] elif len(args) != 1: raise Exception('Too few arguments. (filespec, width, height)') else: if len(args) < 3: raise Exception('Too few arguments. (filespec, width, height)') else: width = args[1] height = args[2] vars = { 'width': width, 'height': height, 'raw_url': raw_url } return """ <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" width="%(width)s" height="%(height)s"> <param name="movie" value="%(raw_url)s"> <param name="quality" value="low"> <param name="play" value="true"> <embed src="%(raw_url)s" quality="low" width="%(width)s" height="%(height)s" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed> </object> """ % dict(map(lambda i: (i[0], escapeHTML(str(i[1]))), vars.items()))
def expand_macro(self, formatter, name, content): args, kwargs = parse_args(content, strict=False) if len(args) != 1: raise TracError( '[[Usage: BibAdd(source:file@rev) or BibAdd(attachment:wikipage/file) or BibAdd(attachment:file)]] ' ) whom = re.compile(":|@").split(args[0]) file = None rev = None pos = None path = None entry = None entries = None # load the file from the repository if whom[0] == 'source': if len(whom) < 2: raise TracError( '[[Missing argument(s) for citing from source; Usage: BibAdd(source:file@rev)]]' ) elif len(whom) == 2: rev = 'latest' else: rev = whom[2] file = whom[1] repos = self.env.get_repository() bib = repos.get_node(file, rev) file = bib.get_content() string = file.read() # load the file from the wiki attachments elif whom[0] == 'attachment': if (len(whom) != 2): raise TracError( 'Wrong syntax for environment \'attachment\'; Usage: BibAdd(attachment:file)' ) pos = 'wiki' page = None file = whom[1] path_info = whom[1].split('/', 1) if len(path_info) == 2: page = path_info[0] file = path_info[1] else: page = formatter.req.args.get('page') if (page == None): page = 'WikiStart' bib = Attachment(self.env, pos, page, file) file = bib.open() string = file.read() # use wiki page itself elif whom[0] == 'wiki': if (len(whom) != 2): raise TracError( 'Wrong syntax for environment \'wiki\'; Usage BibAdd(wiki:page)' ) page = WikiPage(self.env, whom[1]) if page.exists: if '{{{' in page.text and '}}}' in page.text: tmp = re.compile('{{{|}}}', 2).split(page.text) string = tmp[1] else: raise TracError('No code block on page \'' + whom[1] + '\' found.') else: raise TracError('No wiki page named \'' + whom[1] + '\' found.') else: raise TracError('Unknown location \'' + whom[0] + '\'') try: # handle all data as unicode objects try: u = unicode(string, "utf-8") except TypeError: u = string entries = extract_entries(u) except UnicodeDecodeError: raise TracError( "A UnicodeDecodeError occured while loading the data. Try to save the file in UTF-8 encoding." ) if entries == None: raise TracError('No entries from file \'' + args[0] + '\' loaded.') bibdb = getattr(formatter, BIBDB, {}) bibdb.update(entries) setattr(formatter, BIBDB, bibdb)
def getAttachment(self, req, ticket, filename): """ returns the content of an attachment. """ attachment = Attachment(self.env, 'ticket', ticket, filename) return xmlrpclib.Binary(attachment.open().read())