Skip to content

Commit 9cc5253

Browse files
committed
feat: replace print with logging
1 parent d8d98c5 commit 9cc5253

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

tls_requests/models/libraries.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from platform import machine
1313
from typing import List, Optional, Tuple
1414

15+
from ..utils import get_logger
16+
1517
__all__ = ["TLSLibrary"]
1618

1719
LATEST_VERSION_TAG_NAME = "v1.11.2"
@@ -63,6 +65,8 @@
6365

6466
TLS_LIBRARY_PATH = os.getenv("TLS_LIBRARY_PATH")
6567

68+
logger = get_logger("TLSRequests")
69+
6670

6771
@dataclass
6872
class BaseRelease:
@@ -250,9 +254,9 @@ def cleanup_files(cls, keep_file: str = None):
250254
if is_remove:
251255
try:
252256
os.remove(file_path)
253-
print(f"Removed old library file: {file_path}")
257+
logger.debug(f"Removed old library file: {file_path}")
254258
except OSError as e:
255-
print(f"Error removing old library file {file_path}: {e}")
259+
logger.debug(f"Error removing old library file {file_path}: {e}")
256260

257261
@classmethod
258262
def fetch_api(cls, version: str = None, retries: int = 3):
@@ -280,7 +284,7 @@ def _find_release(data, version_: str = None):
280284
_find_release(json.loads(content))
281285
break
282286
except Exception as ex:
283-
print("Unable to fetch GitHub API: %s" % ex)
287+
logger.debug("Unable to fetch GitHub API: %s" % ex)
284288

285289
if not asset_urls and not ubuntu_urls:
286290
_find_release([cls._STATIC_API_DATA])
@@ -305,7 +309,7 @@ def find_all(cls) -> List[str]:
305309
@classmethod
306310
def download(cls, version: str = None) -> str:
307311
try:
308-
print(
312+
logger.debug(
309313
"System Info - Platform: %s, Machine: %s, File Ext : %s."
310314
% (
311315
PLATFORM,
@@ -319,7 +323,7 @@ def download(cls, version: str = None) -> str:
319323
download_url = url
320324
break
321325

322-
print("Library Download URL: %s" % download_url)
326+
logger.debug("Library Download URL: %s" % download_url)
323327
if download_url:
324328
destination_name = download_url.split("/")[-1]
325329
destination = os.path.join(BIN_DIR, destination_name)
@@ -352,13 +356,13 @@ def download(cls, version: str = None) -> str:
352356
sys.stdout.write(f"\rDownloading {destination_name}: [{bar}] {percent:.1f}%")
353357
sys.stdout.flush()
354358

355-
print() # Newline after download completes
359+
logger.debug() # Newline after download completes
356360
return destination
357361

358362
except (urllib.error.URLError, urllib.error.HTTPError) as ex:
359-
print("Unable to download file: %s" % ex)
363+
logger.debug("Unable to download file: %s" % ex)
360364
except Exception as e:
361-
print("An unexpected error occurred during download: %s" % e)
365+
logger.debug("An unexpected error occurred during download: %s" % e)
362366

363367
@classmethod
364368
def set_path(cls, fp: str):
@@ -375,17 +379,17 @@ def _load_library(fp_):
375379
try:
376380
lib = ctypes.cdll.LoadLibrary(fp_)
377381
cls.set_path(fp_)
378-
print(f"Successfully loaded TLS library: {fp_}")
382+
logger.debug(f"Successfully loaded TLS library: {fp_}")
379383
return lib
380384
except Exception as ex:
381-
print(f"Unable to load TLS library '{fp_}', details: {ex}")
385+
logger.debug(f"Unable to load TLS library '{fp_}', details: {ex}")
382386
try:
383387
os.remove(fp_)
384388
except (FileNotFoundError, PermissionError):
385389
pass
386390

387391
target_version = cls._parse_version(LATEST_VERSION_TAG_NAME)
388-
print(f"Required library version: {LATEST_VERSION_TAG_NAME}")
392+
logger.debug(f"Required library version: {LATEST_VERSION_TAG_NAME}")
389393
local_files = cls.find_all()
390394
newest_local_version = (0, 0, 0)
391395
newest_local_file = None
@@ -396,17 +400,17 @@ def _load_library(fp_):
396400
if file_version > newest_local_version:
397401
newest_local_version = file_version
398402
newest_local_file = file_path
399-
print(
403+
logger.debug(
400404
f"Found newest local library: {newest_local_file} (version {'.'.join(map(str, newest_local_version))})"
401405
)
402406
else:
403-
print("No local library found.")
407+
logger.debug("No local library found.")
404408

405409
if newest_local_version < target_version:
406410
if newest_local_file:
407-
print(f"Local library is outdated. Upgrading to {LATEST_VERSION_TAG_NAME}...")
411+
logger.debug(f"Local library is outdated. Upgrading to {LATEST_VERSION_TAG_NAME}...")
408412
else:
409-
print(f"Downloading required library version {LATEST_VERSION_TAG_NAME}...")
413+
logger.debug(f"Downloading required library version {LATEST_VERSION_TAG_NAME}...")
410414

411415
downloaded_fp = cls.download(version=LATEST_VERSION_TAG_NAME)
412416
if downloaded_fp:

0 commit comments

Comments
 (0)