Changeset 4395

Show
Ignore:
Timestamp:
10/06/08 12:08:55 (3 months ago)
Author:
cboos
Message:

GraphvizPlugin: fixed handling of default parameters (#3542).

Besides, this supersedes the fixes for #3073 and #3605.
Note that the hashes will changes, so now might be a good time to clean-up your graphviz cache_dir, as everything will get rebuilt.

Files:

Legend:

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

    r4394 r4395  
    1818 
    1919from StringIO import StringIO 
     20import locale 
    2021import sha 
    2122import os 
     
    5657    encoding = Option("graphviz", "encoding", 'utf-8', 
    5758            """The encoding which should be used for communicating with 
    58             Graphviz
     59            Graphviz (should match -Gcharset if given)
    5960            """) 
    6061 
     
    188189            return buf.getvalue() 
    189190 
    190         if type(content) == type(u''): 
    191             content  = content.encode(self.encoding) 
    192             sha_text = self.processor.encode(self.encoding) + \ 
    193                     self.processor_options.encode(self.encoding) + content 
    194  
    195         else: 
    196             sha_text = self.processor + self.processor_options + content 
    197  
    198         sha_key  = sha.new(sha_text).hexdigest() 
     191        encoded_cmd = (self.processor + unicode(self.processor_options)) \ 
     192                .encode(self.encoding) 
     193        encoded_content = content.encode(self.encoding) 
     194        sha_key  = sha.new(encoded_cmd + encoded_content).hexdigest() 
    199195        img_name = '%s.%s.%s' % (sha_key, self.processor, self.out_format) # cache: hash.<dot>.<png> 
    200196        img_path = os.path.join(self.cache_dir, img_name) 
     
    217213            if self.out_format == 'png' and self.png_anti_alias == True: 
    218214                # 1. SVG output 
    219                 cmd = [proc_cmd, self.processor_options, '-Tsvg', '-o%s.svg' % img_path] 
     215                cmd = [proc_cmd] + self.processor_options + \ 
     216                        ['-Tsvg', '-o%s.svg' % img_path] 
    220217                #self.log.debug('render_macro: svg output - running command %s' % cmd) 
    221                 out, err = self.launch(cmd, content) 
     218                out, err = self.launch(cmd, encoded_content) 
    222219                if len(out) or len(err): 
    223220                    msg = 'The command\n   %s\nfailed with the the following output:\n%s\n%s' % (cmd, out, err) 
     
    233230             
    234231            else: # Render other image formats 
    235                 cmd = [proc_cmd, self.processor_options, '-T%s' % self.out_format, '-o%s' % img_path] 
     232                cmd = [proc_cmd] + self.processor_options + \ 
     233                        ['-T%s' % self.out_format, '-o%s' % img_path] 
    236234                #self.log.debug('render_macro: render other image formats - running command %s' % cmd) 
    237                 out, err = self.launch(cmd, content) 
     235                out, err = self.launch(cmd, encoded_content) 
    238236                if len(out) or len(err): 
    239237                    msg = 'The command\n   %s\nfailed with the the following output:\n%s\n%s' % (cmd, out, err) 
     
    245243                # Create the map if not in cache 
    246244                if not os.path.exists(map_path): 
    247                     cmd = [proc_cmd, self.processor_options, '-Tcmap', '-o%s' % map_path] 
     245                    cmd = [proc_cmd] + self.processor_options + \ 
     246                            ['-Tcmap', '-o%s' % map_path] 
    248247                    #self.log.debug('render_macro: create map if not in cache - running command %s' % cmd) 
    249                     out, err = self.launch(cmd, content) 
     248                    out, err = self.launch(cmd, encoded_content) 
    250249                    if len(out) or len(err): 
    251250                        msg = 'The command\n   %s\nfailed with the the following output:\n%s\n%s' % (cmd, out, err) 
     
    296295 
    297296    def expand_wiki_links(self, content): 
    298         """Expand TracLinks that follow all URL= patterns. 
    299         The `content` input is a `str` encoding using `sel.fencoding` and  
    300         the result should have the same encoding. 
    301         """ 
    302         u_content = unicode(content, self.encoding) 
    303         u_content = re.sub(r'URL="(.*?)"', self._expand_wiki_links, u_content) 
    304         return u_content.encode(self.encoding) 
     297        """Expand TracLinks that follow all URL= patterns.""" 
     298        return re.sub(r'URL="(.*?)"', self._expand_wiki_links, content) 
    305299 
    306300    def _expand_wiki_links(self, match): 
     
    403397 
    404398        # get default graph/node/edge attributes 
    405         self.processor_options = '' 
    406         default_attributes = [ o for o in self.config.options('graphviz') if o[0].startswith('default_') ] 
    407         if default_attributes: 
    408            graph_attributes   = [ o for o in default_attributes if o[0].startswith('default_graph_') ] 
    409            node_attributes    = [ o for o in default_attributes if o[0].startswith('default_node_') ] 
    410            edge_attributes    = [ o for o in default_attributes if o[0].startswith('default_edge_') ] 
    411            if graph_attributes: 
    412                self.processor_options += " ".join([ "-G" + o[0].replace('default_graph_', '') + "=" + o[1] for o in graph_attributes]) + " " 
    413            if node_attributes: 
    414                self.processor_options += " ".join([ "-N" + o[0].replace('default_node_', '') + "=" + o[1] for o in node_attributes]) + " " 
    415            if edge_attributes: 
    416                self.processor_options += " ".join([ "-E" + o[0].replace('default_edge_', '') + "=" + o[1] for o in edge_attributes]) 
    417  
     399        self.processor_options = [] 
     400        defaults = [opt for opt in self.config.options('graphviz')  
     401                    if opt[0].startswith('default_')] 
     402        for name, value in defaults: 
     403            for prefix, optkey in [ 
     404                    ('default_graph_', '-G'),  
     405                    ('default_node_', '-N'), 
     406                    ('default_edge_', '-E')]: 
     407                if name.startswith(prefix): 
     408                    self.processor_options.append("%s%s=%s" %  
     409                            (optkey, name.replace(prefix,''), value)) 
    418410 
    419411        # check if we should run the cache manager 
     
    443435 
    444436 
    445     def launch(self, cmd, input): 
     437    def launch(self, cmd, encoded_input): 
    446438        """Launch a process (cmd), and returns exitcode, stdout + stderr""" 
    447         p = subprocess.Popen([arg for arg in cmd if arg], 
     439        # Note: subprocess.Popen doesn't support unicode options arguments 
     440        # (http://bugs.python.org/issue1759845) so we have to encode them. 
     441        # Anyway, dot expects utf-8 or the encoding specified with -Gcharset. 
     442        encoded_cmd = [] 
     443        for arg in cmd: 
     444            if isinstance(arg, unicode): 
     445                arg = arg.encode(self.encoding, 'replace') 
     446            encoded_cmd.append(arg) 
     447        p = subprocess.Popen(encoded_cmd, 
    448448                             stdin=subprocess.PIPE, 
    449449                             stdout=subprocess.PIPE, 
     
    451451 
    452452        if input: 
    453             p.stdin.write(input) 
     453            p.stdin.write(encoded_input) 
    454454        p.stdin.close() 
    455455        out = p.stdout.read()