X Tutup
Skip to content

Commit fef8c7f

Browse files
author
Gauvain Pocentek
committed
Provide a Gitlab.from_config method
It provides the Gitlab object creation from the ~/.python-gitlab.cfg, just like the CLI does.
1 parent 6cc8126 commit fef8c7f

File tree

4 files changed

+102
-60
lines changed

4 files changed

+102
-60
lines changed

gitlab/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import requests
2727
import six
2828

29+
import gitlab.config
30+
31+
2932
__title__ = 'python-gitlab'
3033
__version__ = '0.9.1'
3134
__author__ = 'Gauvain Pocentek'
@@ -155,6 +158,13 @@ def __init__(self, url, private_token=None,
155158
#: (Passed to requests-library)
156159
self.ssl_verify = ssl_verify
157160

161+
@staticmethod
162+
def from_config(gitlab_id=None, config_files=None):
163+
config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id,
164+
config_files=config_files)
165+
return Gitlab(config.url, private_token=config.token,
166+
ssl_verify=config.ssl_verify, timeout=config.timeout)
167+
158168
def auth(self):
159169
"""Performs an authentication.
160170

gitlab/cli.py

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@
2121
from __future__ import absolute_import
2222
import argparse
2323
import inspect
24-
import os
2524
import re
2625
import sys
27-
try:
28-
import ConfigParser as configparser
29-
except ImportError:
30-
import configparser
3126

3227
import gitlab
3328

@@ -129,14 +124,13 @@ def populate_sub_parser_by_class(cls, sub_parser):
129124
for arg in d['requiredAttrs']]
130125

131126

132-
def do_auth(gitlab_url, gitlab_token, ssl_verify, timeout):
127+
def do_auth(gitlab_id, config_files):
133128
try:
134-
gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token,
135-
ssl_verify=ssl_verify, timeout=timeout)
129+
gl = gitlab.Gitlab.from_config(gitlab_id, config_files)
136130
gl.auth()
137131
return gl
138132
except Exception as e:
139-
die("Could not connect to GitLab %s (%s)" % (gitlab_url, str(e)))
133+
die(str(e))
140134

141135

142136
def get_id(cls, args):
@@ -237,9 +231,6 @@ def do_project_owned(gl, what, args):
237231

238232

239233
def main():
240-
ssl_verify = True
241-
timeout = 60
242-
243234
parser = argparse.ArgumentParser(
244235
description="GitLab API Command Line Interface")
245236
parser.add_argument("-v", "--verbose", "--fancy",
@@ -276,17 +267,7 @@ def main():
276267
arg = parser.parse_args()
277268
args = arg.__dict__
278269

279-
files = arg.config_file or ['/etc/python-gitlab.cfg',
280-
os.path.expanduser('~/.python-gitlab.cfg')]
281-
# read the config
282-
config = configparser.ConfigParser()
283-
try:
284-
config.read(files)
285-
except Exception as e:
286-
print("Impossible to parse the configuration file(s): %s" %
287-
str(e))
288-
sys.exit(1)
289-
270+
config_files = arg.config_file
290271
gitlab_id = arg.gitlab
291272
verbose = arg.verbose
292273
action = arg.action
@@ -298,45 +279,13 @@ def main():
298279
args.pop("verbose")
299280
args.pop("what")
300281

301-
if gitlab_id is None:
302-
try:
303-
gitlab_id = config.get('global', 'default')
304-
except Exception:
305-
die("Impossible to get the gitlab id "
306-
"(not specified in config file)")
307-
308-
try:
309-
gitlab_url = config.get(gitlab_id, 'url')
310-
gitlab_token = config.get(gitlab_id, 'private_token')
311-
except Exception:
312-
die("Impossible to get gitlab informations from configuration "
313-
"(%s)" % gitlab_id)
314-
315-
try:
316-
ssl_verify = config.getboolean('global', 'ssl_verify')
317-
except Exception:
318-
pass
319-
try:
320-
ssl_verify = config.getboolean(gitlab_id, 'ssl_verify')
321-
except Exception:
322-
pass
323-
324-
try:
325-
timeout = config.getint('global', 'timeout')
326-
except Exception:
327-
pass
328-
try:
329-
timeout = config.getint(gitlab_id, 'timeout')
330-
except Exception:
331-
pass
332-
333282
cls = None
334283
try:
335284
cls = gitlab.__dict__[whatToCls(what)]
336285
except Exception:
337286
die("Unknown object: %s" % what)
338287

339-
gl = do_auth(gitlab_url, gitlab_token, ssl_verify, timeout)
288+
gl = do_auth(gitlab_id, config_files)
340289

341290
if action == CREATE or action == GET:
342291
o = globals()['do_%s' % action.lower()](cls, gl, what, args)

gitlab/config.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2013-2015 Gauvain Pocentek <gauvain@pocentek.net>
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Lesser General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
try:
19+
import ConfigParser as configparser
20+
except ImportError:
21+
import configparser
22+
import os
23+
24+
25+
_DEFAULT_FILES = [
26+
'/etc/python-gitlab.cfg',
27+
os.path.expanduser('~/.python-gitlab.cfg')
28+
]
29+
30+
31+
class ConfigError(Exception):
32+
pass
33+
34+
35+
class GitlabIDError(ConfigError):
36+
pass
37+
38+
39+
class GitlabDataError(ConfigError):
40+
pass
41+
42+
43+
class GitlabConfigParser(object):
44+
def __init__(self, gitlab_id=None, config_files=None):
45+
self.gitlab_id = gitlab_id
46+
_files = config_files or _DEFAULT_FILES
47+
self._config = configparser.ConfigParser()
48+
self._config.read(_files)
49+
50+
if self.gitlab_id is None:
51+
try:
52+
self.gitlab_id = self._config.get('global', 'default')
53+
except Exception:
54+
raise GitlabIDError("Impossible to get the gitlab id "
55+
"(not specified in config file)")
56+
57+
try:
58+
self.url = self._config.get(self.gitlab_id, 'url')
59+
self.token = self._config.get(self.gitlab_id, 'private_token')
60+
except Exception:
61+
raise GitlabDataError("Impossible to get gitlab informations from "
62+
"configuration (%s)" % self.gitlab_id)
63+
64+
self.ssl_verify = True
65+
try:
66+
self.ssl_verify = self._config.getboolean('global', 'ssl_verify')
67+
except Exception:
68+
pass
69+
try:
70+
self.ssl_verify = self._config.getboolean(self.gitlab_id,
71+
'ssl_verify')
72+
except Exception:
73+
pass
74+
75+
self.timeout = 60
76+
try:
77+
self.timeout = self._config.getint('global', 'timeout')
78+
except Exception:
79+
pass
80+
try:
81+
self.timeout = self._config.getint(self.gitlab_id, 'timeout')
82+
except Exception:
83+
pass

tools/functional_tests.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ echo -n "Testing project update... "
8484
$GITLAB project update --id $PROJECT_ID --description "My New Description"
8585
$OK
8686

87-
echo -n "Testing project deletion... "
88-
$GITLAB project delete --id $PROJECT_ID
89-
$OK
90-
9187
echo -n "Testing user creation... "
9288
USER_ID=$($GITLAB user create --email fake@email.com --username user1 --name "User One" --password fakepassword | grep ^id: | cut -d' ' -f2)
9389
$OK
@@ -103,3 +99,7 @@ $OK
10399
echo -n "Testing adding member to a project... "
104100
$GITLAB project-member create --project-id $PROJECT_ID --user-id $USER_ID --access-level 40 >/dev/null 2>&1
105101
$OK
102+
103+
echo -n "Testing project deletion... "
104+
$GITLAB project delete --id $PROJECT_ID
105+
$OK

0 commit comments

Comments
 (0)
X Tutup