Skip to content

Commit e411fca

Browse files
authored
__repr__ method (#147)
* `__repr__` method added for most objects(previously it was only present for `FsNode`). * `notifications.Notification` class has been rewritten in the same format as all the others. --------- Signed-off-by: Alexander Piskun <[email protected]>
1 parent 5750e9a commit e411fca

22 files changed

+162
-64
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ All notable changes to this project will be documented in this file.
55
## [0.4.0 - 2023-10-2x]
66

77
As the project moves closer to `beta`, final unification changes are being made.
8-
This release contains some breaking changes in `users` API.
8+
This release contains some breaking changes in `users`, `notifications` API.
9+
10+
### Added
11+
12+
- `__repr__` method added for most objects(previously it was only present for `FsNode`).
913

1014
### Changed
1115

1216
- `users.get_details` renamed to `get_user` and returns a class instead of a dictionary. #145
1317
- Optional argument `displayname` in `users.create` renamed to `display_name`.
14-
- The `apps.ExAppInfo` class has been rewritten in the same format as all the others.
18+
- The `apps.ExAppInfo` class has been rewritten in the same format as all the others. #146
19+
- `notifications.Notification` class has been rewritten in the same format as all the others.
1520

1621
### Fixed
1722

docs/reference/Users/Notifications.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@ Notifications
66

77
.. autoclass:: nc_py_api.notifications.Notification
88
:members:
9-
10-
.. autoclass:: nc_py_api.notifications.NotificationInfo
11-
:members:

nc_py_api/activity.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def priority(self) -> int:
3636
"""Arrangement priority in ascending order. Values from 0 to 99."""
3737
return self._raw_data["priority"]
3838

39+
def __repr__(self):
40+
return f"<{self.__class__.__name__} id={self.filter_id}, name={self.name}, priority={self.priority}>"
41+
3942

4043
@dataclasses.dataclass
4144
class Activity:
@@ -122,10 +125,16 @@ def icon(self) -> str:
122125
return self._raw_data["icon"]
123126

124127
@property
125-
def activity_time(self) -> datetime.datetime:
128+
def time(self) -> datetime.datetime:
126129
"""Time when the activity occurred."""
127130
return nc_iso_time_to_datetime(self._raw_data["datetime"])
128131

132+
def __repr__(self):
133+
return (
134+
f"<{self.__class__.__name__} id={self.activity_id}, app={self.app}, type={self.activity_type},"
135+
f" time={self.time}>"
136+
)
137+
129138

130139
class _ActivityAPI:
131140
"""The class provides the Activity Application API."""

nc_py_api/apps.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def system(self) -> bool:
4747
"""Flag indicating if the application is a system application."""
4848
return bool(self._raw_data["system"])
4949

50+
def __repr__(self):
51+
return f"<{self.__class__.__name__} id={self.app_id}, ver={self.version}>"
52+
5053

5154
class _AppsAPI:
5255
"""The class provides the application management API on the Nextcloud server."""

nc_py_api/files/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ def user_assignable(self) -> bool:
265265
"""Flag indicating if User can assign this Tag."""
266266
return bool(self._raw_data.get("oc:user-assignable", "false").lower() == "true")
267267

268+
def __repr__(self):
269+
return f"<{self.__class__.__name__} id={self.tag_id}, name={self.display_name}>"
270+
268271

269272
class ShareType(enum.IntEnum):
270273
"""Type of the object that will receive share."""

nc_py_api/notes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def last_modified(self) -> datetime.datetime:
7373
modified = self._raw_data.get("modified", 0)
7474
return datetime.datetime.utcfromtimestamp(modified).replace(tzinfo=datetime.timezone.utc)
7575

76+
def __repr__(self):
77+
return f"<{self.__class__.__name__} id={self.note_id}, title={self.title}, last_modified={self.last_modified}>"
78+
7679

7780
class NotesSettings(typing.TypedDict):
7881
"""Settings of Notes App."""

nc_py_api/notifications.py

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,68 @@
1313
from ._session import NcSessionApp, NcSessionBasic
1414

1515

16-
@dataclasses.dataclass
17-
class NotificationInfo:
18-
"""Extra Notification attributes from Nextcloud."""
19-
20-
app_name: str
21-
"""Application name that generated notification."""
22-
user_id: str
23-
"""User name for which this notification is."""
24-
time: datetime.datetime
25-
"""Time when the notification was created."""
26-
subject: str
27-
"""Subject of the notification."""
28-
message: str
29-
"""Message of the notification."""
30-
link: str
31-
"""Link which will be opened when user clicks on notification."""
32-
icon: str
33-
"""Relative to instance url of the icon image."""
34-
35-
def __init__(self, raw_info: dict):
36-
self.app_name = raw_info["app"]
37-
self.user_id = raw_info["user"]
38-
self.time = nc_iso_time_to_datetime(raw_info["datetime"])
39-
self.subject = raw_info["subject"]
40-
self.message = raw_info["message"]
41-
self.link = raw_info.get("link", "")
42-
self.icon = raw_info.get("icon", "")
43-
44-
4516
@dataclasses.dataclass
4617
class Notification:
4718
"""Class representing information about Nextcloud notification."""
4819

49-
notification_id: int
50-
"""ID of the notification."""
51-
object_id: str
52-
"""Randomly generated unique object ID"""
53-
object_type: str
54-
"""Currently not used."""
55-
info: NotificationInfo
56-
"""Additional extra information for the object"""
57-
58-
def __init__(self, raw_info: dict):
59-
self.notification_id = raw_info["notification_id"]
60-
self.object_id = raw_info["object_id"]
61-
self.object_type = raw_info["object_type"]
62-
self.info = NotificationInfo(raw_info)
20+
def __init__(self, raw_data: dict):
21+
self._raw_data = raw_data
22+
23+
@property
24+
def notification_id(self) -> int:
25+
"""ID of the notification."""
26+
return self._raw_data["notification_id"]
27+
28+
@property
29+
def object_id(self) -> str:
30+
"""Randomly generated unique object ID."""
31+
return self._raw_data["object_id"]
32+
33+
@property
34+
def object_type(self) -> str:
35+
"""Currently not used."""
36+
return self._raw_data["object_type"]
37+
38+
@property
39+
def app_name(self) -> str:
40+
"""Application name that generated notification."""
41+
return self._raw_data["app"]
42+
43+
@property
44+
def user_id(self) -> str:
45+
"""User ID of user for which this notification is."""
46+
return self._raw_data["user"]
47+
48+
@property
49+
def subject(self) -> str:
50+
"""Subject of the notification."""
51+
return self._raw_data["subject"]
52+
53+
@property
54+
def message(self) -> str:
55+
"""Message of the notification."""
56+
return self._raw_data["message"]
57+
58+
@property
59+
def time(self) -> datetime.datetime:
60+
"""Time when the notification was created."""
61+
return nc_iso_time_to_datetime(self._raw_data["datetime"])
62+
63+
@property
64+
def link(self) -> str:
65+
"""Link, which will be opened when user clicks on notification."""
66+
return self._raw_data.get("link", "")
67+
68+
@property
69+
def icon(self) -> str:
70+
"""Relative to instance url of the icon image."""
71+
return self._raw_data.get("icon", "")
72+
73+
def __repr__(self):
74+
return (
75+
f"<{self.__class__.__name__} id={self.notification_id}, app_name={self.app_name}, user_id={self.user_id},"
76+
f" time={self.time}>"
77+
)
6378

6479

6580
class _NotificationsAPI:

nc_py_api/talk.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Nextcloud Talk API definitions."""
22

33
import dataclasses
4+
import datetime
45
import enum
56
import os
67
import typing
@@ -280,6 +281,12 @@ def markdown(self) -> bool:
280281
"""Whether the message should be rendered as markdown or shown as plain text."""
281282
return self._raw_data.get("markdown", False)
282283

284+
def __repr__(self):
285+
return (
286+
f"<{self.__class__.__name__} id={self.message_id}, author={self.actor_display_name},"
287+
f" time={datetime.datetime.utcfromtimestamp(self.timestamp).replace(tzinfo=datetime.timezone.utc)}>"
288+
)
289+
283290

284291
class TalkFileMessage(TalkMessage):
285292
"""Subclass of Talk Message representing message-containing file."""
@@ -625,6 +632,12 @@ def status_clear_at(self) -> typing.Optional[int]:
625632
"""
626633
return self._raw_data.get("statusClearAt", None)
627634

635+
def __repr__(self):
636+
return (
637+
f"<{self.__class__.__name__} id={self.conversation_id}, name={self.display_name},"
638+
f" type={self.conversation_type.name}>"
639+
)
640+
628641

629642
@dataclasses.dataclass(init=False)
630643
class Participant(_TalkUserStatus):
@@ -657,7 +670,7 @@ def participant_type(self) -> ParticipantType:
657670

658671
@property
659672
def last_ping(self) -> int:
660-
"""Timestamp of the last ping. Should be used for sorting."""
673+
"""Timestamp of the last ping. Should be used for sorting."""
661674
return self._raw_data["lastPing"]
662675

663676
@property
@@ -685,6 +698,11 @@ def breakout_token(self) -> str:
685698
"""Only available with breakout-rooms-v1 capability."""
686699
return self._raw_data.get("roomToken", "")
687700

701+
def __repr__(self):
702+
return (
703+
f"<{self.__class__.__name__} id={self.attendee_id}, name={self.display_name}, last_ping={self.last_ping}>"
704+
)
705+
688706

689707
@dataclasses.dataclass
690708
class BotInfoBasic:
@@ -713,6 +731,9 @@ def state(self) -> int:
713731
"""One of the Bot states: ``0`` - Disabled, ``1`` - enabled, ``2`` - **No setup**."""
714732
return self._raw_data["state"]
715733

734+
def __repr__(self):
735+
return f"<{self.__class__.__name__} id={self.bot_id}, name={self.bot_name}>"
736+
716737

717738
@dataclasses.dataclass(init=False)
718739
class BotInfo(BotInfoBasic):
@@ -771,6 +792,9 @@ def option(self) -> int:
771792
"""The option that was voted for."""
772793
return self._raw_data["optionId"]
773794

795+
def __repr__(self):
796+
return f"<{self.__class__.__name__} actor={self.actor_display_name}, voted_for={self.option}>"
797+
774798

775799
@dataclasses.dataclass
776800
class Poll:
@@ -856,3 +880,6 @@ def num_voters(self) -> int:
856880
def details(self) -> list[PollDetail]:
857881
"""Detailed list who voted for which option (only available for public closed polls)."""
858882
return [PollDetail(i) for i in self._raw_data.get("details", [])]
883+
884+
def __repr__(self):
885+
return f"<{self.__class__.__name__} id={self.poll_id}, author={self.actor_display_name}>"

nc_py_api/talk_bot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def conversation_name(self) -> str:
7777
"""The name of the conversation in which the message was posted."""
7878
return self._raw_data["target"]["name"]
7979

80+
def __repr__(self):
81+
return f"<{self.__class__.__name__} conversation={self.conversation_name}, actor={self.actor_display_name}>"
82+
8083

8184
class TalkBot:
8285
"""A class that implements the TalkBot functionality."""

nc_py_api/user_status.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def status_type(self) -> str:
7777
"""Status type, on of the: online, away, dnd, invisible, offline."""
7878
return self._raw_data.get("status", "")
7979

80+
def __repr__(self):
81+
return f"<{self.__class__.__name__} user_id={self.user_id}, status_type={self.status_type}>"
82+
8083

8184
@dataclasses.dataclass(init=False)
8285
class CurrentUserStatus(UserStatus):
@@ -97,6 +100,12 @@ def status_type_defined(self) -> bool:
97100
"""*True* if :py:attr:`UserStatus.status_type` is set by user, *False* otherwise."""
98101
return self._raw_data["statusIsUserDefined"]
99102

103+
def __repr__(self):
104+
return (
105+
f"<{self.__class__.__name__} user_id={self.user_id}, status_type={self.status_type},"
106+
f" status_id={self.status_id}>"
107+
)
108+
100109

101110
class _UserStatusAPI:
102111
"""Class providing the user status management API on the Nextcloud server."""

0 commit comments

Comments
 (0)