24
24
25
25
class HTTPTokenAuth (requests .auth .AuthBase ):
26
26
"""Attaches HTTP Token Authentication to the given Request object."""
27
+
27
28
def __init__ (self , token : str ):
28
29
self .token = token
29
30
30
31
def __call__ (
31
- self , r : requests .PreparedRequest ,
32
+ self ,
33
+ r : requests .PreparedRequest ,
32
34
) -> requests .PreparedRequest :
33
35
r .headers ['Authorization' ] = self ._token_auth_str (self .token )
34
36
return r
@@ -46,8 +48,10 @@ def _get_auth(optional: bool = False) -> ty.Optional[requests.auth.AuthBase]:
46
48
return requests .auth .HTTPBasicAuth (CONF .username , CONF .password )
47
49
elif not optional :
48
50
LOG .error ('Authentication information missing' )
49
- LOG .error ('You must configure authentication via git-config or via '
50
- '--token or --username, --password' )
51
+ LOG .error (
52
+ 'You must configure authentication via git-config or via '
53
+ '--token or --username, --password'
54
+ )
51
55
sys .exit (1 )
52
56
return None
53
57
@@ -64,8 +68,10 @@ def _get_server() -> str:
64
68
65
69
if not re .match (r'.*/api/\d\.\d$' , server ):
66
70
LOG .warning ('Server version missing' )
67
- LOG .warning ('You should provide the server version in the URL '
68
- 'configured via git-config or --server' )
71
+ LOG .warning (
72
+ 'You should provide the server version in the URL '
73
+ 'configured via git-config or --server'
74
+ )
69
75
LOG .warning ('This will be required in git-pw 2.0' )
70
76
71
77
if not re .match (r'.*/api(/\d\.\d)?$' , server ):
@@ -77,8 +83,10 @@ def _get_server() -> str:
77
83
return server
78
84
else :
79
85
LOG .error ('Server information missing' )
80
- LOG .error ('You must provide server information via git-config or via '
81
- '--server' )
86
+ LOG .error (
87
+ 'You must provide server information via git-config or via '
88
+ '--server'
89
+ )
82
90
sys .exit (1 )
83
91
84
92
@@ -89,20 +97,25 @@ def _get_project() -> str:
89
97
return CONF .project .strip ()
90
98
else :
91
99
LOG .error ('Project information missing' )
92
- LOG .error ('You must provide project information via git-config or '
93
- 'via --project' )
100
+ LOG .error (
101
+ 'You must provide project information via git-config or '
102
+ 'via --project'
103
+ )
94
104
LOG .error ('To list all projects, set project to "*"' )
95
105
sys .exit (1 )
96
106
97
107
98
108
def _handle_error (
99
- operation : str , exc : requests .exceptions .RequestException ,
109
+ operation : str ,
110
+ exc : requests .exceptions .RequestException ,
100
111
) -> None :
101
112
if exc .response is not None and exc .response .content :
102
113
# server errors should always be reported
103
114
if exc .response .status_code in range (500 , 512 ): # 5xx Server Error
104
- LOG .error ('Server error. Please report this issue to '
105
- 'https://github.com/getpatchwork/patchwork' )
115
+ LOG .error (
116
+ 'Server error. Please report this issue to '
117
+ 'https://github.com/getpatchwork/patchwork'
118
+ )
106
119
raise
107
120
108
121
# we make the assumption that all responses will be JSON encoded
@@ -111,8 +124,10 @@ def _handle_error(
111
124
else :
112
125
LOG .error (exc .response .json ())
113
126
else :
114
- LOG .error ('Failed to %s resource. Is your configuration '
115
- 'correct?' % operation )
127
+ LOG .error (
128
+ 'Failed to %s resource. Is your configuration '
129
+ 'correct?' % operation
130
+ )
116
131
LOG .error ("Use the '--debug' flag for more information" )
117
132
118
133
if CONF .debug :
@@ -122,7 +137,9 @@ def _handle_error(
122
137
123
138
124
139
def _get (
125
- url : str , params : Filters = None , stream : bool = False ,
140
+ url : str ,
141
+ params : Filters = None ,
142
+ stream : bool = False ,
126
143
) -> requests .Response :
127
144
"""Make GET request and handle errors."""
128
145
LOG .debug ('GET %s' , url )
@@ -132,8 +149,12 @@ def _get(
132
149
# 'params' (namely a list of tuples) but it doesn't seem possible to
133
150
# indicate this
134
151
rsp = requests .get (
135
- url , auth = _get_auth (optional = True ), headers = _get_headers (),
136
- stream = stream , params = params ) # type: ignore
152
+ url ,
153
+ auth = _get_auth (optional = True ),
154
+ headers = _get_headers (),
155
+ stream = stream ,
156
+ params = params ,
157
+ ) # type: ignore
137
158
rsp .raise_for_status ()
138
159
except requests .exceptions .RequestException as exc :
139
160
_handle_error ('fetch' , exc )
@@ -144,14 +165,16 @@ def _get(
144
165
145
166
146
167
def _post (
147
- url : str , data : ty .List [ty .Tuple [str , ty .Any ]],
168
+ url : str ,
169
+ data : ty .List [ty .Tuple [str , ty .Any ]],
148
170
) -> requests .Response :
149
171
"""Make POST request and handle errors."""
150
172
LOG .debug ('POST %s, data=%r' , url , data )
151
173
152
174
try :
153
- rsp = requests .post (url , auth = _get_auth (), headers = _get_headers (),
154
- data = data )
175
+ rsp = requests .post (
176
+ url , auth = _get_auth (), headers = _get_headers (), data = data
177
+ )
155
178
rsp .raise_for_status ()
156
179
except requests .exceptions .RequestException as exc :
157
180
_handle_error ('create' , exc )
@@ -162,14 +185,18 @@ def _post(
162
185
163
186
164
187
def _patch (
165
- url : str , data : ty .List [ty .Tuple [str , ty .Any ]],
188
+ url : str ,
189
+ data : ty .List [ty .Tuple [str , ty .Any ]],
166
190
) -> requests .Response :
167
191
"""Make PATCH request and handle errors."""
168
192
LOG .debug ('PATCH %s, data=%r' , url , data )
169
193
170
194
try :
171
195
rsp = requests .patch (
172
- url , auth = _get_auth (), headers = _get_headers (), data = data ,
196
+ url ,
197
+ auth = _get_auth (),
198
+ headers = _get_headers (),
199
+ data = data ,
173
200
)
174
201
rsp .raise_for_status ()
175
202
except requests .exceptions .RequestException as exc :
@@ -208,7 +235,9 @@ def version() -> ty.Tuple[int, int]:
208
235
209
236
210
237
def download (
211
- url : str , params : Filters = None , output : ty .Optional [str ] = None ,
238
+ url : str ,
239
+ params : Filters = None ,
240
+ output : ty .Optional [str ] = None ,
212
241
) -> ty .Optional [str ]:
213
242
"""Retrieve a specific API resource and save it to a file/stdout.
214
243
@@ -231,7 +260,8 @@ def download(
231
260
232
261
# we don't catch anything here because we should break if these are missing
233
262
header = re .search (
234
- 'filename=(.+)' , rsp .headers .get ('content-disposition' ) or '' ,
263
+ 'filename=(.+)' ,
264
+ rsp .headers .get ('content-disposition' ) or '' ,
235
265
)
236
266
if not header :
237
267
LOG .error ('Filename was expected but was not provided in response' )
@@ -247,7 +277,8 @@ def download(
247
277
output_path = os .path .join (output , header .group (1 ))
248
278
else :
249
279
output_path = os .path .join (
250
- tempfile .mkdtemp (prefix = 'git-pw' ), header .group (1 ),
280
+ tempfile .mkdtemp (prefix = 'git-pw' ),
281
+ header .group (1 ),
251
282
)
252
283
LOG .debug ('Saving to %s' , output_path )
253
284
output_file = open (output_path , 'wb' )
@@ -312,7 +343,8 @@ def detail(
312
343
313
344
314
345
def create (
315
- resource_type : str , data : ty .List [ty .Tuple [str , ty .Any ]],
346
+ resource_type : str ,
347
+ data : ty .List [ty .Tuple [str , ty .Any ]],
316
348
) -> dict :
317
349
"""Create a new API resource.
318
350
@@ -373,9 +405,9 @@ def update(
373
405
374
406
375
407
def validate_minimum_version (
376
- min_version : ty .Tuple [int , int ], msg : str ,
408
+ min_version : ty .Tuple [int , int ],
409
+ msg : str ,
377
410
) -> ty .Callable [[ty .Any ], ty .Any ]:
378
-
379
411
def inner (f ):
380
412
@click .pass_context
381
413
def new_func (ctx , * args , ** kwargs ):
@@ -391,7 +423,6 @@ def new_func(ctx, *args, **kwargs):
391
423
392
424
393
425
def validate_multiple_filter_support (f : ty .Callable ) -> ty .Callable :
394
-
395
426
@click .pass_context
396
427
def new_func (ctx , * args , ** kwargs ):
397
428
if version () >= (1 , 1 ):
@@ -406,11 +437,13 @@ def new_func(ctx, *args, **kwargs):
406
437
407
438
value = list (kwargs [param .name ] or [])
408
439
if value and len (value ) > 1 and value != param .default :
409
- msg = ('The `--%s` filter was specified multiple times. '
410
- 'Filtering by multiple %ss is not supported with API '
411
- 'version 1.0. If the server supports it, use version '
412
- '1.1 instead. Refer to https://git.io/vN3vX for more '
413
- 'information.' )
440
+ msg = (
441
+ 'The `--%s` filter was specified multiple times. '
442
+ 'Filtering by multiple %ss is not supported with API '
443
+ 'version 1.0. If the server supports it, use version '
444
+ '1.1 instead. Refer to https://git.io/vN3vX for more '
445
+ 'information.'
446
+ )
414
447
415
448
LOG .warning (msg , param .name , param .name )
416
449
@@ -420,7 +453,9 @@ def new_func(ctx, *args, **kwargs):
420
453
421
454
422
455
def retrieve_filter_ids (
423
- resource_type : str , filter_name : str , filter_value : str ,
456
+ resource_type : str ,
457
+ filter_name : str ,
458
+ filter_value : str ,
424
459
) -> ty .List [ty .Tuple [str , str ]]:
425
460
"""Retrieve IDs for items passed through by filter.
426
461
@@ -447,11 +482,13 @@ def retrieve_filter_ids(
447
482
LOG .warning ('No matching %s found: %s' , filter_name , filter_value )
448
483
elif len (items ) > 1 and version () < (1 , 1 ):
449
484
# we don't support multiple filters in 1.0
450
- msg = ('More than one match for found for `--%s=%s`. '
451
- 'Filtering by multiple %ss is not supported with '
452
- 'API version 1.0. If the server supports it, use '
453
- 'version 1.1 instead. Refer to https://git.io/vN3vX '
454
- 'for more information.' )
485
+ msg = (
486
+ 'More than one match for found for `--%s=%s`. '
487
+ 'Filtering by multiple %ss is not supported with '
488
+ 'API version 1.0. If the server supports it, use '
489
+ 'version 1.1 instead. Refer to https://git.io/vN3vX '
490
+ 'for more information.'
491
+ )
455
492
456
493
LOG .warning (msg , filter_name , filter_value , filter_name )
457
494
0 commit comments