Package node_manager_fkie :: Module xml_highlighter
[frames] | no frames]

Source Code for Module node_manager_fkie.xml_highlighter

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # based on code of Timo Roehling 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions 
  9  # are met: 
 10  # 
 11  #  * Redistributions of source code must retain the above copyright 
 12  #    notice, this list of conditions and the following disclaimer. 
 13  #  * Redistributions in binary form must reproduce the above 
 14  #    copyright notice, this list of conditions and the following 
 15  #    disclaimer in the documentation and/or other materials provided 
 16  #    with the distribution. 
 17  #  * Neither the name of Fraunhofer nor the names of its 
 18  #    contributors may be used to endorse or promote products derived 
 19  #    from this software without specific prior written permission. 
 20  # 
 21  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 22  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 23  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 24  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 25  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 26  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 27  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 28  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 29  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 30  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 31  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32  # POSSIBILITY OF SUCH DAMAGE. 
 33   
 34  from python_qt_binding import QtGui 
 35  from python_qt_binding import QtCore 
 36   
37 -class XmlHighlighter(QtGui.QSyntaxHighlighter):
38 ''' 39 Enabled the syntax highlightning for the ROS launch files. 40 ''' 41 42 LAUNCH_LAUNCH_CHILDS = ['group', 'node', 'test', 'env', 'remap', 'rosparam', 'param', 'machine', 'include', 'arg'] 43 LAUNCH_LAUNCH_ATTR = {'deprecated=' : '"message"'} 44 LAUNCH_GROUP_CHILDS = ['node', 'test', 'env', 'remap', 'rosparam', 'param', 'machine', 'include', 'arg'] 45 LAUNCH_GROUP_ATTR = {'ns=' : '"foo"', 46 'clear_params=' : '"true|false"' 47 } 48 LAUNCH_MACHINE_CHILDS = ['env'] 49 LAUNCH_MACHINE_ATTR = {'name=' : '"machine-name"', 50 'address=' : '"blah.willowgarage.com"', 51 'env-loader=' : '"/opt/ros/fuerte/env.sh"', 52 'default=' : '"true|false|never"', 53 'user=' : '"username"', 54 'password=' : '"passwhat"', 55 'timeout=' : '"10.0"' 56 } 57 LAUNCH_NODE_CHILDS = ['env', 'remap', 'rosparam', 'param'] 58 LAUNCH_NODE_ATTR = {'pkg=' : '"mypackage"', 59 'type=' : '"nodetype"', 60 'name=' : '"nodename"', 61 'args=' : '"arg1"', 62 'machine=' : '"machine-name"', 63 'respawn=' : '"true"', 64 'required=' : '"true"', 65 'ns=' : '"foo"', 66 'clear_params=' : '"true|false"', 67 'output=' : '"log|screen"', 68 'cwd=' : '"ROS_HOME|node"', 69 'launch-prefix=': '"prefix arguments"' 70 } 71 LAUNCH_INCLUDE_CHILDS = ['env', 'arg'] 72 LAUNCH_INCLUDE_ATTR = {'file=' : '"$(find pkg-name)/path/filename.xml"', 73 'ns=' : '"foo"', 74 'clear_params=' : '"true|false"' 75 } 76 77 LAUNCH_REMAP_ATTR = {'from=' : '"originalname"', 78 'to=' : '"newname"' 79 } 80 LAUNCH_ENV_ATTR = {'name=' : '"name"', 81 'value=' : '"value"' 82 } 83 LAUNCH_PARAM_ATTR = {'name=' : '"namespace/name"', 84 'value=' : '"value"', 85 'type=' : '"str|int|double|bool"', 86 'textfile=' : '"$(find pkg-name)/path/file.txt"', 87 'binfile=' : '"$(find pkg-name)/path/file"', 88 'command=' : '"$(find pkg-name)/exe \'$(find pkg-name)/arg.txt\'"' 89 } 90 91 LAUNCH_ROSPARAM_ATTR = {'command=' : '"load|dump|delete"', 92 'file=' : '"$(find pkg-name)/path/foo.yaml"', 93 'param=' : '"name"', 94 'ns=' : '"foo"' 95 } 96 LAUNCH_ARG_ATTR = {'name=' : '"name"', 97 'value=' : '"bar"', 98 'default=' : '"defbar"' 99 } 100 LAUNCH_TEST_CHILDS = ['env', 'remap', 'rosparam', 'param'] 101 LAUNCH_TEST_ATTR = {'pkg=' : '"mypackage"', 102 'type=' : '"nodetype"', 103 'name=' : '"nodename"', 104 'test-name=' : '"test_name"', 105 'args=' : '"arg1"', 106 'ns=' : '"foo"', 107 'clear_params=' : '"true|false"', 108 'retry=' : '"0"', 109 'cwd=' : '"ROS_HOME|node"', 110 'launch-prefix=': '"prefix arguments"', 111 'time-limit=' : '"60.0"' 112 } 113 114 LAUNCH_CHILDS = {'launch' : LAUNCH_LAUNCH_CHILDS, 115 'group' : LAUNCH_GROUP_CHILDS, 116 'machine' : LAUNCH_MACHINE_CHILDS, 117 'node' : LAUNCH_NODE_CHILDS, 118 'include' : LAUNCH_INCLUDE_CHILDS, 119 'remap' : [], 120 'env' : [], 121 'param' : [], 122 'rosparam' : [], 123 'arg' : [], 124 'test' : LAUNCH_TEST_CHILDS 125 } 126 127 LAUNCH_ATT_GLOBAL = {'if=' : '""', 'unless=' : '""'} 128 LAUNCH_LAUNCH_ATTR.update(LAUNCH_ATT_GLOBAL) 129 LAUNCH_GROUP_ATTR.update(LAUNCH_ATT_GLOBAL) 130 LAUNCH_MACHINE_ATTR.update(LAUNCH_ATT_GLOBAL) 131 LAUNCH_NODE_ATTR.update(LAUNCH_ATT_GLOBAL) 132 LAUNCH_INCLUDE_ATTR.update(LAUNCH_ATT_GLOBAL) 133 LAUNCH_REMAP_ATTR.update(LAUNCH_ATT_GLOBAL) 134 LAUNCH_ENV_ATTR.update(LAUNCH_ATT_GLOBAL) 135 LAUNCH_PARAM_ATTR.update(LAUNCH_ATT_GLOBAL) 136 LAUNCH_ROSPARAM_ATTR.update(LAUNCH_ATT_GLOBAL) 137 LAUNCH_ARG_ATTR.update(LAUNCH_ATT_GLOBAL) 138 LAUNCH_TEST_ATTR.update(LAUNCH_ATT_GLOBAL) 139 140 LAUNCH_ATTR = {'launch' : LAUNCH_LAUNCH_ATTR, 141 'group' : LAUNCH_GROUP_ATTR, 142 'machine' : LAUNCH_MACHINE_ATTR, 143 'node' : LAUNCH_NODE_ATTR, 144 'include' : LAUNCH_INCLUDE_ATTR, 145 'remap' : LAUNCH_REMAP_ATTR, 146 'env' : LAUNCH_ENV_ATTR, 147 'param' : LAUNCH_PARAM_ATTR, 148 'rosparam' : LAUNCH_ROSPARAM_ATTR, 149 'arg' : LAUNCH_ARG_ATTR, 150 'test' : LAUNCH_TEST_ATTR, 151 } 152
153 - def __init__(self, parent=None):
154 QtGui.QSyntaxHighlighter.__init__(self, parent) 155 self.rules = [] 156 self.commentStart = QtCore.QRegExp("<!--") 157 self.commentEnd = QtCore.QRegExp("-->") 158 self.commentFormat = QtGui.QTextCharFormat() 159 f = QtGui.QTextCharFormat() 160 r = QtCore.QRegExp() 161 r.setMinimal(True) 162 f.setFontWeight(QtGui.QFont.Normal) 163 f.setForeground (QtCore.Qt.darkBlue) 164 # create patterns for TAG 165 tagList = ["\\b%s\\b"%t for t in self.LAUNCH_CHILDS.keys()] 166 for tag in tagList: 167 r.setPattern(tag) 168 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 169 # create patterns for ATTRIBUTES 170 f.setForeground(QtCore.Qt.darkGreen) 171 attrList = set(["\\b%s"%attr for v in self.LAUNCH_ATTR.values() for attr in v.keys()]) 172 for attr in attrList: 173 r.setPattern(attr) 174 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 175 # create patterns for strings 176 f.setForeground(QtCore.Qt.magenta) 177 r.setPattern("\".*\"") 178 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 179 # create patterns for substitutions 180 f.setForeground(QtGui.QColor(127,64,127)) 181 r.setPattern ("\\$\\(.*\\)") 182 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 183 # create patterns for DOCTYPE 184 f.setForeground (QtCore.Qt.lightGray) 185 r.setPattern ("<!DOCTYPE.*>") 186 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 187 r.setPattern ("<\\?xml.*\\?>") 188 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 189 190 self.commentFormat.setFontItalic(True) 191 self.commentFormat.setForeground(QtCore.Qt.darkGray)
192 193
194 - def highlightBlock(self, text):
195 for pattern, format in self.rules: 196 index = pattern.indexIn(text) 197 while index >= 0: 198 length = pattern.matchedLength() 199 self.setFormat(index, length, format) 200 index = pattern.indexIn(text, index + length) 201 self.setCurrentBlockState(0) 202 startIndex = 0 203 if self.previousBlockState() != 1: 204 startIndex = self.commentStart.indexIn(text) 205 while startIndex >= 0: 206 endIndex = self.commentEnd.indexIn(text, startIndex) 207 commentLength = 0 208 if endIndex == -1: 209 self.setCurrentBlockState(1) 210 commentLength = len(text) - startIndex 211 else: 212 commentLength = endIndex - startIndex + self.commentEnd.matchedLength() 213 self.setFormat(startIndex, commentLength, self.commentFormat) 214 startIndex = self.commentStart.indexIn(text, startIndex + commentLength)
215