Changeset 4397

Show
Ignore:
Timestamp:
10/06/08 12:15:29 (2 months ago)
Author:
cboos
Message:

GraphvizPlugin: PEP:0008 clean-ups, truncate long lines

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • graphvizplugin/0.11/graphviz/graphviz.py

    r4395 r4397  
    139139        """ 
    140140 
    141         #self.log.debug('dir(self): %s' % str(dir(self))) 
    142141        self.formatter = formatter 
    143142        self.env = formatter.env 
     
    150149 
    151150        buf = StringIO() 
    152  
    153151 
    154152        ## Extract processor and format from name 
     
    193191        encoded_content = content.encode(self.encoding) 
    194192        sha_key  = sha.new(encoded_cmd + encoded_content).hexdigest() 
    195         img_name = '%s.%s.%s' % (sha_key, self.processor, self.out_format) # cache: hash.<dot>.<png> 
     193        img_name = '%s.%s.%s' % (sha_key, self.processor, self.out_format) 
     194        # cache: hash.<dot>.<png> 
    196195        img_path = os.path.join(self.cache_dir, img_name) 
    197         map_name = '%s.%s.map' % (sha_key, self.processor)       # cache: hash.<dot>.map 
     196        map_name = '%s.%s.map' % (sha_key, self.processor) 
     197        # cache: hash.<dot>.map 
    198198        map_path = os.path.join(self.cache_dir, map_name) 
    199199 
     
    207207            #self.log.debug('render_macro.URL_in_graph: %s' % str(URL_in_graph)) 
    208208            if URL_in_graph: # translate wiki TracLinks in URL 
    209                 #self.log.debug('content: %s' % content) 
    210209                content = self.expand_wiki_links(content) 
    211210 
     
    215214                cmd = [proc_cmd] + self.processor_options + \ 
    216215                        ['-Tsvg', '-o%s.svg' % img_path] 
    217                 #self.log.debug('render_macro: svg output - running command %s' % cmd) 
    218216                out, err = self.launch(cmd, encoded_content) 
    219217                if len(out) or len(err): 
     
    223221                # 2. SVG to PNG rasterization 
    224222                cmd = [self.rsvg_path, '--dpi-x=%d' % self.dpi, '--dpi-y=%d' % self.dpi, '%s.svg' % img_path, img_path] 
    225                 #self.log.debug('render_macro: svg to png - running command %s' % cmd) 
    226223                out, err = self.launch(cmd, None) 
    227224                if len(out) or len(err): 
     
    232229                cmd = [proc_cmd] + self.processor_options + \ 
    233230                        ['-T%s' % self.out_format, '-o%s' % img_path] 
    234                 #self.log.debug('render_macro: render other image formats - running command %s' % cmd) 
    235231                out, err = self.launch(cmd, encoded_content) 
    236232                if len(out) or len(err): 
     
    245241                    cmd = [proc_cmd] + self.processor_options + \ 
    246242                            ['-Tcmap', '-o%s' % map_path] 
    247                     #self.log.debug('render_macro: create map if not in cache - running command %s' % cmd) 
    248243                    out, err = self.launch(cmd, encoded_content) 
    249244                    if len(out) or len(err): 
     
    265260                (h_val, h_unit) = h.group(1,2) 
    266261                # Graphviz seems to underestimate height/width for SVG images, 
    267                 # so we have to adjust them. The correction factor seems to be constant. 
     262                # so we have to adjust them.  
     263                # The correction factor seems to be constant. 
    268264                [w_val, h_val] = [ 1.35 * x for x in (int(w_val), int(h_val))] 
    269                 dimensions = 'width="%(w_val)s%(w_unit)s" height="%(h_val)s%(h_unit)s"' % locals() 
     265                dimensions = ('width="%(w_val)s%(w_unit)s" ' 
     266                              'height="%(h_val)s%(h_unit)s"' % locals()) 
    270267 
    271268            except: 
     
    273270 
    274271            # insert SVG, IE compatibility 
    275             #buf.write('<!--[if IE]><embed src="%s/graphviz/%s" type="image/svg+xml" %s></embed><![endif]--> ' % (req.base_url, img_name, dimensions)) 
    276             #buf.write('<![if !IE]><object data="%s/graphviz/%s" type="image/svg+xml" %s>SVG Object</object><![endif]>' % (req.base_url, img_name, dimensions)) 
    277  
    278272            buf.write('<object data="%s/graphviz/%s" type="image/svg+xml" %s><embed src="%s/graphviz/%s" type="image/svg+xml" %s></embed></object>' % (req.base_url, img_name, dimensions, req.base_url, img_name, dimensions)) 
    279273 
     
    290284            buf.write('<img src="%s/graphviz/%s"/>' % (req.base_url, img_name)) 
    291285 
    292         #self.log.debug('buf.getvalue(): "%s"' % buf.getvalue()) 
    293286        return buf.getvalue() 
    294287 
     
    300293    def _expand_wiki_links(self, match): 
    301294        wiki_url = match.groups()[0] # TracLink ([1], source:file/, ...) 
    302         #self.log.debug('wiki_url: %s' % wiki_url) 
    303         #self.log.debug('self.env: %s' % str(self.env)) 
    304         #self.log.debug('self.formatter.req: %s' % str(self.formatter.req)) 
    305         html_url = wiki_to_oneliner(wiki_url, self.env, req=self.formatter.req)  # <a href="http://someurl">...</a> 
    306  
    307         #self.log.debug('html_url: %s' % html_url) 
     295        html_url = wiki_to_oneliner(wiki_url, self.env, req=self.formatter.req) 
     296        # <a href="http://someurl">...</a> 
     297 
    308298        href     = re.search('href="(.*?)"', html_url)   # http://someurl 
    309         #self.log.debug('href: %s' % href) 
    310299        url      = href and href.groups()[0] or html_url 
    311         #self.log.debug('url: %s' % url) 
    312300        if self.out_format == 'svg': 
    313301            format = 'URL="javascript:window.parent.location.href=\'%s\'"' 
    314302        else: 
    315303            format = 'URL="%s"' 
    316         #self.log.debug('expand_wiki_links url: %s' % str(format % url)) 
    317304        return format % url 
    318  
    319305 
    320306    def load_config(self): 
     
    339325                    self.cache_dir 
    340326            return True, self.show_err(msg) 
    341         #self.log.debug('self.cache_dir: %s' % self.cache_dir) 
    342  
    343  
    344         #Get optional configuration parameters from trac.ini. 
    345  
     327 
     328        # Get optional configuration parameters from trac.ini. 
    346329 
    347330        # check for the default processor - processor 
    348331        self.processor = self.config.get('graphviz', 'processor', Graphviz.Processors[0]) 
    349         #self.log.debug('self.processor: %s' % self.processor) 
    350  
    351332 
    352333        # check for the cmd_path entry and setup the various program command paths 
     
    366347 
    367348        self.cmds = {} 
    368         pname = self.find_cmd(self.processor, cmd_paths) 
     349        pname = self._find_cmd(self.processor, cmd_paths) 
    369350        if not pname: 
    370351            msg = 'The default processor, %s, was not found in %s.' % (self.processor, str(cmd_paths)) 
     
    372353 
    373354        for name in Graphviz.Processors: 
    374             pname = self.find_cmd(name, cmd_paths) 
     355            pname = self._find_cmd(name, cmd_paths) 
    375356 
    376357            if not pname: 
     
    381362 
    382363        # check for the default output format - out_format 
    383         self.out_format = self.config.get('graphviz', 'out_format', Graphviz.Formats[0]) 
    384         #self.log.debug('self.out_format: %s' % self.out_format
     364        self.out_format = self.config.get('graphviz', 'out_format',  
     365                                          Graphviz.Formats[0]
    385366 
    386367        # check if png anti aliasing should be done - png_antialias 
    387         self.png_anti_alias = self.boolean(self.config.get('graphviz', 'png_antialias', False)) 
    388         #self.log.debug('self.png_anti_alias: %s' % self.png_anti_alias) 
    389  
    390         if self.png_anti_alias == True: 
    391             self.rsvg_path = self.config.get('graphviz', 'rsvg_path', self.find_cmd('rsvg', cmd_paths)) 
     368        self.png_anti_alias = self.config.getbool( 
     369                'graphviz', 'png_antialias', False) 
     370 
     371        if self.png_anti_alias: 
     372            self.rsvg_path = self.config.get('graphviz', 'rsvg_path', 
     373                                             self._find_cmd('rsvg', cmd_paths)) 
    392374 
    393375            if not os.path.exists(self.rsvg_path): 
    394376                err = 'The rsvg program is set to "%s" but that path does not exist.' % self.rsvg_path 
    395377                return True, self.show_err(err) 
    396             #self.log.debug('self.rsvg_path: %s' % self.rsvg_path) 
    397378 
    398379        # get default graph/node/edge attributes 
     
    412393        self.cache_manager = self.boolean(self.config.get('graphviz', 'cache_manager', False)) 
    413394        if self.cache_manager: 
     395            # use IntOption 
    414396            self.cache_max_size  = int(self.config.get('graphviz', 'cache_max_size',  10000000)) 
    415397            self.cache_min_size  = int(self.config.get('graphviz', 'cache_min_size',  5000000)) 
    416398            self.cache_max_count = int(self.config.get('graphviz', 'cache_max_count', 2000)) 
    417399            self.cache_min_count = int(self.config.get('graphviz', 'cache_min_count', 1500)) 
    418  
    419             #self.log.debug('self.cache_max_count: %d' % self.cache_max_count) 
    420             #self.log.debug('self.cache_min_count: %d' % self.cache_min_count) 
    421             #self.log.debug('self.cache_max_size: %d'  % self.cache_max_size) 
    422             #self.log.debug('self.cache_min_size: %d'  % self.cache_min_size) 
    423400 
    424401        # is there a graphviz default DPI setting? 
     
    449426                             stdout=subprocess.PIPE, 
    450427                             stderr=subprocess.PIPE) 
    451  
    452428        if input: 
    453429            p.stdin.write(encoded_input) 
     
    519495            # time. Same for cache_min_size. 
    520496            if count > self.cache_max_count or size > self.cache_max_size: 
    521                 while len(atime_keys) and (self.cache_min_count < count or self.cache_min_size < size): 
     497                while atime_keys and (self.cache_min_count < count or   
     498                                      self.cache_min_size < size): 
    522499                    key = atime_keys.pop(0) 
    523500                    for file in atime_list[key]: 
    524                         #self.log.debug('clean_cache.unlink: %s' % file) 
    525501                        os.unlink(os.path.join(self.cache_dir, file)) 
    526502                        count = count - 1 
    527503                        size = size - entry_list[file][6] 
    528         else: 
    529             #self.log.debug('clean_cache: cache_manager not set') 
    530             pass 
    531  
    532504 
    533505    # Extra helper functions 
     
    557529 
    558530    # IRequestHandler methods 
     531 
    559532    def match_request(self, req): 
    560533        return req.path_info.startswith('/graphviz') 
     
    567540            return msg.getvalue() 
    568541 
    569         pieces = [item for item in req.path_info.split('/graphviz') if len(item)
    570  
    571         if len(pieces)
    572             pieces = [item for item in pieces[0].split('/') if len(item)
    573  
    574             if len(pieces)
     542        pieces = [item for item in req.path_info.split('/graphviz') if item
     543 
     544        if pieces
     545            pieces = [item for item in pieces[0].split('/') if item
     546 
     547            if pieces
    575548                name = pieces[0] 
    576549                img_path = os.path.join(self.cache_dir, name) 
     
    578551        return 
    579552 
    580  
    581     def find_cmd(self, cmd, paths): 
     553    # private methods 
     554 
     555    def _find_cmd(self, cmd, paths): 
    582556        exe_suffix = '' 
    583557        if sys.platform == 'win32':