| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d7c7911 commit 5a4aafb
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,15 +63,15 @@ following important changes in the python API: | |||
| 63 | 63 | gl = gitlab.Gitlab(...) | |
| 64 | 64 | p = gl.projects.get(project_id) | |
| 65 | 65 | ||
| 66 | - * Listing methods (``manager.list()`` for instance) now return generators | ||
| 66 | + * Listing methods (``manager.list()`` for instance) can now return generators | ||
| 67 | 67 | (:class:`~gitlab.base.RESTObjectList`). They handle the calls to the API when | |
| 68 | - needed. | ||
| 68 | + needed to fetch new items. | ||
| 69 | 69 | ||
| 70 | - If you need to get all the items at once, use the ``all=True`` parameter: | ||
| 70 | + By default you will still get lists. To get generators use ``as_list=False``: | ||
| 71 | 71 | ||
| 72 | 72 | .. code-block:: python | |
| 73 | 73 | ||
| 74 | - all_projects = gl.projects.list(all=True) | ||
| 74 | + all_projects_g = gl.projects.list(as_list=False) | ||
| 75 | 75 | ||
| 76 | 76 | * The "nested" managers (for instance ``gl.project_issues`` or | |
| 77 | 77 | ``gl.group_members``) are not available anymore. Their goal was to provide a | |
@@ -114,18 +114,3 @@ following important changes in the python API: | |||
| 114 | 114 | + :attr:`~gitlab.Gitlab.http_post` | |
| 115 | 115 | + :attr:`~gitlab.Gitlab.http_put` | |
| 116 | 116 | + :attr:`~gitlab.Gitlab.http_delete` | |
| 117 | - | ||
| 118 | - * The users ``get_by_username`` method has been removed. It doesn't exist in | ||
| 119 | - the GitLab API. You can use the ``username`` filter attribute when listing to | ||
| 120 | - get a similar behavior: | ||
| 121 | - | ||
| 122 | - .. code-block:: python | ||
| 123 | - | ||
| 124 | - user = list(gl.users.list(username='jdoe'))[0] | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - Undergoing work | ||
| 128 | - =============== | ||
| 129 | - | ||
| 130 | - * The ``page`` and ``per_page`` arguments for listing don't behave as they used | ||
| 131 | - to. Their behavior will be restored. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -712,7 +712,7 @@ def http_get(self, path, query_data={}, streamed=False, **kwargs): | |||
| 712 | 712 | else: | |
| 713 | 713 | return result | |
| 714 | 714 | ||
| 715 | - def http_list(self, path, query_data={}, **kwargs): | ||
| 715 | + def http_list(self, path, query_data={}, as_list=None, **kwargs): | ||
| 716 | 716 | """Make a GET request to the Gitlab server for list-oriented queries. | |
| 717 | 717 | ||
| 718 | 718 | Args: | |
@@ -723,19 +723,33 @@ def http_list(self, path, query_data={}, **kwargs): | |||
| 723 | 723 | all) | |
| 724 | 724 | ||
| 725 | 725 | Returns: | |
| 726 | - GitlabList: A generator giving access to the objects. If an ``all`` | ||
| 727 | - kwarg is defined and True, returns a list of all the objects (will | ||
| 728 | - possibly make numerous calls to the Gtilab server and eat a lot of | ||
| 729 | - memory) | ||
| 726 | + list: A list of the objects returned by the server. If `as_list` is | ||
| 727 | + False and no pagination-related arguments (`page`, `per_page`, | ||
| 728 | + `all`) are defined then a GitlabList object (generator) is returned | ||
| 729 | + instead. This object will make API calls when needed to fetch the | ||
| 730 | + next items from the server. | ||
| 730 | 731 | ||
| 731 | 732 | Raises: | |
| 732 | 733 | GitlabHttpError: When the return code is not 2xx | |
| 733 | 734 | GitlabParsingError: If the json data could not be parsed | |
| 734 | 735 | """ | |
| 736 | + | ||
| 737 | + # In case we want to change the default behavior at some point | ||
| 738 | + as_list = True if as_list is None else as_list | ||
| 739 | + | ||
| 740 | + get_all = kwargs.get('all', False) | ||
| 735 | 741 | url = self._build_url(path) | |
| 736 | - get_all = kwargs.pop('all', False) | ||
| 737 | - obj_gen = GitlabList(self, url, query_data, **kwargs) | ||
| 738 | - return list(obj_gen) if get_all else obj_gen | ||
| 742 | + | ||
| 743 | + if get_all is True: | ||
| 744 | + return list(GitlabList(self, url, query_data, **kwargs)) | ||
| 745 | + | ||
| 746 | + if 'page' in kwargs or 'per_page' in kwargs or as_list is True: | ||
| 747 | + # pagination requested, we return a list | ||
| 748 | + return list(GitlabList(self, url, query_data, get_next=False, | ||
| 749 | + **kwargs)) | ||
| 750 | + | ||
| 751 | + # No pagination, generator requested | ||
| 752 | + return GitlabList(self, url, query_data, **kwargs) | ||
| 739 | 753 | ||
| 740 | 754 | def http_post(self, path, query_data={}, post_data={}, **kwargs): | |
| 741 | 755 | """Make a POST request to the Gitlab server. | |
@@ -816,9 +830,10 @@ class GitlabList(object): | |||
| 816 | 830 | the API again when needed. | |
| 817 | 831 | """ | |
| 818 | 832 | ||
| 819 | - def __init__(self, gl, url, query_data, **kwargs): | ||
| 833 | + def __init__(self, gl, url, query_data, get_next=True, **kwargs): | ||
| 820 | 834 | self._gl = gl | |
| 821 | 835 | self._query(url, query_data, **kwargs) | |
| 836 | + self._get_next = get_next | ||
| 822 | 837 | ||
| 823 | 838 | def _query(self, url, query_data={}, **kwargs): | |
| 824 | 839 | result = self._gl.http_request('get', url, query_data=query_data, | |
@@ -856,7 +871,7 @@ def next(self): | |||
| 856 | 871 | self._current += 1 | |
| 857 | 872 | return item | |
| 858 | 873 | except IndexError: | |
| 859 | - if self._next_url: | ||
| 874 | + if self._next_url and self._get_next is True: | ||
| 860 | 875 | self._query(self._next_url) | |
| 861 | 876 | return self.next() | |
| 862 | 877 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,15 +71,15 @@ def list(self, **kwargs): | |||
| 71 | 71 | """Retrieve a list of objects. | |
| 72 | 72 | ||
| 73 | 73 | Args: | |
| 74 | - **kwargs: Extra options to send to the Gitlab server (e.g. sudo). | ||
| 75 | - If ``all`` is passed and set to True, the entire list of | ||
| 76 | - objects will be returned. | ||
| 74 | + all (bool): If True, return all the items, without pagination | ||
| 75 | + per_page (int): Number of items to retrieve per request | ||
| 76 | + page (int): ID of the page to return (starts with page 1) | ||
| 77 | + as_list (bool): If set to False and no pagination option is | ||
| 78 | + defined, return a generator instead of a list | ||
| 79 | + **kwargs: Extra options to send to the Gitlab server (e.g. sudo) | ||
| 77 | 80 | ||
| 78 | 81 | Returns: | |
| 79 | - RESTObjectList: Generator going through the list of objects, making | ||
| 80 | - queries to the server when required. | ||
| 81 | - If ``all=True`` is passed as argument, returns | ||
| 82 | - list(RESTObjectList). | ||
| 82 | + list: The list of objects, or a generator if `as_list` is False | ||
| 83 | 83 | ||
| 84 | 84 | Raises: | |
| 85 | 85 | GitlabAuthenticationError: If authentication is not correct | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1087,7 +1087,8 @@ def closes_issues(self, **kwargs): | |||
| 1087 | 1087 | RESTObjectList: List of issues | |
| 1088 | 1088 | """ | |
| 1089 | 1089 | path = '%s/%s/closes_issues' % (self.manager.path, self.get_id()) | |
| 1090 | - data_list = self.manager.gitlab.http_list(path, **kwargs) | ||
| 1090 | + data_list = self.manager.gitlab.http_list(path, as_list=False, | ||
| 1091 | + **kwargs) | ||
| 1091 | 1092 | manager = ProjectIssueManager(self.manager.gitlab, | |
| 1092 | 1093 | parent=self.manager._parent) | |
| 1093 | 1094 | return RESTObjectList(manager, ProjectIssue, data_list) | |
@@ -1108,7 +1109,8 @@ def commits(self, **kwargs): | |||
| 1108 | 1109 | """ | |
| 1109 | 1110 | ||
| 1110 | 1111 | path = '%s/%s/commits' % (self.manager.path, self.get_id()) | |
| 1111 | - data_list = self.manager.gitlab.http_list(path, **kwargs) | ||
| 1112 | + data_list = self.manager.gitlab.http_list(path, as_list=False, | ||
| 1113 | + **kwargs) | ||
| 1112 | 1114 | manager = ProjectCommitManager(self.manager.gitlab, | |
| 1113 | 1115 | parent=self.manager._parent) | |
| 1114 | 1116 | return RESTObjectList(manager, ProjectCommit, data_list) | |
@@ -1197,7 +1199,8 @@ def issues(self, **kwargs): | |||
| 1197 | 1199 | """ | |
| 1198 | 1200 | ||
| 1199 | 1201 | path = '%s/%s/issues' % (self.manager.path, self.get_id()) | |
| 1200 | - data_list = self.manager.gitlab.http_list(path, **kwargs) | ||
| 1202 | + data_list = self.manager.gitlab.http_list(path, as_list=False, | ||
| 1203 | + **kwargs) | ||
| 1201 | 1204 | manager = ProjectCommitManager(self.manager.gitlab, | |
| 1202 | 1205 | parent=self.manager._parent) | |
| 1203 | 1206 | # FIXME(gpocentek): the computed manager path is not correct | |
@@ -1218,7 +1221,8 @@ def merge_requests(self, **kwargs): | |||
| 1218 | 1221 | RESTObjectList: The list of merge requests | |
| 1219 | 1222 | """ | |
| 1220 | 1223 | path = '%s/%s/merge_requests' % (self.manager.path, self.get_id()) | |
| 1221 | - data_list = self.manager.gitlab.http_list(path, **kwargs) | ||
| 1224 | + data_list = self.manager.gitlab.http_list(path, as_list=False, | ||
| 1225 | + **kwargs) | ||
| 1222 | 1226 | manager = ProjectCommitManager(self.manager.gitlab, | |
| 1223 | 1227 | parent=self.manager._parent) | |
| 1224 | 1228 | # FIXME(gpocentek): the computed manager path is not correct | |
@@ -2009,6 +2013,11 @@ def all(self, scope=None, **kwargs): | |||
| 2009 | 2013 | Args: | |
| 2010 | 2014 | scope (str): The scope of runners to show, one of: specific, | |
| 2011 | 2015 | shared, active, paused, online | |
| 2016 | + all (bool): If True, return all the items, without pagination | ||
| 2017 | + per_page (int): Number of items to retrieve per request | ||
| 2018 | + page (int): ID of the page to return (starts with page 1) | ||
| 2019 | + as_list (bool): If set to False and no pagination option is | ||
| 2020 | + defined, return a generator instead of a list | ||
| 2012 | 2021 | **kwargs: Extra options to send to the server (e.g. sudo) | |
| 2013 | 2022 | ||
| 2014 | 2023 | Raises: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,7 +55,7 @@ | |||
| 55 | 55 | {'email': 'foobar@example.com', 'username': 'foobar', | |
| 56 | 56 | 'name': 'Foo Bar', 'password': 'foobar_password'}) | |
| 57 | 57 | ||
| 58 | - assert gl.users.list(search='foobar').next().id == foobar_user.id | ||
| 58 | + assert gl.users.list(search='foobar')[0].id == foobar_user.id | ||
| 59 | 59 | usercmp = lambda x,y: cmp(x.id, y.id) | |
| 60 | 60 | expected = sorted([new_user, foobar_user], cmp=usercmp) | |
| 61 | 61 | actual = sorted(list(gl.users.list(search='foo')), cmp=usercmp) | |
@@ -92,7 +92,7 @@ | |||
| 92 | 92 | group1 = gl.groups.create({'name': 'group1', 'path': 'group1'}) | |
| 93 | 93 | group2 = gl.groups.create({'name': 'group2', 'path': 'group2'}) | |
| 94 | 94 | ||
| 95 | - p_id = gl.groups.list(search='group2').next().id | ||
| 95 | + p_id = gl.groups.list(search='group2')[0].id | ||
| 96 | 96 | group3 = gl.groups.create({'name': 'group3', 'path': 'group3', 'parent_id': p_id}) | |
| 97 | 97 | ||
| 98 | 98 | assert(len(gl.groups.list()) == 3) | |
@@ -139,12 +139,11 @@ | |||
| 139 | 139 | assert(len(gl.projects.list(search="admin")) == 1) | |
| 140 | 140 | ||
| 141 | 141 | # test pagination | |
| 142 | - # FIXME => we should return lists, not RESTObjectList | ||
| 143 | - #l1 = gl.projects.list(per_page=1, page=1) | ||
| 144 | - #l2 = gl.projects.list(per_page=1, page=2) | ||
| 145 | - #assert(len(l1) == 1) | ||
| 146 | - #assert(len(l2) == 1) | ||
| 147 | - #assert(l1[0].id != l2[0].id) | ||
| 142 | + l1 = gl.projects.list(per_page=1, page=1) | ||
| 143 | + l2 = gl.projects.list(per_page=1, page=2) | ||
| 144 | + assert(len(l1) == 1) | ||
| 145 | + assert(len(l2) == 1) | ||
| 146 | + assert(l1[0].id != l2[0].id) | ||
| 148 | 147 | ||
| 149 | 148 | # project content (files) | |
| 150 | 149 | admin_project.files.create({'file_path': 'README', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments