Allow specifying class variables in init

This commit is contained in:
Mads Marquart
2019-10-28 12:23:46 +01:00
parent 637ea97ffe
commit b03d0ae3b7
7 changed files with 37 additions and 49 deletions

View File

@@ -37,10 +37,7 @@ class ShareAttachment(Attachment):
#: URL of the original image if Facebook uses ``safe_image``
original_image_url = attr.ib(None)
#: List of additional attachments
attachments = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
# Put here for backwards compatibility, so that the init argument order is preserved
uid = attr.ib(None)
attachments = attr.ib(factory=list)
@classmethod
def _from_graphql(cls, data):

View File

@@ -17,9 +17,6 @@ class FileAttachment(Attachment):
#: Whether Facebook determines that this file may be harmful
is_malicious = attr.ib(None)
# Put here for backwards compatibility, so that the init argument order is preserved
uid = attr.ib(None)
@classmethod
def _from_graphql(cls, data):
return cls(
@@ -43,9 +40,6 @@ class AudioAttachment(Attachment):
#: Audio type
audio_type = attr.ib(None)
# Put here for backwards compatibility, so that the init argument order is preserved
uid = attr.ib(None)
@classmethod
def _from_graphql(cls, data):
return cls(

View File

@@ -11,21 +11,19 @@ class Group(Thread):
type = ThreadType.GROUP
#: Unique list (set) of the group thread's participant user IDs
participants = attr.ib(factory=set, converter=lambda x: set() if x is None else x)
participants = attr.ib(factory=set)
#: A dictionary, containing user nicknames mapped to their IDs
nicknames = attr.ib(factory=dict, converter=lambda x: {} if x is None else x)
nicknames = attr.ib(factory=dict)
#: A :class:`ThreadColor`. The groups's message color
color = attr.ib(None)
#: The groups's default emoji
emoji = attr.ib(None)
# Set containing user IDs of thread admins
admins = attr.ib(factory=set, converter=lambda x: set() if x is None else x)
admins = attr.ib(factory=set)
# True if users need approval to join
approval_mode = attr.ib(None)
# Set containing user IDs requesting to join
approval_requests = attr.ib(
factory=set, converter=lambda x: set() if x is None else x
)
approval_requests = attr.ib(factory=set)
# Link for joining group
join_link = attr.ib(None)

View File

@@ -16,15 +16,12 @@ class LocationAttachment(Attachment):
#: Longitude of the location
longitude = attr.ib(None)
#: Image showing the map of the location
image = attr.ib(None, init=False)
image = attr.ib(None)
#: URL to Bing maps with the location
url = attr.ib(None, init=False)
url = attr.ib(None)
# Address of the location
address = attr.ib(None)
# Put here for backwards compatibility, so that the init argument order is preserved
uid = attr.ib(None)
@classmethod
def _from_graphql(cls, data):
url = data.get("url")

View File

@@ -61,35 +61,35 @@ class Message:
#: The actual message
text = attr.ib(None)
#: A list of :class:`Mention` objects
mentions = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
mentions = attr.ib(factory=list)
#: A :class:`EmojiSize`. Size of a sent emoji
emoji_size = attr.ib(None)
#: The message ID
uid = attr.ib(None, init=False)
uid = attr.ib(None)
#: ID of the sender
author = attr.ib(None, init=False)
author = attr.ib(None)
#: Datetime of when the message was sent
created_at = attr.ib(None, init=False)
created_at = attr.ib(None)
#: Whether the message is read
is_read = attr.ib(None, init=False)
is_read = attr.ib(None)
#: A list of people IDs who read the message, works only with :func:`fbchat.Client.fetch_thread_messages`
read_by = attr.ib(factory=list, init=False)
read_by = attr.ib(factory=list)
#: A dictionary with user's IDs as keys, and their :class:`MessageReaction` as values
reactions = attr.ib(factory=dict, init=False)
reactions = attr.ib(factory=dict)
#: A :class:`Sticker`
sticker = attr.ib(None)
#: A list of attachments
attachments = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
attachments = attr.ib(factory=list)
#: A list of :class:`QuickReply`
quick_replies = attr.ib(factory=list, converter=lambda x: [] if x is None else x)
quick_replies = attr.ib(factory=list)
#: Whether the message is unsent (deleted for everyone)
unsent = attr.ib(False, init=False)
unsent = attr.ib(False)
#: Message ID you want to reply to
reply_to_id = attr.ib(None)
#: Replied message
replied_to = attr.ib(None, init=False)
replied_to = attr.ib(None)
#: Whether the message was forwarded
forwarded = attr.ib(False, init=False)
forwarded = attr.ib(False)
@classmethod
def format_mentions(cls, text, *args, **kwargs):

View File

@@ -14,20 +14,20 @@ class GuestStatus(Enum):
class Plan:
"""Represents a plan."""
#: ID of the plan
uid = attr.ib(None, init=False)
#: Plan time (datetime), only precise down to the minute
time = attr.ib()
#: Plan title
title = attr.ib()
#: ID of the plan
uid = attr.ib(None)
#: Plan location name
location = attr.ib(None, converter=lambda x: x or "")
#: Plan location ID
location_id = attr.ib(None, converter=lambda x: x or "")
#: ID of the plan creator
author_id = attr.ib(None, init=False)
author_id = attr.ib(None)
#: Dictionary of `User` IDs mapped to their `GuestStatus`
guests = attr.ib(None, init=False)
guests = attr.ib(None)
@property
def going(self):

View File

@@ -34,14 +34,17 @@ def test_location_attachment_from_graphql():
"target": {"__typename": "MessageLocation"},
"subattachments": [],
}
expected = LocationAttachment(latitude=55.4, longitude=12.4322, uid=400828513928715)
expected.image = fbchat.Image(
url="https://external-arn2-1.xx.fbcdn.net/static_map.php?v=1020&osm_provider=2&size=545x280&zoom=15&markers=55.40000000%2C12.43220000&language=en",
width=545,
height=280,
)
expected.url = "https://l.facebook.com/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%2Fdefault.aspx%3Fv%3D2%26pc%3DFACEBK%26mid%3D8100%26where1%3D55.4%252C%2B12.4322%26FORM%3DFBKPL1%26mkt%3Den-GB&h=a&s=1"
assert expected == LocationAttachment._from_graphql(data)
assert LocationAttachment(
uid=400828513928715,
latitude=55.4,
longitude=12.4322,
image=fbchat.Image(
url="https://external-arn2-1.xx.fbcdn.net/static_map.php?v=1020&osm_provider=2&size=545x280&zoom=15&markers=55.40000000%2C12.43220000&language=en",
width=545,
height=280,
),
url="https://l.facebook.com/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%2Fdefault.aspx%3Fv%3D2%26pc%3DFACEBK%26mid%3D8100%26where1%3D55.4%252C%2B12.4322%26FORM%3DFBKPL1%26mkt%3Den-GB&h=a&s=1",
) == LocationAttachment._from_graphql(data)
@pytest.mark.skip(reason="need to gather test data")
@@ -76,16 +79,15 @@ def test_live_location_from_graphql_expired():
},
"subattachments": [],
}
expected = LiveLocationAttachment(
assert LiveLocationAttachment(
uid=2254535444791641,
name="Location-sharing ended",
expires_at=datetime.datetime(
2019, 1, 4, 18, 25, 45, tzinfo=datetime.timezone.utc
),
is_expired=True,
)
expected.url = "https://www.facebook.com/"
assert expected == LiveLocationAttachment._from_graphql(data)
url="https://www.facebook.com/",
) == LiveLocationAttachment._from_graphql(data)
@pytest.mark.skip(reason="need to gather test data")