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