Refactor automatic importing in snscrape.modules to something less hacky

Cf. #357
This commit is contained in:
JustAnotherArchivist
2022-02-05 03:22:24 +00:00
parent 560c78c5cf
commit aa7d7d3dc3

View File

@@ -1,15 +1,17 @@
import importlib
import os
import snscrape.base
import pkgutil
__all__ = []
def _import_modules():
files = os.listdir(__path__[0])
for fn in files:
if fn.endswith('.py') and fn != '__init__.py':
# Import module if not already imported
moduleName = f'snscrape.modules.{fn[:-3]}'
module = importlib.import_module(moduleName)
prefixLen = len(__name__) + 1
for importer, moduleName, isPkg in pkgutil.iter_modules(__path__, prefix = f'{__name__}.'):
assert not isPkg
moduleNameWithoutPrefix = moduleName[prefixLen:]
__all__.append(moduleNameWithoutPrefix)
module = importer.find_module(moduleName).load_module(moduleName)
globals()[moduleNameWithoutPrefix] = module
_import_modules()