|
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | 5 |
|
6 | | -def get_config(): |
7 | | - # Get the directory of the calling function |
8 | | - caller_dir = Path(os.path.dirname(os.path.abspath(__file__))) |
9 | | - config_file = f"{caller_dir}/config.json" |
10 | | - if Path(config_file).is_file(): |
11 | | - with open(config_file, "r") as f: |
12 | | - config_data = json.loads(f.read()) |
13 | | - else: |
14 | | - config_data = { |
15 | | - "language_model": { |
16 | | - "provider": "litellm", |
17 | | - "enable_observability_logging": False, |
18 | | - }, |
19 | | - "github_app": { |
20 | | - "check_signature": False, |
21 | | - "auto_pr_review": False, |
22 | | - "edit_pr_desc": False, |
23 | | - }, |
24 | | - } |
25 | | - return config_data |
26 | | - |
27 | | - |
28 | | -def validate_config_settings(config): |
29 | | - "Make sure relvant enviorment variables are set" |
30 | | - if config.get("github_app", {}).get("check_signature", False): |
31 | | - if not os.environ.get("GITHUB_APP_WEBHOOK_SECRET"): |
32 | | - raise EnvironmentError( |
33 | | - "The environment variable 'GITHUB_APP_WEBHOOK_SECRET' is not set." |
34 | | - ) |
35 | | - |
36 | | - if config.get("language_model", {}).get("provider", {}) == "litellm": |
37 | | - if config.get("language_model", {}).get("enable_observability_logging", False): |
38 | | - if not os.environ.get("SUPABASE_URL"): |
39 | | - raise EnvironmentError( |
40 | | - "The environment variable 'SUPABASE_URL' is not set." |
41 | | - ) |
42 | | - if not os.environ.get("SUPABASE_KEY"): |
| 6 | +class ConfigData: |
| 7 | + def __init__(self, config_data=None): |
| 8 | + config_file = "config.json" |
| 9 | + if Path(config_file).is_file(): |
| 10 | + with open(config_file, "r") as f: |
| 11 | + self.config_data = json.loads(f.read()) |
| 12 | + else: |
| 13 | + print(f"Couldnt find config at {config_file} loading default vals") |
| 14 | + self.config_data = { |
| 15 | + "language_model": { |
| 16 | + "provider": "litellm", |
| 17 | + "enable_observability_logging": False, |
| 18 | + }, |
| 19 | + "github_app": { |
| 20 | + "check_signature": False, |
| 21 | + "auto_pr_review": False, |
| 22 | + "edit_pr_desc": False, |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + if config_data: |
| 27 | + self.update_config_data(config_data) |
| 28 | + |
| 29 | + def update_config_data(self, new_config_data): |
| 30 | + self.config_data.update(new_config_data) |
| 31 | + self.validate_config_settings(self.config_data) |
| 32 | + |
| 33 | + def get_config_data(self): |
| 34 | + return self.config_data |
| 35 | + |
| 36 | + def get_language_model_config(self): |
| 37 | + return self.config_data["language_model"] |
| 38 | + |
| 39 | + def get_github_app_config(self): |
| 40 | + return self.config_data["github_app"] |
| 41 | + |
| 42 | + def validate_config_settings(self): |
| 43 | + "Make sure relvant enviorment variables are set" |
| 44 | + if self.config_data.get("github_app", {}).get("check_signature", False): |
| 45 | + if not os.environ.get("GITHUB_APP_WEBHOOK_SECRET"): |
43 | 46 | raise EnvironmentError( |
44 | | - "The environment variable 'SUPABASE_KEY' is not set." |
| 47 | + "The environment variable 'GITHUB_APP_WEBHOOK_SECRET' is not set." |
45 | 48 | ) |
46 | | - return config |
47 | | - |
48 | 49 |
|
49 | | -CONFIG_DATA = validate_config_settings(get_config()) |
| 50 | + if self.config_data.get("language_model", {}).get("provider", {}) == "litellm": |
| 51 | + if self.config_data.get("language_model", {}).get( |
| 52 | + "enable_observability_logging", False |
| 53 | + ): |
| 54 | + if not os.environ.get("SUPABASE_URL"): |
| 55 | + raise EnvironmentError( |
| 56 | + "The environment variable 'SUPABASE_URL' is not set." |
| 57 | + ) |
| 58 | + if not os.environ.get("SUPABASE_KEY"): |
| 59 | + raise EnvironmentError( |
| 60 | + "The environment variable 'SUPABASE_KEY' is not set." |
| 61 | + ) |
0 commit comments