1 #!/usr/bin/env python 2 3 """ 4 Reference abstractions. 5 6 Copyright (C) 2016 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 class Reference: 23 24 "A reference abstraction." 25 26 def __init__(self, kind, origin=None, name=None): 27 28 """ 29 Initialise a reference using 'kind' to indicate the kind of object, 30 'origin' to indicate the actual origin of a referenced object, and a 31 'name' indicating an alias for the object in the program structure. 32 """ 33 34 if isinstance(kind, Reference): 35 raise ValueError, (kind, origin) 36 self.kind = kind 37 self.origin = origin 38 self.name = name 39 40 def __repr__(self): 41 return "Reference(%r, %r, %r)" % (self.kind, self.origin, self.name) 42 43 def __str__(self): 44 45 """ 46 Serialise the reference as '<var>' or a description incorporating the 47 kind and origin. 48 """ 49 50 if self.kind == "<var>": 51 return self.kind 52 else: 53 return "%s:%s" % (self.kind, self.origin) 54 55 def __hash__(self): 56 57 "Hash instances using the kind and origin only." 58 59 return hash((self.kind, self.get_origin())) 60 61 def __cmp__(self, other): 62 63 "Compare with 'other' using the kind and origin only." 64 65 if isinstance(other, Reference): 66 return cmp((self.kind, self.get_origin()), (other.kind, other.get_origin())) 67 else: 68 return cmp(str(self), other) 69 70 def get_name(self): 71 72 "Return the name used for this reference." 73 74 return self.name 75 76 def get_origin(self): 77 78 "Return the origin of the reference." 79 80 return self.kind != "<var>" and self.origin or None 81 82 def get_kind(self): 83 84 "Return the kind of object referenced." 85 86 return self.kind 87 88 def has_kind(self, kinds): 89 90 """ 91 Return whether the reference describes an object from the given 'kinds', 92 where such kinds may be "<class>", "<function>", "<instance>", 93 "<module>" or "<var>". 94 """ 95 96 if not isinstance(kinds, (list, tuple)): 97 kinds = [kinds] 98 return self.get_kind() in kinds 99 100 def get_path(self): 101 102 "Return the attribute names comprising the path to the origin." 103 104 return self.get_origin().split(".") 105 106 def static(self): 107 108 "Return this reference if it refers to a static object, None otherwise." 109 110 return not self.has_kind(["<var>", "<instance>"]) and self or None 111 112 def final(self): 113 114 "Return a reference to either a static object or None." 115 116 static = self.static() 117 return static and static.origin or None 118 119 def instance_of(self): 120 121 "Return a reference to an instance of the referenced class." 122 123 return self.has_kind("<class>") and Reference("<instance>", self.origin) or None 124 125 def as_var(self): 126 127 """ 128 Return a variable version of this reference. Any origin information is 129 discarded since variable references are deliberately ambiguous. 130 """ 131 132 return Reference("<var>", None, self.name) 133 134 def alias(self, name): 135 136 "Alias this reference employing 'name'." 137 138 return Reference(self.get_kind(), self.get_origin(), name) 139 140 def mutate(self, ref): 141 142 "Mutate this reference to have the same details as 'ref'." 143 144 self.kind = ref.kind 145 self.origin = ref.origin 146 self.name = ref.name 147 148 def ancestors(self): 149 150 """ 151 Return ancestors of this reference's origin in order of decreasing 152 depth. 153 """ 154 155 if not self.origin: 156 return None 157 158 parts = self.get_origin().split(".") 159 ancestors = [] 160 161 for i in range(len(parts) - 1, 0, -1): 162 ancestors.append(".".join(parts[:i])) 163 164 return ancestors 165 166 def get_types(self): 167 168 "Return class, instance-only and module types for this reference." 169 170 class_types = self.has_kind("<class>") and [self.get_origin()] or [] 171 instance_types = [] 172 module_types = self.has_kind("<module>") and [self.get_origin()] or [] 173 return class_types, instance_types, module_types 174 175 def decode_reference(s, name=None): 176 177 "Decode 's', making a reference." 178 179 if isinstance(s, Reference): 180 return s.alias(name) 181 182 # Null value. 183 184 elif not s: 185 return Reference("<var>", None, name) 186 187 # Kind and origin. 188 189 elif ":" in s: 190 kind, origin = s.split(":") 191 return Reference(kind, origin, name) 192 193 # Kind-only, origin is indicated name. 194 195 elif s[0] == "<": 196 return Reference(s, name, name) 197 198 # Module-only. 199 200 else: 201 return Reference("<module>", s, name) 202 203 204 205 # Type/reference collection functions. 206 207 def is_single_class_type(all_types): 208 209 """ 210 Return whether 'all_types' is a mixture of class and instance kinds for 211 a single class type. 212 """ 213 214 kinds = set() 215 types = set() 216 217 for type in all_types: 218 kinds.add(type.get_kind()) 219 types.add(type.get_origin()) 220 221 return len(types) == 1 and kinds == set(["<class>", "<instance>"]) 222 223 def combine_types(class_types, instance_types, module_types): 224 225 """ 226 Combine 'class_types', 'instance_types', 'module_types' into a single 227 list of references. 228 """ 229 230 all_types = [] 231 for kind, l in [("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types)]: 232 for t in l: 233 all_types.append(Reference(kind, t)) 234 return all_types 235 236 def separate_types(refs): 237 238 """ 239 Separate 'refs' into type-specific lists, returning a tuple containing 240 lists of class types, instance types, module types, function types and 241 unknown "var" types. 242 """ 243 244 class_types = [] 245 instance_types = [] 246 module_types = [] 247 function_types = [] 248 var_types = [] 249 250 for kind, l in [ 251 ("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types), 252 ("<function>", function_types), ("<var>", var_types) 253 ]: 254 255 for ref in refs: 256 if ref.get_kind() == kind: 257 l.append(ref.get_origin()) 258 259 return class_types, instance_types, module_types, function_types, var_types 260 261 # vim: tabstop=4 expandtab shiftwidth=4