Skip to content

Commit 3e58560

Browse files
authored
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.
1 parent dc4007f commit 3e58560

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

certifi/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .core import where
1+
from .core import contents, where
22

33
__version__ = "2019.11.28"

certifi/__main__.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
from certifi import where
2-
print(where())
1+
import argparse
2+
3+
from certifi import contents, where
4+
5+
parser = argparse.ArgumentParser()
6+
parser.add_argument("-c", "--contents", action="store_true")
7+
args = parser.parse_args()
8+
9+
if args.contents:
10+
print(contents())
11+
else:
12+
print(where())

certifi/core.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,27 @@
44
certifi.py
55
~~~~~~~~~~
66
7-
This module returns the installation location of cacert.pem.
7+
This module returns the installation location of cacert.pem or its contents.
88
"""
99
import os
1010

11+
try:
12+
from importlib.resources import read_text
13+
except ImportError:
14+
# This fallback will work for Python versions prior to 3.7 that lack the
15+
# importlib.resources module but relies on the existing `where` function
16+
# so won't address issues with environments like PyOxidizer that don't set
17+
# __file__ on modules.
18+
def read_text(_module, _path, encoding="ascii"):
19+
with open(where(), "r", encoding=encoding) as data:
20+
return data.read()
21+
1122

1223
def where():
1324
f = os.path.dirname(__file__)
1425

15-
return os.path.join(f, 'cacert.pem')
26+
return os.path.join(f, "cacert.pem")
27+
28+
29+
def contents():
30+
return read_text("certifi", "cacert.pem", encoding="ascii")

0 commit comments

Comments
 (0)