Skip to content

File Path Manager

Source: data/file_path_manager.py

Resolves Flet storage directories and paths under csv_files/ and tts_cache/. Call initialize() at startup before loading or saving sets. See Data and storage.

Resolve Flet storage directories and paths under csv_files/.

FilePathManager is initialized once at application startup so catalog and set CSVs share a single directory root. TTS audio cache lives beside that catalog under tts_cache/. Prefer these helpers over hard-coded absolute paths.

FilePathManager

Manage application storage paths for CSV catalogs and learning sets.

Reads FLET_APP_STORAGE_DATA and FLET_APP_STORAGE_TEMP from the environment. When the data root is unset, csv_files resolution falls back to the process current working directory.

Source code in src/learning_app/data/file_path_manager.py
class FilePathManager:
    """Manage application storage paths for CSV catalogs and learning sets.

    Reads ``FLET_APP_STORAGE_DATA`` and ``FLET_APP_STORAGE_TEMP`` from the
    environment. When the data root is unset, ``csv_files`` resolution falls
    back to the process current working directory.
    """

    _data_dir = os.getenv("FLET_APP_STORAGE_DATA")
    _temp_dir = os.getenv("FLET_APP_STORAGE_TEMP")
    _initialized = False

    @classmethod
    def initialize(cls) -> None:
        """Create the CSV directory if needed and mark the manager ready.

        Safe to call more than once; later calls are no-ops.
        """
        if cls._initialized:
            return

        if cls._data_dir:
            cls._csv_dir = os.path.join(cls._data_dir, "csv_files")
            os.makedirs(cls._csv_dir, exist_ok=True)
        else:
            # If environment variable is not available (e.g., in web mode),
            # use the current directory
            cls._csv_dir = os.getcwd()

        cls._initialized = True

    @classmethod
    def get_csv_path(cls, file_name: str) -> str:
        """Return the absolute path for a set CSV basename or path.

        Args:
            file_name: Set file basename (for example ``animals_words.csv``)
                or an absolute path already under the CSV directory.

        Returns:
            Absolute path to use for load or save.
        """
        cls.initialize()
        if cls._data_dir and os.path.dirname(file_name) and cls._csv_dir in file_name:
            return file_name
        return os.path.join(cls._csv_dir, os.path.basename(file_name))

    @classmethod
    def get_files_data_path(cls) -> str:
        """Return the absolute path of the set catalog ``files.csv``."""
        cls.initialize()
        return os.path.join(cls._csv_dir, "files.csv")

    @classmethod
    def get_data_dir(cls) -> str | None:
        """Return ``FLET_APP_STORAGE_DATA``, or ``None`` when unset."""
        cls.initialize()
        return cls._data_dir

    @classmethod
    def get_temp_dir(cls) -> str | None:
        """Return ``FLET_APP_STORAGE_TEMP``, or ``None`` when unset."""
        cls.initialize()
        return cls._temp_dir

    @classmethod
    def get_csv_dir(cls) -> str:
        """Return the directory that holds set CSVs and ``files.csv``."""
        cls.initialize()
        return cls._csv_dir

    @classmethod
    def get_tts_cache_dir(cls) -> str:
        """Return the directory for persisted TTS MP3 files.

        Uses ``FLET_APP_STORAGE_DATA/tts_cache`` when the data root is set so
        cache survives app restarts. Falls back to ``<cwd>/tts_cache`` when
        the data root is unset (for example web mode).

        Returns:
            Absolute path to the TTS cache directory. Created if missing.
        """
        cls.initialize()
        if cls._data_dir:
            path = os.path.join(cls._data_dir, "tts_cache")
        else:
            path = os.path.join(os.getcwd(), "tts_cache")
        os.makedirs(path, exist_ok=True)
        return path

initialize classmethod

initialize() -> None

Create the CSV directory if needed and mark the manager ready.

Safe to call more than once; later calls are no-ops.

Source code in src/learning_app/data/file_path_manager.py
@classmethod
def initialize(cls) -> None:
    """Create the CSV directory if needed and mark the manager ready.

    Safe to call more than once; later calls are no-ops.
    """
    if cls._initialized:
        return

    if cls._data_dir:
        cls._csv_dir = os.path.join(cls._data_dir, "csv_files")
        os.makedirs(cls._csv_dir, exist_ok=True)
    else:
        # If environment variable is not available (e.g., in web mode),
        # use the current directory
        cls._csv_dir = os.getcwd()

    cls._initialized = True

get_csv_path classmethod

get_csv_path(file_name: str) -> str

Return the absolute path for a set CSV basename or path.

Parameters:

Name Type Description Default
file_name str

Set file basename (for example animals_words.csv) or an absolute path already under the CSV directory.

required

Returns:

Type Description
str

Absolute path to use for load or save.

Source code in src/learning_app/data/file_path_manager.py
@classmethod
def get_csv_path(cls, file_name: str) -> str:
    """Return the absolute path for a set CSV basename or path.

    Args:
        file_name: Set file basename (for example ``animals_words.csv``)
            or an absolute path already under the CSV directory.

    Returns:
        Absolute path to use for load or save.
    """
    cls.initialize()
    if cls._data_dir and os.path.dirname(file_name) and cls._csv_dir in file_name:
        return file_name
    return os.path.join(cls._csv_dir, os.path.basename(file_name))

get_files_data_path classmethod

get_files_data_path() -> str

Return the absolute path of the set catalog files.csv.

Source code in src/learning_app/data/file_path_manager.py
@classmethod
def get_files_data_path(cls) -> str:
    """Return the absolute path of the set catalog ``files.csv``."""
    cls.initialize()
    return os.path.join(cls._csv_dir, "files.csv")

get_data_dir classmethod

get_data_dir() -> str | None

Return FLET_APP_STORAGE_DATA, or None when unset.

Source code in src/learning_app/data/file_path_manager.py
@classmethod
def get_data_dir(cls) -> str | None:
    """Return ``FLET_APP_STORAGE_DATA``, or ``None`` when unset."""
    cls.initialize()
    return cls._data_dir

get_temp_dir classmethod

get_temp_dir() -> str | None

Return FLET_APP_STORAGE_TEMP, or None when unset.

Source code in src/learning_app/data/file_path_manager.py
@classmethod
def get_temp_dir(cls) -> str | None:
    """Return ``FLET_APP_STORAGE_TEMP``, or ``None`` when unset."""
    cls.initialize()
    return cls._temp_dir

get_csv_dir classmethod

get_csv_dir() -> str

Return the directory that holds set CSVs and files.csv.

Source code in src/learning_app/data/file_path_manager.py
@classmethod
def get_csv_dir(cls) -> str:
    """Return the directory that holds set CSVs and ``files.csv``."""
    cls.initialize()
    return cls._csv_dir

get_tts_cache_dir classmethod

get_tts_cache_dir() -> str

Return the directory for persisted TTS MP3 files.

Uses FLET_APP_STORAGE_DATA/tts_cache when the data root is set so cache survives app restarts. Falls back to <cwd>/tts_cache when the data root is unset (for example web mode).

Returns:

Type Description
str

Absolute path to the TTS cache directory. Created if missing.

Source code in src/learning_app/data/file_path_manager.py
@classmethod
def get_tts_cache_dir(cls) -> str:
    """Return the directory for persisted TTS MP3 files.

    Uses ``FLET_APP_STORAGE_DATA/tts_cache`` when the data root is set so
    cache survives app restarts. Falls back to ``<cwd>/tts_cache`` when
    the data root is unset (for example web mode).

    Returns:
        Absolute path to the TTS cache directory. Created if missing.
    """
    cls.initialize()
    if cls._data_dir:
        path = os.path.join(cls._data_dir, "tts_cache")
    else:
        path = os.path.join(os.getcwd(), "tts_cache")
    os.makedirs(path, exist_ok=True)
    return path