jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.7KB

  1. from xml.dom.minidom import getDOMImplementation, parse, Element
  2. import string
  3. impl = getDOMImplementation()
  4. class SessionDom( object ):
  5. def __init__( self, filename=None ):
  6. if filename:
  7. self.dom = parse( filename )
  8. else:
  9. self.dom = impl.createDocument(None,"jacksession",None)
  10. def add_client( self, client ):
  11. cl_elem = Element( "jackclient" )
  12. cl_elem.setAttribute( "cmdline", client.get_commandline() )
  13. cl_elem.setAttribute( "jackname", client.name )
  14. if client.get_uuid():
  15. cl_elem.setAttribute( "uuid", client.get_uuid() )
  16. if client.isinfra:
  17. cl_elem.setAttribute( "infra", "True" )
  18. else:
  19. cl_elem.setAttribute( "infra", "False" )
  20. for p in client.ports:
  21. po_elem = Element( "port" )
  22. po_elem.setAttribute( "name", p.name )
  23. po_elem.setAttribute( "shortname", p.portname )
  24. for c in p.get_connections():
  25. c_elem = Element( "conn" )
  26. c_elem.setAttribute( "dst", c )
  27. po_elem.appendChild( c_elem )
  28. cl_elem.appendChild( po_elem )
  29. self.dom.documentElement.appendChild( cl_elem )
  30. def get_xml(self):
  31. return self.dom.toprettyxml()
  32. def get_client_names(self):
  33. retval = []
  34. doc = self.dom.documentElement
  35. for c in doc.getElementsByTagName( "jackclient" ):
  36. retval.append( c.getAttribute( "jackname" ) )
  37. return retval
  38. def get_reg_client_names(self):
  39. retval = []
  40. doc = self.dom.documentElement
  41. for c in doc.getElementsByTagName( "jackclient" ):
  42. if c.getAttribute( "infra" ) != "True":
  43. retval.append( c.getAttribute( "jackname" ) )
  44. return retval
  45. def get_infra_clients(self):
  46. retval = []
  47. doc = self.dom.documentElement
  48. for c in doc.getElementsByTagName( "jackclient" ):
  49. if c.getAttribute( "infra" ) == "True":
  50. retval.append( (c.getAttribute( "jackname" ), c.getAttribute( "cmdline" ) ) )
  51. return retval
  52. def get_port_names(self):
  53. retval = []
  54. doc = self.dom.documentElement
  55. for c in doc.getElementsByTagName( "port" ):
  56. retval.append( c.getAttribute( "name" ) )
  57. return retval
  58. def get_connections_for_port( self, portname ):
  59. retval = []
  60. doc = self.dom.documentElement
  61. for c in doc.getElementsByTagName( "port" ):
  62. if c.getAttribute( "name" ) == portname:
  63. for i in c.getElementsByTagName( "conn" ):
  64. retval.append( i.getAttribute( "dst" ) )
  65. return retval
  66. def get_commandline_for_client( self, name ):
  67. doc = self.dom.documentElement
  68. for c in doc.getElementsByTagName( "jackclient" ):
  69. if c.getAttribute( "jackname" ) == name:
  70. return c.getAttribute( "cmdline" )
  71. def get_uuid_client_pairs( self ):
  72. retval = []
  73. doc = self.dom.documentElement
  74. for c in doc.getElementsByTagName( "jackclient" ):
  75. if c.getAttribute( "infra" ) != "True":
  76. retval.append( (c.getAttribute( "uuid" ), c.getAttribute( "jackname" )) )
  77. return retval
  78. def renameclient( self, celem, newname ):
  79. doc = self.dom.documentElement
  80. celem.setAttribute( "jackname", newname )
  81. for pelem in celem.getElementsByTagName( "port" ):
  82. old_pname = pelem.getAttribute( "name" )
  83. pname_split = old_pname.split(":")
  84. pname_split[0] = newname
  85. new_pname = string.join( pname_split, ":" )
  86. pelem.setAttribute( "name", new_pname )
  87. for dst in doc.getElementsByTagName( "conn" ):
  88. if dst.getAttribute( "dst" ) == old_pname:
  89. dst.setAttribute( "dst", new_pname )
  90. def fixup_client_names( self, graph ):
  91. doc = self.dom.documentElement
  92. for c in doc.getElementsByTagName( "jackclient" ):
  93. if c.getAttribute( "infra" ) == "True":
  94. continue
  95. cname = c.getAttribute( "jackname" )
  96. if cname in graph.get_taken_names():
  97. free_name = graph.get_free_name( cname, self.get_reg_client_names() )
  98. print "name taken %s rename to %s"%(cname, free_name )
  99. self.renameclient( c, free_name )