summaryrefslogtreecommitdiff
path: root/gitlab/config.py
blob: d5e87b670a742d02aad5368190619bb9bbd2dd25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013-2017 Gauvain Pocentek <gauvain@pocentek.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os

from six.moves import configparser

_DEFAULT_FILES = [
    '/etc/python-gitlab.cfg',
    os.path.expanduser('~/.python-gitlab.cfg')
]


class ConfigError(Exception):
    pass


class GitlabIDError(ConfigError):
    pass


class GitlabDataError(ConfigError):
    pass


class GitlabConfigParser(object):
    def __init__(self, gitlab_id=None, config_files=None):
        self.gitlab_id = gitlab_id
        _files = config_files or _DEFAULT_FILES
        self._config = configparser.ConfigParser()
        self._config.read(_files)

        if self.gitlab_id is None:
            try:
                self.gitlab_id = self._config.get('global', 'default')
            except Exception:
                raise GitlabIDError("Impossible to get the gitlab id "
                                    "(not specified in config file)")

        try:
            self.url = self._config.get(self.gitlab_id, 'url')
            self.token = self._config.get(self.gitlab_id, 'private_token')
        except Exception:
            raise GitlabDataError("Impossible to get gitlab informations from "
                                  "configuration (%s)" % self.gitlab_id)

        self.ssl_verify = True
        try:
            self.ssl_verify = self._config.getboolean('global', 'ssl_verify')
        except Exception:
            pass
        try:
            self.ssl_verify = self._config.getboolean(self.gitlab_id,
                                                      'ssl_verify')
        except Exception:
            pass

        self.timeout = 60
        try:
            self.timeout = self._config.getint('global', 'timeout')
        except Exception:
            pass
        try:
            self.timeout = self._config.getint(self.gitlab_id, 'timeout')
        except Exception:
            pass

        self.http_username = None
        self.http_password = None
        try:
            self.http_username = self._config.get(self.gitlab_id,
                                                  'http_username')
            self.http_password = self._config.get(self.gitlab_id,
                                                  'http_password')
        except Exception:
            pass

        self.api_version = '3'
        try:
            self.api_version = self._config.get(self.gitlab_id, 'api_version')
        except Exception:
            pass
        if self.api_version not in ('3', '4'):
            raise GitlabDataError("Unsupported API version: %s" %
                                  self.api_version)