paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | The micropython package for processing Python source code. The code originates |
paul@0 | 5 | from the simplify package but has had various details related to that package |
paul@0 | 6 | removed. |
paul@0 | 7 | |
paul@175 | 8 | Copyright (C) 2006, 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | |
paul@0 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 13 | version. |
paul@0 | 14 | |
paul@0 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 18 | details. |
paul@0 | 19 | |
paul@0 | 20 | You should have received a copy of the GNU General Public License along with |
paul@0 | 21 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 22 | |
paul@0 | 23 | -------- |
paul@0 | 24 | |
paul@280 | 25 | To use this module, an importer should be constructed. Here, the standard path |
paul@280 | 26 | for module searching is employed: |
paul@0 | 27 | |
paul@0 | 28 | importer = Importer(sys.path) |
paul@0 | 29 | |
paul@189 | 30 | To generate programs, the above importer should be supplied in the |
paul@189 | 31 | initialisation of a program instance, and then various methods are called: |
paul@189 | 32 | |
paul@189 | 33 | program = Program(importer) |
paul@189 | 34 | image = program.get_raw_image() |
paul@189 | 35 | |
paul@189 | 36 | Such importer and program objects are the most convenient mechanism through |
paul@189 | 37 | which the functionality of the micropython package may be accessed. |
paul@0 | 38 | """ |
paul@0 | 39 | |
paul@267 | 40 | from micropython.common import ProcessingError, TableGenerationError |
paul@17 | 41 | import micropython.ast |
paul@168 | 42 | import micropython.data |
paul@154 | 43 | import micropython.opt |
paul@0 | 44 | import micropython.inspect |
paul@7 | 45 | import micropython.table |
paul@149 | 46 | import os |
paul@228 | 47 | |
paul@0 | 48 | try: |
paul@0 | 49 | set |
paul@0 | 50 | except NameError: |
paul@0 | 51 | from sets import Set as set |
paul@0 | 52 | |
paul@175 | 53 | class Program: |
paul@0 | 54 | |
paul@175 | 55 | "This class supports the generation of a program image." |
paul@0 | 56 | |
paul@154 | 57 | supported_optimisations = micropython.opt.Optimiser.supported_optimisations |
paul@63 | 58 | |
paul@175 | 59 | def __init__(self, importer, optimisations=None): |
paul@0 | 60 | |
paul@0 | 61 | """ |
paul@175 | 62 | Initialise the program representation with an 'importer' which is able |
paul@175 | 63 | to locate and load Python modules. |
paul@35 | 64 | |
paul@167 | 65 | The optional 'optimisations' cause certain techniques to be used in |
paul@167 | 66 | reducing program size and improving program efficiency. |
paul@0 | 67 | """ |
paul@0 | 68 | |
paul@175 | 69 | self.importer = importer |
paul@175 | 70 | self.optimisations = optimisations or set() |
paul@0 | 71 | |
paul@60 | 72 | # Remember the tables once generated. |
paul@60 | 73 | |
paul@60 | 74 | self.objtable = None |
paul@60 | 75 | self.paramtable = None |
paul@60 | 76 | |
paul@93 | 77 | # Main program information. |
paul@93 | 78 | |
paul@93 | 79 | self.code = None |
paul@93 | 80 | self.code_location = None |
paul@93 | 81 | |
paul@175 | 82 | def get_importer(self): |
paul@175 | 83 | return self.importer |
paul@137 | 84 | |
paul@169 | 85 | # Access to finalised program information. |
paul@169 | 86 | |
paul@167 | 87 | def get_image(self, with_builtins=0): |
paul@9 | 88 | |
paul@265 | 89 | """ |
paul@265 | 90 | Return the program image including built-in objects if 'with_builtins' |
paul@265 | 91 | is specified and set to a true value. |
paul@265 | 92 | """ |
paul@9 | 93 | |
paul@137 | 94 | if self.code is not None: |
paul@137 | 95 | return self.code |
paul@137 | 96 | |
paul@60 | 97 | objtable = self.get_object_table() |
paul@60 | 98 | paramtable = self.get_parameter_table() |
paul@280 | 99 | |
paul@280 | 100 | # Optimise and regenerate the object table. |
paul@280 | 101 | |
paul@280 | 102 | self.importer.vacuum(objtable) |
paul@286 | 103 | self.importer.finalise() |
paul@280 | 104 | |
paul@286 | 105 | objtable = self.get_object_table(reset=1) |
paul@286 | 106 | paramtable = self.get_parameter_table(reset=1) |
paul@21 | 107 | |
paul@173 | 108 | self.code = [] |
paul@18 | 109 | |
paul@82 | 110 | # Append constants to the image. |
paul@82 | 111 | |
paul@265 | 112 | for const in self.importer.constants(): |
paul@173 | 113 | self.code.append(const) |
paul@82 | 114 | |
paul@175 | 115 | last_module = self.importer.modules_ordered[-1] |
paul@139 | 116 | |
paul@175 | 117 | for module in self.importer.modules_ordered: |
paul@205 | 118 | suppress_builtins = not with_builtins and module.name == "__builtins__" |
paul@15 | 119 | |
paul@55 | 120 | # Position the module in the image and make a translation. |
paul@55 | 121 | |
paul@167 | 122 | trans = micropython.ast.Translation(module, self) |
paul@55 | 123 | |
paul@37 | 124 | # Add header details. |
paul@37 | 125 | |
paul@173 | 126 | self.code.append(module) |
paul@37 | 127 | |
paul@15 | 128 | # Append module attributes to the image. |
paul@15 | 129 | |
paul@9 | 130 | attributes = module.module_attributes() |
paul@173 | 131 | self.code += module.attributes_as_list() |
paul@9 | 132 | |
paul@15 | 133 | # Append classes and functions to the image. |
paul@15 | 134 | |
paul@9 | 135 | for obj in module.all_objects: |
paul@55 | 136 | if isinstance(obj, micropython.inspect.Class): |
paul@54 | 137 | |
paul@22 | 138 | # Add header details. |
paul@22 | 139 | |
paul@173 | 140 | self.code.append(obj) |
paul@15 | 141 | |
paul@15 | 142 | # Append class attributes to the image. |
paul@15 | 143 | |
paul@9 | 144 | attributes = obj.class_attributes() |
paul@173 | 145 | self.code += obj.attributes_as_list() |
paul@9 | 146 | |
paul@203 | 147 | # Omit built-in function code where requested. |
paul@203 | 148 | |
paul@253 | 149 | if suppress_builtins and obj.astnode.doc is None: |
paul@203 | 150 | continue |
paul@203 | 151 | |
paul@137 | 152 | # Generate the instantiator/initialiser. |
paul@137 | 153 | # Append the function code to the image. |
paul@15 | 154 | |
paul@137 | 155 | instantiator = obj.get_instantiator() |
paul@137 | 156 | code = trans.get_instantiator_code(obj) |
paul@173 | 157 | self.code += code |
paul@137 | 158 | |
paul@137 | 159 | # Class-level code is generated separately at the module |
paul@137 | 160 | # level, and the code location is set within the code |
paul@137 | 161 | # generation process for the module. |
paul@9 | 162 | |
paul@9 | 163 | elif isinstance(obj, micropython.inspect.Function): |
paul@15 | 164 | |
paul@22 | 165 | # Add header details. |
paul@22 | 166 | |
paul@173 | 167 | self.code.append(obj) |
paul@9 | 168 | |
paul@66 | 169 | # Append any default values to the image. |
paul@109 | 170 | # Only do this for named functions (not lambdas). |
paul@66 | 171 | |
paul@109 | 172 | if obj.name is not None: |
paul@173 | 173 | self.code += obj.default_attrs |
paul@66 | 174 | |
paul@180 | 175 | # Omit built-in function code where requested. |
paul@180 | 176 | |
paul@253 | 177 | if suppress_builtins and obj.astnode.doc is None: |
paul@180 | 178 | pass |
paul@180 | 179 | |
paul@15 | 180 | # Append the function code to the image. |
paul@15 | 181 | |
paul@180 | 182 | else: |
paul@180 | 183 | code = trans.get_code(obj) |
paul@180 | 184 | self.code += code |
paul@180 | 185 | |
paul@180 | 186 | # Omit built-in module code where requested. |
paul@180 | 187 | |
paul@205 | 188 | if suppress_builtins: |
paul@180 | 189 | pass |
paul@15 | 190 | |
paul@15 | 191 | # Append the module top-level code to the image. |
paul@15 | 192 | |
paul@180 | 193 | else: |
paul@242 | 194 | code = trans.get_module_code() |
paul@180 | 195 | self.code += code |
paul@15 | 196 | |
paul@173 | 197 | return self.code |
paul@173 | 198 | |
paul@265 | 199 | def get_raw_image(self, architecture=None, with_builtins=0): |
paul@173 | 200 | |
paul@173 | 201 | "Return the raw image representation of the program." |
paul@173 | 202 | |
paul@265 | 203 | architecture = architecture or micropython.rsvp |
paul@265 | 204 | |
paul@173 | 205 | self.get_image(with_builtins) |
paul@173 | 206 | |
paul@173 | 207 | objtable = self.get_object_table() |
paul@173 | 208 | paramtable = self.get_parameter_table() |
paul@173 | 209 | |
paul@173 | 210 | # Position the objects. |
paul@173 | 211 | |
paul@173 | 212 | pos = 0 |
paul@215 | 213 | |
paul@173 | 214 | for item in self.code: |
paul@265 | 215 | arch_item = architecture.get_object(item) |
paul@180 | 216 | |
paul@265 | 217 | # Get the raw version for the architecture. |
paul@183 | 218 | |
paul@265 | 219 | if arch_item is not None: |
paul@265 | 220 | pos = arch_item.set_location(pos, with_builtins) |
paul@173 | 221 | else: |
paul@173 | 222 | pos += 1 |
paul@173 | 223 | |
paul@173 | 224 | # Generate the raw code. |
paul@173 | 225 | |
paul@173 | 226 | self.raw_code = [] |
paul@173 | 227 | |
paul@173 | 228 | for item in self.code: |
paul@265 | 229 | arch_item = architecture.get_object(item) |
paul@173 | 230 | |
paul@265 | 231 | # Get the raw version for the architecture. |
paul@139 | 232 | |
paul@265 | 233 | if arch_item is not None: |
paul@265 | 234 | self.raw_code += arch_item.as_raw(objtable, paramtable, with_builtins) |
paul@265 | 235 | arch_item.finalise_location(with_builtins) |
paul@173 | 236 | else: |
paul@173 | 237 | self.raw_code.append(item) |
paul@173 | 238 | |
paul@173 | 239 | # Fix the module locations. |
paul@173 | 240 | |
paul@175 | 241 | for module in self.importer.modules_ordered: |
paul@173 | 242 | |
paul@173 | 243 | if not with_builtins and module.name == "__builtins__": |
paul@173 | 244 | continue |
paul@173 | 245 | |
paul@173 | 246 | module.code_location = module.blocks[0].location |
paul@173 | 247 | |
paul@175 | 248 | self.code_location = self.importer.modules["__main__"].code_location |
paul@173 | 249 | return self.raw_code |
paul@173 | 250 | |
paul@280 | 251 | def get_object_table(self, reset=0): |
paul@7 | 252 | |
paul@7 | 253 | "Return a table with details of attributes for classes and modules." |
paul@7 | 254 | |
paul@280 | 255 | if self.objtable is None or reset: |
paul@171 | 256 | |
paul@132 | 257 | t = self.objtable = micropython.table.ObjectTable() |
paul@175 | 258 | for module in self.importer.get_modules(): |
paul@60 | 259 | t.add(module.full_name(), module.module_attributes()) |
paul@135 | 260 | |
paul@135 | 261 | # Add class and instance attributes for all classes, together |
paul@135 | 262 | # with descendant information. |
paul@135 | 263 | |
paul@60 | 264 | for obj in module.all_objects: |
paul@60 | 265 | if isinstance(obj, micropython.inspect.Class): |
paul@243 | 266 | |
paul@243 | 267 | # Prevent ambiguous classes. |
paul@243 | 268 | |
paul@243 | 269 | full_name = obj.full_name() |
paul@243 | 270 | name = obj.name |
paul@243 | 271 | |
paul@243 | 272 | if module.has_key(name) and module[name].defines_ambiguous_class(): |
paul@243 | 273 | raise TableGenerationError, "Class %r in module %r is ambiguously defined." % (name, module.full_name()) |
paul@243 | 274 | |
paul@243 | 275 | # Define a table entry for the class. |
paul@243 | 276 | |
paul@243 | 277 | attributes = {full_name : obj} |
paul@135 | 278 | attributes.update(obj.all_attributes()) |
paul@135 | 279 | attributes.update(obj.all_descendants()) |
paul@243 | 280 | t.add(full_name, attributes) |
paul@60 | 281 | |
paul@60 | 282 | return self.objtable |
paul@7 | 283 | |
paul@286 | 284 | def get_parameter_table(self, reset=0): |
paul@7 | 285 | |
paul@7 | 286 | "Return a table with details of parameters for functions and methods." |
paul@7 | 287 | |
paul@60 | 288 | # Need the object table to get at class details. |
paul@60 | 289 | |
paul@286 | 290 | if self.paramtable is None or reset: |
paul@132 | 291 | t = self.paramtable = micropython.table.ParameterTable() |
paul@71 | 292 | |
paul@71 | 293 | # Visit each module, getting function and method details. |
paul@71 | 294 | |
paul@175 | 295 | for module in self.importer.get_modules(): |
paul@60 | 296 | for obj in module.all_objects: |
paul@60 | 297 | if isinstance(obj, micropython.inspect.Function): |
paul@60 | 298 | t.add(obj.full_name(), obj.parameters()) |
paul@60 | 299 | |
paul@60 | 300 | # Classes are callable, too. |
paul@61 | 301 | # Take details of the appropriate __init__ method to make an |
paul@61 | 302 | # entry for an instantiation function for the class. |
paul@60 | 303 | |
paul@60 | 304 | elif isinstance(obj, micropython.inspect.Class): |
paul@212 | 305 | t.add(obj.get_instantiator().full_name(), obj.get_instantiator().parameters()) |
paul@60 | 306 | |
paul@71 | 307 | # Filter out all parameter table entries not referenced by keyword |
paul@71 | 308 | # arguments. |
paul@71 | 309 | |
paul@71 | 310 | keyword_names = set() |
paul@71 | 311 | |
paul@175 | 312 | for module in self.importer.get_modules(): |
paul@71 | 313 | keyword_names.update(module.keyword_names) |
paul@71 | 314 | |
paul@71 | 315 | for function_name, parameters in t.table.items(): |
paul@71 | 316 | for name in parameters.keys(): |
paul@71 | 317 | if name in keyword_names: |
paul@71 | 318 | break |
paul@71 | 319 | else: |
paul@71 | 320 | del t.table[function_name] |
paul@71 | 321 | |
paul@60 | 322 | return self.paramtable |
paul@7 | 323 | |
paul@175 | 324 | class Importer: |
paul@175 | 325 | |
paul@175 | 326 | "An import machine, searching for and loading modules." |
paul@175 | 327 | |
paul@175 | 328 | predefined_constants = { |
paul@175 | 329 | "None" : None, |
paul@175 | 330 | "True" : True, |
paul@175 | 331 | "False" : False, |
paul@203 | 332 | #"Ellipsis" : Ellipsis, |
paul@175 | 333 | "NotImplemented" : NotImplemented |
paul@175 | 334 | } |
paul@175 | 335 | |
paul@280 | 336 | names_always_used = [ |
paul@280 | 337 | "bool", "__init__", "__call__", "__bool__", "__builtins__", "__main__" |
paul@280 | 338 | ] |
paul@280 | 339 | |
paul@175 | 340 | def __init__(self, path=None, verbose=0, optimisations=None): |
paul@175 | 341 | |
paul@175 | 342 | """ |
paul@175 | 343 | Initialise the importer with the given search 'path' - a list of |
paul@175 | 344 | directories to search for Python modules. |
paul@175 | 345 | |
paul@175 | 346 | The optional 'verbose' parameter causes output concerning the activities |
paul@175 | 347 | of the object to be produced if set to a true value (not the default). |
paul@175 | 348 | |
paul@175 | 349 | The optional 'optimisations' cause certain techniques to be used in |
paul@175 | 350 | reducing program size and improving program efficiency. |
paul@175 | 351 | """ |
paul@175 | 352 | |
paul@175 | 353 | self.path = path or [os.getcwd()] |
paul@175 | 354 | self.verbose = verbose |
paul@175 | 355 | self.optimisations = optimisations or set() |
paul@175 | 356 | |
paul@175 | 357 | self.modules = {} |
paul@175 | 358 | self.modules_ordered = [] |
paul@175 | 359 | self.loading = set() |
paul@175 | 360 | |
paul@175 | 361 | # Constant records. |
paul@175 | 362 | |
paul@175 | 363 | self.constant_values = {} |
paul@175 | 364 | self.constant_list = None # cache for constants |
paul@175 | 365 | self.init_predefined_constants() |
paul@175 | 366 | |
paul@280 | 367 | # Attribute usage. |
paul@175 | 368 | |
paul@281 | 369 | self.attributes_used = {} |
paul@250 | 370 | self.name_references = {} |
paul@281 | 371 | self.specific_name_references = {} |
paul@289 | 372 | self.attribute_users_visited = set() |
paul@175 | 373 | |
paul@175 | 374 | # Status information. |
paul@175 | 375 | |
paul@175 | 376 | self.vacuumed = 0 |
paul@175 | 377 | self.finalised = 0 |
paul@175 | 378 | |
paul@175 | 379 | def get_modules(self): |
paul@175 | 380 | |
paul@175 | 381 | "Return all modules known to the importer." |
paul@175 | 382 | |
paul@175 | 383 | return self.modules.values() |
paul@175 | 384 | |
paul@228 | 385 | def get_module(self, name): |
paul@228 | 386 | |
paul@228 | 387 | "Return the module with the given 'name'." |
paul@228 | 388 | |
paul@228 | 389 | return self.modules[name] |
paul@228 | 390 | |
paul@175 | 391 | # General maintenance. |
paul@175 | 392 | |
paul@280 | 393 | def vacuum(self, objtable): |
paul@175 | 394 | |
paul@175 | 395 | "Tidy up the modules." |
paul@175 | 396 | |
paul@175 | 397 | if self.vacuumed: |
paul@175 | 398 | return |
paul@175 | 399 | |
paul@280 | 400 | # Complete the list of attribute names used in the program. |
paul@280 | 401 | |
paul@280 | 402 | self.collect_attributes(objtable) |
paul@280 | 403 | |
paul@175 | 404 | for name, module in self.modules.items(): |
paul@175 | 405 | if module.loaded: |
paul@175 | 406 | module.vacuum() |
paul@175 | 407 | else: |
paul@175 | 408 | del self.modules[name] |
paul@175 | 409 | |
paul@175 | 410 | self.vacuumed = 1 |
paul@175 | 411 | |
paul@175 | 412 | def finalise(self): |
paul@175 | 413 | |
paul@280 | 414 | "Finalise the program (which should have been vacuumed first)." |
paul@175 | 415 | |
paul@175 | 416 | if self.finalised: |
paul@175 | 417 | return |
paul@175 | 418 | |
paul@287 | 419 | # Reset any previously compiled information. |
paul@287 | 420 | |
paul@287 | 421 | for module in self.get_modules(): |
paul@287 | 422 | module.unfinalise() |
paul@287 | 423 | |
paul@287 | 424 | # Prepare module information again. |
paul@287 | 425 | |
paul@175 | 426 | for module in self.get_modules(): |
paul@175 | 427 | module.finalise() |
paul@175 | 428 | |
paul@175 | 429 | self.finalised = 1 |
paul@175 | 430 | |
paul@175 | 431 | # Name accounting. |
paul@175 | 432 | |
paul@250 | 433 | def use_name(self, name, from_name): |
paul@250 | 434 | |
paul@250 | 435 | """ |
paul@250 | 436 | Register the given 'name' as being used in the program from within an |
paul@250 | 437 | object with the specified 'from_name'. |
paul@250 | 438 | """ |
paul@250 | 439 | |
paul@250 | 440 | if not self.name_references.has_key(from_name): |
paul@250 | 441 | self.name_references[from_name] = set() |
paul@280 | 442 | self.name_references[from_name].add((name,)) |
paul@280 | 443 | |
paul@280 | 444 | def use_names(self, names, from_name): |
paul@280 | 445 | |
paul@280 | 446 | """ |
paul@280 | 447 | Register the given 'names' as being used in the program from within an |
paul@280 | 448 | object with the specified 'from_name'. |
paul@280 | 449 | """ |
paul@280 | 450 | |
paul@280 | 451 | if not self.name_references.has_key(from_name): |
paul@280 | 452 | self.name_references[from_name] = set() |
paul@280 | 453 | self.name_references[from_name].add(names) |
paul@280 | 454 | |
paul@285 | 455 | def use_specific_name(self, objname, attrname, from_name): |
paul@281 | 456 | |
paul@281 | 457 | """ |
paul@285 | 458 | Register the given 'objname' (for an object) whose 'attrname' is being |
paul@285 | 459 | used in the program from within an object with the specified |
paul@285 | 460 | 'from_name'. |
paul@281 | 461 | """ |
paul@281 | 462 | |
paul@281 | 463 | if not self.specific_name_references.has_key(from_name): |
paul@281 | 464 | self.specific_name_references[from_name] = set() |
paul@285 | 465 | self.specific_name_references[from_name].add((objname, attrname)) |
paul@281 | 466 | |
paul@281 | 467 | # Name accounting products. |
paul@281 | 468 | |
paul@280 | 469 | def uses_attribute(self, obj, name): |
paul@250 | 470 | |
paul@280 | 471 | """ |
paul@280 | 472 | Return whether the attribute of object 'obj' having the given 'name' is |
paul@280 | 473 | used as an attribute in the program. |
paul@280 | 474 | """ |
paul@280 | 475 | |
paul@280 | 476 | return name in self.attributes_used.get(obj.full_name(), []) |
paul@280 | 477 | |
paul@280 | 478 | def use_attribute(self, objname, name): |
paul@175 | 479 | |
paul@280 | 480 | """ |
paul@280 | 481 | Indicate that in the object with the given 'objname', the attribute of |
paul@280 | 482 | the given 'name' is used. |
paul@280 | 483 | """ |
paul@280 | 484 | |
paul@280 | 485 | if not self.attributes_used.has_key(objname): |
paul@280 | 486 | self.attributes_used[objname] = set() |
paul@280 | 487 | self.attributes_used[objname].add(name) |
paul@250 | 488 | |
paul@280 | 489 | def collect_attributes(self, objtable): |
paul@280 | 490 | |
paul@280 | 491 | "Collect attribute references for the entire program." |
paul@280 | 492 | |
paul@283 | 493 | # Start with the "root" modules, finding referenced objects. |
paul@283 | 494 | |
paul@283 | 495 | self._collect_attributes("__builtins__", objtable) |
paul@283 | 496 | self._collect_attributes("__main__", objtable) |
paul@283 | 497 | |
paul@281 | 498 | # Include names which may not be explicitly used in programs. |
paul@281 | 499 | # NOTE: Potentially declare these when inspecting. |
paul@280 | 500 | |
paul@281 | 501 | for name in self.names_always_used: |
paul@281 | 502 | for objname in objtable.all_possible_objects([name]): |
paul@281 | 503 | self.use_attribute(objname, name) |
paul@280 | 504 | |
paul@280 | 505 | def _collect_attributes(self, from_name, objtable): |
paul@250 | 506 | |
paul@280 | 507 | """ |
paul@280 | 508 | Given an object called 'from_name', find all names referenced from such |
paul@280 | 509 | an object according to the register of names. |
paul@280 | 510 | """ |
paul@280 | 511 | |
paul@289 | 512 | if from_name in self.attribute_users_visited: |
paul@280 | 513 | return |
paul@280 | 514 | |
paul@301 | 515 | self.attribute_users_visited.add(from_name) |
paul@301 | 516 | |
paul@281 | 517 | # Get name references and find possible objects which support such |
paul@281 | 518 | # combinations of attribute names. |
paul@281 | 519 | |
paul@280 | 520 | for names in self.name_references.get(from_name, []): |
paul@280 | 521 | for objname in objtable.all_possible_objects(names): |
paul@280 | 522 | for name in names: |
paul@280 | 523 | self.use_attribute(objname, name) |
paul@280 | 524 | self._collect_attributes(objname + "." + name, objtable) |
paul@175 | 525 | |
paul@281 | 526 | # Get specific name references and visit the referenced objects. |
paul@281 | 527 | |
paul@285 | 528 | for objname, attrname in self.specific_name_references.get(from_name, []): |
paul@285 | 529 | self.use_attribute(objname, attrname) |
paul@285 | 530 | self._collect_attributes(objname + "." + attrname, objtable) |
paul@281 | 531 | |
paul@175 | 532 | # Constant accounting. |
paul@175 | 533 | |
paul@175 | 534 | def init_predefined_constants(self): |
paul@175 | 535 | |
paul@175 | 536 | "Ensure the predefined constants." |
paul@175 | 537 | |
paul@175 | 538 | for name, value in self.predefined_constants.items(): |
paul@175 | 539 | self.make_constant(value) |
paul@175 | 540 | |
paul@175 | 541 | def get_predefined_constant(self, name): |
paul@175 | 542 | |
paul@175 | 543 | "Return the predefined constant for the given 'name'." |
paul@175 | 544 | |
paul@175 | 545 | return self.make_constant(self.predefined_constants[name]) |
paul@175 | 546 | |
paul@175 | 547 | def get_constant(self, value): |
paul@175 | 548 | |
paul@175 | 549 | "Return a constant for the given 'value'." |
paul@175 | 550 | |
paul@176 | 551 | const = micropython.data.Const(value) |
paul@176 | 552 | return self.constant_values[const] |
paul@175 | 553 | |
paul@252 | 554 | def get_constant_type_name(self, value): |
paul@252 | 555 | return value.__class__.__name__ |
paul@252 | 556 | |
paul@175 | 557 | def make_constant(self, value): |
paul@175 | 558 | |
paul@175 | 559 | "Make and return a constant for the given 'value'." |
paul@175 | 560 | |
paul@219 | 561 | # Ensure the presence of the constant's type. |
paul@219 | 562 | |
paul@252 | 563 | name = self.get_constant_type_name(value) |
paul@219 | 564 | if self.modules.has_key("__builtins__"): |
paul@219 | 565 | attr = self.modules["__builtins__"].get(name) |
paul@219 | 566 | |
paul@219 | 567 | # Make a constant object and return it. |
paul@219 | 568 | |
paul@176 | 569 | const = micropython.data.Const(value) |
paul@176 | 570 | if not self.constant_values.has_key(const): |
paul@176 | 571 | self.constant_values[const] = const |
paul@176 | 572 | return self.constant_values[const] |
paul@175 | 573 | |
paul@175 | 574 | def constants(self): |
paul@175 | 575 | |
paul@175 | 576 | "Return a list of constants." |
paul@175 | 577 | |
paul@175 | 578 | if self.constant_list is None: |
paul@175 | 579 | self.constant_list = list(self.constant_values.values()) |
paul@175 | 580 | |
paul@175 | 581 | return self.constant_list |
paul@175 | 582 | |
paul@9 | 583 | # Import methods. |
paul@9 | 584 | |
paul@0 | 585 | def find_in_path(self, name): |
paul@0 | 586 | |
paul@0 | 587 | """ |
paul@0 | 588 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 589 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 590 | otherwise. |
paul@0 | 591 | """ |
paul@0 | 592 | |
paul@0 | 593 | for d in self.path: |
paul@0 | 594 | m = self.find(d, name) |
paul@0 | 595 | if m: return m |
paul@0 | 596 | return None |
paul@0 | 597 | |
paul@0 | 598 | def find(self, d, name): |
paul@0 | 599 | |
paul@0 | 600 | """ |
paul@0 | 601 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 602 | either refer to a single file module or to a package. Return None if the |
paul@0 | 603 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 604 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 605 | """ |
paul@0 | 606 | |
paul@0 | 607 | m = self._find_package(d, name) |
paul@0 | 608 | if m: return m |
paul@0 | 609 | m = self._find_module(d, name) |
paul@0 | 610 | if m: return m |
paul@0 | 611 | return None |
paul@0 | 612 | |
paul@0 | 613 | def _find_module(self, d, name): |
paul@0 | 614 | |
paul@0 | 615 | """ |
paul@0 | 616 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 617 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 618 | None (indicating that no package directory is involved) and a filename |
paul@0 | 619 | indicating the location of the module. |
paul@0 | 620 | """ |
paul@0 | 621 | |
paul@0 | 622 | name_py = name + os.extsep + "py" |
paul@0 | 623 | filename = self._find_file(d, name_py) |
paul@0 | 624 | if filename: |
paul@0 | 625 | return None, filename |
paul@0 | 626 | return None |
paul@0 | 627 | |
paul@0 | 628 | def _find_package(self, d, name): |
paul@0 | 629 | |
paul@0 | 630 | """ |
paul@0 | 631 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 632 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 633 | a directory (indicating the location of the package directory itself) |
paul@0 | 634 | and a filename indicating the location of the __init__.py module which |
paul@0 | 635 | declares the package's top-level contents. |
paul@0 | 636 | """ |
paul@0 | 637 | |
paul@0 | 638 | filename = self._find_file(d, name) |
paul@0 | 639 | if filename: |
paul@0 | 640 | init_py = "__init__" + os.path.extsep + "py" |
paul@0 | 641 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 642 | if init_py_filename: |
paul@0 | 643 | return filename, init_py_filename |
paul@0 | 644 | return None |
paul@0 | 645 | |
paul@0 | 646 | def _find_file(self, d, filename): |
paul@0 | 647 | |
paul@0 | 648 | """ |
paul@0 | 649 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 650 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 651 | """ |
paul@0 | 652 | |
paul@0 | 653 | filename = os.path.join(d, filename) |
paul@0 | 654 | if os.path.exists(filename): |
paul@0 | 655 | return filename |
paul@0 | 656 | else: |
paul@0 | 657 | return None |
paul@0 | 658 | |
paul@0 | 659 | def load(self, name, return_leaf=0): |
paul@0 | 660 | |
paul@0 | 661 | """ |
paul@0 | 662 | Load the module or package with the given 'name'. Return an object |
paul@0 | 663 | referencing the loaded module or package, or None if no such module or |
paul@0 | 664 | package exists. |
paul@0 | 665 | """ |
paul@0 | 666 | |
paul@4 | 667 | if self.modules.has_key(name) and self.modules[name].loaded: |
paul@4 | 668 | #print "Cached (%s)" % name |
paul@4 | 669 | return self.modules[name] |
paul@35 | 670 | if self.verbose: |
paul@35 | 671 | print "Loading", name |
paul@0 | 672 | |
paul@0 | 673 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 674 | # the search path. |
paul@0 | 675 | |
paul@0 | 676 | path = name.split(".") |
paul@0 | 677 | m = self.find_in_path(path[0]) |
paul@0 | 678 | if not m: |
paul@35 | 679 | if self.verbose: |
paul@35 | 680 | print "Not found (%s)" % path[0] |
paul@0 | 681 | return None # NOTE: Import error. |
paul@0 | 682 | d, filename = m |
paul@0 | 683 | |
paul@0 | 684 | # Either acquire a reference to an already-imported module, or load the |
paul@0 | 685 | # module from a file. |
paul@0 | 686 | |
paul@2 | 687 | top = module = self.load_from_file(filename, path[0]) |
paul@0 | 688 | |
paul@0 | 689 | # For hierarchical names, traverse each path component... |
paul@0 | 690 | |
paul@0 | 691 | if len(path) > 1: |
paul@0 | 692 | if not d: |
paul@35 | 693 | if self.verbose: |
paul@35 | 694 | print "No package (%s)" % filename |
paul@0 | 695 | return None # NOTE: Import error (package not found). |
paul@1 | 696 | else: |
paul@1 | 697 | self.add_submodules(d, module) |
paul@0 | 698 | |
paul@0 | 699 | path_so_far = path[:1] |
paul@0 | 700 | for p in path[1:]: |
paul@0 | 701 | path_so_far.append(p) |
paul@0 | 702 | |
paul@0 | 703 | # Find the package or module concerned. |
paul@0 | 704 | |
paul@0 | 705 | m = self.find(d, p) |
paul@0 | 706 | if not m: |
paul@35 | 707 | if self.verbose: |
paul@35 | 708 | print "Not found (%s)" % p |
paul@0 | 709 | return None # NOTE: Import error. |
paul@0 | 710 | d, filename = m |
paul@0 | 711 | module_name = ".".join(path_so_far) |
paul@0 | 712 | |
paul@0 | 713 | # Either reference an imported module or load one from a file. |
paul@0 | 714 | |
paul@2 | 715 | submodule = self.load_from_file(filename, module_name) |
paul@0 | 716 | |
paul@1 | 717 | if d: |
paul@1 | 718 | self.add_submodules(d, module) |
paul@1 | 719 | |
paul@0 | 720 | # Store the submodule within its parent module. |
paul@0 | 721 | |
paul@32 | 722 | module.set_module(p, submodule) |
paul@0 | 723 | module = submodule |
paul@0 | 724 | |
paul@0 | 725 | # Return either the deepest or the uppermost module. |
paul@0 | 726 | |
paul@0 | 727 | if return_leaf: |
paul@0 | 728 | return module |
paul@0 | 729 | else: |
paul@0 | 730 | return top |
paul@0 | 731 | |
paul@0 | 732 | def load_from_file(self, name, module_name=None): |
paul@0 | 733 | |
paul@0 | 734 | """ |
paul@0 | 735 | Load the module with the given 'name' (which may be a full module path). |
paul@0 | 736 | """ |
paul@0 | 737 | |
paul@0 | 738 | if module_name is None: |
paul@0 | 739 | module_name = "__main__" |
paul@0 | 740 | |
paul@1 | 741 | module = self.add_module(module_name) |
paul@2 | 742 | if not module.loaded and module not in self.loading: |
paul@2 | 743 | self.loading.add(module) |
paul@4 | 744 | #print "Parsing", name |
paul@2 | 745 | module.parse(name) |
paul@4 | 746 | #print "Done", name |
paul@2 | 747 | self.loading.remove(module) |
paul@2 | 748 | module.loaded = 1 |
paul@0 | 749 | |
paul@0 | 750 | # Record the module. |
paul@0 | 751 | |
paul@4 | 752 | #print "Loaded", module_name, "with namespace", module.namespace.keys() |
paul@0 | 753 | return module |
paul@0 | 754 | |
paul@1 | 755 | def add_module(self, module_name): |
paul@1 | 756 | |
paul@1 | 757 | """ |
paul@1 | 758 | Return the module with the given 'module_name', adding a new module |
paul@1 | 759 | object if one does not already exist. |
paul@1 | 760 | """ |
paul@1 | 761 | |
paul@1 | 762 | if not self.modules.has_key(module_name): |
paul@15 | 763 | self.modules[module_name] = module = micropython.inspect.InspectedModule(module_name, self) |
paul@128 | 764 | self.modules_ordered.append(module) |
paul@1 | 765 | else: |
paul@1 | 766 | module = self.modules[module_name] |
paul@1 | 767 | return module |
paul@1 | 768 | |
paul@1 | 769 | def add_submodules(self, pathname, module): |
paul@1 | 770 | |
paul@1 | 771 | """ |
paul@1 | 772 | Work around insufficient __all__ declarations and examine the directory |
paul@1 | 773 | with the given 'pathname', adding submodules to the given 'module'. |
paul@1 | 774 | """ |
paul@1 | 775 | |
paul@1 | 776 | for filename in os.listdir(pathname): |
paul@50 | 777 | submodule, ext = os.path.splitext(filename) |
paul@50 | 778 | if ext not in ("", ".py"): |
paul@50 | 779 | continue |
paul@32 | 780 | module.set_module(submodule, self.add_module(module.name + "." + submodule)) |
paul@1 | 781 | |
paul@0 | 782 | # vim: tabstop=4 expandtab shiftwidth=4 |