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