Skip to content

Commit

Permalink
Update sceptre to use importlib (#1240)
Browse files Browse the repository at this point in the history
importlib was introducted in python 3.8 but we want to continue
support for python 3.7 therefore we need to use pkg_resources for
python 3.7 while using importlib for python >= 3.8
  • Loading branch information
zaro0508 committed Jun 16, 2022
1 parent 78f8d7c commit a0fc6ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
23 changes: 18 additions & 5 deletions sceptre/config/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import datetime
import fnmatch
import logging
import sys
import yaml

from os import environ, path, walk
from typing import Set

from pkg_resources import iter_entry_points
from pathlib import Path
import yaml

from jinja2 import Environment
from jinja2 import StrictUndefined
from jinja2 import FileSystemLoader
Expand Down Expand Up @@ -137,6 +136,19 @@ def __init__(self, context):

self.templating_vars = {"var": self.context.user_variables}

@staticmethod
def _iterate_entry_points(group):
"""
Helper to determine whether to use pkg_resources or importlib.metadata.
https://docs.python.org/3/library/importlib.metadata.html
"""
if sys.version_info < (3, 10):
from pkg_resources import iter_entry_points
return iter_entry_points(group)
else:
from importlib.metadata import entry_points
return entry_points(group=group)

def _add_yaml_constructors(self, entry_point_groups):
"""
Adds PyYAML constructor functions for all classes found registered at
Expand Down Expand Up @@ -171,7 +183,8 @@ def class_constructor(loader, node):
return class_constructor

for group in entry_point_groups:
for entry_point in iter_entry_points(group):

for entry_point in self._iterate_entry_points(group):
# Retrieve name and class from entry point
node_tag = u'!' + entry_point.name
node_class = entry_point.load()
Expand Down
17 changes: 15 additions & 2 deletions sceptre/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import logging
import threading
import botocore
import sys

from sceptre.exceptions import TemplateHandlerNotFoundError
from pkg_resources import iter_entry_points


class Template(object):
Expand Down Expand Up @@ -234,6 +234,19 @@ def _bucket_region(self, bucket_name):
def _domain_from_region(region):
return "com.cn" if region.startswith("cn-") else "com"

@staticmethod
def _iterate_entry_points(group, name):
"""
Helper to determine whether to use pkg_resources or importlib.metadata.
https://docs.python.org/3/library/importlib.metadata.html
"""
if sys.version_info < (3, 10):
from pkg_resources import iter_entry_points
return iter_entry_points(group, name)
else:
from importlib.metadata import entry_points
return entry_points(group=group, name=name)

def _get_handler_of_type(self, type):
"""
Gets a TemplateHandler type from the registry that can be used to get a string
Expand All @@ -246,7 +259,7 @@ def _get_handler_of_type(self, type):
if not self._registry:
self._registry = {}

for entry_point in iter_entry_points("sceptre.template_handlers", type):
for entry_point in self._iterate_entry_points("sceptre.template_handlers", type):
self._registry[entry_point.name] = entry_point.load()

if type not in self._registry:
Expand Down

0 comments on commit a0fc6ff

Please sign in to comment.