Skip to content

Commit

Permalink
Reorganise the code to handle v3 and v4 objects
Browse files Browse the repository at this point in the history
Having objects managing both versions will only make the code more
complicated, with lots of tests everywhere. This solution might generate
some code duplication, but it should be maintainable.
  • Loading branch information
Gauvain Pocentek committed May 23, 2017
1 parent f373885 commit e853a30
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 576 deletions.
54 changes: 29 additions & 25 deletions gitlab/__init__.py
Expand Up @@ -19,6 +19,7 @@
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import importlib
import inspect
import itertools
import json
Expand All @@ -31,7 +32,7 @@
import gitlab.config
from gitlab.const import * # noqa
from gitlab.exceptions import * # noqa
from gitlab.objects import * # noqa
from gitlab.v3.objects import * # noqa

__title__ = 'python-gitlab'
__version__ = '0.20'
Expand Down Expand Up @@ -91,40 +92,43 @@ def __init__(self, url, private_token=None, email=None, password=None,
#: Create a session object for requests
self.session = requests.Session()

self.broadcastmessages = BroadcastMessageManager(self)
self.keys = KeyManager(self)
self.deploykeys = DeployKeyManager(self)
self.gitlabciymls = GitlabciymlManager(self)
self.gitignores = GitignoreManager(self)
self.groups = GroupManager(self)
self.hooks = HookManager(self)
self.issues = IssueManager(self)
self.licenses = LicenseManager(self)
self.namespaces = NamespaceManager(self)
self.notificationsettings = NotificationSettingsManager(self)
self.projects = ProjectManager(self)
self.runners = RunnerManager(self)
self.settings = ApplicationSettingsManager(self)
self.sidekiq = SidekiqManager(self)
self.snippets = SnippetManager(self)
self.users = UserManager(self)
self.teams = TeamManager(self)
self.todos = TodoManager(self)
objects = importlib.import_module('gitlab.v%s.objects' %
self._api_version)

self.broadcastmessages = objects.BroadcastMessageManager(self)
self.keys = objects.KeyManager(self)
self.deploykeys = objects.DeployKeyManager(self)
self.gitlabciymls = objects.GitlabciymlManager(self)
self.gitignores = objects.GitignoreManager(self)
self.groups = objects.GroupManager(self)
self.hooks = objects.HookManager(self)
self.issues = objects.IssueManager(self)
self.licenses = objects.LicenseManager(self)
self.namespaces = objects.NamespaceManager(self)
self.notificationsettings = objects.NotificationSettingsManager(self)
self.projects = objects.ProjectManager(self)
self.runners = objects.RunnerManager(self)
self.settings = objects.ApplicationSettingsManager(self)
self.sidekiq = objects.SidekiqManager(self)
self.snippets = objects.SnippetManager(self)
self.users = objects.UserManager(self)
self.teams = objects.TeamManager(self)
self.todos = objects.TodoManager(self)

# build the "submanagers"
for parent_cls in six.itervalues(globals()):
for parent_cls in six.itervalues(vars(objects)):
if (not inspect.isclass(parent_cls)
or not issubclass(parent_cls, GitlabObject)
or parent_cls == CurrentUser):
or not issubclass(parent_cls, objects.GitlabObject)
or parent_cls == objects.CurrentUser):
continue

if not parent_cls.managers:
continue

for var, cls, attrs in parent_cls.managers:
for var, cls_name, attrs in parent_cls.managers:
var_name = '%s_%s' % (self._cls_to_manager_prefix(parent_cls),
var)
manager = cls(self)
manager = getattr(objects, cls_name)(self)
setattr(self, var_name, manager)

@property
Expand Down
2 changes: 1 addition & 1 deletion gitlab/tests/test_manager.py
Expand Up @@ -25,7 +25,7 @@
from httmock import urlmatch # noqa

from gitlab import * # noqa
from gitlab.objects import BaseManager # noqa
from gitlab.v3.objects import BaseManager # noqa


class FakeChildObject(GitlabObject):
Expand Down
Empty file added gitlab/v3/__init__.py
Empty file.

0 comments on commit e853a30

Please sign in to comment.