Skip to content

Commit 74eccec

Browse files
committed
perf: ⚡️ flake8 code style
1 parent 52341e6 commit 74eccec

File tree

17 files changed

+116
-100
lines changed

17 files changed

+116
-100
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ jobs:
3030
run: |
3131
pip install --upgrade pip
3232
pip install setuptools==57.5.0
33-
pip install flake8 pytest
33+
pip install -r dev-requirement.txt
3434
pip install .
3535
3636
- name: Lint with flake8
3737
run: |
38-
# stop the build if there are Python syntax errors or undefined names
39-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
40-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
41-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
38+
# Run flake8 on Python files
39+
flake8 pyencrypt --count --show-source --max-complexity=10 --statistics
40+
# Run flake8 on test files
41+
flake8 tests --count --show-source --max-complexity=10 --statistics --ignore F401,E501
42+
4243
- name: Test with pytest
4344
run: |
4445
pytest

dev-requirement.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
isort==5.10.1
33
yapf==0.31.0
44
pytest==6.2.5
5-
ipython==7.16.3
5+
ipython==7.16.3
6+
flake8==4.0.1

pyencrypt/aes.py

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ class AES(object):
642642
0x5d80be9f, 0x548db591, 0x4f9aa883, 0x4697a38d
643643
]
644644

645-
def __init__(self, key):
645+
def __init__(self, key): # noqa: C901
646646

647647
if len(key) not in (16, 24, 32):
648648
raise ValueError('Invalid key size')
@@ -675,10 +675,14 @@ def __init__(self, key):
675675
while t < round_key_count:
676676

677677
tt = tk[KC - 1]
678-
tk[0] ^= ((self.S[(tt >> 16) & 0xFF] << 24) ^
679-
(self.S[(tt >> 8) & 0xFF] << 16) ^
680-
(self.S[tt & 0xFF] << 8) ^ self.S[(tt >> 24) & 0xFF] ^
681-
(self.rcon[rconpointer] << 24))
678+
# noqa: W504
679+
tk[0] ^= (
680+
(self.S[(tt >> 16) & 0xFF] << 24) ^
681+
(self.S[(tt >> 8) & 0xFF] << 16) ^
682+
(self.S[tt & 0xFF] << 8) ^
683+
self.S[(tt >> 24) & 0xFF] ^
684+
(self.rcon[rconpointer] << 24)
685+
)
682686
rconpointer += 1
683687

684688
if KC != 8:
@@ -691,10 +695,12 @@ def __init__(self, key):
691695
tk[i] ^= tk[i - 1]
692696
tt = tk[KC // 2 - 1]
693697

694-
tk[KC //
695-
2] ^= (self.S[tt & 0xFF] ^ (self.S[(tt >> 8) & 0xFF] << 8) ^
696-
(self.S[(tt >> 16) & 0xFF] << 16) ^
697-
(self.S[(tt >> 24) & 0xFF] << 24))
698+
tk[KC // 2] ^= (
699+
self.S[tt & 0xFF] ^
700+
(self.S[(tt >> 8) & 0xFF] << 8) ^
701+
(self.S[(tt >> 16) & 0xFF] << 16) ^
702+
(self.S[(tt >> 24) & 0xFF] << 24)
703+
)
698704

699705
for i in range(KC // 2 + 1, KC):
700706
tk[i] ^= tk[i - 1]
@@ -711,10 +717,12 @@ def __init__(self, key):
711717
for r in range(1, rounds):
712718
for j in range(0, 4):
713719
tt = self._Kd[r][j]
714-
self._Kd[r][j] = (self.U1[(tt >> 24) & 0xFF]
715-
^ self.U2[(tt >> 16) & 0xFF]
716-
^ self.U3[(tt >> 8) & 0xFF]
717-
^ self.U4[tt & 0xFF])
720+
self._Kd[r][j] = (
721+
self.U1[(tt >> 24) & 0xFF] ^
722+
self.U2[(tt >> 16) & 0xFF] ^
723+
self.U3[(tt >> 8) & 0xFF] ^
724+
self.U4[tt & 0xFF]
725+
)
718726

719727
def encrypt(self, plaintext):
720728
'Encrypt a block of plain text using the AES block cipher.'
@@ -733,21 +741,22 @@ def encrypt(self, plaintext):
733741
# Apply round transforms
734742
for r in range(1, rounds):
735743
for i in range(0, 4):
736-
a[i] = (self.T1[(t[i] >> 24) & 0xFF]
737-
^ self.T2[(t[(i + s1) % 4] >> 16) & 0xFF]
738-
^ self.T3[(t[(i + s2) % 4] >> 8) & 0xFF]
739-
^ self.T4[t[(i + s3) % 4] & 0xFF] ^ self._Ke[r][i])
744+
a[i] = (
745+
self.T1[(t[i] >> 24) & 0xFF] ^
746+
self.T2[(t[(i + s1) % 4] >> 16) & 0xFF] ^
747+
self.T3[(t[(i + s2) % 4] >> 8) & 0xFF] ^
748+
self.T4[t[(i + s3) % 4] & 0xFF] ^
749+
self._Ke[r][i]
750+
)
740751
t = copy.copy(a)
741752

742753
# The last round is special
743754
result = []
744755
for i in range(0, 4):
745756
tt = self._Ke[rounds][i]
746757
result.append((self.S[(t[i] >> 24) & 0xFF] ^ (tt >> 24)) & 0xFF)
747-
result.append((self.S[(t[(i + s1) % 4] >> 16) & 0xFF] ^ (tt >> 16))
748-
& 0xFF)
749-
result.append((self.S[(t[(i + s2) % 4] >> 8) & 0xFF] ^ (tt >> 8))
750-
& 0xFF)
758+
result.append((self.S[(t[(i + s1) % 4] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF)
759+
result.append((self.S[(t[(i + s2) % 4] >> 8) & 0xFF] ^ (tt >> 8)) & 0xFF)
751760
result.append((self.S[t[(i + s3) % 4] & 0xFF] ^ tt) & 0xFF)
752761

753762
return result
@@ -769,28 +778,29 @@ def decrypt(self, ciphertext):
769778
# Apply round transforms
770779
for r in range(1, rounds):
771780
for i in range(0, 4):
772-
a[i] = (self.T5[(t[i] >> 24) & 0xFF]
773-
^ self.T6[(t[(i + s1) % 4] >> 16) & 0xFF]
774-
^ self.T7[(t[(i + s2) % 4] >> 8) & 0xFF]
775-
^ self.T8[t[(i + s3) % 4] & 0xFF] ^ self._Kd[r][i])
781+
a[i] = (
782+
self.T5[(t[i] >> 24) & 0xFF] ^
783+
self.T6[(t[(i + s1) % 4] >> 16) & 0xFF] ^
784+
self.T7[(t[(i + s2) % 4] >> 8) & 0xFF] ^
785+
self.T8[t[(i + s3) % 4] & 0xFF] ^ self._Kd[r][i]
786+
)
776787
t = copy.copy(a)
777788

778789
# The last round is special
779790
result = []
780791
for i in range(0, 4):
781792
tt = self._Kd[rounds][i]
782793
result.append((self.Si[(t[i] >> 24) & 0xFF] ^ (tt >> 24)) & 0xFF)
783-
result.append((self.Si[(t[(i + s1) % 4] >> 16) & 0xFF]
784-
^ (tt >> 16)) & 0xFF)
785-
result.append((self.Si[(t[(i + s2) % 4] >> 8) & 0xFF] ^ (tt >> 8))
786-
& 0xFF)
794+
result.append((self.Si[(t[(i + s1) % 4] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF)
795+
result.append((self.Si[(t[(i + s2) % 4] >> 8) & 0xFF] ^ (tt >> 8)) & 0xFF)
787796
result.append((self.Si[t[(i + s3) % 4] & 0xFF] ^ tt) & 0xFF)
788797

789798
return result
790799

791800

792801
class AESBlockModeOfOperation(object):
793802
'''Super-class for AES modes of operation that require blocks.'''
803+
794804
def __init__(self, key):
795805
self._aes = AES(key)
796806

@@ -847,4 +857,3 @@ def aes_decrypt(data: bytes, key: str) -> bytes:
847857
for x in [data[i:i + 16] for i in range(0, len(data), 16)]:
848858
plain.append(AESModeOfOperationECB(key).decrypt(x))
849859
return strip_padding(b''.join(plain))
850-

pyencrypt/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pyencrypt.generate import generate_aes_key
1616
from pyencrypt.license import MAX_DATETIME, MIN_DATETIME, generate_license_file
1717

18-
VERSION = f"""\
18+
VERSION = fr"""
1919
_
2020
_ __ _ _ ___ _ __ ___ _ __ _ _ _ __ | |_
2121
| '_ \| | | |/ _ \ '_ \ / __| '__| | | | '_ \| __|
@@ -29,9 +29,8 @@
2929
"""
3030

3131
KEY_OPTION_HELP = """
32-
Your encryption key.If you dont specify key,
32+
Your encryption key.If you don't specify key,
3333
pyencrypt will generate encryption key randomly.
34-
3534
"""
3635

3736
PYTHON_MAJOR, PYTHON_MINOR = sys.version_info[:2]
@@ -123,6 +122,7 @@ def get_metavar(self, param):
123122
def __repr__(self) -> str:
124123
return "Ipv4Address"
125124

125+
126126
class CustomParamType:
127127
KEY = KeyParamType()
128128
MAC_ADDR = MacAddressParamType()

pyencrypt/encrypt.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def generate_so_file(cipher_key: str, d: int, n: int, base_dir: Path = None, lic
5555
need_import_files = ['ntt.py', 'aes.py', 'decrypt.py', 'license.py']
5656
for file in need_import_files:
5757
file_path = path / file
58-
decrypt_source_ls.append(REMOVE_SELF_IMPORT.sub('',file_path.read_text()))
58+
decrypt_source_ls.append(REMOVE_SELF_IMPORT.sub('', file_path.read_text()))
5959

6060
loader_source_path = path / 'loader.py'
6161
loader_source = REMOVE_SELF_IMPORT.sub('', loader_source_path.read_text()).replace(
@@ -104,6 +104,7 @@ def generate_so_file(cipher_key: str, d: int, n: int, base_dir: Path = None, lic
104104
)
105105
return list(temp_dir.glob('loader.cpython-*-*.so'))[0].absolute()
106106

107+
107108
def encrypt_file(path: Path, key: str, delete_origin: bool = False, new_path: Path = None):
108109
if not can_encrypt(path):
109110
raise Exception(f"{path.name} can't be encrypted.")

pyencrypt/license.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ def check_license(license_path: Path, aes_key: str):
9595
raise Exception("Machine mac address is invalid.")
9696
if ipv4 and ipv4 != get_host_ipv4():
9797
raise Exception("Machine ipv4 address is invalid.")
98-
return True
98+
return True

pyencrypt/loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ def find_spec(self, fullname: str, path: Sequence[_Path], target: types.ModuleTy
8888

8989

9090
# TODO: generate randomly AES Class
91-
sys.meta_path.insert(0,EncryptFileFinder())
91+
sys.meta_path.insert(0, EncryptFileFinder())

pyencrypt/ntt.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ def bitreverse(x: list, length: int):
77
for i in range(0, length):
88
if i > j:
99
x[i], x[j] = x[j], x[i]
10-
l = length >> 1
10+
l = length >> 1 # noqa: E741
1111
while True:
1212
j = j ^ l
1313
if j >= l:
1414
break
15-
l >>= 1
15+
l >>= 1 # noqa: E741
1616

1717

1818
def _ntt(arr: list, inverse=False):
@@ -51,4 +51,3 @@ def ntt(arr: list):
5151

5252
def intt(arr: list):
5353
return _ntt(arr, True)
54-

setup.cfg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ based_on_style = pep8
5353
COLUMN_LIMIT = 119
5454
indent_width = 4
5555
coalesce_brackets = True
56-
dedent_closing_brackets = True
56+
dedent_closing_brackets = True
57+
58+
[flake8]
59+
max_line_length = 119
60+
ignore = E501,W504

tests/conftest.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
from pathlib import Path
32
from pyencrypt.encrypt import encrypt_file, encrypt_key, generate_so_file
43
from pyencrypt.generate import generate_aes_key
54
from pyencrypt.license import generate_license_file
@@ -21,19 +20,17 @@ def file_and_loader(request, tmp_path_factory):
2120
code = file_marker.kwargs.get('code')
2221

2322
license_marker = request.node.get_closest_marker("license")
24-
license, kwargs = False,{}
23+
license, kwargs = False, {}
2524
if license_marker is not None:
2625
kwargs = license_marker.kwargs
2726
license = kwargs.pop('enable', True)
2827

2928
file_path = tmp_path / f'{file_name}.py'
3029
file_path.touch()
31-
file_path.write_text(
32-
"""\
30+
file_path.write_text("""\
3331
def {function_name}():
3432
{code}
35-
""".format(function_name=function_name, code=code)
36-
)
33+
""".format(function_name=function_name, code=code))
3734
# generate loader.so
3835
key = generate_aes_key()
3936
new_path = file_path.with_suffix('.pye')
@@ -47,12 +44,11 @@ def {function_name}():
4744
work_dir.joinpath('loader_origin.py').unlink()
4845

4946
# License
50-
license == True and generate_license_file(key.decode(), work_dir,**kwargs)
47+
license and generate_license_file(key.decode(), work_dir, **kwargs)
5148
generate_license_file(key.decode(), work_dir)
5249
return (new_path, loader_path)
5350

5451

55-
5652
@pytest.fixture(scope='function')
5753
def package_and_loader(request, tmp_path_factory):
5854
pkg_path = tmp_path_factory.mktemp('package')
@@ -63,7 +59,7 @@ def package_and_loader(request, tmp_path_factory):
6359
code = file_marker.kwargs.get('code')
6460

6561
license_marker = request.node.get_closest_marker("license")
66-
license, kwargs = False,{}
62+
license, kwargs = False, {}
6763
if license_marker is not None:
6864
kwargs = license_marker.kwargs
6965
license = kwargs.pop('enable', True)
@@ -92,5 +88,5 @@ def {function_name}():
9288
work_dir.joinpath('loader.c').unlink()
9389
work_dir.joinpath('loader_origin.py').unlink()
9490
# License
95-
license == True and generate_license_file(key.decode(), work_dir, **kwargs)
91+
license and generate_license_file(key.decode(), work_dir, **kwargs)
9692
return pkg_path, loader_path

0 commit comments

Comments
 (0)