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