Ticket #2583: patch_view_repository.diff
| File patch_view_repository.diff, 8.6 kB (added by jerojasro@gmail.com, 9 months ago) |
|---|
-
DiaView.py
old new 9 9 from trac.wiki.macros import ImageMacro 10 10 from trac.util.html import escape, html, Markup 11 11 12 12 13 class DiaViewMacro(WikiMacroBase): 14 def insert_attachment(self, att_path): 15 """att_path MUST exist in the filesystem""" 16 att_fn = att_path[att_path.rfind("/")+1:] #get filename without path 17 from trac.attachment import Attachment 18 try: 19 attachment = Attachment(self.env, self.module, self.id, att_fn) 20 except Exception: 21 db = self.env.get_db_cnx() 22 handle_ta = True 23 size = 0 24 time = 0 25 cursor = db.cursor() 26 cursor.execute("INSERT INTO attachment " 27 "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", 28 (self.module, self.id, att_fn, 29 size, time, 'PNG render of a DIA file', self.req.remote_user, 30 self.req.remote_addr)) 31 if handle_ta: 32 db.commit() 33 attachment = Attachment(self.env, "wiki", self.id, att_fn) 34 url = attachment.href(self.req) 35 att_url = attachment.href(self.req, format='raw') 36 return attachment 37 38 def dia2png(self, dia_path, png_path): 39 try: 40 diacmd = 'dia -l --filter=png --export='+png_path+' '+dia_path 41 self.env.log.info('Running Dia : %s',diacmd) 42 f = popen2.Popen4(diacmd) 43 lines = [] 44 while (f.poll() == -1): 45 lines += f.fromchild.readlines() 46 f.wait() 47 self.env.log.info('Exiting Dia') 48 except Exception, e: 49 self.env.log.info('Dia failed with exception= %s',e) 50 raise Exception('Dia execution failed.') 13 51 14 52 def render_macro(self, req, name, content): 53 self.req = req 15 54 # args will be null if the macro is called without parenthesis. 16 55 if not content: 17 56 return '' … … 52 91 style['border'] = ' %dpx solid' % int(val); 53 92 else: 54 93 attr[str(key)] = val # will be used as a __call__ keyword 55 56 94 # parse filespec argument to get module and id if contained. 57 95 parts = filespec.split(':') 58 96 url = None … … 69 107 except Exception: 70 108 browser_links = [] 71 109 if parts[0] in browser_links: # source:path 72 module, file = parts 110 url = parts[1] 111 self.module = "wiki" 112 self.id = ".diaview" 73 113 rev = None 74 if '@' in file: 75 file, rev = file.split('@') 76 url = req.href.browser(file, rev=rev) 77 raw_url = req.href.browser(file, rev=rev, format='raw') 114 if '@' in url: 115 url, rev = url.split('@') 116 repo = self.env.get_repository(req.authname) 117 node = repo.get_node(url, rev) 118 dia_filename = url[url.rfind("/")+1:] 119 dia_path = "/tmp/lalala.dia" #TODO FIXME choose a proper temp file name and delete it afterwards 120 dia_file = open(dia_path, "w") 121 dia_file.write(node.get_content().read()) 122 dia_file.close() 123 attach_base_path = self.env.path + "/attachments/" + self.module + "/" + self.id + "/" 124 #check dir existence 125 import os 126 if not os.path.isdir(attach_base_path): 127 os.makedirs(attach_base_path) 128 png_fn = attach_base_path + dia_filename.replace(".dia", ".png") 129 self.dia2png(dia_path, png_fn) 130 attachment = self.insert_attachment(png_fn) 131 img_url = attachment.href(req, format='raw') 78 132 desc = filespec 79 133 else: # #ticket:attachment or WikiPage:attachment 80 134 # FIXME: do something generic about shorthand forms... … … 90 144 else: 91 145 module = 'wiki' 92 146 elif len(parts) == 1: # attachment 93 # determine current object94 # FIXME: should be retrieved from the formatter...95 # ...and the formatter should be provided to the macro96 147 file = filespec 97 148 module, id = 'wiki', 'WikiStart' 98 149 path_info = req.path_info.split('/',2) … … 108 159 from trac.attachment import Attachment 109 160 attachment = Attachment(self.env, module, id, file) 110 161 url = attachment.href(req) 111 112 162 dia_url = attachment.href(req, format='raw') 113 dia_path = attachment.path114 dia_filename = attachment.filename163 dia_path = attachment.path 164 dia_filename = attachment.filename 115 165 img_url = dia_url.replace(".dia",".png") 116 img_path = dia_path.replace('.dia','.png') 117 img_filename = dia_filename.replace('.dia','.png') 118 166 img_path = dia_path.replace('.dia','.png') 167 img_filename = dia_filename.replace('.dia','.png') 119 168 self.env.log.info('Getting file modification times.') 120 #get file modification times121 169 try: 122 dia_mtime = os.path.getmtime(dia_path)170 dia_mtime = os.path.getmtime(dia_path) 123 171 except Exception: 124 raise Exception('File does not exist: %s', dia_path) 125 172 raise Exception('File does not exist: %s', dia_path) 126 173 try: 127 img_mtime = os.path.getmtime(img_path)174 img_mtime = os.path.getmtime(img_path) 128 175 except Exception: 129 img_mtime = 0 130 176 img_mtime = 0 131 177 self.env.log.info('Comparing dia and png file modification times : %s, %s',dia_mtime,img_mtime) 132 133 # if diagram is newer than image, then regenerate image134 178 if (dia_mtime > img_mtime): 135 try: 136 # TODO: read this comment and adjust the command line to taste 137 # You should probably use the correct path 138 # The options are correct for the 0.96.1, but you may bee to adjust them 139 diacmd = 'dia -l --filter=png --export='+img_path+' '+dia_path 140 self.env.log.info('Running Dia : %s',diacmd) 141 f = popen2.Popen4(diacmd) 142 lines = [] 143 while (f.poll() == -1): 144 lines += f.fromchild.readlines() 145 f.wait() 146 #ecode = os.spawnl(os.P_WAIT,'/usr/bin/dia','/usr/bin/dia','-l','--export-to-format=png', '--export='+img_path, dia_path) 147 self.env.log.info('Exiting Dia') 148 except Exception, e: 149 self.env.log.info('Dia failed with exception= %s',e) 150 raise Exception('Dia execution failed.') 179 self.dia2png(dia_path, img_path) 151 180 try: 152 attachment._fetch(img_filename)153 except Exception:154 db = self.env.get_db_cnx()155 handle_ta = True156 attachment.size = 0157 attachment.time = 0158 cursor = db.cursor()159 cursor.execute("INSERT INTO attachment "181 attachment._fetch(img_filename) 182 except Exception: 183 db = self.env.get_db_cnx() 184 handle_ta = True 185 attachment.size = 0 186 attachment.time = 0 187 cursor = db.cursor() 188 cursor.execute("INSERT INTO attachment " 160 189 "VALUES (%s,%s,%s,%s,%s,%s,%s,%s)", 161 190 (attachment.parent_type, attachment.parent_id, img_filename, 162 191 attachment.size, attachment.time, 'PNG render of a DIA file', attachment.author, 163 192 attachment.ipnr)) 164 attachment.filename = img_filename193 attachment.filename = img_filename 165 194 166 self.env.log.info('New attachment: %s', img_filename)195 self.env.log.info('New attachment: %s', img_filename) 167 196 168 if handle_ta:169 db.commit()197 if handle_ta: 198 db.commit() 170 199 171 200 desc = 'JPG render of a DIA file.' 172 201 for key in ['title', 'alt']: … … 176 205 attr['style'] = '; '.join(['%s:%s' % (k, escape(v)) 177 206 for k, v in style.iteritems()]) 178 207 result = Markup(html.IMG(src=img_url, **attr)).sanitize() 179 #if not nolink:180 # result = html.A(result, href=url, style='padding:0; border:none')181 208 return result
