Skip to content

Commit 09ef68f

Browse files
author
Gauvain Pocentek
committed
minor syntax/pep8 updates
1 parent 4f001b4 commit 09ef68f

File tree

1 file changed

+60
-73
lines changed

1 file changed

+60
-73
lines changed

gitlab.py

+60-73
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class GitlabAuthenticationError(Exception):
7575

7676
class Gitlab(object):
7777
"""Represents a GitLab server connection"""
78-
def __init__(self, url, private_token=None, email=None, password=None, ssl_verify=True):
78+
def __init__(self, url, private_token=None,
79+
email=None, password=None, ssl_verify=True):
7980
"""Stores informations about the server
8081
8182
url: the URL of the Gitlab server
@@ -122,12 +123,8 @@ def setUrl(self, url):
122123

123124
def setToken(self, token):
124125
"""Sets the private token for authentication"""
125-
if token:
126-
self.private_token = token
127-
self.headers = {"PRIVATE-TOKEN": token}
128-
else:
129-
self.private_token = None
130-
self.headers = {}
126+
self.private_token = token if token else None
127+
self.headers = {"PRIVATE-TOKEN": token} if token else None
131128

132129
def setCredentials(self, email, password):
133130
"""Sets the email/login and password for authentication"""
@@ -137,52 +134,48 @@ def setCredentials(self, email, password):
137134
def rawGet(self, path):
138135
url = '%s%s' % (self._url, path)
139136
try:
140-
r = requests.get(url, headers=self.headers, verify=self.ssl_verify)
137+
return requests.get(url,
138+
headers=self.headers,
139+
verify=self.ssl_verify)
141140
except:
142141
raise GitlabConnectionError(
143142
"Can't connect to GitLab server (%s)" % self._url)
144143

145-
return r
146-
147144
def rawPost(self, path, data):
148145
url = '%s%s' % (self._url, path)
149146
try:
150-
r = requests.post(url, data,
151-
headers=self.headers,
152-
verify=self.ssl_verify)
147+
return requests.post(url, data,
148+
headers=self.headers,
149+
verify=self.ssl_verify)
153150
except:
154151
raise GitlabConnectionError(
155152
"Can't connect to GitLab server (%s)" % self._url)
156153

157-
return r
158-
159154
def rawPut(self, path):
160155
url = '%s%s' % (self._url, path)
161156

162157
try:
163-
r = requests.put(url, headers=self.headers, verify=self.ssl_verify)
158+
return requests.put(url,
159+
headers=self.headers,
160+
verify=self.ssl_verify)
164161
except:
165162
raise GitlabConnectionError(
166163
"Can't connect to GitLab server (%s)" % self._url)
167164

168-
return r
169-
170165
def list(self, obj_class, **kwargs):
171166
missing = []
172167
for k in obj_class.requiredListAttrs:
173168
if k not in kwargs:
174-
missing.append (k)
169+
missing.append(k)
175170
if missing:
176-
raise GitlabListError('Missing attribute(s): %s' % \
177-
", ".join(missing))
171+
raise GitlabListError('Missing attribute(s): %s' %
172+
", ".join(missing))
178173

179-
url = obj_class._url
180-
if kwargs:
181-
url = obj_class._url % kwargs
174+
url = obj_class._url % kwargs
182175
url = '%s%s' % (self._url, url)
183176
if kwargs:
184177
url += "?%s" % ("&".join(
185-
["%s=%s" % (k, v) for k, v in kwargs.items()]))
178+
["%s=%s" % (k, v) for k, v in kwargs.items()]))
186179

187180
try:
188181
r = requests.get(url, headers=self.headers, verify=self.ssl_verify)
@@ -211,24 +204,16 @@ def get(self, obj_class, id=None, **kwargs):
211204
missing = []
212205
for k in obj_class.requiredGetAttrs:
213206
if k not in kwargs:
214-
missing.append (k)
207+
missing.append(k)
215208
if missing:
216-
raise GitlabListError('Missing attribute(s): %s' % \
217-
", ".join(missing))
209+
raise GitlabListError('Missing attribute(s): %s' %
210+
", ".join(missing))
218211

219-
url = obj_class._url
220-
if kwargs:
221-
url = obj_class._url % kwargs
212+
url = obj_class._url % kwargs
222213
if id is not None:
223-
try:
224-
url = '%s%s/%d' % \
225-
(self._url, url, id)
226-
except TypeError: # id might be a str (ProjectBranch)
227-
url = '%s%s/%s' % \
228-
(self._url, url, id)
214+
url = '%s%s/%s' % (self._url, url, str(id))
229215
else:
230-
url = '%s%s' % \
231-
(self._url, url)
216+
url = '%s%s' % (self._url, url)
232217

233218
try:
234219
r = requests.get(url, headers=self.headers, verify=self.ssl_verify)
@@ -247,11 +232,12 @@ def get(self, obj_class, id=None, **kwargs):
247232

248233
def delete(self, obj):
249234
url = obj._url % obj.__dict__
250-
url = '%s%s/%d' % \
251-
(self._url, url, obj.id)
235+
url = '%s%s/%s' % (self._url, url, str(obj.id))
252236

253237
try:
254-
r = requests.delete(url, headers=self.headers, verify=self.ssl_verify)
238+
r = requests.delete(url,
239+
headers=self.headers,
240+
verify=self.ssl_verify)
255241
except:
256242
raise GitlabConnectionError(
257243
"Can't connect to GitLab server (%s)" % self._url)
@@ -268,18 +254,18 @@ def create(self, obj):
268254
missing = []
269255
for k in obj.requiredCreateAttrs:
270256
if k not in obj.__dict__:
271-
missing.append (k)
257+
missing.append(k)
272258
if missing:
273-
raise GitlabCreateError('Missing attribute(s): %s' % \
274-
", ".join(missing))
259+
raise GitlabCreateError('Missing attribute(s): %s' %
260+
", ".join(missing))
275261

276262
url = obj._url % obj.__dict__
277263
url = '%s%s' % (self._url, url)
278264

279265
try:
280-
# TODO: avoid too much work on the server side by filtering the
281-
# __dict__ keys
282-
r = requests.post(url, obj.__dict__, headers=self.headers, verify=self.ssl_verify)
266+
r = requests.post(url, obj.__dict__,
267+
headers=self.headers,
268+
verify=self.ssl_verify)
283269
except:
284270
raise GitlabConnectionError(
285271
"Can't connect to GitLab server (%s)" % self._url)
@@ -293,8 +279,7 @@ def create(self, obj):
293279

294280
def update(self, obj):
295281
url = obj._url % obj.__dict__
296-
url = '%s%s/%d' % \
297-
(self._url, url, obj.id)
282+
url = '%s%s/%s' % (self._url, url, str(obj.id))
298283

299284
# build a dict of data that can really be sent to server
300285
d = {}
@@ -305,7 +290,9 @@ def update(self, obj):
305290
d[k] = str(v.encode(sys.stdout.encoding, "replace"))
306291

307292
try:
308-
r = requests.put(url, d, headers=self.headers, verify=self.ssl_verify)
293+
r = requests.put(url, d,
294+
headers=self.headers,
295+
verify=self.ssl_verify)
309296
except:
310297
raise GitlabConnectionError(
311298
"Can't connect to GitLab server (%s)" % self._url)
@@ -427,17 +414,16 @@ def _getListOrObject(self, cls, id, **kwargs):
427414
if id is None:
428415
if not cls.canList:
429416
raise GitlabGetError
430-
431417
return cls.list(self.gitlab, **kwargs)
418+
432419
elif isinstance(id, dict):
433420
if not cls.canCreate:
434421
raise GitlabCreateError
435-
436422
return cls(self.gitlab, id, **kwargs)
423+
437424
else:
438425
if not cls.canGet:
439426
raise GitlabGetError
440-
441427
return cls(self.gitlab, id, **kwargs)
442428

443429
def _getObject(self, k, v):
@@ -511,18 +497,20 @@ def short_print(self, depth=0):
511497
id = self.__dict__[self.idAttr]
512498
print("%s%s: %s" % (" " * depth * 2, self.idAttr, id))
513499
if self.shortPrintAttr:
514-
print ("%s%s: %s" % (" " * depth * 2,
515-
self.shortPrintAttr.replace('_', '-'),
516-
self.__dict__[self.shortPrintAttr]))
500+
print("%s%s: %s" % (" " * depth * 2,
501+
self.shortPrintAttr.replace('_', '-'),
502+
self.__dict__[self.shortPrintAttr]))
517503

518504
@staticmethod
519505
def _obj_to_str(obj):
520506
if isinstance(obj, dict):
521-
s = ", ".join(["%s: %s" % (x, GitlabObject._obj_to_str(y)) for (x, y) in obj.items()])
507+
s = ", ".join(["%s: %s" %
508+
(x, GitlabObject._obj_to_str(y))
509+
for (x, y) in obj.items()])
522510
return "{ %s }" % s
523511
elif isinstance(obj, list):
524512
s = ", ".join([GitlabObject._obj_to_str(x) for x in obj])
525-
return "[ %s ]" %s
513+
return "[ %s ]" % s
526514
elif isinstance(obj, unicode):
527515
return obj.encode(sys.stdout.encoding, "replace")
528516
else:
@@ -535,7 +523,8 @@ def pretty_print(self, depth=0):
535523
if k == self.idAttr:
536524
continue
537525
v = self.__dict__[k]
538-
pretty_k = k.replace('_', '-').encode(sys.stdout.encoding, "replace")
526+
pretty_k = k.replace('_', '-').encode(sys.stdout.encoding,
527+
"replace")
539528
if isinstance(v, GitlabObject):
540529
if depth == 0:
541530
print("%s:" % pretty_k)
@@ -589,8 +578,7 @@ class Group(GitlabObject):
589578
shortPrintAttr = 'name'
590579

591580
def transfer_project(self, id):
592-
url = '/groups/%d/projects/%d' % \
593-
(self.id, id)
581+
url = '/groups/%d/projects/%d' % (self.id, id)
594582
r = self.gitlab.rawPost(url, None)
595583
if r.status_code != 201:
596584
raise GitlabTransferProjectError()
@@ -625,10 +613,8 @@ class ProjectBranch(GitlabObject):
625613

626614
def protect(self, protect=True):
627615
url = self._url % {'project_id': self.project_id}
628-
if protect:
629-
url = "%s/%s/protect" % (url, self.name)
630-
else:
631-
url = "%s/%s/unprotect" % (url, self.name)
616+
action = 'protect' if protect else 'unprotect'
617+
url = "%s/%s/%s" % (url, self.name, action)
632618
r = self.gitlab.rawPut(url)
633619

634620
if r.status_code == 200:
@@ -653,22 +639,22 @@ class ProjectCommit(GitlabObject):
653639

654640
def diff(self):
655641
url = '/projects/%(project_id)s/repository/commits/%(commit_id)s/diff' % \
656-
{'project_id': self.project_id, 'commit_id': self.id}
642+
{'project_id': self.project_id, 'commit_id': self.id}
657643
r = self.gitlab.rawGet(url)
658644
if r.status_code == 200:
659645
return r.json()
660646

661-
raise GitlabGetError()
647+
raise GitlabGetError
662648

663649
def blob(self, filepath):
664650
url = '/projects/%(project_id)s/repository/blobs/%(commit_id)s' % \
665-
{'project_id': self.project_id, 'commit_id': self.id}
651+
{'project_id': self.project_id, 'commit_id': self.id}
666652
url += '?filepath=%s' % filepath
667653
r = self.gitlab.rawGet(url)
668654
if r.status_code == 200:
669655
return r.content
670656

671-
raise GitlabGetError()
657+
raise GitlabGetError
672658

673659

674660
class ProjectKey(GitlabObject):
@@ -762,7 +748,8 @@ class ProjectMergeRequest(GitlabObject):
762748
canDelete = False
763749
requiredListAttrs = ['project_id']
764750
requiredGetAttrs = ['project_id']
765-
requiredCreateAttrs = ['project_id', 'source_branch', 'target_branch', 'title']
751+
requiredCreateAttrs = ['project_id', 'source_branch',
752+
'target_branch', 'title']
766753
optionalCreateAttrs = ['assignee_id']
767754

768755
def Note(self, id=None, **kwargs):
@@ -891,7 +878,7 @@ def tree(self, path='', ref_name=''):
891878
if r.status_code == 200:
892879
return r.json()
893880

894-
raise GitlabGetError()
881+
raise GitlabGetError
895882

896883
def blob(self, sha, filepath):
897884
url = "%s/%s/repository/blobs/%s" % (self._url, self.id, sha)
@@ -900,7 +887,7 @@ def blob(self, sha, filepath):
900887
if r.status_code == 200:
901888
return r.content
902889

903-
raise GitlabGetError()
890+
raise GitlabGetError
904891

905892

906893
class TeamMember(GitlabObject):

0 commit comments

Comments
 (0)