Skip to content

Commit

Permalink
v 0.6.5 improve config file
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelmansuy committed Jun 29, 2024
1 parent e9f6340 commit 91425d3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
1 change: 0 additions & 1 deletion .code2promptrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"path": "code2prompt",
"suppress_comments": false,
"line_number": false
}
18 changes: 8 additions & 10 deletions code2prompt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from code2prompt.core.process_files import process_files
from code2prompt.core.write_output import write_output
from code2prompt.utils.create_template_directory import create_templates_directory
from code2prompt.utils.logging_utils import log_token_count
from code2prompt.utils.logging_utils import log_token_count, log_error
from code2prompt.utils.config import load_config, merge_options

VERSION = "0.6.4" # Define the version of the CLI tool
VERSION = "0.6.5" # Define the version of the CLI tool

DEFAULT_OPTIONS = {
"path": [],
Expand All @@ -34,7 +34,6 @@
"--path",
"-p",
type=click.Path(exists=True),
required=True,
multiple=True, # Allow multiple paths
help="Path(s) to the directory or file to process.",
)
Expand Down Expand Up @@ -122,21 +121,20 @@ def create_markdown_file(**cli_options):

## Load configuration from .code2promptrc files
config = load_config(".")

print(config)


# Merge options: CLI takes precedence over config, which takes precedence over defaults
# Merge options: CLI takes precedence over config, which takes precedence over defaults
options = merge_options(cli_options, config, DEFAULT_OPTIONS)


print(options)

if options["create_templates"]:
create_templates_directory()
return

if not options["path"]:
log_error(
"Error: No path specified. Please provide a path using --path option or in .code2promptrc file."
)
return

all_files_data = []
for path in options["path"]:
files_data = process_files({**options, "path": path})
Expand Down
11 changes: 8 additions & 3 deletions code2prompt/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ def load_config(current_dir):
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))
file_config = json.load(f)
if 'path' in file_config and isinstance(file_config['path'], str):
file_config['path'] = file_config['path'].split(',')
config.update(file_config)
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:
Expand All @@ -44,4 +45,8 @@ def merge_options(cli_options: dict, config_options: dict, default_options: dict
else:
merged[key] = value

# Special handling for 'path'
if not merged['path'] and 'path' in config_options:
merged['path'] = config_options['path']

return merged
51 changes: 51 additions & 0 deletions code2prompt/utils/logging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,66 @@ def log_success(message):
logger.info(f"{Fore.GREEN}✅ SUCCESS: {message}{Style.RESET_ALL}")

def log_file_processed(file_path):
"""
Logs a message indicating that a file has been processed.
This function logs a message at the INFO level, indicating that a specific file has been processed.
It uses a blue color and a file emoji for visual distinction.
Args:
file_path (str): The path to the file that was processed.
Example:
log_file_processed("/path/to/file.txt")
"""
logger.info(f"{Fore.BLUE}📄 Processed: {file_path}{Style.RESET_ALL}")

def log_token_count(count):
"""
Logs the total number of tokens processed.
This function logs the total count of tokens processed by the application,
using a cyan color and a token emoji for visual distinction.
Args:
count (int): The total number of tokens processed.
Example:
log_token_count(5000)
"""
logger.info(f"{Fore.CYAN}🔢 Token count: {count}{Style.RESET_ALL}")

def log_output_created(output_path):
"""
Logs a message indicating that an output file has been created.
This function logs a message at the INFO level, indicating that an output file has been successfully created.
It uses a green color and a folder emoji for visual distinction.
Args:
output_path (str): The path to the output file that was created.
Example:
log_output_created("/path/to/output/file.txt")
"""
logger.info(f"{Fore.GREEN}📁 Output file created: {output_path}{Style.RESET_ALL}")

def log_clipboard_copy(success=True):
"""
Logs whether the content was successfully copied to the clipboard.
This function logs a message indicating whether the content copying to the clipboard was successful or not.
It uses different emojis and colors depending on the success status.
Args:
success (bool): Indicates whether the content was successfully copied to the clipboard. Defaults to True.
Examples:
log_clipboard_copy(True)
Logs: 📋 Content copied to clipboard
log_clipboard_copy(False)
Logs: 📋 Failed to copy content to clipboard
"""
if success:
logger.info(f"{Fore.GREEN}📋 Content copied to clipboard{Style.RESET_ALL}")
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "code2prompt"
version = "0.6.4"
version = "0.6.5"
description = "A tool to convert code snippets into AI prompts for documentation or explanation purposes."
authors = ["Raphael MANSUY <raphael.mansuy@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit 91425d3

Please sign in to comment.