diff options
author | Gauvain Pocentek <gauvain@pocentek.net> | 2017-05-28 10:53:54 +0200 |
---|---|---|
committer | Gauvain Pocentek <gauvain@pocentek.net> | 2017-06-02 15:41:37 +0200 |
commit | a50690288f9c03ec37ff374839d1f465c74ecf0a (patch) | |
tree | 35f9e07f1c36cd50bcc4bcf791a1294bb50c2028 /gitlab/base.py | |
parent | 9fbdb9461a660181a3a268cd398865cafd0b4a89 (diff) | |
download | gitlab-a50690288f9c03ec37ff374839d1f465c74ecf0a.tar.gz |
Add support for managers in objects for new API
Convert User* to the new REST* API.
Diffstat (limited to 'gitlab/base.py')
-rw-r--r-- | gitlab/base.py | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/gitlab/base.py b/gitlab/base.py index 2ecf1d2..afbcd38 100644 --- a/gitlab/base.py +++ b/gitlab/base.py @@ -575,8 +575,14 @@ class RESTObject(object): 'manager': manager, '_attrs': attrs, '_updated_attrs': {}, + '_module': importlib.import_module(self.__module__) }) + # TODO(gpocentek): manage the creation of new objects from the received + # data (_constructor_types) + + self._create_managers() + def __getattr__(self, name): try: return self.__dict__['_updated_attrs'][name] @@ -602,6 +608,16 @@ class RESTObject(object): else: return '<%s>' % self.__class__.__name__ + def _create_managers(self): + managers = getattr(self, '_managers', None) + if managers is None: + return + + for attr, cls_name in self._managers: + cls = getattr(self._module, cls_name) + manager = cls(self.manager.gitlab, parent=self) + self.__dict__[attr] = manager + def get_id(self): if self._id_attr is None: return None @@ -653,6 +669,19 @@ class RESTManager(object): _path = None _obj_cls = None - def __init__(self, gl, parent_attrs={}): + def __init__(self, gl, parent=None): self.gitlab = gl - self._parent_attrs = {} # for nested managers + self._parent = parent # for nested managers + self._computed_path = self._compute_path() + + def _compute_path(self): + if self._parent is None or not hasattr(self, '_from_parent_attrs'): + return self._path + + data = {self_attr: getattr(self._parent, parent_attr) + for self_attr, parent_attr in self._from_parent_attrs.items()} + return self._path % data + + @property + def path(self): + return self._computed_path |