1 #!/usr/bin/env python 2 3 """ 4 Common classes. 5 6 Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 try: 23 set 24 except NameError: 25 from sets import Set as set 26 27 # Errors. 28 29 class ProcessingError(Exception): 30 31 "A processing error." 32 33 pass 34 35 class InspectError(ProcessingError): 36 37 "An error during the module inspection process." 38 39 pass 40 41 class TableError(ProcessingError): 42 43 "An error occurring during access to a lookup table." 44 45 pass 46 47 class NodeProcessingError(ProcessingError): 48 49 "A processing error associated with a particular program node." 50 51 def __init__(self, unit_name, node, message): 52 self.unit_name = unit_name 53 self.node = node 54 self.message = message 55 56 def __repr__(self): 57 return "Error in %r at line %d: %s" % (self.unit_name, self.node.lineno, self.message) 58 59 def __str__(self): 60 return repr(self) 61 62 class TranslateError(NodeProcessingError): 63 64 "An error during the module translation process." 65 66 pass 67 68 # Inspection representations. 69 70 class AtLeast: 71 72 "A special representation for numbers of a given value or greater." 73 74 def __init__(self, count): 75 self.count = count 76 77 def __eq__(self, other): 78 return 0 79 80 __lt__ = __le__ = __eq__ 81 82 def __ne__(self, other): 83 return 1 84 85 def __gt__(self, other): 86 if isinstance(other, AtLeast): 87 return 0 88 else: 89 return self.count > other 90 91 def __ge__(self, other): 92 if isinstance(other, AtLeast): 93 return 0 94 else: 95 return self.count >= other 96 97 def __iadd__(self, other): 98 if isinstance(other, AtLeast): 99 self.count += other.count 100 else: 101 self.count += other 102 return self 103 104 def __radd__(self, other): 105 if isinstance(other, AtLeast): 106 return AtLeast(self.count + other.count) 107 else: 108 return AtLeast(self.count + other) 109 110 def __repr__(self): 111 return "AtLeast(%r)" % self.count 112 113 class Naming: 114 115 "A mix-in providing naming conveniences." 116 117 def full_name(self): 118 if self.name is not None: 119 return self.parent.full_name() + "." + self.name 120 else: 121 return self.parent.full_name() 122 123 # vim: tabstop=4 expandtab shiftwidth=4