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@9 | 209 | image = [] |
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@82 | 214 | const.location = pos |
paul@82 | 215 | image.append(const) |
paul@82 | 216 | |
paul@139 | 217 | last_module = self.modules_ordered[-1] |
paul@139 | 218 | |
paul@128 | 219 | for module in self.modules_ordered: |
paul@128 | 220 | |
paul@128 | 221 | if not with_builtins and module.name == "__builtins__": |
paul@44 | 222 | continue |
paul@44 | 223 | |
paul@9 | 224 | pos = len(image) |
paul@15 | 225 | |
paul@55 | 226 | # Position the module in the image and make a translation. |
paul@55 | 227 | |
paul@55 | 228 | module.location = pos |
paul@167 | 229 | trans = micropython.ast.Translation(module, self) |
paul@55 | 230 | |
paul@37 | 231 | # Add header details. |
paul@37 | 232 | |
paul@37 | 233 | image.append(module) |
paul@37 | 234 | pos += 1 |
paul@37 | 235 | |
paul@15 | 236 | # Append module attributes to the image. |
paul@15 | 237 | |
paul@9 | 238 | attributes = module.module_attributes() |
paul@49 | 239 | image += module.attributes_as_list() |
paul@37 | 240 | pos += len(attributes.keys()) |
paul@9 | 241 | |
paul@15 | 242 | # Append classes and functions to the image. |
paul@15 | 243 | |
paul@9 | 244 | for obj in module.all_objects: |
paul@55 | 245 | if isinstance(obj, micropython.inspect.Class): |
paul@54 | 246 | |
paul@15 | 247 | # Position the class in the image. |
paul@15 | 248 | |
paul@22 | 249 | obj.location = pos |
paul@22 | 250 | |
paul@22 | 251 | # Add header details. |
paul@22 | 252 | |
paul@22 | 253 | image.append(obj) |
paul@22 | 254 | pos += 1 |
paul@15 | 255 | |
paul@15 | 256 | # Append class attributes to the image. |
paul@15 | 257 | |
paul@9 | 258 | attributes = obj.class_attributes() |
paul@49 | 259 | image += obj.attributes_as_list() |
paul@37 | 260 | pos += len(attributes.keys()) |
paul@9 | 261 | |
paul@137 | 262 | # Generate the instantiator/initialiser. |
paul@137 | 263 | # Append the function code to the image. |
paul@15 | 264 | |
paul@137 | 265 | instantiator = obj.get_instantiator() |
paul@137 | 266 | instantiator.code_location = pos |
paul@137 | 267 | code = trans.get_instantiator_code(obj) |
paul@137 | 268 | image += code |
paul@137 | 269 | pos += len(code) |
paul@137 | 270 | |
paul@137 | 271 | # Class-level code is generated separately at the module |
paul@137 | 272 | # level, and the code location is set within the code |
paul@137 | 273 | # generation process for the module. |
paul@9 | 274 | |
paul@9 | 275 | elif isinstance(obj, micropython.inspect.Function): |
paul@15 | 276 | |
paul@15 | 277 | # Position the function in the image. |
paul@15 | 278 | |
paul@22 | 279 | obj.location = pos |
paul@22 | 280 | |
paul@22 | 281 | # Add header details. |
paul@22 | 282 | |
paul@22 | 283 | image.append(obj) |
paul@22 | 284 | pos += 1 |
paul@9 | 285 | |
paul@66 | 286 | # Append any default values to the image. |
paul@109 | 287 | # Only do this for named functions (not lambdas). |
paul@66 | 288 | |
paul@109 | 289 | if obj.name is not None: |
paul@109 | 290 | image += obj.default_attrs |
paul@109 | 291 | pos += len(obj.default_attrs) |
paul@66 | 292 | |
paul@15 | 293 | # Append the function code to the image. |
paul@15 | 294 | |
paul@44 | 295 | obj.code_location = pos |
paul@17 | 296 | code = trans.get_code(obj) |
paul@17 | 297 | image += code |
paul@17 | 298 | pos += len(code) |
paul@15 | 299 | |
paul@18 | 300 | # Remember the position of the module code. |
paul@18 | 301 | |
paul@19 | 302 | module.code_location = pos |
paul@18 | 303 | |
paul@15 | 304 | # Append the module top-level code to the image. |
paul@15 | 305 | |
paul@139 | 306 | code = trans.get_module_code(final=(module is last_module)) |
paul@17 | 307 | image += code |
paul@17 | 308 | pos += len(code) |
paul@15 | 309 | |
paul@139 | 310 | # Remember the generated code and the location of the first instruction. |
paul@139 | 311 | |
paul@93 | 312 | self.code = image |
paul@93 | 313 | self.code_location = self.modules["__main__"].code_location |
paul@19 | 314 | return image |
paul@9 | 315 | |
paul@7 | 316 | def get_object_table(self): |
paul@7 | 317 | |
paul@7 | 318 | "Return a table with details of attributes for classes and modules." |
paul@7 | 319 | |
paul@60 | 320 | if self.objtable is None: |
paul@171 | 321 | self.vacuum() |
paul@171 | 322 | |
paul@132 | 323 | t = self.objtable = micropython.table.ObjectTable() |
paul@60 | 324 | for module in self.get_modules(): |
paul@60 | 325 | t.add(module.full_name(), module.module_attributes()) |
paul@135 | 326 | |
paul@135 | 327 | # Add class and instance attributes for all classes, together |
paul@135 | 328 | # with descendant information. |
paul@135 | 329 | |
paul@60 | 330 | for obj in module.all_objects: |
paul@60 | 331 | if isinstance(obj, micropython.inspect.Class): |
paul@135 | 332 | attributes = {obj.full_name() : obj} |
paul@135 | 333 | attributes.update(obj.all_attributes()) |
paul@135 | 334 | attributes.update(obj.all_descendants()) |
paul@135 | 335 | t.add(obj.full_name(), attributes) |
paul@60 | 336 | |
paul@60 | 337 | return self.objtable |
paul@7 | 338 | |
paul@7 | 339 | def get_parameter_table(self): |
paul@7 | 340 | |
paul@7 | 341 | "Return a table with details of parameters for functions and methods." |
paul@7 | 342 | |
paul@60 | 343 | # Need the object table to get at class details. |
paul@60 | 344 | |
paul@60 | 345 | objtable = self.get_object_table() |
paul@60 | 346 | |
paul@60 | 347 | if self.paramtable is None: |
paul@132 | 348 | t = self.paramtable = micropython.table.ParameterTable() |
paul@71 | 349 | |
paul@71 | 350 | # Visit each module, getting function and method details. |
paul@71 | 351 | |
paul@60 | 352 | for module in self.get_modules(): |
paul@60 | 353 | for obj in module.all_objects: |
paul@60 | 354 | if isinstance(obj, micropython.inspect.Function): |
paul@60 | 355 | t.add(obj.full_name(), obj.parameters()) |
paul@60 | 356 | |
paul@60 | 357 | # Classes are callable, too. |
paul@61 | 358 | # Take details of the appropriate __init__ method to make an |
paul@61 | 359 | # entry for an instantiation function for the class. |
paul@60 | 360 | |
paul@60 | 361 | elif isinstance(obj, micropython.inspect.Class): |
paul@61 | 362 | t.add(obj.full_name(), obj.get_instantiator().parameters()) |
paul@60 | 363 | |
paul@71 | 364 | # Filter out all parameter table entries not referenced by keyword |
paul@71 | 365 | # arguments. |
paul@71 | 366 | |
paul@71 | 367 | keyword_names = set() |
paul@71 | 368 | |
paul@71 | 369 | for module in self.get_modules(): |
paul@71 | 370 | keyword_names.update(module.keyword_names) |
paul@71 | 371 | |
paul@71 | 372 | for function_name, parameters in t.table.items(): |
paul@71 | 373 | for name in parameters.keys(): |
paul@71 | 374 | if name in keyword_names: |
paul@71 | 375 | break |
paul@71 | 376 | else: |
paul@71 | 377 | del t.table[function_name] |
paul@71 | 378 | |
paul@60 | 379 | return self.paramtable |
paul@7 | 380 | |
paul@9 | 381 | # Import methods. |
paul@9 | 382 | |
paul@0 | 383 | def find_in_path(self, name): |
paul@0 | 384 | |
paul@0 | 385 | """ |
paul@0 | 386 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 387 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 388 | otherwise. |
paul@0 | 389 | """ |
paul@0 | 390 | |
paul@0 | 391 | for d in self.path: |
paul@0 | 392 | m = self.find(d, name) |
paul@0 | 393 | if m: return m |
paul@0 | 394 | return None |
paul@0 | 395 | |
paul@0 | 396 | def find(self, d, name): |
paul@0 | 397 | |
paul@0 | 398 | """ |
paul@0 | 399 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 400 | either refer to a single file module or to a package. Return None if the |
paul@0 | 401 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 402 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 403 | """ |
paul@0 | 404 | |
paul@0 | 405 | m = self._find_package(d, name) |
paul@0 | 406 | if m: return m |
paul@0 | 407 | m = self._find_module(d, name) |
paul@0 | 408 | if m: return m |
paul@0 | 409 | return None |
paul@0 | 410 | |
paul@0 | 411 | def _find_module(self, d, name): |
paul@0 | 412 | |
paul@0 | 413 | """ |
paul@0 | 414 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 415 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 416 | None (indicating that no package directory is involved) and a filename |
paul@0 | 417 | indicating the location of the module. |
paul@0 | 418 | """ |
paul@0 | 419 | |
paul@0 | 420 | name_py = name + os.extsep + "py" |
paul@0 | 421 | filename = self._find_file(d, name_py) |
paul@0 | 422 | if filename: |
paul@0 | 423 | return None, filename |
paul@0 | 424 | return None |
paul@0 | 425 | |
paul@0 | 426 | def _find_package(self, d, name): |
paul@0 | 427 | |
paul@0 | 428 | """ |
paul@0 | 429 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 430 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 431 | a directory (indicating the location of the package directory itself) |
paul@0 | 432 | and a filename indicating the location of the __init__.py module which |
paul@0 | 433 | declares the package's top-level contents. |
paul@0 | 434 | """ |
paul@0 | 435 | |
paul@0 | 436 | filename = self._find_file(d, name) |
paul@0 | 437 | if filename: |
paul@0 | 438 | init_py = "__init__" + os.path.extsep + "py" |
paul@0 | 439 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 440 | if init_py_filename: |
paul@0 | 441 | return filename, init_py_filename |
paul@0 | 442 | return None |
paul@0 | 443 | |
paul@0 | 444 | def _find_file(self, d, filename): |
paul@0 | 445 | |
paul@0 | 446 | """ |
paul@0 | 447 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 448 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 449 | """ |
paul@0 | 450 | |
paul@0 | 451 | filename = os.path.join(d, filename) |
paul@0 | 452 | if os.path.exists(filename): |
paul@0 | 453 | return filename |
paul@0 | 454 | else: |
paul@0 | 455 | return None |
paul@0 | 456 | |
paul@0 | 457 | def load(self, name, return_leaf=0): |
paul@0 | 458 | |
paul@0 | 459 | """ |
paul@0 | 460 | Load the module or package with the given 'name'. Return an object |
paul@0 | 461 | referencing the loaded module or package, or None if no such module or |
paul@0 | 462 | package exists. |
paul@0 | 463 | """ |
paul@0 | 464 | |
paul@4 | 465 | if self.modules.has_key(name) and self.modules[name].loaded: |
paul@4 | 466 | #print "Cached (%s)" % name |
paul@4 | 467 | return self.modules[name] |
paul@35 | 468 | if self.verbose: |
paul@35 | 469 | print "Loading", name |
paul@0 | 470 | |
paul@0 | 471 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 472 | # the search path. |
paul@0 | 473 | |
paul@0 | 474 | path = name.split(".") |
paul@0 | 475 | m = self.find_in_path(path[0]) |
paul@0 | 476 | if not m: |
paul@35 | 477 | if self.verbose: |
paul@35 | 478 | print "Not found (%s)" % path[0] |
paul@0 | 479 | return None # NOTE: Import error. |
paul@0 | 480 | d, filename = m |
paul@0 | 481 | |
paul@0 | 482 | # Either acquire a reference to an already-imported module, or load the |
paul@0 | 483 | # module from a file. |
paul@0 | 484 | |
paul@2 | 485 | top = module = self.load_from_file(filename, path[0]) |
paul@0 | 486 | |
paul@0 | 487 | # For hierarchical names, traverse each path component... |
paul@0 | 488 | |
paul@0 | 489 | if len(path) > 1: |
paul@0 | 490 | if not d: |
paul@35 | 491 | if self.verbose: |
paul@35 | 492 | print "No package (%s)" % filename |
paul@0 | 493 | return None # NOTE: Import error (package not found). |
paul@1 | 494 | else: |
paul@1 | 495 | self.add_submodules(d, module) |
paul@0 | 496 | |
paul@0 | 497 | path_so_far = path[:1] |
paul@0 | 498 | for p in path[1:]: |
paul@0 | 499 | path_so_far.append(p) |
paul@0 | 500 | |
paul@0 | 501 | # Find the package or module concerned. |
paul@0 | 502 | |
paul@0 | 503 | m = self.find(d, p) |
paul@0 | 504 | if not m: |
paul@35 | 505 | if self.verbose: |
paul@35 | 506 | print "Not found (%s)" % p |
paul@0 | 507 | return None # NOTE: Import error. |
paul@0 | 508 | d, filename = m |
paul@0 | 509 | module_name = ".".join(path_so_far) |
paul@0 | 510 | |
paul@0 | 511 | # Either reference an imported module or load one from a file. |
paul@0 | 512 | |
paul@2 | 513 | submodule = self.load_from_file(filename, module_name) |
paul@0 | 514 | |
paul@1 | 515 | if d: |
paul@1 | 516 | self.add_submodules(d, module) |
paul@1 | 517 | |
paul@0 | 518 | # Store the submodule within its parent module. |
paul@0 | 519 | |
paul@32 | 520 | module.set_module(p, submodule) |
paul@0 | 521 | module = submodule |
paul@0 | 522 | |
paul@0 | 523 | # Return either the deepest or the uppermost module. |
paul@0 | 524 | |
paul@0 | 525 | if return_leaf: |
paul@0 | 526 | return module |
paul@0 | 527 | else: |
paul@0 | 528 | return top |
paul@0 | 529 | |
paul@0 | 530 | def load_from_file(self, name, module_name=None): |
paul@0 | 531 | |
paul@0 | 532 | """ |
paul@0 | 533 | Load the module with the given 'name' (which may be a full module path). |
paul@0 | 534 | """ |
paul@0 | 535 | |
paul@0 | 536 | if module_name is None: |
paul@0 | 537 | module_name = "__main__" |
paul@0 | 538 | |
paul@1 | 539 | module = self.add_module(module_name) |
paul@2 | 540 | if not module.loaded and module not in self.loading: |
paul@2 | 541 | self.loading.add(module) |
paul@4 | 542 | #print "Parsing", name |
paul@2 | 543 | module.parse(name) |
paul@4 | 544 | #print "Done", name |
paul@2 | 545 | self.loading.remove(module) |
paul@2 | 546 | module.loaded = 1 |
paul@0 | 547 | |
paul@0 | 548 | # Record the module. |
paul@0 | 549 | |
paul@4 | 550 | #print "Loaded", module_name, "with namespace", module.namespace.keys() |
paul@0 | 551 | return module |
paul@0 | 552 | |
paul@1 | 553 | def add_module(self, module_name): |
paul@1 | 554 | |
paul@1 | 555 | """ |
paul@1 | 556 | Return the module with the given 'module_name', adding a new module |
paul@1 | 557 | object if one does not already exist. |
paul@1 | 558 | """ |
paul@1 | 559 | |
paul@1 | 560 | if not self.modules.has_key(module_name): |
paul@15 | 561 | self.modules[module_name] = module = micropython.inspect.InspectedModule(module_name, self) |
paul@128 | 562 | self.modules_ordered.append(module) |
paul@1 | 563 | else: |
paul@1 | 564 | module = self.modules[module_name] |
paul@1 | 565 | return module |
paul@1 | 566 | |
paul@1 | 567 | def add_submodules(self, pathname, module): |
paul@1 | 568 | |
paul@1 | 569 | """ |
paul@1 | 570 | Work around insufficient __all__ declarations and examine the directory |
paul@1 | 571 | with the given 'pathname', adding submodules to the given 'module'. |
paul@1 | 572 | """ |
paul@1 | 573 | |
paul@1 | 574 | for filename in os.listdir(pathname): |
paul@50 | 575 | submodule, ext = os.path.splitext(filename) |
paul@50 | 576 | if ext not in ("", ".py"): |
paul@50 | 577 | continue |
paul@32 | 578 | module.set_module(submodule, self.add_module(module.name + "." + submodule)) |
paul@1 | 579 | |
paul@0 | 580 | # vim: tabstop=4 expandtab shiftwidth=4 |