File size: 3,071 Bytes
71687cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | # Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""Common compatibility code."""
# Try to keep compat small because it's imported by everything
# What is compat, and what isn't?
# If a piece of code is "general" and used in multiple modules, it goes here.
# If it's only used in one module, keep it in that module, preferably near the top.
# This module should contain ONLY stdlib imports.
import builtins
import sys
on_win = bool(sys.platform == "win32")
on_mac = bool(sys.platform == "darwin")
on_linux = bool(sys.platform == "linux")
# Control some tweakables that will be removed finally.
ENCODE_ENVIRONMENT = True
def encode_for_env_var(value) -> str:
"""Environment names and values need to be string."""
if isinstance(value, str):
return value
elif isinstance(value, bytes):
return value.decode()
return str(value)
def encode_environment(env):
if ENCODE_ENVIRONMENT:
env = {encode_for_env_var(k): encode_for_env_var(v) for k, v in env.items()}
return env
from collections.abc import Iterable
def isiterable(obj):
return not isinstance(obj, str) and isinstance(obj, Iterable)
# #############################
# other
# #############################
from collections import OrderedDict as odict # noqa: F401
def open_utf8(
file, mode="r", buffering=-1, encoding=None, errors=None, newline=None, closefd=True
):
if "b" in mode:
return builtins.open(
file,
str(mode),
buffering=buffering,
errors=errors,
newline=newline,
closefd=closefd,
)
else:
return builtins.open(
file,
str(mode),
buffering=buffering,
encoding=encoding or "utf-8",
errors=errors,
newline=newline,
closefd=closefd,
)
NoneType = type(None)
primitive_types = (str, int, float, complex, bool, NoneType)
def ensure_binary(value):
try:
return value.encode("utf-8")
except AttributeError: # pragma: no cover
# AttributeError: '<>' object has no attribute 'encode'
# In this case assume already binary type and do nothing
return value
def ensure_text_type(value) -> str:
try:
return value.decode("utf-8")
except AttributeError: # pragma: no cover
# AttributeError: '<>' object has no attribute 'decode'
# In this case assume already text_type and do nothing
return value
except UnicodeDecodeError: # pragma: no cover
from charset_normalizer import from_bytes
return str(from_bytes(value).best())
except UnicodeEncodeError: # pragma: no cover
# it's already str, so ignore?
# not sure, surfaced with tests/models/test_match_spec.py test_tarball_match_specs
# using py27
return value
def ensure_utf8_encoding(value):
try:
return value.encode("utf-8")
except AttributeError:
return value
except UnicodeEncodeError:
return value
|