Revert "Bump cherrypy from 18.8.0 to 18.9.0 (#2266)"

This reverts commit faef9a94c4.
This commit is contained in:
JonnyWong16 2024-03-24 17:40:56 -07:00
parent fcd8ef11f4
commit 2fc618c01f
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
673 changed files with 11579 additions and 159846 deletions

View file

@ -1,28 +1,22 @@
"""Color definitions are used as per the CSS3
[CSS Color Module Level 3](http://www.w3.org/TR/css3-color/#svg-color) specification.
"""
Color definitions are used as per CSS3 specification:
http://www.w3.org/TR/css3-color/#svg-color
A few colors have multiple names referring to the sames colors, eg. `grey` and `gray` or `aqua` and `cyan`.
In these cases the _last_ color when sorted alphabetically takes preferences,
eg. `Color((0, 255, 255)).as_named() == 'cyan'` because "cyan" comes after "aqua".
Warning: Deprecated
The `Color` class is deprecated, use `pydantic_extra_types` instead.
See [`pydantic-extra-types.Color`](../usage/types/extra_types/color_types.md)
for more information.
In these cases the LAST color when sorted alphabetically takes preferences,
eg. Color((0, 255, 255)).as_named() == 'cyan' because "cyan" comes after "aqua".
"""
import math
import re
from colorsys import hls_to_rgb, rgb_to_hls
from typing import Any, Callable, Optional, Tuple, Type, Union, cast
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union, cast
from pydantic_core import CoreSchema, PydanticCustomError, core_schema
from typing_extensions import deprecated
from .errors import ColorError
from .utils import Representation, almost_equal_floats
from ._internal import _repr
from ._internal._schema_generation_shared import GetJsonSchemaHandler as _GetJsonSchemaHandler
from .json_schema import JsonSchemaValue
from .warnings import PydanticDeprecatedSince20
if TYPE_CHECKING:
from .typing import CallableGenerator, ReprArgs
ColorTuple = Union[Tuple[int, int, int], Tuple[int, int, int, float]]
ColorType = Union[ColorTuple, str]
@ -30,7 +24,9 @@ HslColorTuple = Union[Tuple[float, float, float], Tuple[float, float, float, flo
class RGBA:
"""Internal use only as a representation of a color."""
"""
Internal use only as a representation of a color.
"""
__slots__ = 'r', 'g', 'b', 'alpha', '_tuple'
@ -47,35 +43,24 @@ class RGBA:
# these are not compiled here to avoid import slowdown, they'll be compiled the first time they're used, then cached
_r_255 = r'(\d{1,3}(?:\.\d+)?)'
_r_comma = r'\s*,\s*'
_r_alpha = r'(\d(?:\.\d+)?|\.\d+|\d{1,2}%)'
_r_h = r'(-?\d+(?:\.\d+)?|-?\.\d+)(deg|rad|turn)?'
_r_sl = r'(\d{1,3}(?:\.\d+)?)%'
r_hex_short = r'\s*(?:#|0x)?([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?\s*'
r_hex_long = r'\s*(?:#|0x)?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?\s*'
# CSS3 RGB examples: rgb(0, 0, 0), rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 50%)
r_rgb = rf'\s*rgba?\(\s*{_r_255}{_r_comma}{_r_255}{_r_comma}{_r_255}(?:{_r_comma}{_r_alpha})?\s*\)\s*'
# CSS3 HSL examples: hsl(270, 60%, 50%), hsla(270, 60%, 50%, 0.5), hsla(270, 60%, 50%, 50%)
r_hsl = rf'\s*hsla?\(\s*{_r_h}{_r_comma}{_r_sl}{_r_comma}{_r_sl}(?:{_r_comma}{_r_alpha})?\s*\)\s*'
# CSS4 RGB examples: rgb(0 0 0), rgb(0 0 0 / 0.5), rgb(0 0 0 / 50%), rgba(0 0 0 / 50%)
r_rgb_v4_style = rf'\s*rgba?\(\s*{_r_255}\s+{_r_255}\s+{_r_255}(?:\s*/\s*{_r_alpha})?\s*\)\s*'
# CSS4 HSL examples: hsl(270 60% 50%), hsl(270 60% 50% / 0.5), hsl(270 60% 50% / 50%), hsla(270 60% 50% / 50%)
r_hsl_v4_style = rf'\s*hsla?\(\s*{_r_h}\s+{_r_sl}\s+{_r_sl}(?:\s*/\s*{_r_alpha})?\s*\)\s*'
_r_255 = r'(\d{1,3}(?:\.\d+)?)'
_r_comma = r'\s*,\s*'
r_rgb = fr'\s*rgb\(\s*{_r_255}{_r_comma}{_r_255}{_r_comma}{_r_255}\)\s*'
_r_alpha = r'(\d(?:\.\d+)?|\.\d+|\d{1,2}%)'
r_rgba = fr'\s*rgba\(\s*{_r_255}{_r_comma}{_r_255}{_r_comma}{_r_255}{_r_comma}{_r_alpha}\s*\)\s*'
_r_h = r'(-?\d+(?:\.\d+)?|-?\.\d+)(deg|rad|turn)?'
_r_sl = r'(\d{1,3}(?:\.\d+)?)%'
r_hsl = fr'\s*hsl\(\s*{_r_h}{_r_comma}{_r_sl}{_r_comma}{_r_sl}\s*\)\s*'
r_hsla = fr'\s*hsl\(\s*{_r_h}{_r_comma}{_r_sl}{_r_comma}{_r_sl}{_r_comma}{_r_alpha}\s*\)\s*'
# colors where the two hex characters are the same, if all colors match this the short version of hex colors can be used
repeat_colors = {int(c * 2, 16) for c in '0123456789abcdef'}
rads = 2 * math.pi
@deprecated(
'The `Color` class is deprecated, use `pydantic_extra_types` instead. '
'See https://docs.pydantic.dev/latest/api/pydantic_extra_types_color/.',
category=PydanticDeprecatedSince20,
)
class Color(_repr.Representation):
"""Represents a color."""
class Color(Representation):
__slots__ = '_original', '_rgba'
def __init__(self, value: ColorType) -> None:
@ -89,39 +74,22 @@ class Color(_repr.Representation):
self._rgba = value._rgba
value = value._original
else:
raise PydanticCustomError(
'color_error', 'value is not a valid color: value must be a tuple, list or string'
)
raise ColorError(reason='value must be a tuple, list or string')
# if we've got here value must be a valid color
self._original = value
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: _GetJsonSchemaHandler
) -> JsonSchemaValue:
field_schema = {}
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
field_schema.update(type='string', format='color')
return field_schema
def original(self) -> ColorType:
"""Original value passed to `Color`."""
"""
Original value passed to Color
"""
return self._original
def as_named(self, *, fallback: bool = False) -> str:
"""Returns the name of the color if it can be found in `COLORS_BY_VALUE` dictionary,
otherwise returns the hexadecimal representation of the color or raises `ValueError`.
Args:
fallback: If True, falls back to returning the hexadecimal representation of
the color instead of raising a ValueError when no named color is found.
Returns:
The name of the color, or the hexadecimal representation of the color.
Raises:
ValueError: When no named color is found and fallback is `False`.
"""
if self._rgba.alpha is None:
rgb = cast(Tuple[int, int, int], self.as_rgb_tuple())
try:
@ -135,13 +103,9 @@ class Color(_repr.Representation):
return self.as_hex()
def as_hex(self) -> str:
"""Returns the hexadecimal representation of the color.
Hex string representing the color can be 3, 4, 6, or 8 characters depending on whether the string
"""
Hex string representing the color can be 3, 4, 6 or 8 characters depending on whether the string
a "short" representation of the color is possible and whether there's an alpha channel.
Returns:
The hexadecimal representation of the color.
"""
values = [float_to_255(c) for c in self._rgba[:3]]
if self._rgba.alpha is not None:
@ -153,7 +117,9 @@ class Color(_repr.Representation):
return '#' + as_hex
def as_rgb(self) -> str:
"""Color as an `rgb(<r>, <g>, <b>)` or `rgba(<r>, <g>, <b>, <a>)` string."""
"""
Color as an rgb(<r>, <g>, <b>) or rgba(<r>, <g>, <b>, <a>) string.
"""
if self._rgba.alpha is None:
return f'rgb({float_to_255(self._rgba.r)}, {float_to_255(self._rgba.g)}, {float_to_255(self._rgba.b)})'
else:
@ -163,18 +129,14 @@ class Color(_repr.Representation):
)
def as_rgb_tuple(self, *, alpha: Optional[bool] = None) -> ColorTuple:
"""Returns the color as an RGB or RGBA tuple.
"""
Color as an RGB or RGBA tuple; red, green and blue are in the range 0 to 255, alpha if included is
in the range 0 to 1.
Args:
alpha: Whether to include the alpha channel. There are three options for this input:
- `None` (default): Include alpha only if it's set. (e.g. not `None`)
- `True`: Always include alpha.
- `False`: Always omit alpha.
Returns:
A tuple that contains the values of the red, green, and blue channels in the range 0 to 255.
If alpha is included, it is in the range 0 to 1.
:param alpha: whether to include the alpha channel, options are
None - (default) include alpha only if it's set (e.g. not None)
True - always include alpha,
False - always omit alpha,
"""
r, g, b = (float_to_255(c) for c in self._rgba[:3])
if alpha is None:
@ -189,7 +151,9 @@ class Color(_repr.Representation):
return r, g, b
def as_hsl(self) -> str:
"""Color as an `hsl(<h>, <s>, <l>)` or `hsl(<h>, <s>, <l>, <a>)` string."""
"""
Color as an hsl(<h>, <s>, <l>) or hsl(<h>, <s>, <l>, <a>) string.
"""
if self._rgba.alpha is None:
h, s, li = self.as_hsl_tuple(alpha=False) # type: ignore
return f'hsl({h * 360:0.0f}, {s:0.0%}, {li:0.0%})'
@ -198,23 +162,18 @@ class Color(_repr.Representation):
return f'hsl({h * 360:0.0f}, {s:0.0%}, {li:0.0%}, {round(a, 2)})'
def as_hsl_tuple(self, *, alpha: Optional[bool] = None) -> HslColorTuple:
"""Returns the color as an HSL or HSLA tuple.
Args:
alpha: Whether to include the alpha channel.
- `None` (default): Include the alpha channel only if it's set (e.g. not `None`).
- `True`: Always include alpha.
- `False`: Always omit alpha.
Returns:
The color as a tuple of hue, saturation, lightness, and alpha (if included).
All elements are in the range 0 to 1.
Note:
This is HSL as used in HTML and most other places, not HLS as used in Python's `colorsys`.
"""
h, l, s = rgb_to_hls(self._rgba.r, self._rgba.g, self._rgba.b) # noqa: E741
Color as an HSL or HSLA tuple, e.g. hue, saturation, lightness and optionally alpha; all elements are in
the range 0 to 1.
NOTE: this is HSL as used in HTML and most other places, not HLS as used in python's colorsys.
:param alpha: whether to include the alpha channel, options are
None - (default) include alpha only if it's set (e.g. not None)
True - always include alpha,
False - always omit alpha,
"""
h, l, s = rgb_to_hls(self._rgba.r, self._rgba.g, self._rgba.b)
if alpha is None:
if self._rgba.alpha is None:
return h, s, l
@ -230,22 +189,14 @@ class Color(_repr.Representation):
return 1 if self._rgba.alpha is None else self._rgba.alpha
@classmethod
def __get_pydantic_core_schema__(
cls, source: Type[Any], handler: Callable[[Any], CoreSchema]
) -> core_schema.CoreSchema:
return core_schema.with_info_plain_validator_function(
cls._validate, serialization=core_schema.to_string_ser_schema()
)
@classmethod
def _validate(cls, __input_value: Any, _: Any) -> 'Color':
return cls(__input_value)
def __get_validators__(cls) -> 'CallableGenerator':
yield cls
def __str__(self) -> str:
return self.as_named(fallback=True)
def __repr_args__(self) -> '_repr.ReprArgs':
return [(None, self.as_named(fallback=True))] + [('rgb', self.as_rgb_tuple())]
def __repr_args__(self) -> 'ReprArgs':
return [(None, self.as_named(fallback=True))] + [('rgb', self.as_rgb_tuple())] # type: ignore
def __eq__(self, other: Any) -> bool:
return isinstance(other, Color) and self.as_rgb_tuple() == other.as_rgb_tuple()
@ -255,16 +206,8 @@ class Color(_repr.Representation):
def parse_tuple(value: Tuple[Any, ...]) -> RGBA:
"""Parse a tuple or list to get RGBA values.
Args:
value: A tuple or list.
Returns:
An `RGBA` tuple parsed from the input tuple.
Raises:
PydanticCustomError: If tuple is not valid.
"""
Parse a tuple or list as a color.
"""
if len(value) == 3:
r, g, b = (parse_color_value(v) for v in value)
@ -273,28 +216,17 @@ def parse_tuple(value: Tuple[Any, ...]) -> RGBA:
r, g, b = (parse_color_value(v) for v in value[:3])
return RGBA(r, g, b, parse_float_alpha(value[3]))
else:
raise PydanticCustomError('color_error', 'value is not a valid color: tuples must have length 3 or 4')
raise ColorError(reason='tuples must have length 3 or 4')
def parse_str(value: str) -> RGBA:
"""Parse a string representing a color to an RGBA tuple.
Possible formats for the input string include:
* named color, see `COLORS_BY_NAME`
"""
Parse a string to an RGBA tuple, trying the following formats (in this order):
* named color, see COLORS_BY_NAME below
* hex short eg. `<prefix>fff` (prefix can be `#`, `0x` or nothing)
* hex long eg. `<prefix>ffffff` (prefix can be `#`, `0x` or nothing)
* `rgb(<r>, <g>, <b>)`
* `rgb(<r>, <g>, <b>) `
* `rgba(<r>, <g>, <b>, <a>)`
Args:
value: A string representing a color.
Returns:
An `RGBA` tuple parsed from the input string.
Raises:
ValueError: If the input string cannot be parsed to an RGBA tuple.
"""
value_lower = value.lower()
try:
@ -324,70 +256,49 @@ def parse_str(value: str) -> RGBA:
alpha = None
return ints_to_rgba(r, g, b, alpha)
m = re.fullmatch(r_rgb, value_lower) or re.fullmatch(r_rgb_v4_style, value_lower)
m = re.fullmatch(r_rgb, value_lower)
if m:
return ints_to_rgba(*m.groups(), None) # type: ignore
m = re.fullmatch(r_rgba, value_lower)
if m:
return ints_to_rgba(*m.groups()) # type: ignore
m = re.fullmatch(r_hsl, value_lower) or re.fullmatch(r_hsl_v4_style, value_lower)
m = re.fullmatch(r_hsl, value_lower)
if m:
return parse_hsl(*m.groups()) # type: ignore
h, h_units, s, l_ = m.groups()
return parse_hsl(h, h_units, s, l_)
raise PydanticCustomError('color_error', 'value is not a valid color: string not recognised as a valid color')
m = re.fullmatch(r_hsla, value_lower)
if m:
h, h_units, s, l_, a = m.groups()
return parse_hsl(h, h_units, s, l_, parse_float_alpha(a))
raise ColorError(reason='string not recognised as a valid color')
def ints_to_rgba(r: Union[int, str], g: Union[int, str], b: Union[int, str], alpha: Optional[float] = None) -> RGBA:
"""Converts integer or string values for RGB color and an optional alpha value to an `RGBA` object.
Args:
r: An integer or string representing the red color value.
g: An integer or string representing the green color value.
b: An integer or string representing the blue color value.
alpha: A float representing the alpha value. Defaults to None.
Returns:
An instance of the `RGBA` class with the corresponding color and alpha values.
"""
def ints_to_rgba(r: Union[int, str], g: Union[int, str], b: Union[int, str], alpha: Optional[float]) -> RGBA:
return RGBA(parse_color_value(r), parse_color_value(g), parse_color_value(b), parse_float_alpha(alpha))
def parse_color_value(value: Union[int, str], max_val: int = 255) -> float:
"""Parse the color value provided and return a number between 0 and 1.
Args:
value: An integer or string color value.
max_val: Maximum range value. Defaults to 255.
Raises:
PydanticCustomError: If the value is not a valid color.
Returns:
A number between 0 and 1.
"""
Parse a value checking it's a valid int in the range 0 to max_val and divide by max_val to give a number
in the range 0 to 1
"""
try:
color = float(value)
except ValueError:
raise PydanticCustomError('color_error', 'value is not a valid color: color values must be a valid number')
raise ColorError(reason='color values must be a valid number')
if 0 <= color <= max_val:
return color / max_val
else:
raise PydanticCustomError(
'color_error',
'value is not a valid color: color values must be in the range 0 to {max_val}',
{'max_val': max_val},
)
raise ColorError(reason=f'color values must be in the range 0 to {max_val}')
def parse_float_alpha(value: Union[None, str, float, int]) -> Optional[float]:
"""Parse an alpha value checking it's a valid float in the range 0 to 1.
Args:
value: The input value to parse.
Returns:
The parsed value as a float, or `None` if the value was None or equal 1.
Raises:
PydanticCustomError: If the input value cannot be successfully parsed as a float in the expected range.
"""
Parse a value checking it's a valid float in the range 0 to 1
"""
if value is None:
return None
@ -397,28 +308,19 @@ def parse_float_alpha(value: Union[None, str, float, int]) -> Optional[float]:
else:
alpha = float(value)
except ValueError:
raise PydanticCustomError('color_error', 'value is not a valid color: alpha values must be a valid float')
raise ColorError(reason='alpha values must be a valid float')
if math.isclose(alpha, 1):
if almost_equal_floats(alpha, 1):
return None
elif 0 <= alpha <= 1:
return alpha
else:
raise PydanticCustomError('color_error', 'value is not a valid color: alpha values must be in the range 0 to 1')
raise ColorError(reason='alpha values must be in the range 0 to 1')
def parse_hsl(h: str, h_units: str, sat: str, light: str, alpha: Optional[float] = None) -> RGBA:
"""Parse raw hue, saturation, lightness, and alpha values and convert to RGBA.
Args:
h: The hue value.
h_units: The unit for hue value.
sat: The saturation value.
light: The lightness value.
alpha: Alpha value.
Returns:
An instance of `RGBA`.
"""
Parse raw hue, saturation, lightness and alpha values and convert to RGBA.
"""
s_value, l_value = parse_color_value(sat, 100), parse_color_value(light, 100)
@ -432,21 +334,10 @@ def parse_hsl(h: str, h_units: str, sat: str, light: str, alpha: Optional[float]
h_value = h_value % 1
r, g, b = hls_to_rgb(h_value, l_value, s_value)
return RGBA(r, g, b, parse_float_alpha(alpha))
return RGBA(r, g, b, alpha)
def float_to_255(c: float) -> int:
"""Converts a float value between 0 and 1 (inclusive) to an integer between 0 and 255 (inclusive).
Args:
c: The float value to be converted. Must be between 0 and 1 (inclusive).
Returns:
The integer equivalent of the given float value rounded to the nearest whole number.
Raises:
ValueError: If the given float value is outside the acceptable range of 0 to 1 (inclusive).
"""
return int(round(c * 255))