Coverage for src/gitlabracadabra/gitlab/connections.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-10 17:02 +0100

1# 

2# Copyright (C) 2019-2025 Mathieu Parent <math.parent@gmail.com> 

3# 

4# This program is free software: you can redistribute it and/or modify 

5# it under the terms of the GNU Lesser General Public License as published by 

6# the Free Software Foundation, either version 3 of the License, or 

7# (at your option) any later version. 

8# 

9# This program is distributed in the hope that it will be useful, 

10# but WITHOUT ANY WARRANTY; without even the implied warranty of 

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12# GNU Lesser General Public License for more details. 

13# 

14# You should have received a copy of the GNU Lesser General Public License 

15# along with this program. If not, see <http://www.gnu.org/licenses/>. 

16 

17 

18from gitlabracadabra.gitlab.connection import GitlabConnection 

19from gitlabracadabra.singleton import SingletonMeta 

20 

21 

22class GitlabConnections(metaclass=SingletonMeta): 

23 """All GitLab connections by id.""" 

24 

25 def __init__(self) -> None: 

26 """All connected GitLabs. 

27 

28 Intented to be used as a singleton. 

29 """ 

30 self._default_id: str | None = None 

31 self._config_files: list[str] | None = None 

32 self._debug: bool = False 

33 self._connections: dict[str | None, GitlabConnection] = {} 

34 

35 def load(self, default_id: str | None, config_files: list[str] | None, *, debug: bool) -> None: 

36 """Load configuration. 

37 

38 Args: 

39 default_id: Default gitlab id. 

40 config_files: None or list of configuration files. 

41 debug: True to enable debugging. 

42 """ 

43 self._default_id = default_id 

44 self._config_files = config_files 

45 self._debug = debug 

46 self._connections = {} 

47 

48 def get_connection(self, gitlab_id: str | None = None, *, auth: bool = True) -> GitlabConnection: 

49 """Get a GitLab connection. 

50 

51 Args: 

52 gitlab_id: Section in python-gitlab config files. 

53 auth: True to authenticate on creation. 

54 

55 Returns: 

56 A GitLab connection. 

57 """ 

58 if gitlab_id is None: 

59 gitlab_id = self._default_id 

60 if self._connections.get(gitlab_id) is None: 

61 self._connections[gitlab_id] = GitlabConnection(gitlab_id, self._config_files, debug=self._debug, auth=auth) 

62 return self._connections[gitlab_id]