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 alias = self.name and ";%s" % self.name or "" 52 return "%s%s" % (self.kind, alias) 53 else: 54 alias = self.name and self.name != self.origin and ";%s" % self.name or "" 55 return "%s:%s%s" % (self.kind, self.origin, alias) 56 57 def __hash__(self): 58 59 "Hash instances using the kind and origin only." 60 61 return hash((self.kind, self.get_origin())) 62 63 def __cmp__(self, other): 64 65 "Compare with 'other' using the kind and origin only." 66 67 if isinstance(other, Reference): 68 return cmp((self.kind, self.get_origin()), (other.kind, other.get_origin())) 69 else: 70 return cmp(str(self), other) 71 72 def get_name(self): 73 74 "Return the name used for this reference." 75 76 return self.name 77 78 def get_origin(self): 79 80 "Return the origin of the reference." 81 82 return self.kind != "<var>" and self.origin or None 83 84 def get_kind(self): 85 86 "Return the kind of object referenced." 87 88 return self.kind 89 90 def has_kind(self, kinds): 91 92 """ 93 Return whether the reference describes an object from the given 'kinds', 94 where such kinds may be "<class>", "<function>", "<instance>", 95 "<module>" or "<var>". Unresolved references may also have kinds of 96 "<depends>" and "<invoke>". 97 """ 98 99 if not isinstance(kinds, (list, tuple)): 100 kinds = [kinds] 101 return self.get_kind() in kinds 102 103 def get_path(self): 104 105 "Return the attribute names comprising the path to the origin." 106 107 return self.get_origin().split(".") 108 109 def unresolved(self): 110 111 "Return whether this reference is unresolved." 112 113 return self.has_kind(["<depends>", "<invoke>", "<var>"]) 114 115 def static(self): 116 117 "Return this reference if it refers to a static object, None otherwise." 118 119 return self.has_kind(["<class>", "<function>", "<module>"]) and self or None 120 121 def final(self): 122 123 "Return a reference to either a static object or None." 124 125 static = self.static() 126 return static and static.origin or None 127 128 def instance_of(self): 129 130 "Return a reference to an instance of the referenced class." 131 132 return self.has_kind("<class>") and Reference("<instance>", self.origin) or None 133 134 def as_var(self): 135 136 """ 137 Return a variable version of this reference. Any origin information is 138 discarded since variable references are deliberately ambiguous. 139 """ 140 141 return Reference("<var>", None, self.name) 142 143 def alias(self, name): 144 145 "Alias this reference employing 'name'." 146 147 return Reference(self.get_kind(), self.get_origin(), name) 148 149 def unaliased(self): 150 151 "Return this reference without any alias." 152 153 return Reference(self.get_kind(), self.get_origin()) 154 155 def mutate(self, ref): 156 157 "Mutate this reference to have the same details as 'ref'." 158 159 self.kind = ref.kind 160 self.origin = ref.origin 161 self.name = ref.name 162 163 def parent(self): 164 165 "Return the parent of this reference's origin." 166 167 if not self.get_origin(): 168 return None 169 170 return self.get_origin().rsplit(".", 1)[0] 171 172 def name_parent(self): 173 174 "Return the parent of this reference's aliased name." 175 176 if not self.get_name(): 177 return None 178 179 return self.get_name().rsplit(".", 1)[0] 180 181 def ancestors(self): 182 183 """ 184 Return ancestors of this reference's origin in order of decreasing 185 depth. 186 """ 187 188 if not self.get_origin(): 189 return None 190 191 parts = self.get_origin().split(".") 192 ancestors = [] 193 194 for i in range(len(parts) - 1, 0, -1): 195 ancestors.append(".".join(parts[:i])) 196 197 return ancestors 198 199 def get_types(self): 200 201 "Return class, instance-only and module types for this reference." 202 203 class_types = self.has_kind("<class>") and [self.get_origin()] or [] 204 instance_types = [] 205 module_types = self.has_kind("<module>") and [self.get_origin()] or [] 206 return class_types, instance_types, module_types 207 208 def decode_reference(s, name=None): 209 210 "Decode 's', making a reference." 211 212 if isinstance(s, Reference): 213 return s.alias(name) 214 215 # Null value. 216 217 elif not s: 218 return Reference("<var>", None, name) 219 220 # Kind and origin. 221 222 elif ":" in s: 223 kind, origin = s.split(":") 224 if ";" in origin: 225 origin, name = origin.split(";") 226 return Reference(kind, origin, name) 227 228 # Kind and name. 229 230 elif ";" in s: 231 kind, name = s.split(";") 232 return Reference(kind, None, name) 233 234 # Kind-only, origin is indicated name. 235 236 elif s[0] == "<": 237 return Reference(s, name, name) 238 239 # Module-only. 240 241 else: 242 return Reference("<module>", s, name) 243 244 245 246 # Type/reference collection functions. 247 248 def is_single_class_type(all_types): 249 250 """ 251 Return whether 'all_types' is a mixture of class and instance kinds for 252 a single class type. 253 """ 254 255 kinds = set() 256 types = set() 257 258 for type in all_types: 259 kinds.add(type.get_kind()) 260 types.add(type.get_origin()) 261 262 return len(types) == 1 and kinds == set(["<class>", "<instance>"]) 263 264 def combine_types(class_types, instance_types, module_types): 265 266 """ 267 Combine 'class_types', 'instance_types', 'module_types' into a single 268 list of references. 269 """ 270 271 all_types = [] 272 for kind, l in [("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types)]: 273 for t in l: 274 all_types.append(Reference(kind, t)) 275 return all_types 276 277 def separate_types(refs): 278 279 """ 280 Separate 'refs' into type-specific lists, returning a tuple containing 281 lists of class types, instance types, module types, function types and 282 unknown "var" types. 283 """ 284 285 class_types = [] 286 instance_types = [] 287 module_types = [] 288 function_types = [] 289 var_types = [] 290 291 for kind, l in [ 292 ("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types), 293 ("<function>", function_types), ("<var>", var_types) 294 ]: 295 296 for ref in refs: 297 if ref.get_kind() == kind: 298 l.append(ref.get_origin()) 299 300 return class_types, instance_types, module_types, function_types, var_types 301 302 # vim: tabstop=4 expandtab shiftwidth=4