Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit09ef68f

Browse files
author
Gauvain Pocentek
committed
minor syntax/pep8 updates
1 parent4f001b4 commit09ef68f

File tree

1 file changed

+60
-73
lines changed

1 file changed

+60
-73
lines changed

‎gitlab.py

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

7676
classGitlab(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
defsetToken(self,token):
124125
"""Sets the private token for authentication"""
125-
iftoken:
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=tokeniftokenelseNone
127+
self.headers= {"PRIVATE-TOKEN":token}iftokenelseNone
131128

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

145-
returnr
146-
147144
defrawPost(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+
returnrequests.post(url,data,
148+
headers=self.headers,
149+
verify=self.ssl_verify)
153150
except:
154151
raiseGitlabConnectionError(
155152
"Can't connect to GitLab server (%s)"%self._url)
156153

157-
returnr
158-
159154
defrawPut(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+
returnrequests.put(url,
159+
headers=self.headers,
160+
verify=self.ssl_verify)
164161
except:
165162
raiseGitlabConnectionError(
166163
"Can't connect to GitLab server (%s)"%self._url)
167164

168-
returnr
169-
170165
deflist(self,obj_class,**kwargs):
171166
missing= []
172167
forkinobj_class.requiredListAttrs:
173168
ifknotinkwargs:
174-
missing.append(k)
169+
missing.append(k)
175170
ifmissing:
176-
raiseGitlabListError('Missing attribute(s): %s'% \
177-
", ".join(missing))
171+
raiseGitlabListError('Missing attribute(s): %s'%
172+
", ".join(missing))
178173

179-
url=obj_class._url
180-
ifkwargs:
181-
url=obj_class._url%kwargs
174+
url=obj_class._url%kwargs
182175
url='%s%s'% (self._url,url)
183176
ifkwargs:
184177
url+="?%s"% ("&".join(
185-
["%s=%s"% (k,v)fork,vinkwargs.items()]))
178+
["%s=%s"% (k,v)fork,vinkwargs.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
forkinobj_class.requiredGetAttrs:
213206
ifknotinkwargs:
214-
missing.append(k)
207+
missing.append(k)
215208
ifmissing:
216-
raiseGitlabListError('Missing attribute(s): %s'% \
217-
", ".join(missing))
209+
raiseGitlabListError('Missing attribute(s): %s'%
210+
", ".join(missing))
218211

219-
url=obj_class._url
220-
ifkwargs:
221-
url=obj_class._url%kwargs
212+
url=obj_class._url%kwargs
222213
ifidisnotNone:
223-
try:
224-
url='%s%s/%d'% \
225-
(self._url,url,id)
226-
exceptTypeError:# 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
defdelete(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
raiseGitlabConnectionError(
257243
"Can't connect to GitLab server (%s)"%self._url)
@@ -268,18 +254,18 @@ def create(self, obj):
268254
missing= []
269255
forkinobj.requiredCreateAttrs:
270256
ifknotinobj.__dict__:
271-
missing.append(k)
257+
missing.append(k)
272258
ifmissing:
273-
raiseGitlabCreateError('Missing attribute(s): %s'% \
274-
", ".join(missing))
259+
raiseGitlabCreateError('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
raiseGitlabConnectionError(
285271
"Can't connect to GitLab server (%s)"%self._url)
@@ -293,8 +279,7 @@ def create(self, obj):
293279

294280
defupdate(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
raiseGitlabConnectionError(
311298
"Can't connect to GitLab server (%s)"%self._url)
@@ -427,17 +414,16 @@ def _getListOrObject(self, cls, id, **kwargs):
427414
ifidisNone:
428415
ifnotcls.canList:
429416
raiseGitlabGetError
430-
431417
returncls.list(self.gitlab,**kwargs)
418+
432419
elifisinstance(id,dict):
433420
ifnotcls.canCreate:
434421
raiseGitlabCreateError
435-
436422
returncls(self.gitlab,id,**kwargs)
423+
437424
else:
438425
ifnotcls.canGet:
439426
raiseGitlabGetError
440-
441427
returncls(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
ifself.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
ifisinstance(obj,dict):
521-
s=", ".join(["%s: %s"% (x,GitlabObject._obj_to_str(y))for (x,y)inobj.items()])
507+
s=", ".join(["%s: %s"%
508+
(x,GitlabObject._obj_to_str(y))
509+
for (x,y)inobj.items()])
522510
return"{ %s }"%s
523511
elifisinstance(obj,list):
524512
s=", ".join([GitlabObject._obj_to_str(x)forxinobj])
525-
return"[ %s ]"%s
513+
return"[ %s ]"%s
526514
elifisinstance(obj,unicode):
527515
returnobj.encode(sys.stdout.encoding,"replace")
528516
else:
@@ -535,7 +523,8 @@ def pretty_print(self, depth=0):
535523
ifk==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
ifisinstance(v,GitlabObject):
540529
ifdepth==0:
541530
print("%s:"%pretty_k)
@@ -589,8 +578,7 @@ class Group(GitlabObject):
589578
shortPrintAttr='name'
590579

591580
deftransfer_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
ifr.status_code!=201:
596584
raiseGitlabTransferProjectError()
@@ -625,10 +613,8 @@ class ProjectBranch(GitlabObject):
625613

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

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

654640
defdiff(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
ifr.status_code==200:
659645
returnr.json()
660646

661-
raiseGitlabGetError()
647+
raiseGitlabGetError
662648

663649
defblob(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
ifr.status_code==200:
669655
returnr.content
670656

671-
raiseGitlabGetError()
657+
raiseGitlabGetError
672658

673659

674660
classProjectKey(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
defNote(self,id=None,**kwargs):
@@ -891,7 +878,7 @@ def tree(self, path='', ref_name=''):
891878
ifr.status_code==200:
892879
returnr.json()
893880

894-
raiseGitlabGetError()
881+
raiseGitlabGetError
895882

896883
defblob(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
ifr.status_code==200:
901888
returnr.content
902889

903-
raiseGitlabGetError()
890+
raiseGitlabGetError
904891

905892

906893
classTeamMember(GitlabObject):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp