diff options
author | Florian Weimer <fw@deneb.enyo.de> | 2010-05-07 21:03:10 +0000 |
---|---|---|
committer | Florian Weimer <fw@deneb.enyo.de> | 2010-05-07 21:03:10 +0000 |
commit | 8bf3653393970280ba1a0edd4fd4647ae80630c6 (patch) | |
tree | 87415b4d608816b5ac3fe3fe00d3c1d643067d6c /lib/python/sectracker/diagnostics.py | |
parent | a18d05666236a74c154c14b14e325becd90d9108 (diff) |
sectracker.diagnostics: introduce separate diagnostics module
git-svn-id: svn+ssh://svn.debian.org/svn/secure-testing@14628 e39458fd-73e7-0310-bf30-c45bca0a0e42
Diffstat (limited to 'lib/python/sectracker/diagnostics.py')
-rw-r--r-- | lib/python/sectracker/diagnostics.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/python/sectracker/diagnostics.py b/lib/python/sectracker/diagnostics.py new file mode 100644 index 0000000000..635358cf88 --- /dev/null +++ b/lib/python/sectracker/diagnostics.py @@ -0,0 +1,80 @@ +# sectracker.diagnostics -- keeping track of errors and warnings +# Copyright (C) 2010 Florian Weimer <fw@deneb.enyo.de> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +import xcollections + +Message = xcollections.namedtuple("Message", "file line level message") + +def _checkfile(file): + if not isinstance(file, basestring): + raise ValueError("file name is not a string: " + repr(file)) + return file + +def _checkline(line): + if not isinstance(line, int): + raise ValueError("not a number: " + repr(line)) + if line <= 0: + raise ValueError("line number must be positive: " + repr(line)) + return line + +class Diagnostics: + def __init__(self): + self._messages = [] + self._file = None + self._line = None + + def setlocation(self, file, line=1): + if file is None and line is None: + self._file = self._line = None + else: + self._file = _checkfile(file) + self._line = _checkline(line) + + def error(self, message, file=None, line=None): + self.record(file, line, "error", message) + + def warning(self, message, file=None, line=None): + self.record(file, line, "warning", message) + + def record(self, file, line, level, message): + if file is None: + file = self._file + if file is None: + raise Excpetion("location has not been set") + else: + _checkfile(file) + if line is None: + line = self._line + if line is None: + raise Excpetion("location has not been set") + else: + _checkline(line) + self._messages.append(Message(file, line, level, message)) + + def file(self): + if self._file is None: + raise Excpetion("location has not been set") + return self._file + + def line(self): + if self._line is None: + raise Excpetion("location has not been set") + return self._line + + def messages(self): + return tuple(self._messages) + |