annotate certifi/core.py @ 16:dc2c003078e9 tip

planemo upload for repository https://toolrepo.galaxytrakr.org/view/jpayne/bioproject_to_srr_2/556cac4fb538
author jpayne
date Tue, 21 May 2024 01:09:25 -0400
parents 5eb2d5e3bf22
children
rev   line source
jpayne@7 1 """
jpayne@7 2 certifi.py
jpayne@7 3 ~~~~~~~~~~
jpayne@7 4
jpayne@7 5 This module returns the installation location of cacert.pem or its contents.
jpayne@7 6 """
jpayne@7 7 import sys
jpayne@7 8 import atexit
jpayne@7 9
jpayne@7 10 def exit_cacert_ctx() -> None:
jpayne@7 11 _CACERT_CTX.__exit__(None, None, None) # type: ignore[union-attr]
jpayne@7 12
jpayne@7 13
jpayne@7 14 if sys.version_info >= (3, 11):
jpayne@7 15
jpayne@7 16 from importlib.resources import as_file, files
jpayne@7 17
jpayne@7 18 _CACERT_CTX = None
jpayne@7 19 _CACERT_PATH = None
jpayne@7 20
jpayne@7 21 def where() -> str:
jpayne@7 22 # This is slightly terrible, but we want to delay extracting the file
jpayne@7 23 # in cases where we're inside of a zipimport situation until someone
jpayne@7 24 # actually calls where(), but we don't want to re-extract the file
jpayne@7 25 # on every call of where(), so we'll do it once then store it in a
jpayne@7 26 # global variable.
jpayne@7 27 global _CACERT_CTX
jpayne@7 28 global _CACERT_PATH
jpayne@7 29 if _CACERT_PATH is None:
jpayne@7 30 # This is slightly janky, the importlib.resources API wants you to
jpayne@7 31 # manage the cleanup of this file, so it doesn't actually return a
jpayne@7 32 # path, it returns a context manager that will give you the path
jpayne@7 33 # when you enter it and will do any cleanup when you leave it. In
jpayne@7 34 # the common case of not needing a temporary file, it will just
jpayne@7 35 # return the file system location and the __exit__() is a no-op.
jpayne@7 36 #
jpayne@7 37 # We also have to hold onto the actual context manager, because
jpayne@7 38 # it will do the cleanup whenever it gets garbage collected, so
jpayne@7 39 # we will also store that at the global level as well.
jpayne@7 40 _CACERT_CTX = as_file(files("certifi").joinpath("cacert.pem"))
jpayne@7 41 _CACERT_PATH = str(_CACERT_CTX.__enter__())
jpayne@7 42 atexit.register(exit_cacert_ctx)
jpayne@7 43
jpayne@7 44 return _CACERT_PATH
jpayne@7 45
jpayne@7 46 def contents() -> str:
jpayne@7 47 return files("certifi").joinpath("cacert.pem").read_text(encoding="ascii")
jpayne@7 48
jpayne@7 49 elif sys.version_info >= (3, 7):
jpayne@7 50
jpayne@7 51 from importlib.resources import path as get_path, read_text
jpayne@7 52
jpayne@7 53 _CACERT_CTX = None
jpayne@7 54 _CACERT_PATH = None
jpayne@7 55
jpayne@7 56 def where() -> str:
jpayne@7 57 # This is slightly terrible, but we want to delay extracting the
jpayne@7 58 # file in cases where we're inside of a zipimport situation until
jpayne@7 59 # someone actually calls where(), but we don't want to re-extract
jpayne@7 60 # the file on every call of where(), so we'll do it once then store
jpayne@7 61 # it in a global variable.
jpayne@7 62 global _CACERT_CTX
jpayne@7 63 global _CACERT_PATH
jpayne@7 64 if _CACERT_PATH is None:
jpayne@7 65 # This is slightly janky, the importlib.resources API wants you
jpayne@7 66 # to manage the cleanup of this file, so it doesn't actually
jpayne@7 67 # return a path, it returns a context manager that will give
jpayne@7 68 # you the path when you enter it and will do any cleanup when
jpayne@7 69 # you leave it. In the common case of not needing a temporary
jpayne@7 70 # file, it will just return the file system location and the
jpayne@7 71 # __exit__() is a no-op.
jpayne@7 72 #
jpayne@7 73 # We also have to hold onto the actual context manager, because
jpayne@7 74 # it will do the cleanup whenever it gets garbage collected, so
jpayne@7 75 # we will also store that at the global level as well.
jpayne@7 76 _CACERT_CTX = get_path("certifi", "cacert.pem")
jpayne@7 77 _CACERT_PATH = str(_CACERT_CTX.__enter__())
jpayne@7 78 atexit.register(exit_cacert_ctx)
jpayne@7 79
jpayne@7 80 return _CACERT_PATH
jpayne@7 81
jpayne@7 82 def contents() -> str:
jpayne@7 83 return read_text("certifi", "cacert.pem", encoding="ascii")
jpayne@7 84
jpayne@7 85 else:
jpayne@7 86 import os
jpayne@7 87 import types
jpayne@7 88 from typing import Union
jpayne@7 89
jpayne@7 90 Package = Union[types.ModuleType, str]
jpayne@7 91 Resource = Union[str, "os.PathLike"]
jpayne@7 92
jpayne@7 93 # This fallback will work for Python versions prior to 3.7 that lack the
jpayne@7 94 # importlib.resources module but relies on the existing `where` function
jpayne@7 95 # so won't address issues with environments like PyOxidizer that don't set
jpayne@7 96 # __file__ on modules.
jpayne@7 97 def read_text(
jpayne@7 98 package: Package,
jpayne@7 99 resource: Resource,
jpayne@7 100 encoding: str = 'utf-8',
jpayne@7 101 errors: str = 'strict'
jpayne@7 102 ) -> str:
jpayne@7 103 with open(where(), encoding=encoding) as data:
jpayne@7 104 return data.read()
jpayne@7 105
jpayne@7 106 # If we don't have importlib.resources, then we will just do the old logic
jpayne@7 107 # of assuming we're on the filesystem and munge the path directly.
jpayne@7 108 def where() -> str:
jpayne@7 109 f = os.path.dirname(__file__)
jpayne@7 110
jpayne@7 111 return os.path.join(f, "cacert.pem")
jpayne@7 112
jpayne@7 113 def contents() -> str:
jpayne@7 114 return read_text("certifi", "cacert.pem", encoding="ascii")