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