[utils] Use `importlib` to load plugins (#1277)

Authored by: sulyi
pull/1332/head
Ákos Sülyi 3 years ago committed by GitHub
parent e69585f8c6
commit 019a94f7d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,7 +18,7 @@ import functools
import gzip import gzip
import hashlib import hashlib
import hmac import hmac
import imp import importlib.util
import io import io
import itertools import itertools
import json import json
@ -6302,12 +6302,13 @@ def get_executable_path():
def load_plugins(name, suffix, namespace): def load_plugins(name, suffix, namespace):
plugin_info = [None]
classes = {} classes = {}
try: try:
plugin_info = imp.find_module( plugins_spec = importlib.util.spec_from_file_location(
name, [os.path.join(get_executable_path(), 'ytdlp_plugins')]) name, os.path.join(get_executable_path(), 'ytdlp_plugins', name, '__init__.py'))
plugins = imp.load_module(name, *plugin_info) plugins = importlib.util.module_from_spec(plugins_spec)
sys.modules[plugins_spec.name] = plugins
plugins_spec.loader.exec_module(plugins)
for name in dir(plugins): for name in dir(plugins):
if name in namespace: if name in namespace:
continue continue
@ -6315,11 +6316,8 @@ def load_plugins(name, suffix, namespace):
continue continue
klass = getattr(plugins, name) klass = getattr(plugins, name)
classes[name] = namespace[name] = klass classes[name] = namespace[name] = klass
except ImportError: except FileNotFoundError:
pass pass
finally:
if plugin_info[0] is not None:
plugin_info[0].close()
return classes return classes

Loading…
Cancel
Save