(self, mapping=(), fvars={}, autoreload=None)
| 46 | # PY3DOCTEST: b'hello' |
| 47 | |
| 48 | def __init__(self, mapping=(), fvars={}, autoreload=None): |
| 49 | if autoreload is None: |
| 50 | autoreload = web.config.get("debug", False) |
| 51 | self.init_mapping(mapping) |
| 52 | self.fvars = fvars |
| 53 | self.processors = [] |
| 54 | |
| 55 | self.add_processor(loadhook(self._load)) |
| 56 | self.add_processor(unloadhook(self._unload)) |
| 57 | |
| 58 | if autoreload: |
| 59 | |
| 60 | def main_module_name(): |
| 61 | mod = sys.modules["__main__"] |
| 62 | file = getattr( |
| 63 | mod, "__file__", None |
| 64 | ) # make sure this works even from python interpreter |
| 65 | return file and os.path.splitext(os.path.basename(file))[0] |
| 66 | |
| 67 | def modname(fvars): |
| 68 | """find name of the module name from fvars.""" |
| 69 | file, name = fvars.get("__file__"), fvars.get("__name__") |
| 70 | if file is None or name is None: |
| 71 | return None |
| 72 | |
| 73 | if name == "__main__": |
| 74 | # Since the __main__ module can't be reloaded, the module has |
| 75 | # to be imported using its file name. |
| 76 | name = main_module_name() |
| 77 | return name |
| 78 | |
| 79 | mapping_name = utils.dictfind(fvars, mapping) |
| 80 | module_name = modname(fvars) |
| 81 | |
| 82 | def reload_mapping(): |
| 83 | """loadhook to reload mapping and fvars.""" |
| 84 | mod = __import__(module_name, None, None, [""]) |
| 85 | mapping = getattr(mod, mapping_name, None) |
| 86 | if mapping: |
| 87 | self.fvars = mod.__dict__ |
| 88 | self.init_mapping(mapping) |
| 89 | |
| 90 | self.add_processor(loadhook(Reloader())) |
| 91 | if mapping_name and module_name: |
| 92 | # when app is ran as part of a package, this puts the app into |
| 93 | # `sys.modules` correctly, otherwise the first change to the |
| 94 | # app module will not be picked up by Reloader |
| 95 | reload_mapping() |
| 96 | |
| 97 | self.add_processor(loadhook(reload_mapping)) |
| 98 | |
| 99 | # load __main__ module usings its filename, so that it can be reloaded. |
| 100 | if main_module_name() and "__main__" in sys.argv: |
| 101 | try: |
| 102 | __import__(main_module_name()) |
| 103 | except ImportError: |
| 104 | pass |
| 105 |
nothing calls this directly
no test coverage detected