Skip to content

Commit e1ff97f

Browse files
author
Alex Baranov
committed
style: Fix ruff lint warnings in initial_setup.py and generateNADTaxonomies.py
- Replace os.path.dirname with Path.parents (PTH120) - Rename unused variable base to _base (RUF059) - Add noqa for intentional insecure SSL context (S323) - Add noqa for conditional imports (PLC0415)
1 parent 63fdff4 commit e1ff97f

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

src/python/generateNADTaxonomies.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import gzip
2121
import os
22+
from pathlib import Path
2223
from zipfile import ZipFile
2324

2425
STATE_INDEX = 1
@@ -34,9 +35,10 @@ def generate_nad_taxonomy(base_dir=None, input_file="NAD_r21_TXT.zip"):
3435
base_dir: Base directory containing the data folder. If None, defaults to
3536
BASE_DIR env var or relative path from this script.
3637
input_file: Name of the NAD zip file to process. Defaults to NAD_r21_TXT.zip.
38+
3739
"""
3840
if base_dir is None:
39-
base_dir = os.environ.get("BASE_DIR", os.path.join(os.path.dirname(__file__), "..", "..", ".."))
41+
base_dir = os.environ.get("BASE_DIR", str(Path(__file__).resolve().parents[3]))
4042

4143
input_zip = os.path.join(base_dir, "data", input_file)
4244
output_file = os.path.join(base_dir, "data", "NAD_taxonomy.txt.gz")

src/python/initial_setup.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ def get_java_from_env():
6464
6565
Returns:
6666
tuple: (java_home, java_exe, javac_exe, found) where found is True if JAVA_HOME is set.
67+
6768
"""
6869
java_home = os.environ.get("JAVA_HOME")
6970
if java_home and os.path.isdir(java_home):
7071
java_exe = os.path.join(java_home, "bin", "java")
7172
javac_exe = os.path.join(java_home, "bin", "javac")
7273
return java_home, java_exe, javac_exe, True
73-
else:
74-
return "/path/to/java", "/path/to/java/bin/java", "/path/to/java/bin/javac", False
74+
return "/path/to/java", "/path/to/java/bin/java", "/path/to/java/bin/javac", False
7575

7676

7777
def runSetup(download, insecure_ssl=False):
@@ -80,7 +80,7 @@ def runSetup(download, insecure_ssl=False):
8080
print("=" * 60)
8181

8282
cwd = os.getcwd()
83-
parent, base = os.path.split(cwd)
83+
parent, _base = os.path.split(cwd)
8484
data_dir = os.path.join(parent, "data")
8585
idx_dir = os.path.join(parent, "indices")
8686

@@ -155,11 +155,11 @@ def runSetup(download, insecure_ssl=False):
155155

156156
print(f"\n [{i}/{len(DATA_FILES)}] Processing: {local_filename}")
157157
if os.path.exists(target_file):
158-
print(f" File already exists - skipping")
158+
print(" File already exists - skipping")
159159
else:
160160
print(f" Source URL: {url_source}")
161161
print(f" Destination: {target_file}")
162-
print(f" Starting download (this might take a while)...")
162+
print(" Starting download (this might take a while)...")
163163
Downloader(url_source, target_file, insecure_ssl).download()
164164
print()
165165
print(f" Download complete: {local_filename}")
@@ -179,8 +179,8 @@ def runSetup(download, insecure_ssl=False):
179179
print(f" NAD zip file not found at {nad_zip} - skipping taxonomy generation")
180180
else:
181181
print(f" Generating NAD taxonomy from {nad_zip}")
182-
print(f" This processes ~92M address records and may take several minutes...")
183-
from generateNADTaxonomies import generate_nad_taxonomy
182+
print(" This processes ~92M address records and may take several minutes...")
183+
from generateNADTaxonomies import generate_nad_taxonomy # noqa: PLC0415 - conditional import, only needed when NAD data exists
184184

185185
generate_nad_taxonomy(parent)
186186
print(f" NAD taxonomy generation complete: {nad_taxonomy}")
@@ -205,31 +205,31 @@ def __init__(self, url, target_path, insecure_ssl=False):
205205
Downloader.index = 0
206206

207207
def download(self):
208-
print(f" Configuring SSL context...")
208+
print(" Configuring SSL context...")
209209
# Handle SSL certificate configuration
210210
if self.__insecure_ssl:
211211
print(" Warning: Using insecure SSL context (certificate verification disabled)")
212-
ssl_context = ssl._create_unverified_context()
212+
ssl_context = ssl._create_unverified_context() # noqa: S323 - intentional: user explicitly opted in via --insecure-ssl
213213
else:
214214
try:
215215
# Try to use certifi bundle if available
216-
import certifi
216+
import certifi # noqa: PLC0415 - conditional import for optional dependency
217217

218-
print(f" Using certifi SSL certificates")
218+
print(" Using certifi SSL certificates")
219219
ssl_context = ssl.create_default_context(cafile=certifi.where())
220220
except ImportError:
221221
# Fall back to system certificates
222-
print(f" Using system SSL certificates")
222+
print(" Using system SSL certificates")
223223
ssl_context = ssl.create_default_context()
224224

225225
# Install SSL context
226-
print(f" Setting up HTTP handler...")
226+
print(" Setting up HTTP handler...")
227227
https_handler = request.HTTPSHandler(context=ssl_context)
228228
opener = request.build_opener(https_handler)
229229
opener.addheaders = [("User-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36")]
230230
request.install_opener(opener)
231231

232-
print(f" Initiating download...")
232+
print(" Initiating download...")
233233
try:
234234
request.urlretrieve(self.__url, self.__target_path, Downloader.reporthook)
235235
except ssl.SSLError as e:

0 commit comments

Comments
 (0)