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