Skip to content

gitea.config.manager

manager

Configuration manager for gitea.

Classes

gitea.config.manager.ConfigManager

ConfigManager(filename: Path | str | None = None)

Configuration manager for gitea.

Initialize ConfigManager.

Parameters:

Name Type Description Default
filename Path | str | None

Name of the configuration file.

None
Source code in src/gitea/config/manager.py
def __init__(self, filename: Path | str | None = None) -> None:
    """Initialize ConfigManager.

    Args:
        filename: Name of the configuration file.

    """
    filename = filename or Path(platformdirs.user_config_dir(appname="gitea")) / "config.yaml"
    filename = Path(filename)
    filename.parent.mkdir(parents=True, exist_ok=True)
    self.config_path = filename
    self._config: Config | None = None
Attributes
gitea.config.manager.ConfigManager.config property writable
config: Config

Get the current configuration.

Returns:

Name Type Description
Config Config

Current configuration.

Methods:
gitea.config.manager.ConfigManager.get_config
get_config(name: str | None) -> AccountConfig

Get the configuration for a specific account.

Parameters:

Name Type Description Default
name str | None

Name of the account. If None, the default account is used.

required

Returns:

Name Type Description
AccountConfig AccountConfig

Configuration of the specified account.

Source code in src/gitea/config/manager.py
def get_config(self, name: str | None) -> AccountConfig:
    """Get the configuration for a specific account.

    Args:
        name: Name of the account. If None, the default account is used.

    Returns:
        AccountConfig: Configuration of the specified account.

    """
    if self._config is None:
        self.load_config()

    name = self.config.default_account if name is None else name

    if self._config is None:
        self.load_config()

    if name not in self.config.accounts:
        raise ValueError(f"Account '{name}' does not exist in the configuration.")

    return self.config.accounts[name]
gitea.config.manager.ConfigManager.add_account
add_account(
    name: str,
    token: str,
    base_url: str = "https://gitea.com",
    is_default: bool = False,
) -> None

Add a new account to the configuration.

Parameters:

Name Type Description Default
name str

Name of the account.

required
token str

Authentication token for the account.

required
base_url str

Base URL of the Gitea platform.

'https://gitea.com'
is_default bool

Set as default account.

False
Source code in src/gitea/config/manager.py
def add_account(self, name: str, token: str, base_url: str = "https://gitea.com", is_default: bool = False) -> None:
    """Add a new account to the configuration.

    Args:
        name: Name of the account.
        token: Authentication token for the account.
        base_url: Base URL of the Gitea platform.
        is_default: Set as default account.

    """
    if self._config is None:
        self.load_config()

    if name in self.config.accounts:
        raise ValueError(f"Account '{name}' already exists in the configuration.")

    self.config.accounts[name] = AccountConfig(name=name, token=token, base_url=base_url)

    if is_default or len(self.config.accounts) == 1:
        self.config.default_account = name
gitea.config.manager.ConfigManager.update_account
update_account(
    name: str,
    token: str | None = None,
    base_url: str | None = None,
    is_default: bool | None = None,
) -> None

Update an existing account in the configuration.

Parameters:

Name Type Description Default
name str

Name of the account to update.

required
token str | None

New authentication token for the account (optional).

None
base_url str | None

New base URL of the account (optional).

None
is_default bool | None

Set as default account (optional).

None
Source code in src/gitea/config/manager.py
def update_account(
    self,
    name: str,
    token: str | None = None,
    base_url: str | None = None,
    is_default: bool | None = None,
) -> None:
    """Update an existing account in the configuration.

    Args:
        name: Name of the account to update.
        token: New authentication token for the account (optional).
        base_url: New base URL of the account (optional).
        is_default: Set as default account (optional).

    """
    if self._config is None:
        self.load_config()

    if name not in self.config.accounts:
        raise ValueError(f"Account '{name}' does not exist in the configuration.")

    account = self.config.accounts[name]

    if token is not None:
        account.token = token
    if base_url is not None:
        account.base_url = base_url

    if is_default is not None:
        if is_default:
            self.config.default_account = name
        elif self.config.default_account == name:
            self.config.default_account = None
        else:
            logger.warning("Account '%s' is not the default account. No changes made to default account.", name)
gitea.config.manager.ConfigManager.delete_account
delete_account(name: str) -> None

Delete an account from the configuration.

Parameters:

Name Type Description Default
name str

Name of the account to delete.

required
Source code in src/gitea/config/manager.py
def delete_account(self, name: str) -> None:
    """Delete an account from the configuration.

    Args:
        name: Name of the account to delete.

    """
    if self._config is None:
        self.load_config()

    if name not in self.config.accounts:
        raise ValueError(f"Account '{name}' does not exist in the configuration.")

    del self.config.accounts[name]

    if self.config.default_account == name:
        self.config.default_account = None
gitea.config.manager.ConfigManager.load_config
load_config(filename: Path | str | None = None) -> None

Load configuration from the YAML file.

Parameters:

Name Type Description Default
filename Path | str | None

Optional path to the configuration file.

None
Source code in src/gitea/config/manager.py
def load_config(self, filename: Path | str | None = None) -> None:
    """Load configuration from the YAML file.

    Args:
        filename: Optional path to the configuration file.

    """
    self.config = self._load_config(filename)
gitea.config.manager.ConfigManager.save_config
save_config(filename: Path | str | None = None) -> None

Save configuration to the YAML file.

Parameters:

Name Type Description Default
filename Path | str | None

Optional path to the configuration file.

None
Source code in src/gitea/config/manager.py
def save_config(self, filename: Path | str | None = None) -> None:
    """Save configuration to the YAML file.

    Args:
        filename: Optional path to the configuration file.

    """
    filename = filename or self.config_path
    filename = Path(filename)
    with filename.open("w", encoding="utf-8") as file:
        yaml.safe_dump(self.config.model_dump(), file)
gitea.config.manager.ConfigManager.has_default_account
has_default_account() -> bool

Check if a default account is set.

Returns:

Name Type Description
bool bool

True if a default account is set, False otherwise.

Source code in src/gitea/config/manager.py
def has_default_account(self) -> bool:
    """Check if a default account is set.

    Returns:
        bool: True if a default account is set, False otherwise.

    """
    if self._config is None:
        self.load_config()

    return self.config.default_account is not None