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 parent(self): 149 150 "Return the parent of this reference's origin." 151 152 if not self.get_origin(): 153 return None 154 155 return self.get_origin().rsplit(".", 1)[0] 156 157 def name_parent(self): 158 159 "Return the parent of this reference's aliased name." 160 161 if not self.get_name(): 162 return None 163 164 return self.get_name().rsplit(".", 1)[0] 165 166 def ancestors(self): 167 168 """ 169 Return ancestors of this reference's origin in order of decreasing 170 depth. 171 """ 172 173 if not self.get_origin(): 174 return None 175 176 parts = self.get_origin().split(".") 177 ancestors = [] 178 179 for i in range(len(parts) - 1, 0, -1): 180 ancestors.append(".".join(parts[:i])) 181 182 return ancestors 183 184 def get_types(self): 185 186 "Return class, instance-only and module types for this reference." 187 188 class_types = self.has_kind("<class>") and [self.get_origin()] or [] 189 instance_types = [] 190 module_types = self.has_kind("<module>") and [self.get_origin()] or [] 191 return class_types, instance_types, module_types 192 193 def decode_reference(s, name=None): 194 195 "Decode 's', making a reference." 196 197 if isinstance(s, Reference): 198 return s.alias(name) 199 200 # Null value. 201 202 elif not s: 203 return Reference("<var>", None, name) 204 205 # Kind and origin. 206 207 elif ":" in s: 208 kind, origin = s.split(":") 209 return Reference(kind, origin, name) 210 211 # Kind-only, origin is indicated name. 212 213 elif s[0] == "<": 214 return Reference(s, name, name) 215 216 # Module-only. 217 218 else: 219 return Reference("<module>", s, name) 220 221 222 223 # Type/reference collection functions. 224 225 def is_single_class_type(all_types): 226 227 """ 228 Return whether 'all_types' is a mixture of class and instance kinds for 229 a single class type. 230 """ 231 232 kinds = set() 233 types = set() 234 235 for type in all_types: 236 kinds.add(type.get_kind()) 237 types.add(type.get_origin()) 238 239 return len(types) == 1 and kinds == set(["<class>", "<instance>"]) 240 241 def combine_types(class_types, instance_types, module_types): 242 243 """ 244 Combine 'class_types', 'instance_types', 'module_types' into a single 245 list of references. 246 """ 247 248 all_types = [] 249 for kind, l in [("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types)]: 250 for t in l: 251 all_types.append(Reference(kind, t)) 252 return all_types 253 254 def separate_types(refs): 255 256 """ 257 Separate 'refs' into type-specific lists, returning a tuple containing 258 lists of class types, instance types, module types, function types and 259 unknown "var" types. 260 """ 261 262 class_types = [] 263 instance_types = [] 264 module_types = [] 265 function_types = [] 266 var_types = [] 267 268 for kind, l in [ 269 ("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types), 270 ("<function>", function_types), ("<var>", var_types) 271 ]: 272 273 for ref in refs: 274 if ref.get_kind() == kind: 275 l.append(ref.get_origin()) 276 277 return class_types, instance_types, module_types, function_types, var_types 278 279 # vim: tabstop=4 expandtab shiftwidth=4