From 6affef51c7d2efce4489c164aaed8e57437117c2 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 8 Jan 2024 09:44:07 +0100 Subject: [PATCH] Migrate to importlib_resources I'm getting: pkg_resources.DistributionNotFound: The 'flufl.lock>=5.1' distribution was not found and is required by mailman The reason is that flufl.lock switched to PDM and it looks like pkg_resources can't deal with it. Migrating to importlib_resources resolves the problem. This is following: https://importlib-resources.readthedocs.io/en/latest/migration.html Signed-off-by: Andreas Schneider --- authheaders/__init__.py | 25 +++++++++++++------------ authheaders/dmarc_lookup.py | 36 ++++++++++++------------------------ 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/authheaders/__init__.py b/authheaders/__init__.py index d37eca7..0d7be3f 100644 --- a/authheaders/__init__.py +++ b/authheaders/__init__.py @@ -18,6 +18,7 @@ # Contact: Gene Shuman # +import importlib_resources import re import sys import copy @@ -28,7 +29,6 @@ from authres.dmarc import DMARCAuthenticationResult from dkim import ARC, DKIM, arc_verify, dkim_verify, DKIMException, rfc822_parse from dns.exception import DNSException -from pkg_resources import resource_filename # Part of setuptools # Please accept my appologies for doing this try: @@ -57,17 +57,18 @@ def check_psddmarc_list(psdname, dnsfunc=dns_query): """Check psddmarc.org list of PSD DMARC participants""" try: # If the PSD registry is locally available, use it. - psdfile_name = resource_filename('authheaders', 'psddmarc.csv') - psd_file = open(psdfile_name) - psds = [] - for line in psd_file.readlines(): - sp = line.split(',') - if sp[1] == 'current': - psds.append(sp[0][1:]) - if psdname in psds: - return True - else: - return False + ref = importlib_resources.files('authheaders') / 'psddmarc.csv' + with importlib_resources.as_file(ref) as psdfile_name: + with open(psdfile_name) as psd_file: + psds = [] + for line in psd_file.readlines(): + sp = line.split(',') + if sp[1] == 'current': + psds.append(sp[0][1:]) + if psdname in psds: + return True + else: + return False except: # If not, use the DNS query list. psd_list_host = '.psddmarc.org' diff --git a/authheaders/dmarc_lookup.py b/authheaders/dmarc_lookup.py index 6e1ffe3..7a00fb0 100644 --- a/authheaders/dmarc_lookup.py +++ b/authheaders/dmarc_lookup.py @@ -19,7 +19,7 @@ # ############################################################################ from __future__ import absolute_import, unicode_literals, print_function -from pkg_resources import resource_filename # Part of setuptools +import importlib_resources try: # typing is needed by mypy, but is unused otherwise from typing import Dict, Text # noqa: F401 @@ -166,30 +166,18 @@ def receiver_record_walk(host, dnsfunc=dns_query): result[newHost] = retval return result -def get_org_domain(domain): - fn = get_suffix_list_file_name() - with open(fn) as suffixList: + +def get_org_domain_from_suffix_list(location, domain): + with open(location) as suffixList: psl = PublicSuffixList(suffixList) return psl.get_public_suffix(domain) -def get_suffix_list_file_name(): - # type: () -> Text - '''Get the file name for the public-suffix list data file - - :returns: The filename for the datafile in this module. - :rtype: ``str``''' - # TODO: automatically update the suffix list data file - # - - if sys.version_info < (3, 0): - try: - from authheaders.findpsl import location - except ImportError: - location = resource_filename('authheaders', 'public_suffix_list.txt') - else: - try: - from authheaders.findpsl import location - except ModuleNotFoundError: - location = resource_filename('authheaders', 'public_suffix_list.txt') - return location +def get_org_domain(domain): + try: + from authheaders.findpsl import location + return get_org_domain_from_suffix_list(location, domain) + except ModuleNotFoundError: + ref = importlib_resources.files('authheaders') / 'public_suffix_list.txt' + with importlib_resources.as_file(ref) as location: + return get_org_domain_from_suffix_list(location, domain)