Team Fortress 2 Source Code as on 22/4/2020
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.

175 lines
4.4 KiB

  1. import sys, re, vsdotnetxmlparser
  2. class EZXMLError:
  3. pass
  4. class EZXMLElement:
  5. """
  6. Members:
  7. Name : string
  8. Attrs : list with attributes [(name,value), (name, value), ...]
  9. Children : list of XMLElements
  10. Parent : parent XMLElement
  11. """
  12. def GetAttributeValue( self, attrName ):
  13. for a in self.Attrs:
  14. if a[0] == attrName:
  15. return a[1]
  16. return None
  17. def RemoveAttribute( self, attrName ):
  18. for i,a in enumerate( self.Attrs ):
  19. if a[0] == attrName:
  20. self.Attrs.pop( i )
  21. return 1
  22. return 0
  23. def RemoveFromParent( self ):
  24. if not self.Parent:
  25. raise EZXMLError
  26. self.Parent.Children.remove( self )
  27. class EZXMLFile:
  28. #
  29. # PUBLIC INTERFACE
  30. #
  31. def __init__( self, data ):
  32. self.Version = ''
  33. self.Encoding = ''
  34. self.RootElement = None
  35. self.__CurElement = None
  36. self.__ElementIndexRE = re.compile( r'<(?P<index>.+)>(?P<name>.+)' )
  37. #p = xml.parsers.expat.ParserCreate()
  38. p = vsdotnetxmlparser.VSDotNetXMLParser()
  39. p.ordered_attributes = 1
  40. p.XmlDeclHandler = self.__xml_decl_handler
  41. p.StartElementHandler = self.__start_element
  42. p.EndElementHandler = self.__end_element
  43. p.Parse( data, 1 )
  44. # Write the current contents to a file.
  45. def WriteFile( self, file ):
  46. file.write( '<?xml version="%s" encoding="%s"?>\r\n' % (self.Version, self.Encoding) )
  47. if self.RootElement:
  48. self.__WriteFile_R( file, self.RootElement, 0 )
  49. """
  50. Find an element (starting at the root).
  51. If elementName has backslashes in it (like 'VisualStudioProject\Configurations\Tool'), then it will recurse into each element.
  52. Each element can also have an optional count telling which element to find:
  53. 'VisualStudioProject\Configurations\<3>Tool'
  54. """
  55. def GetElement( self, elementName ):
  56. if not self.RootElement:
  57. return None
  58. pathList = elementName.split( '\\' )
  59. return self.__GetElementByPath_R( [self.RootElement], pathList, 0 )
  60. """
  61. AttributePath is specified the same way as GetElement.
  62. Returns None if the path to the element or attribute doesn't exist.
  63. """
  64. def GetAttribute( self, attributePath ):
  65. if not self.RootElement:
  66. return None
  67. pathList = attributePath.split( '\\' )
  68. attributeName = pathList.pop()
  69. e = self.__GetElementByPath_R( [self.RootElement], pathList, 0 )
  70. if e:
  71. return e.GetAttributeValue( attributeName )
  72. else:
  73. return None
  74. #
  75. # INTERNAL STUFF
  76. #
  77. def __GetElementByPath_R( self, curElementList, pathList, index ):
  78. indexToFind = 1
  79. indexFound = 0
  80. m = self.__ElementIndexRE.match( pathList[index] )
  81. if m:
  82. nameToFind = m.group('name').upper()
  83. indexToFind = int( m.group('index') )
  84. else:
  85. nameToFind = pathList[index].upper()
  86. for x in curElementList:
  87. if x.Name.upper() == nameToFind:
  88. indexFound += 1
  89. if indexFound == indexToFind:
  90. if index == len( pathList ) - 1:
  91. return x
  92. else:
  93. return self.__GetElementByPath_R( x.Children, pathList, index+1 )
  94. return None
  95. def __WriteFile_R( self, file, curElement, indentLevel ):
  96. tabChars = ""
  97. for i in xrange(0,indentLevel):
  98. tabChars = tabChars + '\t'
  99. file.write( tabChars )
  100. keyListLen = len( curElement.Attrs )
  101. if keyListLen == 0:
  102. file.write( '<%s>\r\n' % (curElement.Name) )
  103. else:
  104. file.write( '<%s\r\n' % (curElement.Name) )
  105. bClosedBlock = 0
  106. for i,e in enumerate( curElement.Attrs ):
  107. file.write( tabChars + '\t' )
  108. file.write( '%s="%s"' % (e[0], e[1]) )
  109. # VS.NET XML files use this special syntax on elements that have attributes and no children.
  110. if len( curElement.Children ) == 0 and i == keyListLen-1 and curElement.Name != 'File':
  111. file.write( '/>\r\n' )
  112. bClosedBlock = 1
  113. elif i == keyListLen-1:
  114. file.write( '>\r\n' )
  115. else:
  116. file.write( '\r\n' )
  117. for child in curElement.Children:
  118. self.__WriteFile_R( file, child, indentLevel+1 )
  119. if not bClosedBlock:
  120. file.write( tabChars )
  121. file.write( '</%s>\r\n' % (curElement.Name) )
  122. # XML parser handler functions
  123. def __xml_decl_handler( self, version, encoding, standalone ):
  124. self.Version = version
  125. self.Encoding = encoding
  126. def __start_element( self, name, attrs ):
  127. e = EZXMLElement()
  128. e.Name = name
  129. e.Attrs = zip( attrs[::2], attrs[1::2] ) # Cool slicing trick.. this takes a list like [1,2,3,4] and makes it into [[1,2], [3,4]]
  130. e.Children = None
  131. e.Parent = self.__CurElement
  132. e.Children = []
  133. if self.__CurElement:
  134. self.__CurElement.Children.append( e )
  135. else:
  136. self.RootElement = e
  137. self.__CurElement = e
  138. def __end_element( self, name ):
  139. self.__CurElement = self.__CurElement.Parent