Ticket #2583: patch_view_repository.diff

File patch_view_repository.diff, 8.6 kB (added by jerojasro@gmail.com, 9 months ago)

modifications to show dia files taking them from the repository

  • DiaView.py

    old new  
    99from trac.wiki.macros import ImageMacro 
    1010from trac.util.html import escape, html, Markup 
    1111 
     12 
    1213class 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.') 
    1351 
    1452    def render_macro(self, req, name, content): 
     53        self.req = req 
    1554        # args will be null if the macro is called without parenthesis. 
    1655        if not content: 
    1756            return '' 
     
    5291                    style['border'] = ' %dpx solid' % int(val); 
    5392                else: 
    5493                    attr[str(key)] = val # will be used as a __call__ keyword 
    55  
    5694        # parse filespec argument to get module and id if contained. 
    5795        parts = filespec.split(':') 
    5896        url = None 
     
    69107            except Exception: 
    70108                browser_links = [] 
    71109            if parts[0] in browser_links:   # source:path 
    72                 module, file = parts 
     110                url = parts[1] 
     111                self.module = "wiki" 
     112                self.id = ".diaview" 
    73113                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') 
    78132                desc = filespec 
    79133            else: # #ticket:attachment or WikiPage:attachment 
    80134                # FIXME: do something generic about shorthand forms... 
     
    90144                else: 
    91145                    module = 'wiki' 
    92146        elif len(parts) == 1:               # attachment 
    93             # determine current object 
    94             # FIXME: should be retrieved from the formatter... 
    95             # ...and the formatter should be provided to the macro 
    96147            file = filespec 
    97148            module, id = 'wiki', 'WikiStart' 
    98149            path_info = req.path_info.split('/',2) 
     
    108159            from trac.attachment import Attachment 
    109160            attachment = Attachment(self.env, module, id, file) 
    110161            url = attachment.href(req) 
    111  
    112162            dia_url = attachment.href(req, format='raw') 
    113            dia_path = attachment.path 
    114            dia_filename = attachment.filename 
     163            dia_path = attachment.path 
     164            dia_filename = attachment.filename 
    115165            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') 
    119168            self.env.log.info('Getting file modification times.') 
    120             #get file modification times 
    121169            try: 
    122               dia_mtime = os.path.getmtime(dia_path) 
     170                dia_mtime = os.path.getmtime(dia_path) 
    123171            except Exception: 
    124               raise Exception('File does not exist: %s', dia_path) 
    125  
     172                raise Exception('File does not exist: %s', dia_path) 
    126173            try: 
    127               img_mtime = os.path.getmtime(img_path) 
     174                img_mtime = os.path.getmtime(img_path) 
    128175            except Exception: 
    129               img_mtime = 0 
    130                  
     176                img_mtime = 0 
    131177            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 image 
    134178            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) 
    151180            try: 
    152              attachment._fetch(img_filename) 
    153            except Exception: 
    154               db = self.env.get_db_cnx() 
    155               handle_ta = True 
    156               attachment.size = 0 
    157               attachment.time = 0 
    158               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 " 
    160189                             "VALUES (%s,%s,%s,%s,%s,%s,%s,%s)", 
    161190                             (attachment.parent_type, attachment.parent_id, img_filename, 
    162191                              attachment.size, attachment.time, 'PNG render of a DIA file', attachment.author, 
    163192                              attachment.ipnr)) 
    164               attachment.filename = img_filename 
     193                attachment.filename = img_filename 
    165194 
    166               self.env.log.info('New attachment: %s', img_filename) 
     195                self.env.log.info('New attachment: %s', img_filename) 
    167196 
    168               if handle_ta: 
    169                   db.commit() 
     197                if handle_ta: 
     198                    db.commit() 
    170199 
    171200            desc = 'JPG render of a DIA file.' 
    172201        for key in ['title', 'alt']: 
     
    176205            attr['style'] = '; '.join(['%s:%s' % (k, escape(v)) 
    177206                                       for k, v in style.iteritems()]) 
    178207        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') 
    181208        return result