Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6b47c26

Browse files
nejchJohnVillalovos
authored andcommitted
feat: display human-readable attribute inrepr() if present
1 parent2373a4f commit6b47c26

18 files changed

+85
-51
lines changed

‎gitlab/base.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,20 @@ class RESTObject:
4949
another. This allows smart updates, if the object allows it.
5050
5151
You can redefine ``_id_attr`` in child classes to specify which attribute
52-
must be used asuniq ID. ``None`` means that the object can be updated
52+
must be used asthe unique ID. ``None`` means that the object can be updated
5353
without ID in the url.
54+
55+
Likewise, you can define a ``_repr_attr`` in subclasses to specify which
56+
attribute should be added as a human-readable identifier when called in the
57+
object's ``__repr__()`` method.
5458
"""
5559

5660
_id_attr:Optional[str]="id"
5761
_attrs:Dict[str,Any]
5862
_created_from_list:bool# Indicates if object was created from a list() action
5963
_module:ModuleType
6064
_parent_attrs:Dict[str,Any]
61-
_short_print_attr:Optional[str]=None
65+
_repr_attr:Optional[str]=None
6266
_updated_attrs:Dict[str,Any]
6367
manager:"RESTManager"
6468

@@ -158,10 +162,19 @@ def pprint(self) -> None:
158162
print(self.pformat())
159163

160164
def__repr__(self)->str:
165+
name=self.__class__.__name__
166+
167+
if (self._id_attrandself._repr_attr)and (self._id_attr!=self._repr_attr):
168+
return (
169+
f"<{name}{self._id_attr}:{self.get_id()} "
170+
f"{self._repr_attr}:{getattr(self,self._repr_attr)}>"
171+
)
161172
ifself._id_attr:
162-
returnf"<{self.__class__.__name__}{self._id_attr}:{self.get_id()}>"
163-
else:
164-
returnf"<{self.__class__.__name__}>"
173+
returnf"<{name}{self._id_attr}:{self.get_id()}>"
174+
ifself._repr_attr:
175+
returnf"<{name}{self._repr_attr}:{getattr(self,self._repr_attr)}>"
176+
177+
returnf"<{name}>"
165178

166179
def__eq__(self,other:object)->bool:
167180
ifnotisinstance(other,RESTObject):

‎gitlab/v4/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,12 @@ def display_dict(d: Dict[str, Any], padding: int) -> None:
449449
ifobj._id_attr:
450450
id=getattr(obj,obj._id_attr)
451451
print(f"{obj._id_attr.replace('_','-')}:{id}")
452-
ifobj._short_print_attr:
453-
value=getattr(obj,obj._short_print_attr)or"None"
452+
ifobj._repr_attr:
453+
value=getattr(obj,obj._repr_attr,"None")
454454
value=value.replace("\r","").replace("\n"," ")
455455
# If the attribute is a note (ProjectCommitComment) then we do
456456
# some modifications to fit everything on one line
457-
line=f"{obj._short_print_attr}:{value}"
457+
line=f"{obj._repr_attr}:{value}"
458458
# ellipsize long lines (comments)
459459
iflen(line)>79:
460460
line=f"{line[:76]}..."

‎gitlab/v4/objects/applications.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
classApplication(ObjectDeleteMixin,RESTObject):
1111
_url="/applications"
12-
_short_print_attr="name"
12+
_repr_attr="name"
1313

1414

1515
classApplicationManager(ListMixin,CreateMixin,DeleteMixin,RESTManager):

‎gitlab/v4/objects/commits.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
classProjectCommit(RESTObject):
24-
_short_print_attr="title"
24+
_repr_attr="title"
2525

2626
comments:"ProjectCommitCommentManager"
2727
discussions:ProjectCommitDiscussionManager
@@ -172,7 +172,7 @@ def get(
172172

173173
classProjectCommitComment(RESTObject):
174174
_id_attr=None
175-
_short_print_attr="note"
175+
_repr_attr="note"
176176

177177

178178
classProjectCommitCommentManager(ListMixin,CreateMixin,RESTManager):

‎gitlab/v4/objects/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
classEvent(RESTObject):
3131
_id_attr=None
32-
_short_print_attr="target_title"
32+
_repr_attr="target_title"
3333

3434

3535
classEventManager(ListMixin,RESTManager):

‎gitlab/v4/objects/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
classProjectFile(SaveMixin,ObjectDeleteMixin,RESTObject):
2626
_id_attr="file_path"
27-
_short_print_attr="file_path"
27+
_repr_attr="file_path"
2828
file_path:str
2929
manager:"ProjectFileManager"
3030

‎gitlab/v4/objects/groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949

5050
classGroup(SaveMixin,ObjectDeleteMixin,RESTObject):
51-
_short_print_attr="name"
51+
_repr_attr="name"
5252

5353
access_tokens:GroupAccessTokenManager
5454
accessrequests:GroupAccessRequestManager

‎gitlab/v4/objects/hooks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
classHook(ObjectDeleteMixin,RESTObject):
1717
_url="/hooks"
18-
_short_print_attr="url"
18+
_repr_attr="url"
1919

2020

2121
classHookManager(NoUpdateMixin,RESTManager):
@@ -28,7 +28,7 @@ def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Hook:
2828

2929

3030
classProjectHook(SaveMixin,ObjectDeleteMixin,RESTObject):
31-
_short_print_attr="url"
31+
_repr_attr="url"
3232

3333

3434
classProjectHookManager(CRUDMixin,RESTManager):
@@ -75,7 +75,7 @@ def get(
7575

7676

7777
classGroupHook(SaveMixin,ObjectDeleteMixin,RESTObject):
78-
_short_print_attr="url"
78+
_repr_attr="url"
7979

8080

8181
classGroupHookManager(CRUDMixin,RESTManager):

‎gitlab/v4/objects/issues.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
classIssue(RESTObject):
4444
_url="/issues"
45-
_short_print_attr="title"
45+
_repr_attr="title"
4646

4747

4848
classIssueManager(RetrieveMixin,RESTManager):
@@ -108,7 +108,7 @@ class ProjectIssue(
108108
ObjectDeleteMixin,
109109
RESTObject,
110110
):
111-
_short_print_attr="title"
111+
_repr_attr="title"
112112
_id_attr="iid"
113113

114114
awardemojis:ProjectIssueAwardEmojiManager

‎gitlab/v4/objects/members.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
classGroupMember(SaveMixin,ObjectDeleteMixin,RESTObject):
31-
_short_print_attr="username"
31+
_repr_attr="username"
3232

3333

3434
classGroupMemberManager(CRUDMixin,RESTManager):
@@ -50,7 +50,7 @@ def get(
5050

5151

5252
classGroupBillableMember(ObjectDeleteMixin,RESTObject):
53-
_short_print_attr="username"
53+
_repr_attr="username"
5454

5555
memberships:"GroupBillableMemberMembershipManager"
5656

@@ -73,7 +73,7 @@ class GroupBillableMemberMembershipManager(ListMixin, RESTManager):
7373

7474

7575
classGroupMemberAll(RESTObject):
76-
_short_print_attr="username"
76+
_repr_attr="username"
7777

7878

7979
classGroupMemberAllManager(RetrieveMixin,RESTManager):
@@ -88,7 +88,7 @@ def get(
8888

8989

9090
classProjectMember(SaveMixin,ObjectDeleteMixin,RESTObject):
91-
_short_print_attr="username"
91+
_repr_attr="username"
9292

9393

9494
classProjectMemberManager(CRUDMixin,RESTManager):
@@ -110,7 +110,7 @@ def get(
110110

111111

112112
classProjectMemberAll(RESTObject):
113-
_short_print_attr="username"
113+
_repr_attr="username"
114114

115115

116116
classProjectMemberAllManager(RetrieveMixin,RESTManager):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp