Changeset 4395
- Timestamp:
- 10/06/08 12:08:55 (2 months ago)
- Files:
-
- graphvizplugin/0.11/graphviz/graphviz.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
graphvizplugin/0.11/graphviz/graphviz.py
r4394 r4395 18 18 19 19 from StringIO import StringIO 20 import locale 20 21 import sha 21 22 import os … … 56 57 encoding = Option("graphviz", "encoding", 'utf-8', 57 58 """The encoding which should be used for communicating with 58 Graphviz .59 Graphviz (should match -Gcharset if given). 59 60 """) 60 61 … … 188 189 return buf.getvalue() 189 190 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() 199 195 img_name = '%s.%s.%s' % (sha_key, self.processor, self.out_format) # cache: hash.<dot>.<png> 200 196 img_path = os.path.join(self.cache_dir, img_name) … … 217 213 if self.out_format == 'png' and self.png_anti_alias == True: 218 214 # 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] 220 217 #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) 222 219 if len(out) or len(err): 223 220 msg = 'The command\n %s\nfailed with the the following output:\n%s\n%s' % (cmd, out, err) … … 233 230 234 231 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] 236 234 #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) 238 236 if len(out) or len(err): 239 237 msg = 'The command\n %s\nfailed with the the following output:\n%s\n%s' % (cmd, out, err) … … 245 243 # Create the map if not in cache 246 244 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] 248 247 #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) 250 249 if len(out) or len(err): 251 250 msg = 'The command\n %s\nfailed with the the following output:\n%s\n%s' % (cmd, out, err) … … 296 295 297 296 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) 305 299 306 300 def _expand_wiki_links(self, match): … … 403 397 404 398 # 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)) 418 410 419 411 # check if we should run the cache manager … … 443 435 444 436 445 def launch(self, cmd, input):437 def launch(self, cmd, encoded_input): 446 438 """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, 448 448 stdin=subprocess.PIPE, 449 449 stdout=subprocess.PIPE, … … 451 451 452 452 if input: 453 p.stdin.write( input)453 p.stdin.write(encoded_input) 454 454 p.stdin.close() 455 455 out = p.stdout.read()
