Skip to content

Commit dc40590

Browse files
committed
lint
1 parent 9e68867 commit dc40590

File tree

3 files changed

+41
-55
lines changed

3 files changed

+41
-55
lines changed

src/cryptojwt/key_bundle.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import time
99
from datetime import datetime
1010
from functools import cmp_to_key
11-
from typing import List
12-
from typing import Optional
11+
from typing import List, Optional
1312

1413
import requests
1514

@@ -18,23 +17,19 @@
1817
from cryptojwt.jwk.okp import OKP_CRV2PUBLIC
1918
from cryptojwt.jwk.x509 import import_private_key_from_pem_file
2019

21-
from .exception import JWKException
22-
from .exception import UnknownKeyType
23-
from .exception import UnsupportedAlgorithm
24-
from .exception import UnsupportedECurve
25-
from .exception import UpdateFailed
26-
from .jwk.ec import ECKey
27-
from .jwk.ec import new_ec_key
20+
from .exception import (
21+
JWKException,
22+
UnknownKeyType,
23+
UnsupportedAlgorithm,
24+
UnsupportedECurve,
25+
UpdateFailed,
26+
)
27+
from .jwk.ec import ECKey, new_ec_key
2828
from .jwk.hmac import SYMKey
29-
from .jwk.jwk import dump_jwk
30-
from .jwk.jwk import import_jwk
31-
from .jwk.okp import OKPKey
32-
from .jwk.okp import new_okp_key
33-
from .jwk.rsa import RSAKey
34-
from .jwk.rsa import new_rsa_key
35-
from .utils import as_unicode
36-
from .utils import check_content_type
37-
from .utils import httpc_params_loader
29+
from .jwk.jwk import dump_jwk, import_jwk
30+
from .jwk.okp import OKPKey, new_okp_key
31+
from .jwk.rsa import RSAKey, new_rsa_key
32+
from .utils import as_unicode, check_content_type, httpc_params_loader
3833

3934
__author__ = "Roland Hedberg"
4035

@@ -935,15 +930,13 @@ def dump_jwks(kbl, target, private=False, symmetric_too=False):
935930
res = {"keys": keys}
936931

937932
try:
938-
_fp = open(target, "w")
933+
with open(target, "w") as fp:
934+
json.dump(res, fp)
939935
except OSError:
940936
head, _ = os.path.split(target)
941937
os.makedirs(head)
942-
_fp = open(target, "w")
943-
944-
_txt = json.dumps(res)
945-
_fp.write(_txt)
946-
_fp.close()
938+
with open(target, "w") as fp:
939+
json.dump(res, fp)
947940

948941

949942
def _set_kid(spec, bundle, kid_template, kid):

src/cryptojwt/key_issuer.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import json
22
import logging
33
import os
4-
from typing import List
5-
from typing import Optional
4+
from typing import List, Optional
65

76
from requests import request
87

98
from .jwe.utils import alg2keytype as jwe_alg2keytype
109
from .jws.utils import alg2keytype as jws_alg2keytype
11-
from .key_bundle import KeyBundle
12-
from .key_bundle import build_key_bundle
13-
from .key_bundle import key_diff
14-
from .key_bundle import update_key_bundle
15-
from .utils import httpc_params_loader
16-
from .utils import importer
17-
from .utils import qualified_name
10+
from .key_bundle import KeyBundle, build_key_bundle, key_diff, update_key_bundle
11+
from .utils import httpc_params_loader, importer, qualified_name
1812

1913
__author__ = "Roland Hedberg"
2014

@@ -586,7 +580,8 @@ def init_key_issuer(public_path="", private_path="", key_defs="", read_only=True
586580

587581
if private_path:
588582
if os.path.isfile(private_path):
589-
_jwks = open(private_path).read()
583+
with open(private_path) as fp:
584+
_jwks = fp.read()
590585
_issuer = KeyIssuer()
591586
_issuer.import_jwks(json.loads(_jwks))
592587
if key_defs:
@@ -599,31 +594,29 @@ def init_key_issuer(public_path="", private_path="", key_defs="", read_only=True
599594
else:
600595
_issuer.set([_kb])
601596
jwks = _issuer.export_jwks(private=True)
602-
fp = open(private_path, "w")
603-
fp.write(json.dumps(jwks))
604-
fp.close()
597+
with open(private_path, "w") as fp:
598+
json.dump(jwks, fp)
605599
else:
606600
_issuer = build_keyissuer(key_defs)
607601
if not read_only:
608602
jwks = _issuer.export_jwks(private=True)
609603
head, tail = os.path.split(private_path)
610604
if head and not os.path.isdir(head):
611605
os.makedirs(head)
612-
fp = open(private_path, "w")
613-
fp.write(json.dumps(jwks))
614-
fp.close()
606+
with open(private_path, "w") as fp:
607+
json.dump(jwks, fp)
615608

616609
if public_path and not read_only:
617610
jwks = _issuer.export_jwks() # public part
618611
head, tail = os.path.split(public_path)
619612
if head and not os.path.isdir(head):
620613
os.makedirs(head)
621-
fp = open(public_path, "w")
622-
fp.write(json.dumps(jwks))
623-
fp.close()
614+
with open(public_path, "w") as fp:
615+
json.dump(jwks, fp)
624616
elif public_path:
625617
if os.path.isfile(public_path):
626-
_jwks = open(public_path).read()
618+
with open(public_path) as fp:
619+
_jwks = fp.read()
627620
_issuer = KeyIssuer()
628621
_issuer.import_jwks(json.loads(_jwks))
629622
if key_defs:
@@ -636,19 +629,17 @@ def init_key_issuer(public_path="", private_path="", key_defs="", read_only=True
636629
update_key_bundle(_kb, _diff)
637630
_issuer.set([_kb])
638631
jwks = _issuer.export_jwks()
639-
fp = open(public_path, "w")
640-
fp.write(json.dumps(jwks))
641-
fp.close()
632+
with open(public_path, "w") as fp:
633+
json.dump(jwks, fp)
642634
else:
643635
_issuer = build_keyissuer(key_defs)
644636
if not read_only:
645637
_jwks = _issuer.export_jwks()
646638
head, tail = os.path.split(public_path)
647639
if head and not os.path.isdir(head):
648640
os.makedirs(head)
649-
fp = open(public_path, "w")
650-
fp.write(json.dumps(_jwks))
651-
fp.close()
641+
with open(public_path, "w") as fp:
642+
json.dump(_jwks, fp)
652643
else:
653644
_issuer = build_keyissuer(key_defs)
654645

src/cryptojwt/tools/jwtpeek.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
from cryptojwt.jwe import jwe
1414
from cryptojwt.jwk.hmac import SYMKey
1515
from cryptojwt.jwk.jwk import key_from_jwk_dict
16-
from cryptojwt.jwk.rsa import RSAKey
17-
from cryptojwt.jwk.rsa import import_rsa_key
16+
from cryptojwt.jwk.rsa import RSAKey, import_rsa_key
1817
from cryptojwt.jws import jws
1918
from cryptojwt.key_bundle import KeyBundle
2019
from cryptojwt.key_issuer import KeyIssuer
@@ -112,12 +111,14 @@ def main():
112111
keys.append(SYMKey(key=args.hmac_key, kid=_kid))
113112

114113
if args.jwk:
115-
_key = key_from_jwk_dict(open(args.jwk).read())
114+
with open(args.jwk) as fp:
115+
_key = key_from_jwk_dict(fp.read())
116116
keys.append(_key)
117117

118118
if args.jwks:
119119
_iss = KeyIssuer()
120-
_iss.import_jwks(open(args.jwks).read())
120+
with open(args.jwks) as fp:
121+
_iss.import_jwks(fp.read())
121122
keys.extend(_iss.all_keys())
122123

123124
if args.jwks_url:
@@ -128,7 +129,8 @@ def main():
128129
message = sys.stdin.read()
129130
else:
130131
if os.path.isfile(args.msg):
131-
message = open(args.msg).read().strip("\n")
132+
with open(args.msg) as fp:
133+
message = fp.read().strip("\n")
132134
else:
133135
message = args.msg
134136

0 commit comments

Comments
 (0)