mirror of
https://github.com/bellingcat/snscrape.git
synced 2026-06-12 12:28:28 +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 pkgutil
|
||||||
import os
|
|
||||||
import snscrape.base
|
|
||||||
|
__all__ = []
|
||||||
|
|
||||||
|
|
||||||
def _import_modules():
|
def _import_modules():
|
||||||
files = os.listdir(__path__[0])
|
prefixLen = len(__name__) + 1
|
||||||
for fn in files:
|
for importer, moduleName, isPkg in pkgutil.iter_modules(__path__, prefix = f'{__name__}.'):
|
||||||
if fn.endswith('.py') and fn != '__init__.py':
|
assert not isPkg
|
||||||
# Import module if not already imported
|
moduleNameWithoutPrefix = moduleName[prefixLen:]
|
||||||
moduleName = f'snscrape.modules.{fn[:-3]}'
|
__all__.append(moduleNameWithoutPrefix)
|
||||||
module = importlib.import_module(moduleName)
|
module = importer.find_module(moduleName).load_module(moduleName)
|
||||||
|
globals()[moduleNameWithoutPrefix] = module
|
||||||
|
|
||||||
|
|
||||||
_import_modules()
|
_import_modules()
|
||||||
|
|||||||
Reference in New Issue
Block a user