Ana içeriğe geç

Yapılandırma Modülü

config

Configuration management for Weeb CLI.

This module provides a centralized configuration system that stores settings in a SQLite database with fallback to default values. Supports both interactive and headless (API) modes.

Configuration is stored persistently in the database and can be accessed throughout the application using the global config instance.

Example

Basic usage::

from weeb_cli.config import config

# Get configuration value
download_dir = config.get("download_dir")
aria2_enabled = config.get("aria2_enabled", True)

# Set configuration value
config.set("language", "tr")

# Enable headless mode (no database access)
config.set_headless(True)

Attributes:

Name Type Description
APP_NAME str

Application name for directory naming.

CONFIG_DIR Path

User configuration directory (~/.weeb-cli).

DEFAULT_CONFIG dict

Default configuration values.

config Config

Global configuration instance.

Config

Configuration manager with database persistence.

Provides a simple key-value interface for application settings with automatic persistence to SQLite database. Supports headless mode for API usage without database access.

Attributes:

Name Type Description
_db Optional[Database]

Lazy-loaded database instance.

_headless bool

Whether running in headless mode (no database).

Source code in weeb_cli/config.py
class Config:
    """Configuration manager with database persistence.

    Provides a simple key-value interface for application settings with
    automatic persistence to SQLite database. Supports headless mode for
    API usage without database access.

    Attributes:
        _db (Optional[Database]): Lazy-loaded database instance.
        _headless (bool): Whether running in headless mode (no database).
    """

    def __init__(self) -> None:
        """Initialize configuration manager."""
        self._db: Optional['Database'] = None
        self._headless: bool = False

    @property
    def db(self) -> 'Database':
        """Get database instance with lazy loading.

        Returns:
            Database instance for configuration storage.
        """
        if self._db is None:
            from weeb_cli.services.database import db
            self._db = db
        return self._db

    def get(self, key: str, default: Optional[Any] = None) -> Any:
        """Get configuration value by key.

        Attempts to retrieve value from database first, then falls back to
        provided default or DEFAULT_CONFIG. Special handling for download_dir
        to generate localized default path.

        Args:
            key: Configuration key name.
            default: Default value if key not found in database.

        Returns:
            Configuration value, default, or None.

        Example:
            >>> config.get("language", "en")
            "tr"
            >>> config.get("aria2_max_connections")
            16
        """
        if not self._headless:
            try:
                val = self.db.get_config(key)
                if val is not None:
                    return val
            except Exception:
                # Avoid circular import with logger, just pass silently
                pass

        # Special handling for download_dir
        if key == "download_dir":
            return default if default is not None else get_default_download_dir()

        # Use provided default, fallback to DEFAULT_CONFIG, then None
        if default is not None:
            return default
        return DEFAULT_CONFIG.get(key)

    def set(self, key: str, value: Any) -> None:
        """Set configuration value.

        Persists the value to database for future retrieval.

        Args:
            key: Configuration key name.
            value: Value to store (must be JSON-serializable).

        Example:
            >>> config.set("language", "tr")
            >>> config.set("aria2_max_connections", 32)
        """
        self.db.set_config(key, value)

    def set_headless(self, headless: bool = True) -> None:
        """Enable or disable headless mode.

        In headless mode, configuration is read from DEFAULT_CONFIG only,
        without database access. Useful for API commands and testing.

        Args:
            headless: Whether to enable headless mode.
        """
        self._headless = headless

db property

db: Database

Get database instance with lazy loading.

Returns:

Type Description
Database

Database instance for configuration storage.

__init__

__init__() -> None

Initialize configuration manager.

Source code in weeb_cli/config.py
def __init__(self) -> None:
    """Initialize configuration manager."""
    self._db: Optional['Database'] = None
    self._headless: bool = False

get

get(key: str, default: Optional[Any] = None) -> Any

Get configuration value by key.

Attempts to retrieve value from database first, then falls back to provided default or DEFAULT_CONFIG. Special handling for download_dir to generate localized default path.

Parameters:

Name Type Description Default
key str

Configuration key name.

required
default Optional[Any]

Default value if key not found in database.

None

Returns:

Type Description
Any

Configuration value, default, or None.

Example

config.get("language", "en") "tr" config.get("aria2_max_connections") 16

Source code in weeb_cli/config.py
def get(self, key: str, default: Optional[Any] = None) -> Any:
    """Get configuration value by key.

    Attempts to retrieve value from database first, then falls back to
    provided default or DEFAULT_CONFIG. Special handling for download_dir
    to generate localized default path.

    Args:
        key: Configuration key name.
        default: Default value if key not found in database.

    Returns:
        Configuration value, default, or None.

    Example:
        >>> config.get("language", "en")
        "tr"
        >>> config.get("aria2_max_connections")
        16
    """
    if not self._headless:
        try:
            val = self.db.get_config(key)
            if val is not None:
                return val
        except Exception:
            # Avoid circular import with logger, just pass silently
            pass

    # Special handling for download_dir
    if key == "download_dir":
        return default if default is not None else get_default_download_dir()

    # Use provided default, fallback to DEFAULT_CONFIG, then None
    if default is not None:
        return default
    return DEFAULT_CONFIG.get(key)

set

set(key: str, value: Any) -> None

Set configuration value.

Persists the value to database for future retrieval.

Parameters:

Name Type Description Default
key str

Configuration key name.

required
value Any

Value to store (must be JSON-serializable).

required
Example

config.set("language", "tr") config.set("aria2_max_connections", 32)

Source code in weeb_cli/config.py
def set(self, key: str, value: Any) -> None:
    """Set configuration value.

    Persists the value to database for future retrieval.

    Args:
        key: Configuration key name.
        value: Value to store (must be JSON-serializable).

    Example:
        >>> config.set("language", "tr")
        >>> config.set("aria2_max_connections", 32)
    """
    self.db.set_config(key, value)

set_headless

set_headless(headless: bool = True) -> None

Enable or disable headless mode.

In headless mode, configuration is read from DEFAULT_CONFIG only, without database access. Useful for API commands and testing.

Parameters:

Name Type Description Default
headless bool

Whether to enable headless mode.

True
Source code in weeb_cli/config.py
def set_headless(self, headless: bool = True) -> None:
    """Enable or disable headless mode.

    In headless mode, configuration is read from DEFAULT_CONFIG only,
    without database access. Useful for API commands and testing.

    Args:
        headless: Whether to enable headless mode.
    """
    self._headless = headless

get_default_download_dir

get_default_download_dir() -> str

Get the default download directory path.

Uses localized folder name from i18n translations.

Returns:

Type Description
str

Absolute path to default download directory in current working directory.

Source code in weeb_cli/config.py
def get_default_download_dir() -> str:
    """Get the default download directory path.

    Uses localized folder name from i18n translations.

    Returns:
        Absolute path to default download directory in current working directory.
    """
    from weeb_cli.i18n import i18n

    folder_name = i18n.t("downloads.default_folder_name", "weeb-downloads")
    return os.path.join(os.getcwd(), folder_name)

Genel Bakış

Yapılandırma modülü, Weeb CLI için merkezi ayar yönetimi sağlar. Tüm yapılandırma, mantıklı varsayılanlara geri dönüşle birlikte bir SQLite veritabanında saklanır.

Kullanım Örnekleri

Yapılandırma Değerlerini Alma

from weeb_cli.config import config

# Varsayılan geri dönüşle al
language = config.get("language", "en")
download_dir = config.get("download_dir")
aria2_enabled = config.get("aria2_enabled", True)

Yapılandırma Değerlerini Ayarlama

from weeb_cli.config import config

# Dili ayarla
config.set("language", "tr")

# İndirme dizinini ayarla
config.set("download_dir", "/yol/indirmeler")

# Özellikleri etkinleştir/devre dışı bırak
config.set("discord_rpc_enabled", False)

Headless Modu

Veritabanı erişimi olmadan API kullanımı için:

from weeb_cli.config import config

# Headless modunu etkinleştir
config.set_headless(True)

# Şimdi config.get() yalnızca DEFAULT_CONFIG değerlerini döndürür
language = config.get("language")  # None döndürür (varsayılan)

Varsayılan Yapılandırma

Veritabanı değeri olmadığında aşağıdaki varsayılan değerler kullanılır:

Anahtar Varsayılan Değer Açıklama
language None Arayüz dili (tr, en, de, pl)
aria2_enabled True İndirmeler için Aria2'yi etkinleştir
ytdlp_enabled True İndirmeler için yt-dlp'yi etkinleştir
aria2_max_connections 16 İndirme başına maksimum bağlantı
max_concurrent_downloads 3 Maksimum eşzamanlı indirme
download_dir None İndirme dizini yolu
ytdlp_format "bestvideo+bestaudio/best" yt-dlp format dizesi
scraping_source None Varsayılan sağlayıcı
show_description True Anime açıklamalarını göster
debug_mode False Hata ayıklama günlüğünü etkinleştir
download_max_retries 3 İndirme yeniden deneme sayısı
download_retry_delay 10 Denemeler arası gecikme (saniye)
discord_rpc_enabled True Discord Rich Presence'ı etkinleştir
shortcuts_enabled False Klavye kısayollarını etkinleştir

Yapılandırma Dizini

Yapılandırma ve veri şurada saklanır:

~/.weeb-cli/
├── weeb.db          # SQLite veritabanı
├── cache/           # Önbelleğe alınmış veri
├── bin/             # İndirilen bağımlılıklar
└── logs/            # Hata ayıklama günlükleri

API Referansı

Configuration manager with database persistence.

Provides a simple key-value interface for application settings with automatic persistence to SQLite database. Supports headless mode for API usage without database access.

Attributes:

Name Type Description
_db Optional[Database]

Lazy-loaded database instance.

_headless bool

Whether running in headless mode (no database).

Source code in weeb_cli/config.py
class Config:
    """Configuration manager with database persistence.

    Provides a simple key-value interface for application settings with
    automatic persistence to SQLite database. Supports headless mode for
    API usage without database access.

    Attributes:
        _db (Optional[Database]): Lazy-loaded database instance.
        _headless (bool): Whether running in headless mode (no database).
    """

    def __init__(self) -> None:
        """Initialize configuration manager."""
        self._db: Optional['Database'] = None
        self._headless: bool = False

    @property
    def db(self) -> 'Database':
        """Get database instance with lazy loading.

        Returns:
            Database instance for configuration storage.
        """
        if self._db is None:
            from weeb_cli.services.database import db
            self._db = db
        return self._db

    def get(self, key: str, default: Optional[Any] = None) -> Any:
        """Get configuration value by key.

        Attempts to retrieve value from database first, then falls back to
        provided default or DEFAULT_CONFIG. Special handling for download_dir
        to generate localized default path.

        Args:
            key: Configuration key name.
            default: Default value if key not found in database.

        Returns:
            Configuration value, default, or None.

        Example:
            >>> config.get("language", "en")
            "tr"
            >>> config.get("aria2_max_connections")
            16
        """
        if not self._headless:
            try:
                val = self.db.get_config(key)
                if val is not None:
                    return val
            except Exception:
                # Avoid circular import with logger, just pass silently
                pass

        # Special handling for download_dir
        if key == "download_dir":
            return default if default is not None else get_default_download_dir()

        # Use provided default, fallback to DEFAULT_CONFIG, then None
        if default is not None:
            return default
        return DEFAULT_CONFIG.get(key)

    def set(self, key: str, value: Any) -> None:
        """Set configuration value.

        Persists the value to database for future retrieval.

        Args:
            key: Configuration key name.
            value: Value to store (must be JSON-serializable).

        Example:
            >>> config.set("language", "tr")
            >>> config.set("aria2_max_connections", 32)
        """
        self.db.set_config(key, value)

    def set_headless(self, headless: bool = True) -> None:
        """Enable or disable headless mode.

        In headless mode, configuration is read from DEFAULT_CONFIG only,
        without database access. Useful for API commands and testing.

        Args:
            headless: Whether to enable headless mode.
        """
        self._headless = headless

get

get(key: str, default: Optional[Any] = None) -> Any

Get configuration value by key.

Attempts to retrieve value from database first, then falls back to provided default or DEFAULT_CONFIG. Special handling for download_dir to generate localized default path.

Parameters:

Name Type Description Default
key str

Configuration key name.

required
default Optional[Any]

Default value if key not found in database.

None

Returns:

Type Description
Any

Configuration value, default, or None.

Example

config.get("language", "en") "tr" config.get("aria2_max_connections") 16

Source code in weeb_cli/config.py
def get(self, key: str, default: Optional[Any] = None) -> Any:
    """Get configuration value by key.

    Attempts to retrieve value from database first, then falls back to
    provided default or DEFAULT_CONFIG. Special handling for download_dir
    to generate localized default path.

    Args:
        key: Configuration key name.
        default: Default value if key not found in database.

    Returns:
        Configuration value, default, or None.

    Example:
        >>> config.get("language", "en")
        "tr"
        >>> config.get("aria2_max_connections")
        16
    """
    if not self._headless:
        try:
            val = self.db.get_config(key)
            if val is not None:
                return val
        except Exception:
            # Avoid circular import with logger, just pass silently
            pass

    # Special handling for download_dir
    if key == "download_dir":
        return default if default is not None else get_default_download_dir()

    # Use provided default, fallback to DEFAULT_CONFIG, then None
    if default is not None:
        return default
    return DEFAULT_CONFIG.get(key)

set

set(key: str, value: Any) -> None

Set configuration value.

Persists the value to database for future retrieval.

Parameters:

Name Type Description Default
key str

Configuration key name.

required
value Any

Value to store (must be JSON-serializable).

required
Example

config.set("language", "tr") config.set("aria2_max_connections", 32)

Source code in weeb_cli/config.py
def set(self, key: str, value: Any) -> None:
    """Set configuration value.

    Persists the value to database for future retrieval.

    Args:
        key: Configuration key name.
        value: Value to store (must be JSON-serializable).

    Example:
        >>> config.set("language", "tr")
        >>> config.set("aria2_max_connections", 32)
    """
    self.db.set_config(key, value)

set_headless

set_headless(headless: bool = True) -> None

Enable or disable headless mode.

In headless mode, configuration is read from DEFAULT_CONFIG only, without database access. Useful for API commands and testing.

Parameters:

Name Type Description Default
headless bool

Whether to enable headless mode.

True
Source code in weeb_cli/config.py
def set_headless(self, headless: bool = True) -> None:
    """Enable or disable headless mode.

    In headless mode, configuration is read from DEFAULT_CONFIG only,
    without database access. Useful for API commands and testing.

    Args:
        headless: Whether to enable headless mode.
    """
    self._headless = headless