Add a "contents" function that returns the content of the cert chain. (#116) * Add a "what" function that returns the content of the cert chain. * Use read_text because it makes more sense. * Oh, and specify encoding. * Add a workaround for pre-3.7 version of Python. This workaround won't work in PyOxidizer but it allows those to keep working while allowing 3.7+ usage in PyOxidizer. * Rename "what()" to "contents()". * Fix __main__ after the change from what() to contents(). * Update docstring. * Add a comment describing when the fallback is used. * Black keeps nagging me. * Missed this after the what->contents change. * Use a with statement to make sure things get dealt with properly.
diff --git a/certifi/__init__.py b/certifi/__init__.py index 0d59a05..644365f 100644 --- a/certifi/__init__.py +++ b/certifi/__init__.py
@@ -1,3 +1,3 @@ -from .core import where +from .core import contents, where __version__ = "2019.11.28"
diff --git a/certifi/__main__.py b/certifi/__main__.py index 5f1da0d..8945b5d 100644 --- a/certifi/__main__.py +++ b/certifi/__main__.py
@@ -1,2 +1,12 @@ -from certifi import where -print(where()) +import argparse + +from certifi import contents, where + +parser = argparse.ArgumentParser() +parser.add_argument("-c", "--contents", action="store_true") +args = parser.parse_args() + +if args.contents: + print(contents()) +else: + print(where())
diff --git a/certifi/core.py b/certifi/core.py index 7271acf..56b52a3 100644 --- a/certifi/core.py +++ b/certifi/core.py
@@ -4,12 +4,27 @@ certifi.py ~~~~~~~~~~ -This module returns the installation location of cacert.pem. +This module returns the installation location of cacert.pem or its contents. """ import os +try: + from importlib.resources import read_text +except ImportError: + # 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(): f = os.path.dirname(__file__) - return os.path.join(f, 'cacert.pem') + return os.path.join(f, "cacert.pem") + + +def contents(): + return read_text("certifi", "cacert.pem", encoding="ascii")