/
listing.py
68 lines (46 loc) · 1.89 KB
/
listing.py
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
"""Provide the Listing class."""
from typing import Any, Optional
from ..base import AsyncPRAWBase
class Listing(AsyncPRAWBase):
"""A listing is a collection of :class:`.RedditBase` instances."""
CHILD_ATTRIBUTE = "children"
def __len__(self) -> int:
"""Return the number of items in the Listing."""
return len(getattr(self, self.CHILD_ATTRIBUTE))
def __getitem__(self, index: int) -> Any:
"""Return the item at position index in the list."""
return getattr(self, self.CHILD_ATTRIBUTE)[index]
def __setattr__(self, attribute: str, value: Any):
"""Objectify the ``CHILD_ATTRIBUTE`` attribute."""
if attribute == self.CHILD_ATTRIBUTE:
value = self._reddit._objector.objectify(value)
super().__setattr__(attribute, value)
class FlairListing(Listing):
"""Special Listing for handling flair lists."""
CHILD_ATTRIBUTE = "users"
@property
def after(self) -> Optional[Any]:
"""Return the next attribute or ``None``."""
return getattr(self, "next", None)
class ModeratorListing(Listing):
"""Special Listing for handling moderator lists."""
CHILD_ATTRIBUTE = "moderators"
class ModNoteListing(Listing):
"""Special Listing for handling :class:`.ModNote` lists."""
CHILD_ATTRIBUTE = "mod_notes"
@property
def after(self) -> Optional[Any]:
"""Return the next attribute or None."""
if not getattr(self, "has_next_page", True):
return None
return getattr(self, "end_cursor", None)
class ModmailConversationsListing(Listing):
"""Special Listing for handling :class:`.ModmailConversation` lists."""
CHILD_ATTRIBUTE = "conversations"
@property
def after(self) -> Optional[str]:
"""Return the next attribute or ``None``."""
try:
return self.conversations[-1].id
except IndexError:
return None