Skip to content

Commit

Permalink
Implement disk cache
Browse files Browse the repository at this point in the history
  • Loading branch information
atick-faisal committed Apr 28, 2024
1 parent b1ea00f commit 58b94f7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion peter_explains/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.9"
__version__ = "0.1.0"
31 changes: 23 additions & 8 deletions peter_explains/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import asyncio
from diskcache import Cache
from .peter_ai import explain_command
from .utils import (
parse_arguments,
stylize_output,
show_loading_message,
show_error_message,
get_cache_dir,
)

cache = Cache(get_cache_dir("peter_explains"))


async def main():
"""
Expand All @@ -17,14 +21,25 @@ async def main():
"""
try:
command = parse_arguments()
tasks = [
asyncio.create_task(show_loading_message()),
asyncio.create_task(explain_command(command)),
]
done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)

pending.pop().cancel() # Cancel the loading message task
result = done.pop().result() # Get the result of the explain_command task

result = None

if command in cache: # Check in cache
result = cache[command]

else:
tasks = [
asyncio.create_task(show_loading_message()),
asyncio.create_task(explain_command(command)),
]
done, pending = await asyncio.wait(
tasks, return_when=asyncio.FIRST_COMPLETED
)

pending.pop().cancel() # Cancel the loading message task
result = done.pop().result() # Get the result of the explain_command task

cache[command] = result # Save in cache

stylize_output(result)

Expand Down
29 changes: 29 additions & 0 deletions peter_explains/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import random
import asyncio
Expand Down Expand Up @@ -29,6 +30,34 @@
]


def get_cache_dir(app_name):
"""
This function gets the cache directory for the Peter Explains CLI based on the operating system.
Args:
- app_name (str): The name of the Peter Explains CLI.
Returns:
- cache_dir (str): The cache directory for the Peter Explains CLI.
"""
if os.name == "nt": # Windows
cache_dir = os.path.join(os.getenv("LOCALAPPDATA"), app_name, "cache")
elif os.name == "posix":
home = os.path.expanduser("~")
if sys.platform == "darwin": # macOS
cache_dir = os.path.join(
home, "Library", "Application Support", app_name, "cache"
)
else: # Linux and other UNIX like
cache_dir = os.path.join(home, ".config", app_name, "cache")
else:
cache_dir = app_name

os.makedirs(cache_dir, exist_ok=True)

return cache_dir


def forgot_api_key():
"""
Function to display a message when the API key is missing.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
colorama
diskcache
json_repair
google-generativeai

0 comments on commit 58b94f7

Please sign in to comment.