Skip to content

Commit 509394d

Browse files
committed
refactor: structure python objects in a reasonable way
1 parent 7f6b6e6 commit 509394d

24 files changed

+1978
-1916
lines changed

gitlab/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@
1818

1919
from __future__ import print_function
2020
from __future__ import absolute_import
21+
from gitlab.v4 import objects
2122
import importlib
2223
import time
2324
import warnings
24-
2525
import requests
26-
2726
import gitlab.config
28-
from gitlab.const import * # noqa
29-
from gitlab.exceptions import * # noqa
30-
from gitlab import utils # noqa
3127

3228
__title__ = "python-gitlab"
3329
__version__ = "2.0.0"
@@ -110,7 +106,6 @@ def __init__(
110106

111107
self.per_page = per_page
112108

113-
objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
114109
self._objects = objects
115110

116111
self.broadcastmessages = objects.BroadcastMessageManager(self)

gitlab/mixins.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
import gitlab
19-
from gitlab import base
20-
from gitlab import cli
19+
from gitlab import base, cli, utils
2120
from gitlab import exceptions as exc
2221
from gitlab import types as g_types
23-
from gitlab import utils
2422

2523

2624
class GetMixin(object):
@@ -415,7 +413,7 @@ class AccessRequestMixin(object):
415413
("ProjectAccessRequest", "GroupAccessRequest"), tuple(), ("access_level",)
416414
)
417415
@exc.on_http_error(exc.GitlabUpdateError)
418-
def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
416+
def approve(self, access_level=gitlab.const.DEVELOPER_ACCESS, **kwargs):
419417
"""Approve an access request.
420418
421419
Args:

gitlab/v4/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from gitlab.const import * # noqa

gitlab/v4/objects/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from gitlab.v4.objects.application import * # noqa
2+
from gitlab.v4.objects.broadcast_message import * # noqa
3+
from gitlab.v4.objects.current_user import * # noqa
4+
from gitlab.v4.objects.deploy_key import * # noqa
5+
from gitlab.v4.objects.ee import * # noqa
6+
from gitlab.v4.objects.event import * # noqa
7+
from gitlab.v4.objects.feature import * # noqa
8+
from gitlab.v4.objects.group import * # noqa
9+
from gitlab.v4.objects.hook import * # noqa
10+
from gitlab.v4.objects.issue import * # noqa
11+
from gitlab.v4.objects.mergerequest import * # noqa
12+
from gitlab.v4.objects.namespace import * # noqa
13+
from gitlab.v4.objects.notification_settings import * # noqa
14+
from gitlab.v4.objects.project import * # noqa
15+
from gitlab.v4.objects.runner import * # noqa
16+
from gitlab.v4.objects.sidekiq import * # noqa
17+
from gitlab.v4.objects.snippet import * # noqa
18+
from gitlab.v4.objects.template import * # noqa
19+
from gitlab.v4.objects.todo import * # noqa
20+
from gitlab.v4.objects.user import * # noqa

gitlab/v4/objects/application.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from gitlab.base import * # noqa
2+
from gitlab.exceptions import * # noqa
3+
from gitlab.mixins import * # noqa
4+
from gitlab import types
5+
from gitlab import utils
6+
7+
8+
class ApplicationAppearance(SaveMixin, RESTObject):
9+
_id_attr = None
10+
11+
12+
class ApplicationAppearanceManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
13+
_path = "/application/appearance"
14+
_obj_cls = ApplicationAppearance
15+
_update_attrs = (
16+
tuple(),
17+
(
18+
"title",
19+
"description",
20+
"logo",
21+
"header_logo",
22+
"favicon",
23+
"new_project_guidelines",
24+
"header_message",
25+
"footer_message",
26+
"message_background_color",
27+
"message_font_color",
28+
"email_header_and_footer_enabled",
29+
),
30+
)
31+
32+
@exc.on_http_error(exc.GitlabUpdateError)
33+
def update(self, id=None, new_data=None, **kwargs):
34+
"""Update an object on the server.
35+
36+
Args:
37+
id: ID of the object to update (can be None if not required)
38+
new_data: the update data for the object
39+
**kwargs: Extra options to send to the server (e.g. sudo)
40+
41+
Returns:
42+
dict: The new object data (*not* a RESTObject)
43+
44+
Raises:
45+
GitlabAuthenticationError: If authentication is not correct
46+
GitlabUpdateError: If the server cannot perform the request
47+
"""
48+
new_data = new_data or {}
49+
data = new_data.copy()
50+
super(ApplicationAppearanceManager, self).update(id, data, **kwargs)
51+
52+
53+
class ApplicationSettings(SaveMixin, RESTObject):
54+
_id_attr = None
55+
56+
57+
class ApplicationSettingsManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
58+
_path = "/application/settings"
59+
_obj_cls = ApplicationSettings
60+
_update_attrs = (
61+
tuple(),
62+
(
63+
"id",
64+
"default_projects_limit",
65+
"signup_enabled",
66+
"password_authentication_enabled_for_web",
67+
"gravatar_enabled",
68+
"sign_in_text",
69+
"created_at",
70+
"updated_at",
71+
"home_page_url",
72+
"default_branch_protection",
73+
"restricted_visibility_levels",
74+
"max_attachment_size",
75+
"session_expire_delay",
76+
"default_project_visibility",
77+
"default_snippet_visibility",
78+
"default_group_visibility",
79+
"outbound_local_requests_whitelist",
80+
"domain_whitelist",
81+
"domain_blacklist_enabled",
82+
"domain_blacklist",
83+
"external_authorization_service_enabled",
84+
"external_authorization_service_url",
85+
"external_authorization_service_default_label",
86+
"external_authorization_service_timeout",
87+
"user_oauth_applications",
88+
"after_sign_out_path",
89+
"container_registry_token_expire_delay",
90+
"repository_storages",
91+
"plantuml_enabled",
92+
"plantuml_url",
93+
"terminal_max_session_time",
94+
"polling_interval_multiplier",
95+
"rsa_key_restriction",
96+
"dsa_key_restriction",
97+
"ecdsa_key_restriction",
98+
"ed25519_key_restriction",
99+
"first_day_of_week",
100+
"enforce_terms",
101+
"terms",
102+
"performance_bar_allowed_group_id",
103+
"instance_statistics_visibility_private",
104+
"user_show_add_ssh_key_message",
105+
"file_template_project_id",
106+
"local_markdown_version",
107+
"asset_proxy_enabled",
108+
"asset_proxy_url",
109+
"asset_proxy_whitelist",
110+
"geo_node_allowed_ips",
111+
"allow_local_requests_from_hooks_and_services",
112+
"allow_local_requests_from_web_hooks_and_services",
113+
"allow_local_requests_from_system_hooks",
114+
),
115+
)
116+
117+
@exc.on_http_error(exc.GitlabUpdateError)
118+
def update(self, id=None, new_data=None, **kwargs):
119+
"""Update an object on the server.
120+
121+
Args:
122+
id: ID of the object to update (can be None if not required)
123+
new_data: the update data for the object
124+
**kwargs: Extra options to send to the server (e.g. sudo)
125+
126+
Returns:
127+
dict: The new object data (*not* a RESTObject)
128+
129+
Raises:
130+
GitlabAuthenticationError: If authentication is not correct
131+
GitlabUpdateError: If the server cannot perform the request
132+
"""
133+
new_data = new_data or {}
134+
data = new_data.copy()
135+
if "domain_whitelist" in data and data["domain_whitelist"] is None:
136+
data.pop("domain_whitelist")
137+
super(ApplicationSettingsManager, self).update(id, data, **kwargs)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from gitlab.base import * # noqa
2+
from gitlab.exceptions import * # noqa
3+
from gitlab.mixins import * # noqa
4+
from gitlab import types
5+
from gitlab import utils
6+
7+
8+
class BroadcastMessage(SaveMixin, ObjectDeleteMixin, RESTObject):
9+
pass
10+
11+
12+
class BroadcastMessageManager(CRUDMixin, RESTManager):
13+
_path = "/broadcast_messages"
14+
_obj_cls = BroadcastMessage
15+
16+
_create_attrs = (("message",), ("starts_at", "ends_at", "color", "font"))
17+
_update_attrs = (tuple(), ("message", "starts_at", "ends_at", "color", "font"))

gitlab/v4/objects/current_user.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from gitlab.base import * # noqa
2+
from gitlab.exceptions import * # noqa
3+
from gitlab.mixins import * # noqa
4+
from gitlab import types
5+
from gitlab import utils
6+
7+
8+
class CurrentUserEmail(ObjectDeleteMixin, RESTObject):
9+
_short_print_attr = "email"
10+
11+
12+
class CurrentUserEmailManager(RetrieveMixin, CreateMixin, DeleteMixin, RESTManager):
13+
_path = "/user/emails"
14+
_obj_cls = CurrentUserEmail
15+
_create_attrs = (("email",), tuple())
16+
17+
18+
class CurrentUserGPGKey(ObjectDeleteMixin, RESTObject):
19+
pass
20+
21+
22+
class CurrentUserGPGKeyManager(RetrieveMixin, CreateMixin, DeleteMixin, RESTManager):
23+
_path = "/user/gpg_keys"
24+
_obj_cls = CurrentUserGPGKey
25+
_create_attrs = (("key",), tuple())
26+
27+
28+
class CurrentUserKey(ObjectDeleteMixin, RESTObject):
29+
_short_print_attr = "title"
30+
31+
32+
class CurrentUserKeyManager(RetrieveMixin, CreateMixin, DeleteMixin, RESTManager):
33+
_path = "/user/keys"
34+
_obj_cls = CurrentUserKey
35+
_create_attrs = (("title", "key"), tuple())
36+
37+
38+
class CurrentUserStatus(SaveMixin, RESTObject):
39+
_id_attr = None
40+
_short_print_attr = "message"
41+
42+
43+
class CurrentUserStatusManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
44+
_path = "/user/status"
45+
_obj_cls = CurrentUserStatus
46+
_update_attrs = (tuple(), ("emoji", "message"))
47+
48+
49+
class CurrentUser(RESTObject):
50+
_id_attr = None
51+
_short_print_attr = "username"
52+
_managers = (
53+
("status", "CurrentUserStatusManager"),
54+
("emails", "CurrentUserEmailManager"),
55+
("gpgkeys", "CurrentUserGPGKeyManager"),
56+
("keys", "CurrentUserKeyManager"),
57+
)
58+
59+
60+
class CurrentUserManager(GetWithoutIdMixin, RESTManager):
61+
_path = "/user"
62+
_obj_cls = CurrentUser

gitlab/v4/objects/deploy_key.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from gitlab.base import * # noqa
2+
from gitlab.exceptions import * # noqa
3+
from gitlab.mixins import * # noqa
4+
from gitlab import types
5+
from gitlab import utils
6+
7+
8+
class DeployKey(RESTObject):
9+
pass
10+
11+
12+
class DeployKeyManager(ListMixin, RESTManager):
13+
_path = "/deploy_keys"
14+
_obj_cls = DeployKey

0 commit comments

Comments
 (0)