Skip to content

Commit 77a6d00

Browse files
committed
more lint fixes
1 parent 56797e6 commit 77a6d00

File tree

6 files changed

+122
-123
lines changed

6 files changed

+122
-123
lines changed

database/data_factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Index dictionary data definitions"""
22

3+
34
def make_trle_page_data():
45
"""trle.net page, represents the same data as a page on the site"""
56
return {
@@ -24,6 +25,7 @@ def make_trle_level_data():
2425

2526

2627
def make_trle_tombll_data():
28+
"""This is the app data at the moment"""
2729
return {
2830
"title": "",
2931
"authors": [],
@@ -81,5 +83,3 @@ def make_zip_file():
8183
"release": "",
8284
"version": ""
8385
}
84-
85-

database/get_leaf_cert.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
"""This module get the certificate for "broken" servers that don't follow
2-
the standard handshake procedure, that is not sending the chain,
3-
curl can still connect to this server by specifying the leaf and curl will
4-
by default look for the chain in /etc/ssl/certs but requests module
5-
require a bundle so that one would have to compile this bundle into the chain"""
1+
"""
2+
This module retrieves only the leaf certificate from servers that don't
3+
follow the standard TLS handshake procedure and fail to provide the complete
4+
certificate chain. While `curl` can still connect to these servers by
5+
specifying only the leaf certificate (and defaults to finding the chain in
6+
/etc/ssl/certs), the `requests` module requires a full certificate bundle.
7+
This module is intended for use cases where only the leaf certificate
8+
is needed.
9+
"""
610

711
import sys
812
import ssl
@@ -12,6 +16,7 @@
1216
from cryptography.hazmat.backends import default_backend
1317
from cryptography.hazmat.primitives import hashes, serialization
1418

19+
1520
def get_certificate(hostname):
1621
"""OpenSSL with TCP get the certificate"""
1722
context = ssl.create_default_context()
@@ -27,13 +32,15 @@ def get_certificate(hostname):
2732
return x509.load_der_x509_certificate(cert_der, default_backend())
2833
return None
2934

35+
3036
def get_sha256_fingerprint(cert):
3137
"""Identify the sum, we might want verify the certificate"""
3238
cert_der = cert.public_bytes(serialization.Encoding.DER)
3339
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
3440
digest.update(cert_der)
3541
return digest.finalize()
3642

43+
3744
def get_serial_number_hex(cert):
3845
"""Identify the serial, this can be use to look for the certificate"""
3946
# Get the serial number in a byte format
@@ -42,6 +49,7 @@ def get_serial_number_hex(cert):
4249
# Format it as a hex string
4350
return ':'.join(f'{b:02X}' for b in serial_number_bytes)
4451

52+
4553
def print_certificate_details(cert):
4654
"""Log basic certificate information"""
4755
fingerprint = get_sha256_fingerprint(cert)
@@ -56,6 +64,7 @@ def print_certificate_details(cert):
5664

5765

5866
def run(url):
67+
"""Retrieve and return the certificate's public key in PEM byte format."""
5968
if url.startswith("https://www.trle.net"):
6069
host = 'trle.net'
6170
elif url.startswith("https://trcustoms.org"):
@@ -72,6 +81,7 @@ def run(url):
7281

7382
return certificate.public_bytes(encoding=serialization.Encoding.PEM)
7483

84+
7585
'''
7686
def validate_downloaded_key(id_number, expected_serial):
7787
"""Validate the certificate in binary form with the cryptography module"""

database/get_trle_by_id_range.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
This script retrieves all TRLE levels by a specified ID range and saves the data as JSON files.
33
Usage: python3 get_trle_by_id_range.py FROM_ID TO_ID
44
"""
5-
65
import sys
76
import time
87
import json
98

109
import scrape
1110
import data_factory
1211

12+
1313
def safe_string_to_int(id_str):
1414
"""Converts a string to an integer with error checking.
15-
15+
1616
Args:
1717
s (str): The string to convert.
1818
1919
Returns:
2020
int: The converted integer if valid.
21-
21+
2222
Exits:
2323
Exits with status code 1 if the input string is not a valid integer.
2424
"""
@@ -31,7 +31,7 @@ def safe_string_to_int(id_str):
3131

3232
def trle_by_id(trle_id):
3333
"""Fetches TRLE level data by ID and saves it as a JSON file.
34-
34+
3535
Args:
3636
trle_id (int): The ID of the TRLE level to fetch.
3737
"""

database/index_query.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
# Set the working directory to the script's location to ensure relative paths work correctly
88
os.chdir(os.path.dirname(os.path.abspath(__file__)))
99

10+
1011
def check_trle_doubles():
1112
"""
1213
Checks for duplicate entries in the Trle table.
13-
14-
This function connects to the 'index.db' SQLite database and queries the Trle table
15-
to identify any duplicate records based on certain fields (trleExternId, author,
16-
title, difficulty, duration, class, type, and release). It groups the results by
17-
these fields and counts occurrences. If any grouped records appear more than once,
14+
15+
This function connects to the 'index.db' SQLite database and queries the Trle table
16+
to identify any duplicate records based on certain fields (trleExternId, author,
17+
title, difficulty, duration, class, type, and release). It groups the results by
18+
these fields and counts occurrences. If any grouped records appear more than once,
1819
they are considered duplicates, and those entries are returned and printed.
1920
"""
2021
# Connect to the SQLite database
@@ -23,7 +24,7 @@ def check_trle_doubles():
2324

2425
# Execute query to find duplicates
2526
result = query_return_fetchall("""
26-
SELECT
27+
SELECT
2728
tr.trleExternId,
2829
tr.author,
2930
tr.title,
@@ -57,12 +58,12 @@ def check_trle_doubles():
5758
def query_return_fetchall(query, params, cursor):
5859
"""
5960
Executes a SELECT query and fetches all results from the database.
60-
61+
6162
Parameters:
6263
- query (str): The SQL query string to execute. Must be a SELECT query.
6364
- params (tuple or None): Parameters to bind to the query, or None if there are no parameters.
6465
- cursor (sqlite3.Cursor): The database cursor used to execute the query.
65-
66+
6667
Returns:
6768
- list: The result set from the query as a list of tuples.
6869
@@ -100,7 +101,7 @@ def query_return_id(query, params, cursor):
100101
- cursor (sqlite3.Cursor): The database cursor used to execute the query.
101102
102103
Returns:
103-
- int or None: The last inserted row ID if an INSERT query was run,
104+
- int or None: The last inserted row ID if an INSERT query was run,
104105
or the first integer result from a SELECT query, if available.
105106
Returns None if no valid result is found.
106107
@@ -139,7 +140,7 @@ def query_return_id(query, params, cursor):
139140
def query_run(query, params, cursor):
140141
"""
141142
Executes a query that does not return results (e.g., UPDATE, DELETE, or non-returning INSERT).
142-
143+
143144
Parameters:
144145
- query (str): The SQL query to execute.
145146
- params (tuple): Parameters to bind to the query.

database/sanitize_downloads.py

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88
import re
99

10+
1011
def new_input(data, file_path):
1112
"""Take new input"""
1213
zip_file = data['zip_files'][0]
@@ -27,71 +28,57 @@ def new_input(data, file_path):
2728
def sanitize(data, file_path):
2829
"""
2930
Validates the 'zip_file' data from the given dictionary.
30-
31+
3132
This function checks:
3233
1. That the 'name' attribute exists and ends with ".zip".
3334
2. That the 'size' attribute is a float, greater than 2, and has exactly two decimal places.
3435
3. That the 'md5' attribute is a valid 32-character hexadecimal MD5 hash.
3536
4. That the 'url' attribute matches one of the allowed URL patterns.
36-
37+
3738
Args:
3839
data (dict): The input dictionary containing 'zip_files' data.
39-
40+
4041
Exits:
4142
Exits the program with status 1 if any validation fails.
4243
"""
43-
44-
# Extract the first zip file data
4544
zip_file = data['zip_files'][0]
46-
47-
# Validate attributes
48-
if not zip_file.get('name'):
49-
print("The 'name' attribute is missing.")
50-
new_input(data, file_path)
51-
return
52-
53-
if not isinstance(zip_file.get('size'), float):
54-
print("The 'size' attribute is missing.")
55-
new_input(data, file_path)
56-
return
57-
58-
if not zip_file.get('md5'):
59-
print("The 'md5' attribute is missing.")
60-
new_input(data, file_path)
61-
return
62-
63-
if not zip_file.get('url'):
64-
print("The 'url' attribute is missing.")
65-
new_input(data, file_path)
66-
return
67-
68-
# Validate name end with ".zip"
69-
if not zip_file['name'].endswith(".zip") or "$" in zip_file['name']:
70-
print(f"The file {zip_file['name']} is not a .zip file.")
71-
new_input(data, file_path)
72-
return
73-
74-
# Validate 'size' attribute - must be a float, >2, and have exactly two decimal places
75-
if zip_file['size'] <= 2:
76-
print("The 'size' attribute is smaller then 2 MiB")
77-
new_input(data, file_path)
78-
return
79-
80-
# Validate 'md5' attribute - must be a valid MD5 hash (32 hexadecimal characters)
81-
if not re.fullmatch(r"^[a-fA-F0-9]{32}$", zip_file.get('md5', '')):
82-
print("The 'md5' attribute is not a valid 32-character hexadecimal MD5 hash.")
83-
new_input(data, file_path)
84-
return
85-
86-
# Validate 'url' attribute - must match one of the expected patterns
45+
errors = []
46+
47+
# Validate name
48+
name = zip_file.get('name')
49+
if not name:
50+
errors.append("The 'name' attribute is missing.")
51+
elif not name.endswith(".zip") or "$" in name:
52+
errors.append(f"The file '{name}' is not a valid .zip file.")
53+
54+
# Validate size
55+
size = zip_file.get('size')
56+
if not isinstance(size, float):
57+
errors.append("The 'size' attribute is missing or not a float.")
58+
elif size <= 2:
59+
errors.append("The 'size' attribute is smaller than 2 MiB.")
60+
61+
# Validate md5
62+
md5 = zip_file.get('md5')
63+
if not md5:
64+
errors.append("The 'md5' attribute is missing.")
65+
elif not re.fullmatch(r"^[a-fA-F0-9]{32}$", md5):
66+
errors.append("The 'md5' attribute is not a valid 32-character hexadecimal MD5 hash.")
67+
68+
# Validate url
69+
url = zip_file.get('url')
8770
pattern1 = r"^https://trcustoms\.org/api/level_files/\d+/download$"
8871
pattern2 = r"^https://www\.trle\.net/levels/levels/\d{4}/\d{4}/[a-zA-Z0-9%-_\.$]+\.zip$"
89-
90-
if not re.match(pattern1, zip_file.get('url', '')) \
91-
and not re.match(pattern2, zip_file.get('url', '')):
92-
print("The 'url' attribute does not match any of the expected patterns.")
72+
if not url:
73+
errors.append("The 'url' attribute is missing.")
74+
elif not re.match(pattern1, url) and not re.match(pattern2, url):
75+
errors.append("The 'url' attribute does not match any of the expected patterns.")
76+
77+
# Handle errors
78+
if errors:
79+
for error in errors:
80+
print(error)
9381
new_input(data, file_path)
94-
return
9582

9683

9784
def safe_string_to_int(id_str):

0 commit comments

Comments
 (0)