Fix code style issues with Black
This commit is contained in:
@ -30,9 +30,7 @@ from typing import Any, Dict, Final, List, Mapping, Protocol, TYPE_CHECKING, Typ
|
||||
from . import utils
|
||||
from .colour import Colour
|
||||
|
||||
__all__ = (
|
||||
'Embed',
|
||||
)
|
||||
__all__ = ("Embed",)
|
||||
|
||||
|
||||
class _EmptyEmbed:
|
||||
@ -40,7 +38,7 @@ class _EmptyEmbed:
|
||||
return False
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return 'Embed.Empty'
|
||||
return "Embed.Empty"
|
||||
|
||||
def __len__(self) -> int:
|
||||
return 0
|
||||
@ -57,51 +55,45 @@ class EmbedProxy:
|
||||
return len(self.__dict__)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
inner = ', '.join((f'{k}={v!r}' for k, v in self.__dict__.items() if not k.startswith('_')))
|
||||
return f'EmbedProxy({inner})'
|
||||
inner = ", ".join((f"{k}={v!r}" for k, v in self.__dict__.items() if not k.startswith("_")))
|
||||
return f"EmbedProxy({inner})"
|
||||
|
||||
def __getattr__(self, attr: str) -> _EmptyEmbed:
|
||||
return EmptyEmbed
|
||||
|
||||
|
||||
E = TypeVar('E', bound='Embed')
|
||||
E = TypeVar("E", bound="Embed")
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from discord.types.embed import Embed as EmbedData, EmbedType
|
||||
|
||||
T = TypeVar('T')
|
||||
T = TypeVar("T")
|
||||
MaybeEmpty = Union[T, _EmptyEmbed]
|
||||
|
||||
|
||||
class _EmbedFooterProxy(Protocol):
|
||||
text: MaybeEmpty[str]
|
||||
icon_url: MaybeEmpty[str]
|
||||
|
||||
|
||||
class _EmbedFieldProxy(Protocol):
|
||||
name: MaybeEmpty[str]
|
||||
value: MaybeEmpty[str]
|
||||
inline: bool
|
||||
|
||||
|
||||
class _EmbedMediaProxy(Protocol):
|
||||
url: MaybeEmpty[str]
|
||||
proxy_url: MaybeEmpty[str]
|
||||
height: MaybeEmpty[int]
|
||||
width: MaybeEmpty[int]
|
||||
|
||||
|
||||
class _EmbedVideoProxy(Protocol):
|
||||
url: MaybeEmpty[str]
|
||||
height: MaybeEmpty[int]
|
||||
width: MaybeEmpty[int]
|
||||
|
||||
|
||||
class _EmbedProviderProxy(Protocol):
|
||||
name: MaybeEmpty[str]
|
||||
url: MaybeEmpty[str]
|
||||
|
||||
|
||||
class _EmbedAuthorProxy(Protocol):
|
||||
name: MaybeEmpty[str]
|
||||
url: MaybeEmpty[str]
|
||||
@ -163,33 +155,33 @@ class Embed:
|
||||
"""
|
||||
|
||||
__slots__ = (
|
||||
'title',
|
||||
'url',
|
||||
'type',
|
||||
'_timestamp',
|
||||
'_colour',
|
||||
'_footer',
|
||||
'_image',
|
||||
'_thumbnail',
|
||||
'_video',
|
||||
'_provider',
|
||||
'_author',
|
||||
'_fields',
|
||||
'description',
|
||||
"title",
|
||||
"url",
|
||||
"type",
|
||||
"_timestamp",
|
||||
"_colour",
|
||||
"_footer",
|
||||
"_image",
|
||||
"_thumbnail",
|
||||
"_video",
|
||||
"_provider",
|
||||
"_author",
|
||||
"_fields",
|
||||
"description",
|
||||
)
|
||||
|
||||
Empty: Final = EmptyEmbed
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
colour: Union[int, Colour, _EmptyEmbed] = EmptyEmbed,
|
||||
color: Union[int, Colour, _EmptyEmbed] = EmptyEmbed,
|
||||
title: MaybeEmpty[Any] = EmptyEmbed,
|
||||
type: EmbedType = 'rich',
|
||||
url: MaybeEmpty[Any] = EmptyEmbed,
|
||||
description: MaybeEmpty[Any] = EmptyEmbed,
|
||||
timestamp: datetime.datetime = None,
|
||||
self,
|
||||
*,
|
||||
colour: Union[int, Colour, _EmptyEmbed] = EmptyEmbed,
|
||||
color: Union[int, Colour, _EmptyEmbed] = EmptyEmbed,
|
||||
title: MaybeEmpty[Any] = EmptyEmbed,
|
||||
type: EmbedType = "rich",
|
||||
url: MaybeEmpty[Any] = EmptyEmbed,
|
||||
description: MaybeEmpty[Any] = EmptyEmbed,
|
||||
timestamp: datetime.datetime = None,
|
||||
):
|
||||
|
||||
self.colour = colour if colour is not EmptyEmbed else color
|
||||
@ -231,10 +223,10 @@ class Embed:
|
||||
|
||||
# fill in the basic fields
|
||||
|
||||
self.title = data.get('title', EmptyEmbed)
|
||||
self.type = data.get('type', EmptyEmbed)
|
||||
self.description = data.get('description', EmptyEmbed)
|
||||
self.url = data.get('url', EmptyEmbed)
|
||||
self.title = data.get("title", EmptyEmbed)
|
||||
self.type = data.get("type", EmptyEmbed)
|
||||
self.description = data.get("description", EmptyEmbed)
|
||||
self.url = data.get("url", EmptyEmbed)
|
||||
|
||||
if self.title is not EmptyEmbed:
|
||||
self.title = str(self.title)
|
||||
@ -248,22 +240,22 @@ class Embed:
|
||||
# try to fill in the more rich fields
|
||||
|
||||
try:
|
||||
self._colour = Colour(value=data['color'])
|
||||
self._colour = Colour(value=data["color"])
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
try:
|
||||
self._timestamp = utils.parse_time(data['timestamp'])
|
||||
self._timestamp = utils.parse_time(data["timestamp"])
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
for attr in ('thumbnail', 'video', 'provider', 'author', 'fields', 'image', 'footer'):
|
||||
for attr in ("thumbnail", "video", "provider", "author", "fields", "image", "footer"):
|
||||
try:
|
||||
value = data[attr]
|
||||
except KeyError:
|
||||
continue
|
||||
else:
|
||||
setattr(self, '_' + attr, value)
|
||||
setattr(self, "_" + attr, value)
|
||||
|
||||
return self
|
||||
|
||||
@ -273,11 +265,11 @@ class Embed:
|
||||
|
||||
def __len__(self) -> int:
|
||||
total = len(self.title) + len(self.description)
|
||||
for field in getattr(self, '_fields', []):
|
||||
total += len(field['name']) + len(field['value'])
|
||||
for field in getattr(self, "_fields", []):
|
||||
total += len(field["name"]) + len(field["value"])
|
||||
|
||||
try:
|
||||
footer_text = self._footer['text']
|
||||
footer_text = self._footer["text"]
|
||||
except (AttributeError, KeyError):
|
||||
pass
|
||||
else:
|
||||
@ -288,7 +280,7 @@ class Embed:
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
total += len(author['name'])
|
||||
total += len(author["name"])
|
||||
|
||||
return total
|
||||
|
||||
@ -312,7 +304,7 @@ class Embed:
|
||||
|
||||
@property
|
||||
def colour(self) -> MaybeEmpty[Colour]:
|
||||
return getattr(self, '_colour', EmptyEmbed)
|
||||
return getattr(self, "_colour", EmptyEmbed)
|
||||
|
||||
@colour.setter
|
||||
def colour(self, value: Union[int, Colour, _EmptyEmbed]): # type: ignore
|
||||
@ -321,13 +313,15 @@ class Embed:
|
||||
elif isinstance(value, int):
|
||||
self._colour = Colour(value=value)
|
||||
else:
|
||||
raise TypeError(f'Expected discord.Colour, int, or Embed.Empty but received {value.__class__.__name__} instead.')
|
||||
raise TypeError(
|
||||
f"Expected discord.Colour, int, or Embed.Empty but received {value.__class__.__name__} instead."
|
||||
)
|
||||
|
||||
color = colour
|
||||
|
||||
@property
|
||||
def timestamp(self) -> MaybeEmpty[datetime.datetime]:
|
||||
return getattr(self, '_timestamp', EmptyEmbed)
|
||||
return getattr(self, "_timestamp", EmptyEmbed)
|
||||
|
||||
@timestamp.setter
|
||||
def timestamp(self, value: MaybeEmpty[datetime.datetime]):
|
||||
@ -348,7 +342,7 @@ class Embed:
|
||||
|
||||
If the attribute has no value then :attr:`Empty` is returned.
|
||||
"""
|
||||
return EmbedProxy(getattr(self, '_footer', {})) # type: ignore
|
||||
return EmbedProxy(getattr(self, "_footer", {})) # type: ignore
|
||||
|
||||
def set_footer(self: E, *, text: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> E:
|
||||
"""Sets the footer for the embed content.
|
||||
@ -366,10 +360,10 @@ class Embed:
|
||||
|
||||
self._footer = {}
|
||||
if text is not EmptyEmbed:
|
||||
self._footer['text'] = str(text)
|
||||
self._footer["text"] = str(text)
|
||||
|
||||
if icon_url is not EmptyEmbed:
|
||||
self._footer['icon_url'] = str(icon_url)
|
||||
self._footer["icon_url"] = str(icon_url)
|
||||
|
||||
return self
|
||||
|
||||
@ -401,7 +395,7 @@ class Embed:
|
||||
|
||||
If the attribute has no value then :attr:`Empty` is returned.
|
||||
"""
|
||||
return EmbedProxy(getattr(self, '_image', {})) # type: ignore
|
||||
return EmbedProxy(getattr(self, "_image", {})) # type: ignore
|
||||
|
||||
@image.setter
|
||||
def image(self: E, url: Any):
|
||||
@ -409,7 +403,7 @@ class Embed:
|
||||
del self._image
|
||||
else:
|
||||
self._image = {
|
||||
'url': str(url),
|
||||
"url": str(url),
|
||||
}
|
||||
|
||||
@image.deleter
|
||||
@ -451,7 +445,7 @@ class Embed:
|
||||
|
||||
If the attribute has no value then :attr:`Empty` is returned.
|
||||
"""
|
||||
return EmbedProxy(getattr(self, '_thumbnail', {})) # type: ignore
|
||||
return EmbedProxy(getattr(self, "_thumbnail", {})) # type: ignore
|
||||
|
||||
@thumbnail.setter
|
||||
def thumbnail(self: E, url: Any):
|
||||
@ -459,7 +453,7 @@ class Embed:
|
||||
del self._thumbnail
|
||||
else:
|
||||
self._thumbnail = {
|
||||
'url': str(url),
|
||||
"url": str(url),
|
||||
}
|
||||
|
||||
@thumbnail.deleter
|
||||
@ -500,7 +494,7 @@ class Embed:
|
||||
|
||||
If the attribute has no value then :attr:`Empty` is returned.
|
||||
"""
|
||||
return EmbedProxy(getattr(self, '_video', {})) # type: ignore
|
||||
return EmbedProxy(getattr(self, "_video", {})) # type: ignore
|
||||
|
||||
@property
|
||||
def provider(self) -> _EmbedProviderProxy:
|
||||
@ -510,7 +504,7 @@ class Embed:
|
||||
|
||||
If the attribute has no value then :attr:`Empty` is returned.
|
||||
"""
|
||||
return EmbedProxy(getattr(self, '_provider', {})) # type: ignore
|
||||
return EmbedProxy(getattr(self, "_provider", {})) # type: ignore
|
||||
|
||||
@property
|
||||
def author(self) -> _EmbedAuthorProxy:
|
||||
@ -520,9 +514,11 @@ class Embed:
|
||||
|
||||
If the attribute has no value then :attr:`Empty` is returned.
|
||||
"""
|
||||
return EmbedProxy(getattr(self, '_author', {})) # type: ignore
|
||||
return EmbedProxy(getattr(self, "_author", {})) # type: ignore
|
||||
|
||||
def set_author(self: E, *, name: Any, url: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> E:
|
||||
def set_author(
|
||||
self: E, *, name: Any, url: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed
|
||||
) -> E:
|
||||
"""Sets the author for the embed content.
|
||||
|
||||
This function returns the class instance to allow for fluent-style
|
||||
@ -539,14 +535,14 @@ class Embed:
|
||||
"""
|
||||
|
||||
self._author = {
|
||||
'name': str(name),
|
||||
"name": str(name),
|
||||
}
|
||||
|
||||
if url is not EmptyEmbed:
|
||||
self._author['url'] = str(url)
|
||||
self._author["url"] = str(url)
|
||||
|
||||
if icon_url is not EmptyEmbed:
|
||||
self._author['icon_url'] = str(icon_url)
|
||||
self._author["icon_url"] = str(icon_url)
|
||||
|
||||
return self
|
||||
|
||||
@ -573,7 +569,7 @@ class Embed:
|
||||
|
||||
If the attribute has no value then :attr:`Empty` is returned.
|
||||
"""
|
||||
return [EmbedProxy(d) for d in getattr(self, '_fields', [])] # type: ignore
|
||||
return [EmbedProxy(d) for d in getattr(self, "_fields", [])] # type: ignore
|
||||
|
||||
def add_field(self: E, *, name: Any, value: Any, inline: bool = True) -> E:
|
||||
"""Adds a field to the embed object.
|
||||
@ -592,9 +588,9 @@ class Embed:
|
||||
"""
|
||||
|
||||
field = {
|
||||
'inline': inline,
|
||||
'name': str(name),
|
||||
'value': str(value),
|
||||
"inline": inline,
|
||||
"name": str(name),
|
||||
"value": str(value),
|
||||
}
|
||||
|
||||
try:
|
||||
@ -625,9 +621,9 @@ class Embed:
|
||||
"""
|
||||
|
||||
field = {
|
||||
'inline': inline,
|
||||
'name': str(name),
|
||||
'value': str(value),
|
||||
"inline": inline,
|
||||
"name": str(name),
|
||||
"value": str(value),
|
||||
}
|
||||
|
||||
try:
|
||||
@ -693,11 +689,11 @@ class Embed:
|
||||
try:
|
||||
field = self._fields[index]
|
||||
except (TypeError, IndexError, AttributeError):
|
||||
raise IndexError('field index out of range')
|
||||
raise IndexError("field index out of range")
|
||||
|
||||
field['name'] = str(name)
|
||||
field['value'] = str(value)
|
||||
field['inline'] = inline
|
||||
field["name"] = str(name)
|
||||
field["value"] = str(value)
|
||||
field["inline"] = inline
|
||||
return self
|
||||
|
||||
def to_dict(self) -> EmbedData:
|
||||
@ -715,35 +711,35 @@ class Embed:
|
||||
# deal with basic convenience wrappers
|
||||
|
||||
try:
|
||||
colour = result.pop('colour')
|
||||
colour = result.pop("colour")
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
if colour:
|
||||
result['color'] = colour.value
|
||||
result["color"] = colour.value
|
||||
|
||||
try:
|
||||
timestamp = result.pop('timestamp')
|
||||
timestamp = result.pop("timestamp")
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
if timestamp:
|
||||
if timestamp.tzinfo:
|
||||
result['timestamp'] = timestamp.astimezone(tz=datetime.timezone.utc).isoformat()
|
||||
result["timestamp"] = timestamp.astimezone(tz=datetime.timezone.utc).isoformat()
|
||||
else:
|
||||
result['timestamp'] = timestamp.replace(tzinfo=datetime.timezone.utc).isoformat()
|
||||
result["timestamp"] = timestamp.replace(tzinfo=datetime.timezone.utc).isoformat()
|
||||
|
||||
# add in the non raw attribute ones
|
||||
if self.type:
|
||||
result['type'] = self.type
|
||||
result["type"] = self.type
|
||||
|
||||
if self.description:
|
||||
result['description'] = self.description
|
||||
result["description"] = self.description
|
||||
|
||||
if self.url:
|
||||
result['url'] = self.url
|
||||
result["url"] = self.url
|
||||
|
||||
if self.title:
|
||||
result['title'] = self.title
|
||||
result["title"] = self.title
|
||||
|
||||
return result # type: ignore
|
||||
|
Reference in New Issue
Block a user