Skip to content

Commit

Permalink
Fix plugin loading mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
akariv committed Mar 12, 2024
1 parent c97f665 commit 58fb66b
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions tableschema/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import json
import requests
from copy import deepcopy
from importlib import import_module
from importlib.util import find_spec

This comment has been minimized.

Copy link
@ThrawnCA

ThrawnCA Mar 20, 2024

This breaks Python 2 compatibility.

from . import exceptions
from . import config

Expand Down Expand Up @@ -128,27 +128,10 @@ def register(self):
if self not in sys.meta_path:
sys.meta_path.append(self)

def find_module(self, fullname, path=None):
def find_spec(self, fullname, path=None, target=None):
if fullname.startswith(self.virtual):
return self
# Transform the module name
transformed_name = fullname.replace(self.virtual, self.actual)
return find_spec(transformed_name)
return None

def find_spec(self, fullname, path=None, target=None):
return self.find_module(fullname, path)

def load_module(self, fullname):
if fullname in sys.modules:
return sys.modules[fullname]
if not fullname.startswith(self.virtual):
raise ImportError(fullname)
realname = fullname.replace(self.virtual, self.actual)
try:
module = import_module(realname)
except ImportError:
message = 'Plugin "%s" is not installed. '
message += 'Run "pip install %s" to install.'
message = message % (fullname, realname.replace('_', '-'))
raise ImportError(message)
sys.modules[realname] = module
sys.modules[fullname] = module
return module

0 comments on commit 58fb66b

Please sign in to comment.