-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from dougollerenshaw/add_config_manager
Added ConfigManager to handle keys
- Loading branch information
Showing
4 changed files
with
77 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import os | ||
import platform | ||
import sys | ||
from pathlib import Path | ||
from decouple import Config, RepositoryEnv, UndefinedValueError | ||
|
||
|
||
class ConfigManager: | ||
def __init__(self): | ||
self.is_packaged_app = getattr(sys, "frozen", False) | ||
if self.is_packaged_app: | ||
self.config_dir = self._get_app_config_dir() | ||
self.keyring_service = "CodeAIde" | ||
else: | ||
self.config_dir = Path(__file__).parent.parent.parent | ||
self.env_file = self.config_dir / ".env" | ||
self._ensure_env_file() | ||
|
||
def _get_app_config_dir(self): | ||
system = platform.system() | ||
if system == "Darwin": # macOS | ||
return Path.home() / "Library" / "Application Support" / "CodeAIde" | ||
elif system == "Windows": | ||
return Path(os.getenv("APPDATA")) / "CodeAIde" | ||
else: # Linux and others | ||
return Path.home() / ".config" / "codeaide" | ||
|
||
def _ensure_env_file(self): | ||
if not self.env_file.exists(): | ||
self.env_file.touch() | ||
|
||
def get_api_key(self, provider): | ||
if self.is_packaged_app: | ||
import keyring | ||
|
||
return keyring.get_password( | ||
self.keyring_service, f"{provider.upper()}_API_KEY" | ||
) | ||
else: | ||
try: | ||
config = Config(RepositoryEnv(self.env_file)) | ||
return config(f"{provider.upper()}_API_KEY") | ||
except UndefinedValueError: | ||
return None | ||
|
||
def set_api_key(self, provider, api_key): | ||
if self.is_packaged_app: | ||
import keyring | ||
|
||
keyring.set_password( | ||
self.keyring_service, f"{provider.upper()}_API_KEY", api_key | ||
) | ||
else: | ||
with open(self.env_file, "a") as f: | ||
f.write(f'\n{provider.upper()}_API_KEY="{api_key}"\n') | ||
|
||
def delete_api_key(self, provider): | ||
if self.is_packaged_app: | ||
import keyring | ||
|
||
keyring.delete_password(self.keyring_service, f"{provider.upper()}_API_KEY") | ||
else: | ||
# Read the .env file, remove the line with the API key, and write it back | ||
if self.env_file.exists(): | ||
lines = self.env_file.read_text().splitlines() | ||
lines = [ | ||
line | ||
for line in lines | ||
if not line.startswith(f"{provider.upper()}_API_KEY=") | ||
] | ||
self.env_file.write_text("\n".join(lines) + "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters