-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bb59d9
commit e9f6340
Showing
4 changed files
with
121 additions
and
20 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"path": "code2prompt", | ||
"suppress_comments": false, | ||
"line_number": false | ||
} |
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
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,47 @@ | ||
# code2prompt/config.py | ||
import json | ||
from pathlib import Path | ||
|
||
def load_config(current_dir): | ||
""" | ||
Load configuration from .code2promptrc files. | ||
Searches in the current directory and all parent directories up to the home directory. | ||
""" | ||
config = {} | ||
current_path = Path(current_dir).resolve() | ||
home_path = Path.home() | ||
|
||
while current_path >= home_path: | ||
rc_file = current_path / '.code2promptrc' | ||
if rc_file.is_file(): | ||
with open(rc_file, 'r', encoding='utf-8') as f: | ||
config.update(json.load(f)) | ||
if current_path == home_path: | ||
break | ||
current_path = current_path.parent | ||
|
||
return config | ||
|
||
def merge_options(cli_options: dict, config_options: dict, default_options: dict) -> dict: | ||
""" | ||
Merge CLI options, config options, and default options. | ||
CLI options take precedence over config options, which take precedence over default options. | ||
""" | ||
merged = default_options.copy() | ||
|
||
# Update with config options | ||
for key, value in config_options.items(): | ||
if isinstance(value, dict) and isinstance(merged.get(key), dict): | ||
merged[key] = merge_options({}, value, merged[key]) | ||
else: | ||
merged[key] = value | ||
|
||
# Update with CLI options, but only if they're different from the default | ||
for key, value in cli_options.items(): | ||
if value != default_options.get(key): | ||
if isinstance(value, dict) and isinstance(merged.get(key), dict): | ||
merged[key] = merge_options(value, {}, merged[key]) | ||
else: | ||
merged[key] = value | ||
|
||
return merged |