Skip to content

Commit 79fc702

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent ab8b37b commit 79fc702

File tree

4 files changed

+28
-49
lines changed

4 files changed

+28
-49
lines changed

hooks/python/configuring-your-server/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
@app.route("/payload", methods=['POST'])
1111
def payload():
12-
print('I got some JSON: {}'.format(request.json))
12+
print(f'I got some JSON: {request.json}')
1313
return 'ok'

hooks/python/flask-github-webhooks/hooks/example

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ if __name__ == '__main__':
4949
action = payload['action']
5050
repo = payload['repository']['full_name']
5151
if action == 'deleted':
52-
create_github_issue('%s was deleted' % repo, 'Seems we\'ve got ourselves a bit of an issue here.\n\n@<user>',
53-
['deleted'])
52+
create_github_issue(
53+
f'{repo} was deleted',
54+
'Seems we\'ve got ourselves a bit of an issue here.\n\n@<user>',
55+
['deleted'],
56+
)
5457

55-
outfile = '/tmp/webhook-{}.log'.format(repo)
58+
outfile = f'/tmp/webhook-{repo}.log'
5659
with open(outfile, 'w') as f:
5760
f.write(json.dumps(payload))

hooks/python/flask-github-webhooks/webhooks.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@ def index():
3434
hooks = config.get('hooks_path', join(path, 'hooks'))
3535
# Allow Github IPs only
3636
if config.get('github_ips_only', True):
37-
src_ip = ip_address(u'{}'.format(request.remote_addr))
37+
src_ip = ip_address(f'{request.remote_addr}')
3838
whitelist = requests.get('https://api.github.com/meta').json()['hooks']
3939
for valid_ip in whitelist:
4040
if src_ip in ip_network(valid_ip):
4141
break
4242
else:
4343
abort(403, "Unable to validate source IP")
4444

45-
# Enforce secret, so not just anybody can trigger these hooks
46-
secret = config.get('enforce_secret', '')
47-
if secret:
45+
if secret := config.get('enforce_secret', ''):
4846
# Only SHA1 is supported
4947
header_signature = request.headers.get('X-Hub-Signature')
5048
if header_signature is None:
@@ -61,12 +59,8 @@ def index():
6159
if hexversion >= 0x020707F0:
6260
if not hmac.compare_digest(str(mac.hexdigest()), str(signature)):
6361
abort(403)
64-
else:
65-
# What compare_digest provides is protection against timing
66-
# attacks; we can live without this protection for a web-based
67-
# application
68-
if not str(mac.hexdigest()) == str(signature):
69-
abort(403)
62+
elif str(mac.hexdigest()) != str(signature):
63+
abort(403)
7064

7165
# Implement ping
7266
event = request.headers.get('X-GitHub-Event', 'ping')
@@ -114,10 +108,10 @@ def index():
114108
'branch': branch,
115109
'event': event
116110
}
117-
logging.info('Metadata:\n{}'.format(dumps(meta)))
111+
logging.info(f'Metadata:\n{dumps(meta)}')
118112
# Skip push-delete
119113
if event == 'push' and payload['deleted']:
120-
logging.info('Skipping push-delete event for {}'.format(dumps(meta)))
114+
logging.info(f'Skipping push-delete event for {dumps(meta)}')
121115
return dumps({'status': 'skipped'})
122116

123117
# Possible hooks
@@ -126,9 +120,7 @@ def index():
126120
scripts.append(join(hooks, '{event}-{name}-{branch}'.format(**meta)))
127121
if name:
128122
scripts.append(join(hooks, '{event}-{name}'.format(**meta)))
129-
scripts.append(join(hooks, '{event}'.format(**meta)))
130-
scripts.append(join(hooks, 'all'))
131-
123+
scripts.extend((join(hooks, '{event}'.format(**meta)), join(hooks, 'all')))
132124
# Check permissions
133125
scripts = [s for s in scripts if isfile(s) and access(s, X_OK)]
134126
if not scripts:
@@ -154,9 +146,7 @@ def index():
154146
}
155147
# Log errors if a hook failed
156148
if proc.returncode != 0:
157-
logging.error('{} : {} \n{}'.format(
158-
s, proc.returncode, stderr
159-
))
149+
logging.error(f'{s} : {proc.returncode} \n{stderr}')
160150

161151
# Remove temporal file
162152
remove(tmpfile)

scripts/git-find-lfs-extensions

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ import os
2828
import sys
2929

3030
# Threshold that defines a large file
31-
if len(sys.argv) > 1:
32-
THRESHOLD_IN_MB = float(sys.argv[1]) / 1024
33-
else:
34-
THRESHOLD_IN_MB = 0.5
35-
31+
THRESHOLD_IN_MB = float(sys.argv[1]) / 1024 if len(sys.argv) > 1 else 0.5
3632
CWD = os.getcwd()
3733
CHUNKSIZE = 1024
3834
MAX_TYPE_LEN = len("Type")
@@ -77,38 +73,28 @@ def add_file(ext, type, size_mb):
7773
if size_mb > THRESHOLD_IN_MB:
7874
result[ext]['count_large'] = result[ext]['count_large'] + 1
7975
result[ext]['size_large'] = result[ext]['size_large'] + size_mb
80-
if not 'max' in result[ext] or size_mb > result[ext]['max']:
76+
if 'max' not in result[ext] or size_mb > result[ext]['max']:
8177
result[ext]['max'] = size_mb
82-
if not 'min' in result[ext] or size_mb < result[ext]['min']:
78+
if 'min' not in result[ext] or size_mb < result[ext]['min']:
8379
result[ext]['min'] = size_mb
8480

8581
def print_line(type, ext, share_large, count_large, count_all, size_all, min, max):
86-
print('{}{}{}{}{}{}{}{}'.format(
87-
type.ljust(3+MAX_TYPE_LEN),
88-
ext.ljust(3+MAX_EXT_LEN),
89-
str(share_large).rjust(10),
90-
str(count_large).rjust(10),
91-
str(count_all).rjust(10),
92-
str(size_all).rjust(10),
93-
str(min).rjust(10),
94-
str(max).rjust(10)
95-
))
82+
print(
83+
f'{type.ljust(3 + MAX_TYPE_LEN)}{ext.ljust(3 + MAX_EXT_LEN)}{str(share_large).rjust(10)}{str(count_large).rjust(10)}{str(count_all).rjust(10)}{str(size_all).rjust(10)}{str(min).rjust(10)}{str(max).rjust(10)}'
84+
)
9685

9786
for root, dirs, files in os.walk(CWD):
9887
for basename in files:
9988
filename = os.path.join(root, basename)
10089
try:
10190
size_mb = float(os.path.getsize(filename)) / 1024 / 1024
10291
if not filename.startswith(os.path.join(CWD, '.git')) and size_mb > 0:
103-
if is_binary(filename):
104-
file_type = "binary"
105-
else:
106-
file_type = "text"
92+
file_type = "binary" if is_binary(filename) else "text"
10793
ext = os.path.basename(filename)
10894
add_file('*', 'all', size_mb)
10995
if ext.find('.') == -1:
11096
# files w/o extension
111-
add_file(ext, file_type + " w/o ext", size_mb)
97+
add_file(ext, f"{file_type} w/o ext", size_mb)
11298
else:
11399
while ext.find('.') >= 0:
114100
ext = ext[ext.find('.')+1:]
@@ -128,17 +114,17 @@ for ext in sorted(result, key=lambda x: (result[x]['type'], -result[x]['size_lar
128114
print_line(
129115
result[ext]['type'],
130116
ext,
131-
str(round(large_share)) + ' %',
117+
f'{str(round(large_share))} %',
132118
result[ext]['count_large'],
133119
result[ext]['count_all'],
134120
int(result[ext]['size_all']),
135121
int(result[ext]['min']),
136-
int(result[ext]['max'])
122+
int(result[ext]['max']),
137123
)
138124

139125
print("\nAdd to .gitattributes:\n")
140126
for ext in sorted(result, key=lambda x: (result[x]['type'], x)):
141127
if len(ext) > 0 and result[ext]['type'] == "binary" and result[ext]['count_large'] > 0:
142-
print('*.{} filter=lfs diff=lfs merge=lfs -text'.format(
143-
"".join("[" + c.upper() + c.lower() + "]" if (('a' <= c <= 'z') or ('A' <= c <= 'Z')) else c for c in ext)
144-
))
128+
print(
129+
f"""*.{"".join(f"[{c.upper()}{c.lower()}]" if 'a' <= c <= 'z' or 'A' <= c <= 'Z' else c for c in ext)} filter=lfs diff=lfs merge=lfs -text"""
130+
)

0 commit comments

Comments
 (0)