Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
core.py115 linesDownload Raw Back to certifi
1"""2certifi.py3~~~~~~~~~~4 5This module returns the installation location of cacert.pem or its contents.6"""7import sys8import atexit9 10def exit_cacert_ctx() -> None:11    _CACERT_CTX.__exit__(None, None, None)  # type: ignore[union-attr]12 13 14if sys.version_info >= (3, 11):15 16    from importlib.resources import as_file, files17 18    _CACERT_CTX = None19    _CACERT_PATH = None20 21    def where() -> str:22        # This is slightly terrible, but we want to delay extracting the file23        # in cases where we're inside of a zipimport situation until someone24        # actually calls where(), but we don't want to re-extract the file25        # on every call of where(), so we'll do it once then store it in a26        # global variable.27        global _CACERT_CTX28        global _CACERT_PATH29        if _CACERT_PATH is None:30            # This is slightly janky, the importlib.resources API wants you to31            # manage the cleanup of this file, so it doesn't actually return a32            # path, it returns a context manager that will give you the path33            # when you enter it and will do any cleanup when you leave it. In34            # the common case of not needing a temporary file, it will just35            # return the file system location and the __exit__() is a no-op.36            #37            # We also have to hold onto the actual context manager, because38            # it will do the cleanup whenever it gets garbage collected, so39            # we will also store that at the global level as well.40            _CACERT_CTX = as_file(files("certifi").joinpath("cacert.pem"))41            _CACERT_PATH = str(_CACERT_CTX.__enter__())42            atexit.register(exit_cacert_ctx)43 44        return _CACERT_PATH45 46    def contents() -> str:47        return files("certifi").joinpath("cacert.pem").read_text(encoding="ascii")48 49elif sys.version_info >= (3, 7):50 51    from importlib.resources import path as get_path, read_text52 53    _CACERT_CTX = None54    _CACERT_PATH = None55 56    def where() -> str:57        # This is slightly terrible, but we want to delay extracting the58        # file in cases where we're inside of a zipimport situation until59        # someone actually calls where(), but we don't want to re-extract60        # the file on every call of where(), so we'll do it once then store61        # it in a global variable.62        global _CACERT_CTX63        global _CACERT_PATH64        if _CACERT_PATH is None:65            # This is slightly janky, the importlib.resources API wants you66            # to manage the cleanup of this file, so it doesn't actually67            # return a path, it returns a context manager that will give68            # you the path when you enter it and will do any cleanup when69            # you leave it. In the common case of not needing a temporary70            # file, it will just return the file system location and the71            # __exit__() is a no-op.72            #73            # We also have to hold onto the actual context manager, because74            # it will do the cleanup whenever it gets garbage collected, so75            # we will also store that at the global level as well.76            _CACERT_CTX = get_path("certifi", "cacert.pem")77            _CACERT_PATH = str(_CACERT_CTX.__enter__())78            atexit.register(exit_cacert_ctx)79 80        return _CACERT_PATH81 82    def contents() -> str:83        return read_text("certifi", "cacert.pem", encoding="ascii")84 85else:86    import os87    import types88    from typing import Union89 90    Package = Union[types.ModuleType, str]91    Resource = Union[str, "os.PathLike"]92 93    # This fallback will work for Python versions prior to 3.7 that lack the94    # importlib.resources module but relies on the existing `where` function95    # so won't address issues with environments like PyOxidizer that don't set96    # __file__ on modules.97    def read_text(98        package: Package,99        resource: Resource,100        encoding: str = 'utf-8',101        errors: str = 'strict'102    ) -> str:103        with open(where(), encoding=encoding) as data:104            return data.read()105 106    # If we don't have importlib.resources, then we will just do the old logic107    # of assuming we're on the filesystem and munge the path directly.108    def where() -> str:109        f = os.path.dirname(__file__)110 111        return os.path.join(f, "cacert.pem")112 113    def contents() -> str:114        return read_text("certifi", "cacert.pem", encoding="ascii")115 
codekingpro/portable-devtools · Team Ai