home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / freeware / openvip.exe / {app} / visualize.py < prev    next >
Encoding:
Python Source  |  2003-06-19  |  4.0 KB  |  158 lines

  1. #!/usr/bin/env python
  2.  
  3. #
  4. # This file is part of OpenVIP (http://openvip.sourceforge.net)
  5. #
  6. # Copyright (C) 2002-2003
  7. # Michal Dvorak, Jiri Sedlar, Antonin Slavik, Vaclav Slavik, Jozef Smizansky
  8. #
  9. # This program is licensed under GNU General Public License version 2;
  10. # see file COPYING in the top level directory for details.
  11. #
  12. # $Id: visualize.py,v 1.4 2003/06/19 13:37:23 vaclavslavik Exp $
  13. #
  14.  
  15. #
  16. # This program visualizes OpenVIP processing network in the form of directed
  17. # graph. It doesn't do any validity checkings, so the input must be valid
  18. # .openvip file.
  19. #
  20. # Requires the "dot" program from graphviz package.
  21. #
  22.  
  23. import xml.dom.minidom
  24. import sys, string, os
  25.  
  26. if not (len(sys.argv) == 3 or 
  27.         (len(sys.argv) == 4 and sys.argv[1] in ['--brief','--very-brief'])):
  28.     print "Usage: %s [--brief|--very-brief] input.openvip output.png" % sys.argv[0]
  29.     sys.exit(1)
  30.  
  31. showID         = 1
  32. showParams     = 1
  33. showConnectors = 1
  34.  
  35. args = 0
  36. if sys.argv[1] == '--brief':
  37.     showID = 0
  38.     showParams = 0
  39.     args += 1
  40. elif sys.argv[1] == '--very-brief':
  41.     showID = 0
  42.     showParams = 0
  43.     showConnectors = 0
  44.     args += 1
  45.  
  46.  
  47. tree = xml.dom.minidom.parse(sys.argv[args+1])
  48. root = tree.documentElement
  49.  
  50.  
  51. # colors in HSV:
  52. COLOR_NORMAL = "black"
  53. COLOR_INPUT = "blue"
  54. COLOR_OUTPUT = "red"
  55.  
  56.  
  57. output = """
  58. digraph network
  59. {
  60. bgcolor=white
  61. margin=10
  62. """
  63.  
  64. class ModuleInfo:
  65.     def __init__(self):
  66.         self.cin = []
  67.         self.cout = []
  68. mods = {}
  69.  
  70. for n in root.getElementsByTagName('module'):
  71.     mods[n.getAttribute('id')] = ModuleInfo()
  72.     
  73. for n in root.getElementsByTagName('connect'):
  74.     c = n.getAttribute('conn_in')
  75.     if not c in mods[n.getAttribute('module_in')].cout:
  76.         mods[n.getAttribute('module_in')].cout.append(c)
  77.     c = n.getAttribute('conn_out')
  78.     if not c in mods[n.getAttribute('module_out')].cin:
  79.         mods[n.getAttribute('module_out')].cin.append(c)
  80.  
  81. significantParams = {
  82.    'VideoFilter' : 'videofilter',
  83.    'AudioFilter' : 'audiofilter',
  84.    'VideoTransition' : 'transition',
  85.    'AudioTransition' : 'transition',
  86.    'Output' : 'format',
  87. }
  88.  
  89. for n in root.getElementsByTagName('module'):
  90.     m = mods[n.getAttribute('id')]
  91.  
  92.     if len(m.cin) == 0:
  93.         color = COLOR_INPUT
  94.     elif len(m.cout) == 0:
  95.         color = COLOR_OUTPUT
  96.     else:
  97.         color = COLOR_NORMAL
  98.     
  99.     output += """%s [ shape=record,fontcolor="%s",fontsize=10,fontname="Helvetica",
  100. label=\"{{""" % (n.getAttribute('id'),color)
  101.  
  102.     if showConnectors:
  103.         cons = []
  104.         for c in m.cin:
  105.             cons.append("<I%s> (%s)" % (c,c))
  106.         output += string.join(cons,"|") + '}|'
  107.  
  108.     paramsDict = {}
  109.     params = []
  110.     for p in n.getElementsByTagName('param'):
  111.         value = p.firstChild.data.replace('\\', '\\\\\\\\')
  112.         params.append('%s=%s' % (p.getAttribute('name'), value))
  113.         paramsDict[p.getAttribute('name')] = value
  114.     if showParams:
  115.         params = '|%s' % string.join(params, "\\n")
  116.     else:
  117.         params = ''
  118.  
  119.     klass = n.getAttribute('class')
  120.     if showID:
  121.         nameStr = '(%s)\\nclass=%s' % (n.getAttribute('id'), klass)
  122.     else:
  123.         nameStr = klass
  124.  
  125.     if not showParams and klass in significantParams:
  126.         nameStr += ':%s' % paramsDict[significantParams[klass]]
  127.  
  128.     output += """%s%s""" % (nameStr, params)
  129.     
  130.     if showConnectors:
  131.         output += '|'
  132.         cons = []
  133.         for c in m.cout:
  134.             cons.append("<O%s> (%s)" % (c,c))
  135.         output += '{' + string.join(cons,"|");
  136.  
  137.     output += """}}"];\n"""
  138.  
  139.  
  140. for n in root.getElementsByTagName('connect'):
  141.     if showConnectors:
  142.         output += """%s:O%s -> %s:I%s [];\n""" % (n.getAttribute('module_in'),
  143.                                       n.getAttribute('conn_in'),
  144.                                       n.getAttribute('module_out'),
  145.                                       n.getAttribute('conn_out'))
  146.     else:
  147.         output += "%s -> %s [];\n" % (n.getAttribute('module_in'),
  148.                                       n.getAttribute('module_out'))
  149.  
  150.  
  151. output += '}'
  152.  
  153. tree.unlink()
  154.  
  155. os.system("""dot -o %s -Tpng <<EOF
  156. %s
  157. EOF"""% (sys.argv[args+2], output))
  158.