Changeset 4397
- Timestamp:
- 10/06/08 12:15:29 (2 months ago)
- Files:
-
- graphvizplugin/0.11/graphviz/graphviz.py (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
graphvizplugin/0.11/graphviz/graphviz.py
r4395 r4397 139 139 """ 140 140 141 #self.log.debug('dir(self): %s' % str(dir(self)))142 141 self.formatter = formatter 143 142 self.env = formatter.env … … 150 149 151 150 buf = StringIO() 152 153 151 154 152 ## Extract processor and format from name … … 193 191 encoded_content = content.encode(self.encoding) 194 192 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> 196 195 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 198 198 map_path = os.path.join(self.cache_dir, map_name) 199 199 … … 207 207 #self.log.debug('render_macro.URL_in_graph: %s' % str(URL_in_graph)) 208 208 if URL_in_graph: # translate wiki TracLinks in URL 209 #self.log.debug('content: %s' % content)210 209 content = self.expand_wiki_links(content) 211 210 … … 215 214 cmd = [proc_cmd] + self.processor_options + \ 216 215 ['-Tsvg', '-o%s.svg' % img_path] 217 #self.log.debug('render_macro: svg output - running command %s' % cmd)218 216 out, err = self.launch(cmd, encoded_content) 219 217 if len(out) or len(err): … … 223 221 # 2. SVG to PNG rasterization 224 222 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)226 223 out, err = self.launch(cmd, None) 227 224 if len(out) or len(err): … … 232 229 cmd = [proc_cmd] + self.processor_options + \ 233 230 ['-T%s' % self.out_format, '-o%s' % img_path] 234 #self.log.debug('render_macro: render other image formats - running command %s' % cmd)235 231 out, err = self.launch(cmd, encoded_content) 236 232 if len(out) or len(err): … … 245 241 cmd = [proc_cmd] + self.processor_options + \ 246 242 ['-Tcmap', '-o%s' % map_path] 247 #self.log.debug('render_macro: create map if not in cache - running command %s' % cmd)248 243 out, err = self.launch(cmd, encoded_content) 249 244 if len(out) or len(err): … … 265 260 (h_val, h_unit) = h.group(1,2) 266 261 # 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. 268 264 [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()) 270 267 271 268 except: … … 273 270 274 271 # 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 278 272 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)) 279 273 … … 290 284 buf.write('<img src="%s/graphviz/%s"/>' % (req.base_url, img_name)) 291 285 292 #self.log.debug('buf.getvalue(): "%s"' % buf.getvalue())293 286 return buf.getvalue() 294 287 … … 300 293 def _expand_wiki_links(self, match): 301 294 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 308 298 href = re.search('href="(.*?)"', html_url) # http://someurl 309 #self.log.debug('href: %s' % href)310 299 url = href and href.groups()[0] or html_url 311 #self.log.debug('url: %s' % url)312 300 if self.out_format == 'svg': 313 301 format = 'URL="javascript:window.parent.location.href=\'%s\'"' 314 302 else: 315 303 format = 'URL="%s"' 316 #self.log.debug('expand_wiki_links url: %s' % str(format % url))317 304 return format % url 318 319 305 320 306 def load_config(self): … … 339 325 self.cache_dir 340 326 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. 346 329 347 330 # check for the default processor - processor 348 331 self.processor = self.config.get('graphviz', 'processor', Graphviz.Processors[0]) 349 #self.log.debug('self.processor: %s' % self.processor)350 351 332 352 333 # check for the cmd_path entry and setup the various program command paths … … 366 347 367 348 self.cmds = {} 368 pname = self. find_cmd(self.processor, cmd_paths)349 pname = self._find_cmd(self.processor, cmd_paths) 369 350 if not pname: 370 351 msg = 'The default processor, %s, was not found in %s.' % (self.processor, str(cmd_paths)) … … 372 353 373 354 for name in Graphviz.Processors: 374 pname = self. find_cmd(name, cmd_paths)355 pname = self._find_cmd(name, cmd_paths) 375 356 376 357 if not pname: … … 381 362 382 363 # 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]) 385 366 386 367 # 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)) 392 374 393 375 if not os.path.exists(self.rsvg_path): 394 376 err = 'The rsvg program is set to "%s" but that path does not exist.' % self.rsvg_path 395 377 return True, self.show_err(err) 396 #self.log.debug('self.rsvg_path: %s' % self.rsvg_path)397 378 398 379 # get default graph/node/edge attributes … … 412 393 self.cache_manager = self.boolean(self.config.get('graphviz', 'cache_manager', False)) 413 394 if self.cache_manager: 395 # use IntOption 414 396 self.cache_max_size = int(self.config.get('graphviz', 'cache_max_size', 10000000)) 415 397 self.cache_min_size = int(self.config.get('graphviz', 'cache_min_size', 5000000)) 416 398 self.cache_max_count = int(self.config.get('graphviz', 'cache_max_count', 2000)) 417 399 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)423 400 424 401 # is there a graphviz default DPI setting? … … 449 426 stdout=subprocess.PIPE, 450 427 stderr=subprocess.PIPE) 451 452 428 if input: 453 429 p.stdin.write(encoded_input) … … 519 495 # time. Same for cache_min_size. 520 496 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): 522 499 key = atime_keys.pop(0) 523 500 for file in atime_list[key]: 524 #self.log.debug('clean_cache.unlink: %s' % file)525 501 os.unlink(os.path.join(self.cache_dir, file)) 526 502 count = count - 1 527 503 size = size - entry_list[file][6] 528 else:529 #self.log.debug('clean_cache: cache_manager not set')530 pass531 532 504 533 505 # Extra helper functions … … 557 529 558 530 # IRequestHandler methods 531 559 532 def match_request(self, req): 560 533 return req.path_info.startswith('/graphviz') … … 567 540 return msg.getvalue() 568 541 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: 575 548 name = pieces[0] 576 549 img_path = os.path.join(self.cache_dir, name) … … 578 551 return 579 552 580 581 def find_cmd(self, cmd, paths): 553 # private methods 554 555 def _find_cmd(self, cmd, paths): 582 556 exe_suffix = '' 583 557 if sys.platform == 'win32':
