initial commit

This commit is contained in:
2025-04-08 13:15:01 +02:00
commit d3bbe63aa1
8 changed files with 384 additions and 0 deletions

88
src/komcache/cache.py Normal file
View File

@@ -0,0 +1,88 @@
import sqlite3
from komconfig import KomConfig, CONFIG_PATH
from pathlib import Path
import os
class KomCache:
def __init__(self, db_path: str = CONFIG_PATH):
self.db_path = Path(db_path, "komcache.db")
if "~" in str(self.db_path):
self.db_path = os.path.expanduser(str(self.db_path))
print(f"Database path: {self.db_path}")
self.conn = sqlite3.connect(self.db_path)
self.cursor = self.conn.cursor()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.conn.close()
pass
def create_table(self, table_query: str) -> None:
"""
Create a table in the database.
Parameters
----------
table_query : str
the SQL query to create the table.
"""
self.cursor.execute(table_query)
self.conn.commit()
def delete_table(self, table_name: str) -> bool:
try:
self.cursor.execute(f"DROP TABLE IF EXISTS {table_name}")
self.conn.commit()
return True
except sqlite3.Error as e:
print(f"Error deleting table {table_name}: {e}")
return False
def query_table(self, table: str, query: str) -> list:
try:
self.cursor.execute(f"SELECT {query} FROM {table}")
return self.cursor.fetchall()
except sqlite3.Error as e:
print(f"Error querying table {table}: {e}")
return []
def query(self, query, args=None):
if args is None:
args = []
try:
self.cursor.execute(query, args)
return self.cursor.fetchall()[0]
except sqlite3.Error as e:
print(f"Error executing query: {e}")
return []
def insert(self, query, args=None):
if args is None:
args = []
try:
self.cursor.execute(query, args)
self.conn.commit()
return True
except sqlite3.Error as e:
print(f"Error inserting data: {e}")
return False
def update(self, query, args=None):
if args is None:
args = []
try:
self.cursor.execute(query, args)
self.conn.commit()
return True
except sqlite3.Error as e:
print(f"Error updating data: {e}")
return False
if __name__ == "__main__":
# Example usage
with KomCache() as cache:
cache.delete_table("komgrabber")