Python hook

Friday, Jul 5, 2024 | 1 minute read | Updated at Friday, Jul 5, 2024

@
Python hook

python hook

# -*- coding: utf-8 -*-

import sys
import types
from pathlib import Path
from importlib.machinery import ModuleSpec
from importlib.abc import MetaPathFinder, Loader
# sys.path.insert(0, 'module')


class DynamicLoader(Loader):
    def __init__(self, path):
        self.__path = path

    def create_module(self, spec):
        module = types.ModuleType(spec.name)
        return module

    def exec_module(self, module):
        print(self.__path)
        source = self.__path.read_bytes()
        exec(source, module.__dict__)


class DynamicFinder(MetaPathFinder):
    base_path = Path('module')

    def find_spec(self, fullname, path, target=None):
        if fullname in sys.modules: 
            return sys.modules[fullname]
        if self.base_path.joinpath(f'{fullname}.py').exists():
            abs_path = self.base_path / f'{fullname}.py'
        elif self.base_path.joinpath(fullname, '__init__.py').exists():
            abs_path = self.base_path / fullname / '__init__.py'
        else:
            return None
        return ModuleSpec('msg', DynamicLoader(abs_path))


def dynamic_hook(path):
    return DynamicFinder()


sys.meta_path.append(DynamicFinder())

#sys.path_hooks = [dynamic_hook]
sys.path_importer_cache.clear()


if __name__ == '__main__':
    import msg
    print(msg.message(sys.argv[1]))

© 2016 - 2025 Caisong's Blog

🌱 Powered by Hugo with theme Dream.

About Me

大龄程序员,喜欢折腾各种环境部署、软件应用。

博客记录日常。