utils.py

source core utilities

File Path: src/core/utils.py

Purpose: General-purpose utility functions for logging, file I/O, and string parsing.

Overview

Provides shared helper functions used across the application for:

  • Loading dataset labels (Arabic/English)
  • Parsing metadata from checkpoint filenames (Class count + Model Architecture)
  • Configuring the application logger
  • Verifying file integrity (Git LFS check)

Global Variables

AR_WORDS & EN_WORDS

Type: list[str] Source: Loaded from KARSL-502_Labels.json Purpose: Global lists of sign labels in Arabic and English. Initialized By: init_signs()


Functions

init_signs()

def init_signs() -> None:

Purpose: Loads and parses the JSON labels file into the global AR_WORDS and EN_WORDS lists.

Action:

  • Reads KARSL-502_Labels.json.
  • Populates AR_WORDS and EN_WORDS.

Usage:

init_signs() # Usually called at module level

extract_metadata_from_checkpoint(checkpoint_path)

def extract_metadata_from_checkpoint(checkpoint_path) -> Optional[tuple[int, ModelSize]]:

Purpose: Extracts both the number of classes and the model architecture (ModelSize) from a checkpoint filename.

Regex: r".*?signs_(\d+)_(\w_\d_\d).*?"

Example:

  • Input: checkpoint_signs_502_s_4_2.pth
  • Output: (502, ModelSize(small, 4, 2))

Raises: ValueError if the pattern is not found.

get_default_logger()

def get_default_logger() -> logging.Logger:

Purpose: Singleton-like accessor for the application logger.

Configuration:

  • Name: wl-ar-sl
  • Output: File (logs/server_producer.log)
  • Format: %(asctime)s - %(levelname)s - %(message)s
  • Level: logging.DEBUG

is_git_lfs_pointer(filepath)

def is_git_lfs_pointer(filepath) -> bool:

Purpose: Checks if a file is actually downloaded or just a Git LFS pointer.

Logic: Reads the first 50 bytes and checks for the LFS prefix version https://git-lfs.github.com.


Depends On:

Used By: