From fef8c7f7bc9f4a853012a5294f0731cc7f266625 Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Fri, 21 Aug 2015 19:05:20 +0200 Subject: Provide a Gitlab.from_config method It provides the Gitlab object creation from the ~/.python-gitlab.cfg, just like the CLI does. --- gitlab/config.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 gitlab/config.py (limited to 'gitlab/config.py') 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 +# +# 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 . + +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 -- cgit v1.2.1