mirror of
https://github.com/bellingcat/snscrape.git
synced 2026-06-08 02:28:29 +03:00
Refactor automatic importing in snscrape.modules to something less hacky
Cf. #357
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user