Home | Trees | Indices | Help |
---|
|
1 ######################################################################## 2 # 3 # File: diagnostic.py 4 # Author: Alex Samuel 5 # Date: 2001-02-27 6 # 7 # Contents: 8 # Code for managing and generating diagnostics. 9 # 10 # Copyright (c) 2001, 2002 by CodeSourcery, LLC. All rights reserved. 11 # 12 # For license terms see the file COPYING. 13 # 14 ######################################################################## 15 16 """Table-based diagnostic message generation. 17 18 Diagnostics are loaded from text files. These files are laid out 19 according to special rules: 20 21 - Lines beginning with a hash mark are ignored. 22 23 - Each diagnostic begins with a line that contains an at sign (@) and 24 a tag used to identify the diagnostic. 25 26 - Subsequent text until the start of the next diagnostic is 27 the diagnostic template. 28 29 - Diagnostic templates may contain named-substition tokens as 30 used by the Python % operator on a string. 31 32 - Diagnostic messages are interpreted as structured text. 33 34 For example: 35 36 # This line is a comment 37 38 @ my first diagnostic 39 The command you entered, '$(command)s', is bogus. Please try again. 40 41 @ my second diagnostic 42 The value you specified, '$(value)d', is completely bogus. Don't 43 even bother trying again. 44 45 """ 46 47 ######################################################################## 48 # imports 49 ######################################################################## 50 51 import common 52 import os 53 import qm 54 import re 55 import string 56 import types 57 58 ######################################################################## 59 # classes 60 ######################################################################## 6163 64 # Regular expression to match comment lines. 65 __comment_regex = re.compile("^[ \t]*#.*$", re.MULTILINE) 66 67 # Regular express that matches the start of a new diagnostic entry. 68 __separator_regex = re.compile("^@", re.MULTILINE) 69 70 75 76128 129 130 ######################################################################## 131 # Variables 132 ######################################################################## 133 134 __diagnostic_set = None 135 """The 'DiagnosticSet' object from which diagnostics are generated.""" 136 137 __help_set = None 138 """The 'DiagnosticSet'object from which help text messages are 139 generated.""" 140 141 ######################################################################## 142 # functions 143 ######################################################################## 14478 """Load diagnostics from a file. 79 80 'path' -- Path to the file containing diagnostics.""" 81 82 # Read the file. 83 file = open(path, "r") 84 contents = file.read() 85 file.close() 86 # Erase comment lines. 87 contents = self.__comment_regex.sub("", contents) 88 # Split the file's contents into entries. 89 entries = self.__separator_regex.split(contents) 90 91 for entry in entries: 92 if not "\n" in entry: 93 continue 94 # The tag is everything up to the first newline. 95 tag, message = string.split(entry, "\n", 1) 96 # Clean up the tag and the diagnostic message. 97 tag = string.strip(tag) 98 message = string.strip(message) 99 # Store it. 100 self.__diagnostics[tag] = message101 102104 """Generate a diagnostic message. 105 106 'tag' -- The tag of the diagnostic to generate. 107 108 'severity' -- A string representing the severity of the 109 diagnostic, for instance "warning" or "error". 110 111 'output' -- If not 'None', the a file object to which the 112 a full diagnostic is written. 113 114 'substitutions' -- Named values for substitution into the 115 diagnostic message. 116 117 returns -- The bare diagnostic message.""" 118 119 substitutions = substitutions.copy() 120 substitutions["program_name"] = common.program_name 121 message = self.__diagnostics[tag] % substitutions 122 if output is None: 123 pass 124 else: 125 output.write("%s: %s: %s\n" 126 % (common.program_name, severity, message)) 127 return message146 """Return the 'DiagnosticSet' containing warning/error messages. 147 148 returns -- The 'DiagnosticSet' containing warning/error messages.""" 149 150 global __diagnostic_set 151 if __diagnostic_set is None: 152 __diagnostic_set = DiagnosticSet() 153 __diagnostic_set.ReadFromFile(qm.get_share_directory("diagnostics", 154 "common.txt")) 155 156 return __diagnostic_set157 158160 """Return the 'DiagnosticSet' for help messages. 161 162 returns -- The 'DiagnosticSet' containing help messages.""" 163 164 global __help_set 165 if __help_set is None: 166 __help_set = DiagnosticSet() 167 __help_set.ReadFromFile(qm.get_share_directory("diagnostics", 168 "common-help.txt")) 169 170 return __help_set171 172174 """Generate a diagnostic message.""" 175 176 return apply(get_diagnostic_set().Generate, 177 (tag, "message", None), 178 substitutions)179 180182 """Generate or emit an error diagnostic.""" 183 184 return apply(get_diagnostic_set().Generate, 185 (tag, "error", output, ), 186 substitutions)187 188190 """Generate or emit a warning diagnostic.""" 191 192 return apply(get_diagnostic_set().Generate, 193 (tag, "warning", output, ), 194 substitutions)195 196198 """Read messages that apply to 'tool'. 199 200 'tool' -- A string giving the name of a QM tool.""" 201 202 # Load diagnostics. 203 diagnostic_file = qm.get_share_directory('messages', 'diagnostics.txt') 204 get_diagnostic_set().ReadFromFile(diagnostic_file) 205 # Load help messages. 206 diagnostic_file = qm.get_share_directory('messages', 'help.txt') 207 get_help_set().ReadFromFile(diagnostic_file)208 209 ######################################################################## 210 # Local Variables: 211 # mode: python 212 # indent-tabs-mode: nil 213 # fill-column: 72 214 # End: 215
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Fri Dec 23 12:30:46 2011 | http://epydoc.sourceforge.net |