initial commit
This commit is contained in:
1
src/komcache/__init__.py
Normal file
1
src/komcache/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .cache import KomCache
|
||||
88
src/komcache/cache.py
Normal file
88
src/komcache/cache.py
Normal 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")
|
||||
0
src/komcache/py.typed
Normal file
0
src/komcache/py.typed
Normal file
Reference in New Issue
Block a user