Skip to content

Commit

Permalink
Fix reading of .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
sultur committed Sep 8, 2023
1 parent c705074 commit 9030acf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
35 changes: 20 additions & 15 deletions src/icespeak/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"""
# We dont import annotations from __future__ here
# due to pydantic
from typing import Any, Optional, Union
from typing import Any, Optional

import json
import tempfile
Expand All @@ -31,7 +31,7 @@
from logging import getLogger
from pathlib import Path

from pydantic import BaseModel, Field, SecretStr, validator
from pydantic import BaseModel, Field, SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict

# Library wide logger instance
Expand Down Expand Up @@ -80,12 +80,25 @@ class Settings(BaseSettings):
Attributes are read from environment variables or `.env` file.
"""

model_config = SettingsConfigDict(env_prefix="ICESPEAK_")
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
env_prefix="ICESPEAK_",
env_nested_delimiter="__",
case_sensitive=False,
# Run validators when attributes are modified
validate_assignment=True,
)

DEFAULT_VOICE: str = Field(
default="Gudrun", description="Default TTS voice if none is requested."
)
DEFAULT_VOICE_SPEED: float = Field(default=1.0, le=MAX_SPEED, ge=MIN_SPEED)
DEFAULT_VOICE_SPEED: float = Field(
default=1.0,
le=MAX_SPEED,
ge=MIN_SPEED,
description="Default TTS voice speed.",
)
DEFAULT_TEXT_FORMAT: TextFormats = Field(
default=TextFormats.SSML,
description="Default format to interpret input text as.",
Expand Down Expand Up @@ -124,16 +137,6 @@ class Settings(BaseSettings):
description="Name of the Google API key file.",
)

# Logging settings
LOG_LEVEL: Union[str, int] = Field(default="INFO", description="Logging level.")

@validator("LOG_LEVEL")
def set_log_level(cls, lvl: Union[str, int]) -> Union[str, int]:
# Set logging level for root Ratatoskur logger,
# this raises a ValueError if the provided level is invalid
LOG.setLevel(lvl)
return lvl

def get_audio_dir(self) -> Path:
"""
Return directory for saving output audio files.
Expand Down Expand Up @@ -182,7 +185,9 @@ class Keys(BaseModel):

_kd = SETTINGS.KEYS_DIR
if not (_kd.exists() and _kd.is_dir()):
LOG.warning("Keys directory missing or incorrect, TTS will not work!")
LOG.warning(
"Keys directory missing or incorrect, TTS will not work! Set to: %s", _kd
)
else:
# Load API keys, logging exceptions in level DEBUG so they aren't logged twice,
# as exceptions are logged as warnings when voice modules are initialized
Expand Down
6 changes: 4 additions & 2 deletions src/icespeak/voices/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ def load_api_keys(self):
# See issues regarding compatibility with OpenSSL version >=3:
# https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/1747
# https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/1986
LOG.warning("OpenSSL version not compatible with Azure Cognitive Services.")
raise RuntimeError("Incompatible OpenSSL version.")
LOG.warning(
"OpenSSL version not compatible with "
"Azure Cognitive Services, TTS might not work."
)

if API_KEYS.azure is None:
raise RuntimeError("Azure API keys missing.")
Expand Down

0 comments on commit 9030acf

Please sign in to comment.