Skip to content

Commit defc7e6

Browse files
committed
Blackify code
Run code through black. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent caa9b11 commit defc7e6

15 files changed

+894
-375
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ repos:
1212
- id: end-of-file-fixer
1313
- id: flake8
1414
- id: trailing-whitespace
15+
- repo: https://github.com/psf/black
16+
rev: 22.1.0
17+
hooks:
18+
- id: black

git_pw/api.py

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
class HTTPTokenAuth(requests.auth.AuthBase):
2626
"""Attaches HTTP Token Authentication to the given Request object."""
27+
2728
def __init__(self, token: str):
2829
self.token = token
2930

3031
def __call__(
31-
self, r: requests.PreparedRequest,
32+
self,
33+
r: requests.PreparedRequest,
3234
) -> requests.PreparedRequest:
3335
r.headers['Authorization'] = self._token_auth_str(self.token)
3436
return r
@@ -46,8 +48,10 @@ def _get_auth(optional: bool = False) -> ty.Optional[requests.auth.AuthBase]:
4648
return requests.auth.HTTPBasicAuth(CONF.username, CONF.password)
4749
elif not optional:
4850
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+
)
5155
sys.exit(1)
5256
return None
5357

@@ -64,8 +68,10 @@ def _get_server() -> str:
6468

6569
if not re.match(r'.*/api/\d\.\d$', server):
6670
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+
)
6975
LOG.warning('This will be required in git-pw 2.0')
7076

7177
if not re.match(r'.*/api(/\d\.\d)?$', server):
@@ -77,8 +83,10 @@ def _get_server() -> str:
7783
return server
7884
else:
7985
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+
)
8290
sys.exit(1)
8391

8492

@@ -89,20 +97,25 @@ def _get_project() -> str:
8997
return CONF.project.strip()
9098
else:
9199
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+
)
94104
LOG.error('To list all projects, set project to "*"')
95105
sys.exit(1)
96106

97107

98108
def _handle_error(
99-
operation: str, exc: requests.exceptions.RequestException,
109+
operation: str,
110+
exc: requests.exceptions.RequestException,
100111
) -> None:
101112
if exc.response is not None and exc.response.content:
102113
# server errors should always be reported
103114
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+
)
106119
raise
107120

108121
# we make the assumption that all responses will be JSON encoded
@@ -111,8 +124,10 @@ def _handle_error(
111124
else:
112125
LOG.error(exc.response.json())
113126
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+
)
116131
LOG.error("Use the '--debug' flag for more information")
117132

118133
if CONF.debug:
@@ -122,7 +137,9 @@ def _handle_error(
122137

123138

124139
def _get(
125-
url: str, params: Filters = None, stream: bool = False,
140+
url: str,
141+
params: Filters = None,
142+
stream: bool = False,
126143
) -> requests.Response:
127144
"""Make GET request and handle errors."""
128145
LOG.debug('GET %s', url)
@@ -132,8 +149,12 @@ def _get(
132149
# 'params' (namely a list of tuples) but it doesn't seem possible to
133150
# indicate this
134151
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
137158
rsp.raise_for_status()
138159
except requests.exceptions.RequestException as exc:
139160
_handle_error('fetch', exc)
@@ -144,14 +165,16 @@ def _get(
144165

145166

146167
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]],
148170
) -> requests.Response:
149171
"""Make POST request and handle errors."""
150172
LOG.debug('POST %s, data=%r', url, data)
151173

152174
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+
)
155178
rsp.raise_for_status()
156179
except requests.exceptions.RequestException as exc:
157180
_handle_error('create', exc)
@@ -162,14 +185,18 @@ def _post(
162185

163186

164187
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]],
166190
) -> requests.Response:
167191
"""Make PATCH request and handle errors."""
168192
LOG.debug('PATCH %s, data=%r', url, data)
169193

170194
try:
171195
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,
173200
)
174201
rsp.raise_for_status()
175202
except requests.exceptions.RequestException as exc:
@@ -208,7 +235,9 @@ def version() -> ty.Tuple[int, int]:
208235

209236

210237
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,
212241
) -> ty.Optional[str]:
213242
"""Retrieve a specific API resource and save it to a file/stdout.
214243
@@ -231,7 +260,8 @@ def download(
231260

232261
# we don't catch anything here because we should break if these are missing
233262
header = re.search(
234-
'filename=(.+)', rsp.headers.get('content-disposition') or '',
263+
'filename=(.+)',
264+
rsp.headers.get('content-disposition') or '',
235265
)
236266
if not header:
237267
LOG.error('Filename was expected but was not provided in response')
@@ -247,7 +277,8 @@ def download(
247277
output_path = os.path.join(output, header.group(1))
248278
else:
249279
output_path = os.path.join(
250-
tempfile.mkdtemp(prefix='git-pw'), header.group(1),
280+
tempfile.mkdtemp(prefix='git-pw'),
281+
header.group(1),
251282
)
252283
LOG.debug('Saving to %s', output_path)
253284
output_file = open(output_path, 'wb')
@@ -312,7 +343,8 @@ def detail(
312343

313344

314345
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]],
316348
) -> dict:
317349
"""Create a new API resource.
318350
@@ -373,9 +405,9 @@ def update(
373405

374406

375407
def validate_minimum_version(
376-
min_version: ty.Tuple[int, int], msg: str,
408+
min_version: ty.Tuple[int, int],
409+
msg: str,
377410
) -> ty.Callable[[ty.Any], ty.Any]:
378-
379411
def inner(f):
380412
@click.pass_context
381413
def new_func(ctx, *args, **kwargs):
@@ -391,7 +423,6 @@ def new_func(ctx, *args, **kwargs):
391423

392424

393425
def validate_multiple_filter_support(f: ty.Callable) -> ty.Callable:
394-
395426
@click.pass_context
396427
def new_func(ctx, *args, **kwargs):
397428
if version() >= (1, 1):
@@ -406,11 +437,13 @@ def new_func(ctx, *args, **kwargs):
406437

407438
value = list(kwargs[param.name] or [])
408439
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+
)
414447

415448
LOG.warning(msg, param.name, param.name)
416449

@@ -420,7 +453,9 @@ def new_func(ctx, *args, **kwargs):
420453

421454

422455
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,
424459
) -> ty.List[ty.Tuple[str, str]]:
425460
"""Retrieve IDs for items passed through by filter.
426461
@@ -447,11 +482,13 @@ def retrieve_filter_ids(
447482
LOG.warning('No matching %s found: %s', filter_name, filter_value)
448483
elif len(items) > 1 and version() < (1, 1):
449484
# 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+
)
455492

456493
LOG.warning(msg, filter_name, filter_value, filter_name)
457494

0 commit comments

Comments
 (0)