Skip to content

Commit

Permalink
glad: improves importlib.metadata fallbacks
Browse files Browse the repository at this point in the history
No more checks for importlib_metadata backports,
if you have to install a backport you can also install setuptools.
  • Loading branch information
Dav1dde committed Oct 28, 2022
1 parent 0106fe7 commit 74e6688
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions glad/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,22 @@
import logging
import inspect
try:
from importlib.metadata import entry_points as imp_entry_points
ENTRY_POINTS = 'importlib'
from importlib.metadata import entry_points

if sys.version_info < (3, 10):
_entry_points = entry_points

def entry_points(group=None):
return _entry_points()[group]
except ImportError:
try:
from importlib_metadata import entry_points as imp_entry_points
ENTRY_POINTS = 'importlib'
except ImportError:
from pkg_resources import iter_entry_points
ENTRY_POINTS = 'pkg_resources'
from pkg_resources import iter_entry_points as entry_points

import glad.specification
from glad.generator.c import CGenerator
from glad.generator.rust import RustGenerator
from glad.parse import Specification


def _entry_points(group=None):
if ENTRY_POINTS == 'importlib':
if sys.version_info < (3, 10):
return imp_entry_points()[group]
else:
return imp_entry_points(group=group)
else:
return iter_entry_points(group=group, name=None)


logger = logging.getLogger(__name__)


Expand All @@ -49,7 +39,7 @@ def _entry_points(group=None):
def find_generators(default=None, entry_point=GENERATOR_ENTRY_POINT):
generators = dict(DEFAULT_GENERATORS if default is None else default)

for entry_point in _entry_points(group=entry_point):
for entry_point in entry_points(group=entry_point):
generators[entry_point.name] = entry_point.load()
logger.debug('loaded language %s: %s', entry_point.name, generators[entry_point.name])

Expand All @@ -59,7 +49,7 @@ def find_generators(default=None, entry_point=GENERATOR_ENTRY_POINT):
def find_specifications(default=None, entry_point=SPECIFICATION_ENTRY_POINT):
specifications = dict(DEFAULT_SPECIFICATIONS if default is None else default)

for entry_point in _entry_points(group=entry_point):
for entry_point in entry_points(group=entry_point):
specifications[entry_point.name] = entry_point.load()
logger.debug('loaded specification %s: %s', entry_point.name, specifications[entry_point.name])

Expand Down

0 comments on commit 74e6688

Please sign in to comment.