File indexing completed on 2024-05-12 05:43:36

0001 """
0002 Base class for creating connections to all known instances
0003 """
0004 
0005 # SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
0006 #
0007 # SPDX-License-Identifier: GPL-2.0-or-later
0008 
0009 import sys
0010 
0011 from typing import List, Optional
0012 
0013 from gitlab import Gitlab
0014 from gitlab.exceptions import GitlabAuthenticationError
0015 
0016 from lab.config import Config
0017 from lab.utils import Utils, LogType
0018 
0019 
0020 class AllInstancesConnection:  # pylint: disable=too-few-public-methods
0021     """
0022     Base class that connects to all known instances
0023     """
0024 
0025     # protected
0026     _connections: List[Gitlab] = []
0027 
0028     # private
0029     __config: Config
0030 
0031     def __login(self, hostname: str, token: str) -> None:
0032         try:
0033             connection: Gitlab = Gitlab(hostname, private_token=token)
0034             connection.auth()
0035             self._connections.append(connection)
0036         except GitlabAuthenticationError:
0037             Utils.log(LogType.ERROR, "Could not log into GitLab")
0038             sys.exit(1)
0039 
0040     def __init__(self) -> None:
0041         self.__config = Config()
0042         instances = self.__config.instances()
0043 
0044         for hostname in instances:
0045             token: Optional[str] = self.__config.token(hostname)
0046 
0047             if isinstance(token, str):
0048                 self.__login("https://" + hostname, token)