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 NodeProcessingError(ProcessingError): 42 43 "A processing error associated with a particular program node." 44 45 def __init__(self, unit_name, node, message): 46 self.unit_name = unit_name 47 self.node = node 48 self.message = message 49 50 def __repr__(self): 51 return "Error in %r at line %d: %s" % (self.unit_name, self.node.lineno, self.message) 52 53 def __str__(self): 54 return repr(self) 55 56 class TranslateError(NodeProcessingError): 57 58 "An error during the module translation process." 59 60 pass 61 62 # Inspection representations. 63 64 class AtLeast: 65 66 "A special representation for numbers of a given value or greater." 67 68 def __init__(self, count): 69 self.count = count 70 71 def __eq__(self, other): 72 return 0 73 74 __lt__ = __le__ = __eq__ 75 76 def __ne__(self, other): 77 return 1 78 79 def __gt__(self, other): 80 if isinstance(other, AtLeast): 81 return 0 82 else: 83 return self.count > other 84 85 def __ge__(self, other): 86 if isinstance(other, AtLeast): 87 return 0 88 else: 89 return self.count >= other 90 91 def __iadd__(self, other): 92 if isinstance(other, AtLeast): 93 self.count += other.count 94 else: 95 self.count += other 96 return self 97 98 def __radd__(self, other): 99 if isinstance(other, AtLeast): 100 return AtLeast(self.count + other.count) 101 else: 102 return AtLeast(self.count + other) 103 104 def __repr__(self): 105 return "AtLeast(%r)" % self.count 106 107 class Naming: 108 109 "A mix-in providing naming conveniences." 110 111 def full_name(self): 112 if self.name is not None: 113 return self.parent.full_name() + "." + self.name 114 else: 115 return self.parent.full_name() 116 117 # vim: tabstop=4 expandtab shiftwidth=4