Skip to content

Commit e57a86b

Browse files
author
Gauvain Pocentek
committed
Build managers on demand on GitlabObject's
1 parent 11c1425 commit e57a86b

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

gitlab/objects.py

+35-27
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,19 @@ def __init__(self, gl, data=None, **kwargs):
407407
if not hasattr(self, "id"):
408408
self.id = None
409409

410-
self._set_managers()
410+
def _set_manager(self, var, cls, attrs):
411+
manager = cls(self.gitlab, self, attrs)
412+
setattr(self, var, manager)
411413

412-
def _set_managers(self):
414+
def __getattr__(self, name):
415+
# build a manager if it doesn't exist yet
413416
for var, cls, attrs in self.managers:
414-
manager = cls(self.gitlab, self, attrs)
415-
setattr(self, var, manager)
417+
if var != name:
418+
continue
419+
self._set_manager(var, cls, attrs)
420+
return getattr(self, var)
421+
422+
raise AttributeError
416423

417424
def __str__(self):
418425
return '%s => %s' % (type(self), str(self.__dict__))
@@ -582,10 +589,10 @@ class User(GitlabObject):
582589
'projects_limit', 'extern_uid', 'provider', 'bio',
583590
'admin', 'can_create_group', 'website_url',
584591
'confirm', 'external']
585-
managers = [
592+
managers = (
586593
('emails', UserEmailManager, [('user_id', 'id')]),
587594
('keys', UserKeyManager, [('user_id', 'id')])
588-
]
595+
)
589596

590597
def _data_for_gitlab(self, extra_parameters={}, update=False,
591598
as_json=True):
@@ -690,11 +697,10 @@ class CurrentUser(GitlabObject):
690697
canUpdate = False
691698
canDelete = False
692699
shortPrintAttr = 'username'
693-
managers = [
700+
managers = (
694701
('emails', CurrentUserEmailManager, [('user_id', 'id')]),
695702
('keys', CurrentUserKeyManager, [('user_id', 'id')])
696-
]
697-
703+
)
698704

699705
class ApplicationSettings(GitlabObject):
700706
_url = '/application/settings'
@@ -867,14 +873,14 @@ class Group(GitlabObject):
867873
optionalCreateAttrs = ['description', 'visibility_level']
868874
optionalUpdateAttrs = ['name', 'path', 'description', 'visibility_level']
869875
shortPrintAttr = 'name'
870-
managers = [
876+
managers = (
871877
('accessrequests', GroupAccessRequestManager, [('group_id', 'id')]),
872878
('members', GroupMemberManager, [('group_id', 'id')]),
873879
('notificationsettings', GroupNotificationSettingsManager,
874880
[('group_id', 'id')]),
875881
('projects', GroupProjectManager, [('group_id', 'id')]),
876882
('issues', GroupIssueManager, [('group_id', 'id')])
877-
]
883+
)
878884

879885
GUEST_ACCESS = gitlab.GUEST_ACCESS
880886
REPORTER_ACCESS = gitlab.REPORTER_ACCESS
@@ -994,8 +1000,8 @@ class ProjectBoard(GitlabObject):
9941000
canUpdate = False
9951001
canCreate = False
9961002
canDelete = False
997-
managers = [('lists', ProjectBoardListManager,
998-
[('project_id', 'project_id'), ('board_id', 'id')])]
1003+
managers = (('lists', ProjectBoardListManager,
1004+
[('project_id', 'project_id'), ('board_id', 'id')]))
9991005

10001006

10011007
class ProjectBoardManager(BaseManager):
@@ -1168,10 +1174,12 @@ class ProjectCommit(GitlabObject):
11681174
canCreate = False
11691175
requiredUrlAttrs = ['project_id']
11701176
shortPrintAttr = 'title'
1171-
managers = [('comments', ProjectCommitCommentManager,
1172-
[('project_id', 'project_id'), ('commit_id', 'id')]),
1173-
('statuses', ProjectCommitStatusManager,
1174-
[('project_id', 'project_id'), ('commit_id', 'id')])]
1177+
managers = (
1178+
('comments', ProjectCommitCommentManager,
1179+
[('project_id', 'project_id'), ('commit_id', 'id')]),
1180+
('statuses', ProjectCommitStatusManager,
1181+
[('project_id', 'project_id'), ('commit_id', 'id')])
1182+
)
11751183

11761184
def diff(self, **kwargs):
11771185
"""Generate the commit diff."""
@@ -1335,8 +1343,8 @@ class ProjectIssue(GitlabObject):
13351343
'milestone_id', 'labels', 'created_at',
13361344
'state_event']
13371345
shortPrintAttr = 'title'
1338-
managers = [('notes', ProjectIssueNoteManager,
1339-
[('project_id', 'project_id'), ('issue_id', 'id')])]
1346+
managers = (('notes', ProjectIssueNoteManager,
1347+
[('project_id', 'project_id'), ('issue_id', 'id')]))
13401348

13411349
def _data_for_gitlab(self, extra_parameters={}, update=False,
13421350
as_json=True):
@@ -1518,8 +1526,8 @@ class ProjectMergeRequest(GitlabObject):
15181526
'milestone_id']
15191527
optionalListAttrs = ['iid', 'state', 'order_by', 'sort']
15201528

1521-
managers = [('notes', ProjectMergeRequestNoteManager,
1522-
[('project_id', 'project_id'), ('merge_request_id', 'id')])]
1529+
managers = (('notes', ProjectMergeRequestNoteManager,
1530+
[('project_id', 'project_id'), ('merge_request_id', 'id')]))
15231531

15241532
def _data_for_gitlab(self, extra_parameters={}, update=False,
15251533
as_json=True):
@@ -1827,8 +1835,8 @@ class ProjectSnippet(GitlabObject):
18271835
optionalCreateAttrs = ['lifetime', 'visibility_level']
18281836
optionalUpdateAttrs = ['title', 'file_name', 'code', 'visibility_level']
18291837
shortPrintAttr = 'title'
1830-
managers = [('notes', ProjectSnippetNoteManager,
1831-
[('project_id', 'project_id'), ('snippet_id', 'id')])]
1838+
managers = (('notes', ProjectSnippetNoteManager,
1839+
[('project_id', 'project_id'), ('snippet_id', 'id')]))
18321840

18331841
def Content(self, **kwargs):
18341842
warnings.warn("`Content` is deprecated, use `content` instead",
@@ -2021,7 +2029,7 @@ class Project(GitlabObject):
20212029
'public_builds',
20222030
'only_allow_merge_if_build_succeeds']
20232031
shortPrintAttr = 'path'
2024-
managers = [
2032+
managers = (
20252033
('accessrequests', ProjectAccessRequestManager,
20262034
[('project_id', 'id')]),
20272035
('boards', ProjectBoardManager, [('project_id', 'id')]),
@@ -2054,7 +2062,7 @@ class Project(GitlabObject):
20542062
('tags', ProjectTagManager, [('project_id', 'id')]),
20552063
('triggers', ProjectTriggerManager, [('project_id', 'id')]),
20562064
('variables', ProjectVariableManager, [('project_id', 'id')]),
2057-
]
2065+
)
20582066

20592067
VISIBILITY_PRIVATE = gitlab.VISIBILITY_PRIVATE
20602068
VISIBILITY_INTERNAL = gitlab.VISIBILITY_INTERNAL
@@ -2531,10 +2539,10 @@ class Team(GitlabObject):
25312539
shortPrintAttr = 'name'
25322540
requiredCreateAttrs = ['name', 'path']
25332541
canUpdate = False
2534-
managers = [
2542+
managers = (
25352543
('members', TeamMemberManager, [('team_id', 'id')]),
25362544
('projects', TeamProjectManager, [('team_id', 'id')])
2537-
]
2545+
)
25382546

25392547

25402548
class TeamManager(BaseManager):

0 commit comments

Comments
 (0)