Compare commits

..

No commits in common. "a8bcb386184182ec99850bcad045ab8fdb5d8150" and "ec24193a72bc809d241e0a72f11381eb08bef873" have entirely different histories.

3 changed files with 15 additions and 31 deletions

View File

@ -1,15 +1,8 @@
import pathlib import pathlib
from .connections import Connection, SilentConnection
from .cache import clean_cache, clear_cache, set_cache_directory, get_cache_stats
__name__ = "python_requests" __name__ = "python_requests"
__folder__ = str(pathlib.Path(__file__).parent) __folder__ = str(pathlib.Path(__file__).parent)
__all__ = [
"Connection",
"SilentConnection", CACHE_DIRECTORY = "cache"
"clean_cache",
"clear_cache",
"get_cache_stats",
"set_cache_directory",
]

View File

@ -1,7 +1,8 @@
import argparse import argparse
import logging import logging
from . import Connection, clean_cache, clear_cache, set_cache_directory, get_cache_stats, __folder__ from .connections import Connection, SilentConnection
from . import cache
def main(): def main():
@ -48,13 +49,11 @@ def cli():
format='%(asctime)s - %(levelname)s - %(message)s' format='%(asctime)s - %(levelname)s - %(message)s'
) )
logging.debug("Debug logging enabled") logging.debug("Debug logging enabled")
set_cache_directory("cache")
else: else:
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s' format='%(asctime)s - %(levelname)s - %(message)s'
) )
set_cache_directory()
if hasattr(args, 'func'): if hasattr(args, 'func'):
@ -66,7 +65,7 @@ def cli():
def handle_show_cache(args): def handle_show_cache(args):
"""Handle the show-cache command""" """Handle the show-cache command"""
try: try:
file_count, db_count = get_cache_stats() file_count, db_count = cache.get_cache_stats()
logging.info(f"Cache Statistics:") logging.info(f"Cache Statistics:")
logging.info(f" - Files in cache: {file_count}") logging.info(f" - Files in cache: {file_count}")
logging.info(f" - Database entries: {db_count}") logging.info(f" - Database entries: {db_count}")
@ -76,7 +75,7 @@ def handle_show_cache(args):
def handle_clean_cache(args): def handle_clean_cache(args):
"""Handle the clean-cache command""" """Handle the clean-cache command"""
try: try:
files_deleted, entries_deleted = clean_cache() files_deleted, entries_deleted = cache.clean_cache()
logging.info(f"Cleaned cache:") logging.info(f"Cleaned cache:")
logging.info(f" - Files deleted: {files_deleted}") logging.info(f" - Files deleted: {files_deleted}")
logging.info(f" - Database entries removed: {entries_deleted}") logging.info(f" - Database entries removed: {entries_deleted}")
@ -89,7 +88,7 @@ def handle_clear_cache(args):
# Confirm before clearing all cache # Confirm before clearing all cache
confirm = input("Are you sure you want to clear ALL cache? This cannot be undone. [y/N]: ") confirm = input("Are you sure you want to clear ALL cache? This cannot be undone. [y/N]: ")
if confirm.lower() == 'y': if confirm.lower() == 'y':
files_deleted, entries_deleted = clear_cache() files_deleted, entries_deleted = cache.clear_cache()
logging.info(f"Cleared ALL cache:") logging.info(f"Cleared ALL cache:")
logging.info(f" - Files deleted: {files_deleted}") logging.info(f" - Files deleted: {files_deleted}")
logging.info(f" - Database entries removed: {entries_deleted}") logging.info(f" - Database entries removed: {entries_deleted}")

View File

@ -1,4 +1,4 @@
from typing import Optional, Tuple, Union from typing import Optional, Tuple
from codecs import encode from codecs import encode
from hashlib import sha1 from hashlib import sha1
from pathlib import Path from pathlib import Path
@ -7,12 +7,13 @@ import pickle
import sqlite3 import sqlite3
from datetime import datetime, timedelta from datetime import datetime, timedelta
from . import __name__ from . import CACHE_DIRECTORY
CACHE_DIRECTORY = Path(f"/tmp/{__name__}")
# SQLite database file path
DB_FILE = Path(CACHE_DIRECTORY, "cache_metadata.db") DB_FILE = Path(CACHE_DIRECTORY, "cache_metadata.db")
# Initialize the database
def _init_db(): def _init_db():
with sqlite3.connect(DB_FILE) as conn: with sqlite3.connect(DB_FILE) as conn:
conn.execute(""" conn.execute("""
@ -23,17 +24,8 @@ def _init_db():
""") """)
conn.commit() conn.commit()
# Initialize the database when module is imported
def set_cache_directory(cache_directory: Optional[Union[str, Path]] = None): _init_db()
global CACHE_DIRECTORY, DB_FILE
if cache_directory is not None:
CACHE_DIRECTORY = cache_directory
DB_FILE = Path(CACHE_DIRECTORY, "cache_metadata.db")
_init_db()
print(CACHE_DIRECTORY, DB_FILE)
Path(CACHE_DIRECTORY).mkdir(exist_ok=True, parents=True)
def get_url_hash(url: str) -> str: def get_url_hash(url: str) -> str: