Mercurial > repos > jpayne > bioproject_to_srr_2
comparison certifi/core.py @ 7:5eb2d5e3bf22
planemo upload for repository https://toolrepo.galaxytrakr.org/view/jpayne/bioproject_to_srr_2/556cac4fb538
author | jpayne |
---|---|
date | Sun, 05 May 2024 23:32:17 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
6:b2745907b1eb | 7:5eb2d5e3bf22 |
---|---|
1 """ | |
2 certifi.py | |
3 ~~~~~~~~~~ | |
4 | |
5 This module returns the installation location of cacert.pem or its contents. | |
6 """ | |
7 import sys | |
8 import atexit | |
9 | |
10 def exit_cacert_ctx() -> None: | |
11 _CACERT_CTX.__exit__(None, None, None) # type: ignore[union-attr] | |
12 | |
13 | |
14 if sys.version_info >= (3, 11): | |
15 | |
16 from importlib.resources import as_file, files | |
17 | |
18 _CACERT_CTX = None | |
19 _CACERT_PATH = None | |
20 | |
21 def where() -> str: | |
22 # This is slightly terrible, but we want to delay extracting the file | |
23 # in cases where we're inside of a zipimport situation until someone | |
24 # actually calls where(), but we don't want to re-extract the file | |
25 # on every call of where(), so we'll do it once then store it in a | |
26 # global variable. | |
27 global _CACERT_CTX | |
28 global _CACERT_PATH | |
29 if _CACERT_PATH is None: | |
30 # This is slightly janky, the importlib.resources API wants you to | |
31 # manage the cleanup of this file, so it doesn't actually return a | |
32 # path, it returns a context manager that will give you the path | |
33 # when you enter it and will do any cleanup when you leave it. In | |
34 # the common case of not needing a temporary file, it will just | |
35 # return the file system location and the __exit__() is a no-op. | |
36 # | |
37 # We also have to hold onto the actual context manager, because | |
38 # it will do the cleanup whenever it gets garbage collected, so | |
39 # 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_PATH | |
45 | |
46 def contents() -> str: | |
47 return files("certifi").joinpath("cacert.pem").read_text(encoding="ascii") | |
48 | |
49 elif sys.version_info >= (3, 7): | |
50 | |
51 from importlib.resources import path as get_path, read_text | |
52 | |
53 _CACERT_CTX = None | |
54 _CACERT_PATH = None | |
55 | |
56 def where() -> str: | |
57 # This is slightly terrible, but we want to delay extracting the | |
58 # file in cases where we're inside of a zipimport situation until | |
59 # someone actually calls where(), but we don't want to re-extract | |
60 # the file on every call of where(), so we'll do it once then store | |
61 # it in a global variable. | |
62 global _CACERT_CTX | |
63 global _CACERT_PATH | |
64 if _CACERT_PATH is None: | |
65 # This is slightly janky, the importlib.resources API wants you | |
66 # to manage the cleanup of this file, so it doesn't actually | |
67 # return a path, it returns a context manager that will give | |
68 # you the path when you enter it and will do any cleanup when | |
69 # you leave it. In the common case of not needing a temporary | |
70 # file, it will just return the file system location and the | |
71 # __exit__() is a no-op. | |
72 # | |
73 # We also have to hold onto the actual context manager, because | |
74 # it will do the cleanup whenever it gets garbage collected, so | |
75 # 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_PATH | |
81 | |
82 def contents() -> str: | |
83 return read_text("certifi", "cacert.pem", encoding="ascii") | |
84 | |
85 else: | |
86 import os | |
87 import types | |
88 from typing import Union | |
89 | |
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 the | |
94 # importlib.resources module but relies on the existing `where` function | |
95 # so won't address issues with environments like PyOxidizer that don't set | |
96 # __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 logic | |
107 # 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") |