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