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