From 21a77510b379724b1f8dfb429a66e995497e9776 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Fri, 25 Jun 2021 07:58:57 +0100 Subject: [PATCH] Rewrite unnecessary importlib dependency in DynamoDB (#4035) --- moto/dynamodb2/parsing/reserved_keywords.py | 13 ++++++------- moto/utilities/utils.py | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/moto/dynamodb2/parsing/reserved_keywords.py b/moto/dynamodb2/parsing/reserved_keywords.py index d82b16e98..6a14baacc 100644 --- a/moto/dynamodb2/parsing/reserved_keywords.py +++ b/moto/dynamodb2/parsing/reserved_keywords.py @@ -1,3 +1,7 @@ +from moto.utilities.utils import load_resource +from pkg_resources import resource_filename + + class ReservedKeywords(list): """ DynamoDB has an extensive list of keywords. Keywords are considered when validating the expression Tree. @@ -18,12 +22,7 @@ class ReservedKeywords(list): """ Get a list of reserved keywords of DynamoDB """ - try: - import importlib.resources as pkg_resources - except ImportError: - import importlib_resources as pkg_resources - - reserved_keywords = pkg_resources.read_text( - "moto.dynamodb2.parsing", "reserved_keywords.txt" + reserved_keywords = load_resource( + resource_filename(__name__, "reserved_keywords.txt"), as_json=False ) return reserved_keywords.split() diff --git a/moto/utilities/utils.py b/moto/utilities/utils.py index 255d29199..14d73e981 100644 --- a/moto/utilities/utils.py +++ b/moto/utilities/utils.py @@ -15,7 +15,7 @@ def random_string(length=None): return random_str -def load_resource(filename): +def load_resource(filename, as_json=True): """ Open a file, and return the contents as JSON. Usage: @@ -23,4 +23,4 @@ def load_resource(filename): load_resource(resource_filename(__name__, "resources/file.json")) """ with open(filename, "r", encoding="utf-8") as f: - return json.load(f) + return json.load(f) if as_json else f.read()