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