Eliminate exec() in Universe.new()
[mudpy.git] / mudpy / __init__.py
1 """Core modules package for the mudpy engine."""
2
3 # Copyright (c) 2004-2019 mudpy authors. Permission to use, copy,
4 # modify, and distribute this software is granted under terms
5 # provided in the LICENSE file distributed with this software.
6
7 import importlib
8
9 import mudpy
10
11
12 def load():
13     """Import/reload some modules (be careful, as this can result in loops)."""
14
15     # hard-coded fallback list of modules expected in this package
16     # TODO(fungi) remove this once Python 3.6 is no longer supported
17     fallback_modules = [
18             "command",
19             "daemon",
20             "data",
21             "misc",
22             "password",
23             "telnet",
24             "version",
25             ]
26     try:
27         # dynamically build module list from package contents (this only works
28         # in Python 3.7 and later, hence the try/except)
29         modules = []
30         for module in mudpy.__loader__.contents():
31
32             if (
33                     # make sure it's a module file, not a directory
34                     module.endswith('.py')
35
36                     # don't include this file, we're inside it
37                     and module != '__init__.py'):
38
39                 # trim off the .py file extension
40                 modules.append(module[:-3])
41
42         # make sure the fallback list is kept up to date with package contents
43         if fallback_modules != sorted(modules):
44             raise Exception("Fallback module list is incomplete")
45
46     except AttributeError:
47         modules = fallback_modules
48
49     # iterate over the list of module files included in the package
50     for module in modules:
51
52         # attempt to reload the module, assuming it was probably imported
53         # earlier
54         try:
55             importlib.reload(getattr(mudpy, module))
56
57         # must not have been, so import it now
58         except AttributeError:
59             importlib.import_module("mudpy.%s" % module)
60
61
62 load()