codekingpro/portable-devtools
114k
1import sys2from ssl import SSLContext3from typing import Any, Optional, Iterable4 5from httpie.cookies import HTTPieCookiePolicy6from http import cookiejar # noqa7 8 9# Request does not carry the original policy attached to the10# cookie jar, so until it is resolved we change the global cookie11# policy. <https://github.com/psf/requests/issues/5449>12cookiejar.DefaultCookiePolicy = HTTPieCookiePolicy13 14is_windows = 'win32' in str(sys.platform).lower()15is_frozen = getattr(sys, 'frozen', False)16 17MIN_SUPPORTED_PY_VERSION = (3, 7)18MAX_SUPPORTED_PY_VERSION = (3, 11)19 20try:21 from functools import cached_property22except ImportError:23 # Can be removed once we drop Python <3.8 support.24 # Taken from `django.utils.functional.cached_property`.25 class cached_property:26 """27 Decorator that converts a method with a single self argument into a28 property cached on the instance.29 30 A cached property can be made out of an existing method:31 (e.g. ``url = cached_property(get_absolute_url)``).32 The optional ``name`` argument is obsolete as of Python 3.6 and will be33 deprecated in Django 4.0 (#30127).34 """35 name = None36 37 @staticmethod38 def func(instance):39 raise TypeError(40 'Cannot use cached_property instance without calling '41 '__set_name__() on it.'42 )43 44 def __init__(self, func, name=None):45 self.real_func = func46 self.__doc__ = getattr(func, '__doc__')47 48 def __set_name__(self, owner, name):49 if self.name is None:50 self.name = name51 self.func = self.real_func52 elif name != self.name:53 raise TypeError(54 "Cannot assign the same cached_property to two different names "55 "(%r and %r)." % (self.name, name)56 )57 58 def __get__(self, instance, cls=None):59 """60 Call the function and put the return value in instance.__dict__ so that61 subsequent attribute access on the instance returns the cached value62 instead of calling cached_property.__get__().63 """64 if instance is None:65 return self66 res = instance.__dict__[self.name] = self.func(instance)67 return res68 69# importlib_metadata was a provisional module, so the APIs changed quite a few times70# between 3.8-3.10. It was also not included in the standard library until 3.8, so71# we install the backport for <3.8.72 73if sys.version_info >= (3, 8):74 import importlib.metadata as importlib_metadata75else:76 import importlib_metadata77 78 79def find_entry_points(entry_points: Any, group: str) -> Iterable[importlib_metadata.EntryPoint]:80 if hasattr(entry_points, "select"): # Python 3.10+ / importlib_metadata >= 3.9.081 return entry_points.select(group=group)82 else:83 return set(entry_points.get(group, ()))84 85 86def get_dist_name(entry_point: importlib_metadata.EntryPoint) -> Optional[str]:87 dist = getattr(entry_point, "dist", None)88 if dist is not None: # Python 3.10+89 return dist.name90 91 match = entry_point.pattern.match(entry_point.value)92 if not (match and match.group('module')):93 return None94 95 package = match.group('module').split('.')[0]96 try:97 metadata = importlib_metadata.metadata(package)98 except importlib_metadata.PackageNotFoundError:99 return None100 else:101 return metadata.get('name')102 103 104def ensure_default_certs_loaded(ssl_context: SSLContext) -> None:105 """106 Workaround for a bug in Requests 2.32.3107 108 See <https://github.com/httpie/cli/issues/1583>109 110 """111 if hasattr(ssl_context, 'load_default_certs'):112 if not ssl_context.get_ca_certs():113 ssl_context.load_default_certs()114 