Make dist checking directory-agnostic
[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             "menu",
22             "misc",
23             "password",
24             "telnet",
25             "version",
26             ]
27     try:
28         # dynamically build module list from package contents (this only works
29         # in Python 3.7 and later, hence the try/except)
30         modules = []
31         for module in mudpy.__loader__.contents():
32
33             if (
34                     # make sure it's a module file, not a directory
35                     module.endswith('.py')
36
37                     # don't include this file, we're inside it
38                     and module != '__init__.py'):
39
40                 # trim off the .py file extension
41                 modules.append(module[:-3])
42
43         # make sure the fallback list is kept up to date with package contents
44         if fallback_modules != sorted(modules):
45             raise Exception("Fallback module list is incomplete")
46
47     except AttributeError:
48         modules = fallback_modules
49
50     # iterate over the list of module files included in the package
51     for module in modules:
52
53         # attempt to reload the module, assuming it was probably imported
54         # earlier
55         try:
56             importlib.reload(getattr(mudpy, module))
57
58         # must not have been, so import it now
59         except AttributeError:
60             importlib.import_module("mudpy.%s" % module)
61
62
63 load()