Skip to content

Commit 05a7856

Browse files
Enable the add-trailing-comma pre-commit fixer (#661)
Co-authored-by: Abhinav Singh <[email protected]>
1 parent d8bd694 commit 05a7856

File tree

89 files changed

+2293
-1330
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+2293
-1330
lines changed

.pre-commit-config.yaml

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
---
22
repos:
3-
# - repo: https://github.com/asottile/add-trailing-comma.git
4-
# rev: v2.0.1
5-
# hooks:
6-
# - id: add-trailing-comma
3+
- repo: https://github.com/asottile/add-trailing-comma.git
4+
rev: v2.0.1
5+
hooks:
6+
- id: add-trailing-comma
7+
args:
8+
- --py36-plus
79

810
# - repo: https://github.com/timothycrosley/isort.git
911
# rev: 5.4.2

examples/https_connect_tunnel.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@
2323
class HttpsConnectTunnelHandler(BaseTcpTunnelHandler):
2424
"""A https CONNECT tunnel."""
2525

26-
PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT = memoryview(build_http_response(
27-
httpStatusCodes.OK,
28-
reason=b'Connection established'
29-
))
30-
31-
PROXY_TUNNEL_UNSUPPORTED_SCHEME = memoryview(build_http_response(
32-
httpStatusCodes.BAD_REQUEST,
33-
headers={b'Connection': b'close'},
34-
reason=b'Unsupported protocol scheme'
35-
))
26+
PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT = memoryview(
27+
build_http_response(
28+
httpStatusCodes.OK,
29+
reason=b'Connection established',
30+
),
31+
)
32+
33+
PROXY_TUNNEL_UNSUPPORTED_SCHEME = memoryview(
34+
build_http_response(
35+
httpStatusCodes.BAD_REQUEST,
36+
headers={b'Connection': b'close'},
37+
reason=b'Unsupported protocol scheme',
38+
),
39+
)
3640

3741
def __init__(self, *args: Any, **kwargs: Any) -> None:
3842
super().__init__(*args, **kwargs)
@@ -49,7 +53,8 @@ def handle_data(self, data: memoryview) -> Optional[bool]:
4953
# Drop the request if not a CONNECT request
5054
if self.request.method != httpMethods.CONNECT:
5155
self.client.queue(
52-
HttpsConnectTunnelHandler.PROXY_TUNNEL_UNSUPPORTED_SCHEME)
56+
HttpsConnectTunnelHandler.PROXY_TUNNEL_UNSUPPORTED_SCHEME,
57+
)
5358
return True
5459

5560
# CONNECT requests are short and we need not worry about
@@ -61,7 +66,8 @@ def handle_data(self, data: memoryview) -> Optional[bool]:
6166

6267
# Queue tunnel established response to client
6368
self.client.queue(
64-
HttpsConnectTunnelHandler.PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT)
69+
HttpsConnectTunnelHandler.PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT,
70+
)
6571

6672
return None
6773

@@ -70,7 +76,8 @@ def main() -> None:
7076
# This example requires `threadless=True`
7177
pool = AcceptorPool(
7278
flags=Proxy.initialize(port=12345, num_workers=1, threadless=True),
73-
work_klass=HttpsConnectTunnelHandler)
79+
work_klass=HttpsConnectTunnelHandler,
80+
)
7481
try:
7582
pool.setup()
7683
while True:

examples/pubsub_eventing.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@
2525
num_events_received = [0, 0]
2626

2727

28-
def publisher_process(shutdown_event: multiprocessing.synchronize.Event,
29-
dispatcher_queue: EventQueue) -> None:
28+
def publisher_process(
29+
shutdown_event: multiprocessing.synchronize.Event,
30+
dispatcher_queue: EventQueue,
31+
) -> None:
3032
print('publisher starting')
3133
try:
3234
while not shutdown_event.is_set():
3335
dispatcher_queue.publish(
3436
request_id=process_publisher_request_id,
3537
event_name=eventNames.WORK_STARTED,
3638
event_payload={'time': time.time()},
37-
publisher_id='eventing_pubsub_process'
39+
publisher_id='eventing_pubsub_process',
3840
)
3941
except KeyboardInterrupt:
4042
pass
@@ -70,7 +72,8 @@ def on_event(payload: Dict[str, Any]) -> None:
7072
publisher_shutdown_event = multiprocessing.Event()
7173
publisher = multiprocessing.Process(
7274
target=publisher_process, args=(
73-
publisher_shutdown_event, event_manager.event_queue, ))
75+
publisher_shutdown_event, event_manager.event_queue, ),
76+
)
7477
publisher.start()
7578

7679
try:
@@ -80,7 +83,7 @@ def on_event(payload: Dict[str, Any]) -> None:
8083
request_id=main_publisher_request_id,
8184
event_name=eventNames.WORK_STARTED,
8285
event_payload={'time': time.time()},
83-
publisher_id='eventing_pubsub_main'
86+
publisher_id='eventing_pubsub_main',
8487
)
8588
except KeyboardInterrupt:
8689
print('bye!!!')
@@ -92,5 +95,8 @@ def on_event(payload: Dict[str, Any]) -> None:
9295
subscriber.unsubscribe()
9396
# Signal dispatcher to shutdown
9497
event_manager.stop_event_dispatcher()
95-
print('Received {0} events from main thread, {1} events from another process, in {2} seconds'.format(
96-
num_events_received[0], num_events_received[1], time.time() - start_time))
98+
print(
99+
'Received {0} events from main thread, {1} events from another process, in {2} seconds'.format(
100+
num_events_received[0], num_events_received[1], time.time() - start_time,
101+
),
102+
)

examples/ssl_echo_server.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ def initialize(self) -> None:
2929
conn = wrap_socket(
3030
self.client.connection,
3131
self.flags.keyfile,
32-
self.flags.certfile)
32+
self.flags.certfile,
33+
)
3334
conn.setblocking(False)
3435
# Upgrade plain TcpClientConnection to SSL connection object
3536
self.client = TcpClientConnection(
36-
conn=conn, addr=self.client.addr)
37+
conn=conn, addr=self.client.addr,
38+
)
3739

3840
def handle_data(self, data: memoryview) -> Optional[bool]:
3941
# echo back to client
@@ -49,8 +51,10 @@ def main() -> None:
4951
num_workers=1,
5052
threadless=True,
5153
keyfile='https-key.pem',
52-
certfile='https-signed-cert.pem'),
53-
work_klass=EchoSSLServerHandler)
54+
certfile='https-signed-cert.pem',
55+
),
56+
work_klass=EchoSSLServerHandler,
57+
)
5458
try:
5559
pool.setup()
5660
while True:

examples/tcp_echo_server.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def main() -> None:
3232
# This example requires `threadless=True`
3333
pool = AcceptorPool(
3434
flags=Proxy.initialize(port=12345, num_workers=1, threadless=True),
35-
work_klass=EchoServerHandler)
35+
work_klass=EchoServerHandler,
36+
)
3637
try:
3738
pool.setup()
3839
while True:

examples/websocket_client.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
def on_message(frame: WebsocketFrame) -> None:
2323
"""WebsocketClient on_message callback."""
2424
global client, num_echos, last_dispatch_time
25-
print('Received %r after %d millisec' %
26-
(frame.data, (time.time() - last_dispatch_time) * 1000))
27-
assert(frame.data == b'hello' and frame.opcode ==
28-
websocketOpcodes.TEXT_FRAME)
25+
print(
26+
'Received %r after %d millisec' %
27+
(frame.data, (time.time() - last_dispatch_time) * 1000),
28+
)
29+
assert(
30+
frame.data == b'hello' and frame.opcode ==
31+
websocketOpcodes.TEXT_FRAME
32+
)
2933
if num_echos > 0:
3034
client.queue(static_frame)
3135
last_dispatch_time = time.time()
@@ -40,7 +44,8 @@ def on_message(frame: WebsocketFrame) -> None:
4044
b'echo.websocket.org',
4145
80,
4246
b'/',
43-
on_message=on_message)
47+
on_message=on_message,
48+
)
4449
# Perform handshake
4550
client.handshake()
4651
# Queue some data for client

proxy/common/flag.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self) -> None:
3636
self.actions: List[str] = []
3737
self.parser = argparse.ArgumentParser(
3838
description='proxy.py v%s' % __version__,
39-
epilog='Proxy.py not working? Report at: %s/issues/new' % __homepage__
39+
epilog='Proxy.py not working? Report at: %s/issues/new' % __homepage__,
4040
)
4141

4242
def add_argument(self, *args: Any, **kwargs: Any) -> argparse.Action:
@@ -46,7 +46,8 @@ def add_argument(self, *args: Any, **kwargs: Any) -> argparse.Action:
4646
return action
4747

4848
def parse_args(
49-
self, input_args: Optional[List[str]]) -> argparse.Namespace:
49+
self, input_args: Optional[List[str]],
50+
) -> argparse.Namespace:
5051
"""Parse flags from input arguments."""
5152
self.args = self.parser.parse_args(input_args)
5253
return self.args

proxy/common/pki.py

+39-23
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ def remove_passphrase(
5757
key_in_path: str,
5858
password: str,
5959
key_out_path: str,
60-
timeout: int = 10) -> bool:
60+
timeout: int = 10,
61+
) -> bool:
6162
"""Remove passphrase from a private key."""
6263
command = [
6364
'openssl', 'rsa',
6465
'-passin', 'pass:%s' % password,
6566
'-in', key_in_path,
66-
'-out', key_out_path
67+
'-out', key_out_path,
6768
]
6869
return run_openssl_command(command, timeout)
6970

@@ -72,12 +73,13 @@ def gen_private_key(
7273
key_path: str,
7374
password: str,
7475
bits: int = 2048,
75-
timeout: int = 10) -> bool:
76+
timeout: int = 10,
77+
) -> bool:
7678
"""Generates a private key."""
7779
command = [
7880
'openssl', 'genrsa', '-aes256',
7981
'-passout', 'pass:%s' % password,
80-
'-out', key_path, str(bits)
82+
'-out', key_path, str(bits),
8183
]
8284
return run_openssl_command(command, timeout)
8385

@@ -90,15 +92,16 @@ def gen_public_key(
9092
alt_subj_names: Optional[List[str]] = None,
9193
extended_key_usage: Optional[str] = None,
9294
validity_in_days: int = 365,
93-
timeout: int = 10) -> bool:
95+
timeout: int = 10,
96+
) -> bool:
9497
"""For a given private key, generates a corresponding public key."""
9598
with ssl_config(alt_subj_names, extended_key_usage) as (config_path, has_extension):
9699
command = [
97100
'openssl', 'req', '-new', '-x509', '-sha256',
98101
'-days', str(validity_in_days), '-subj', subject,
99102
'-passin', 'pass:%s' % private_key_password,
100103
'-config', config_path,
101-
'-key', private_key_path, '-out', public_key_path
104+
'-key', private_key_path, '-out', public_key_path,
102105
]
103106
if has_extension:
104107
command.extend([
@@ -112,13 +115,14 @@ def gen_csr(
112115
key_path: str,
113116
password: str,
114117
crt_path: str,
115-
timeout: int = 10) -> bool:
118+
timeout: int = 10,
119+
) -> bool:
116120
"""Generates a CSR based upon existing certificate and key file."""
117121
command = [
118122
'openssl', 'x509', '-x509toreq',
119123
'-passin', 'pass:%s' % password,
120124
'-in', crt_path, '-signkey', key_path,
121-
'-out', csr_path
125+
'-out', csr_path,
122126
]
123127
return run_openssl_command(command, timeout)
124128

@@ -133,7 +137,8 @@ def sign_csr(
133137
alt_subj_names: Optional[List[str]] = None,
134138
extended_key_usage: Optional[str] = None,
135139
validity_in_days: int = 365,
136-
timeout: int = 10) -> bool:
140+
timeout: int = 10,
141+
) -> bool:
137142
"""Sign a CSR using CA key and certificate."""
138143
with ext_file(alt_subj_names, extended_key_usage) as extension_path:
139144
command = [
@@ -152,7 +157,8 @@ def sign_csr(
152157

153158
def get_ext_config(
154159
alt_subj_names: Optional[List[str]] = None,
155-
extended_key_usage: Optional[str] = None) -> bytes:
160+
extended_key_usage: Optional[str] = None,
161+
) -> bytes:
156162
config = b''
157163
# Add SAN extension
158164
if alt_subj_names is not None and len(alt_subj_names) > 0:
@@ -169,12 +175,14 @@ def get_ext_config(
169175
@contextlib.contextmanager
170176
def ext_file(
171177
alt_subj_names: Optional[List[str]] = None,
172-
extended_key_usage: Optional[str] = None) -> Generator[str, None, None]:
178+
extended_key_usage: Optional[str] = None,
179+
) -> Generator[str, None, None]:
173180
# Write config to temp file
174181
config_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex)
175182
with open(config_path, 'wb') as cnf:
176183
cnf.write(
177-
get_ext_config(alt_subj_names, extended_key_usage))
184+
get_ext_config(alt_subj_names, extended_key_usage),
185+
)
178186

179187
yield config_path
180188

@@ -185,7 +193,8 @@ def ext_file(
185193
@contextlib.contextmanager
186194
def ssl_config(
187195
alt_subj_names: Optional[List[str]] = None,
188-
extended_key_usage: Optional[str] = None) -> Generator[Tuple[str, bool], None, None]:
196+
extended_key_usage: Optional[str] = None,
197+
) -> Generator[Tuple[str, bool], None, None]:
189198
config = DEFAULT_CONFIG
190199

191200
has_extension = False
@@ -212,7 +221,7 @@ def run_openssl_command(command: List[str], timeout: int) -> bool:
212221
cmd = subprocess.Popen(
213222
command,
214223
stdout=subprocess.PIPE,
215-
stderr=subprocess.PIPE
224+
stderr=subprocess.PIPE,
216225
)
217226
cmd.communicate(timeout=timeout)
218227
return cmd.returncode == 0
@@ -221,7 +230,7 @@ def run_openssl_command(command: List[str], timeout: int) -> bool:
221230
if __name__ == '__main__':
222231
available_actions = (
223232
'remove_passphrase', 'gen_private_key', 'gen_public_key',
224-
'gen_csr', 'sign_csr'
233+
'gen_csr', 'sign_csr',
225234
)
226235

227236
parser = argparse.ArgumentParser(
@@ -231,7 +240,7 @@ def run_openssl_command(command: List[str], timeout: int) -> bool:
231240
'action',
232241
type=str,
233242
default=None,
234-
help='Valid actions: ' + ', '.join(available_actions)
243+
help='Valid actions: ' + ', '.join(available_actions),
235244
)
236245
parser.add_argument(
237246
'--password',
@@ -294,17 +303,24 @@ def run_openssl_command(command: List[str], timeout: int) -> bool:
294303
if args.action == 'gen_private_key':
295304
gen_private_key(args.private_key_path, args.password)
296305
elif args.action == 'gen_public_key':
297-
gen_public_key(args.public_key_path, args.private_key_path,
298-
args.password, args.subject)
306+
gen_public_key(
307+
args.public_key_path, args.private_key_path,
308+
args.password, args.subject,
309+
)
299310
elif args.action == 'remove_passphrase':
300-
remove_passphrase(args.private_key_path, args.password,
301-
args.private_key_path)
311+
remove_passphrase(
312+
args.private_key_path, args.password,
313+
args.private_key_path,
314+
)
302315
elif args.action == 'gen_csr':
303316
gen_csr(
304317
args.csr_path,
305318
args.private_key_path,
306319
args.password,
307-
args.public_key_path)
320+
args.public_key_path,
321+
)
308322
elif args.action == 'sign_csr':
309-
sign_csr(args.csr_path, args.crt_path, args.private_key_path, args.password,
310-
args.public_key_path, str(int(time.time())), alt_subj_names=[args.hostname, ])
323+
sign_csr(
324+
args.csr_path, args.crt_path, args.private_key_path, args.password,
325+
args.public_key_path, str(int(time.time())), alt_subj_names=[args.hostname],
326+
)

0 commit comments

Comments
 (0)