-
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.
feat(config): add configuration management and update documentation (#26
) * feat(config): add config file * fix(config): change to ensure config default keys exists * feat(config): add gh_token to config and remove unused parts * feat: add config command * test: change test makefile to be verbose * docs(config): add config reset command
- Loading branch information
1 parent
c2fbefb
commit 60eef33
Showing
8 changed files
with
362 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ publish: build | |
|
||
lint: | ||
pipenv run flake8 . | ||
|
||
test: | ||
pipenv run pytest -vv . |
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,97 @@ | ||
from copy import deepcopy | ||
import configparser | ||
import os | ||
|
||
|
||
def config_command_example(name, value_sample): | ||
return f'gpt-pr-config set {name} {value_sample}' | ||
|
||
|
||
CONFIG_README_SECTION = 'https://github.com/alissonperez/gpt-pr?tab=readme-ov-file#authentication--api-keys' | ||
|
||
|
||
class Config: | ||
|
||
config_filename = '.gpt-pr.ini' | ||
|
||
_default_config = { | ||
# Github | ||
'GH_TOKEN': '', | ||
|
||
# Open AI info | ||
'OPENAI_MODEL': 'gpt-4o', | ||
'OPENAI_API_KEY': '', | ||
} | ||
|
||
def __init__(self, config_dir=None): | ||
self.default_config = deepcopy(self._default_config) | ||
self._config_dir = config_dir or os.path.expanduser('~') | ||
self._config = configparser.ConfigParser() | ||
self._initialized = False | ||
|
||
def load(self): | ||
if self._initialized: | ||
return | ||
|
||
config_file_path = self.get_filepath() | ||
|
||
if os.path.exists(config_file_path): | ||
self._config.read(config_file_path) | ||
self._ensure_default_values() | ||
else: | ||
self._config['user'] = {} | ||
self._config['DEFAULT'] = deepcopy(self.default_config) | ||
self.persist() | ||
|
||
self._initialized = True | ||
|
||
def _ensure_default_values(self): | ||
added = False | ||
for key, value in self.default_config.items(): | ||
if key not in self._config['DEFAULT']: | ||
self._config['DEFAULT'][key] = value | ||
added = True | ||
|
||
if added: | ||
self.persist() | ||
|
||
def persist(self): | ||
config_file_path = self.get_filepath() | ||
|
||
with open(config_file_path, 'w') as configfile: | ||
self._config.write(configfile) | ||
|
||
def get_filepath(self): | ||
return os.path.join(self._config_dir, self.config_filename) | ||
|
||
def set_user_config(self, name, value): | ||
self.load() | ||
self._config['user'][name] = value | ||
|
||
def reset_user_config(self, name): | ||
self.load() | ||
self._config['user'][name] = self.default_config[name] | ||
self.persist() | ||
|
||
def get_user_config(self, name): | ||
self.load() | ||
return self._config['user'][name] | ||
|
||
def all_values(self): | ||
self.load() | ||
|
||
# iterate over all sections and values and return them in a list | ||
result = [] | ||
|
||
# add default section | ||
for option in self._config['DEFAULT']: | ||
result.append(('DEFAULT', option, self._config['DEFAULT'][option])) | ||
|
||
for section in self._config.sections(): | ||
for option in self._config[section]: | ||
result.append((section, option, self._config[section][option])) | ||
|
||
return result | ||
|
||
|
||
config = Config() |
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
Oops, something went wrong.