diff options
author | Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> | 2015-08-21 19:05:20 +0200 |
---|---|---|
committer | Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> | 2015-08-21 19:07:21 +0200 |
commit | fef8c7f7bc9f4a853012a5294f0731cc7f266625 (patch) | |
tree | c83317455a0f96e597f21c77712d5966443a4607 /gitlab/config.py | |
parent | 6cc8126381d0d241aeaca69d9932f0b425538f4f (diff) | |
download | gitlab-fef8c7f7bc9f4a853012a5294f0731cc7f266625.tar.gz |
Provide a Gitlab.from_config method
It provides the Gitlab object creation from the ~/.python-gitlab.cfg,
just like the CLI does.
Diffstat (limited to 'gitlab/config.py')
-rw-r--r-- | gitlab/config.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/gitlab/config.py b/gitlab/config.py new file mode 100644 index 0000000..c9dc5aa --- /dev/null +++ b/gitlab/config.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2013-2015 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/>. + +try: + import ConfigParser as configparser +except ImportError: + import configparser +import os + + +_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 |