Skip to content

Commit

Permalink
Revert "Use resources loader to handle non-filesystem situations (#120)"
Browse files Browse the repository at this point in the history
This reverts commit a9ab4b3.
  • Loading branch information
Lukasa committed Apr 5, 2020
1 parent 341e59d commit d4e52bd
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions certifi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,25 @@
This module returns the installation location of cacert.pem or its contents.
"""
import importlib
import os

try:
import importlib.resources
# Defeat lazy module importers.
importlib.resources.open_binary
_HAVE_RESOURCE_READER = True
from importlib.resources import read_text
except ImportError:
_HAVE_RESOURCE_READER = False

try:
import pkg_resources
# Defeat lazy module importers.
_HAVE_PKG_RESOURCES = True
except ImportError:
_HAVE_PKG_RESOURCES = False

_PACKAGE_NAME = "certifi"
_CACERT_NAME = "cacert.pem"
# This fallback will work for Python versions prior to 3.7 that lack the
# importlib.resources module but relies on the existing `where` function
# so won't address issues with environments like PyOxidizer that don't set
# __file__ on modules.
def read_text(_module, _path, encoding="ascii"):
with open(where(), "r", encoding=encoding) as data:
return data.read()


def where():
if _HAVE_PKG_RESOURCES:
return pkg_resources.resource_filename(_PACKAGE_NAME, _CACERT_NAME)
else:
mod = importlib.import_module(_PACKAGE_NAME)
path = os.path.dirname(mod.__file__)
return os.path.join(path, _CACERT_NAME)
f = os.path.dirname(__file__)

return os.path.join(f, "cacert.pem")


def contents():
if _HAVE_RESOURCE_READER:
return importlib.resources.read_text(_PACKAGE_NAME, _CACERT_NAME, encoding="ascii")
else:
with open(where(), "r", encoding="ascii") as data:
return data.read()
return read_text("certifi", "cacert.pem", encoding="ascii")

0 comments on commit d4e52bd

Please sign in to comment.