oceansweep commited on
Commit
6815452
·
verified ·
1 Parent(s): 734cfb7

Delete App_Function_Libraries/DB_Manager.py

Browse files
Files changed (1) hide show
  1. App_Function_Libraries/DB_Manager.py +0 -472
App_Function_Libraries/DB_Manager.py DELETED
@@ -1,472 +0,0 @@
1
- import configparser
2
- import logging
3
- import os
4
- from contextlib import contextmanager
5
- from time import sleep
6
- from typing import Tuple
7
- import sqlite3
8
- # 3rd-Party Libraries
9
- from elasticsearch import Elasticsearch
10
-
11
- ############################################################################################################
12
- #
13
- # This file contains the DatabaseManager class, which is responsible for managing the database connection, i.e. either SQLite or Elasticsearch.
14
-
15
- ####
16
- # The DatabaseManager class provides the following methods:
17
- # - add_media: Add a new media item to the database
18
- # - fetch_items_by_keyword: Fetch media items from the database based on a keyword
19
- # - fetch_item_details: Fetch details of a specific media item from the database
20
- # - update_media_content: Update the content of a specific media item in the database
21
- # - search_and_display_items: Search for media items in the database and display the results
22
- # - close_connection: Close the database connection
23
- ####
24
-
25
- # Import your existing SQLite functions
26
- from SQLite_DB import (
27
- update_media_content as sqlite_update_media_content,
28
- list_prompts as sqlite_list_prompts,
29
- search_and_display as sqlite_search_and_display,
30
- fetch_prompt_details as sqlite_fetch_prompt_details,
31
- keywords_browser_interface as sqlite_keywords_browser_interface,
32
- add_keyword as sqlite_add_keyword,
33
- delete_keyword as sqlite_delete_keyword,
34
- export_keywords_to_csv as sqlite_export_keywords_to_csv,
35
- ingest_article_to_db as sqlite_ingest_article_to_db,
36
- add_media_to_database as sqlite_add_media_to_database,
37
- import_obsidian_note_to_db as sqlite_import_obsidian_note_to_db,
38
- add_prompt as sqlite_add_prompt,
39
- delete_chat_message as sqlite_delete_chat_message,
40
- update_chat_message as sqlite_update_chat_message,
41
- add_chat_message as sqlite_add_chat_message,
42
- get_chat_messages as sqlite_get_chat_messages,
43
- search_chat_conversations as sqlite_search_chat_conversations,
44
- create_chat_conversation as sqlite_create_chat_conversation,
45
- save_chat_history_to_database as sqlite_save_chat_history_to_database,
46
- view_database as sqlite_view_database,
47
- get_transcripts as sqlite_get_transcripts,
48
- get_trashed_items as sqlite_get_trashed_items,
49
- user_delete_item as sqlite_user_delete_item,
50
- empty_trash as sqlite_empty_trash,
51
- create_automated_backup as sqlite_create_automated_backup,
52
- add_or_update_prompt as sqlite_add_or_update_prompt,
53
- load_prompt_details as sqlite_load_prompt_details,
54
- load_preset_prompts as sqlite_load_preset_prompts,
55
- insert_prompt_to_db as sqlite_insert_prompt_to_db,
56
- delete_prompt as sqlite_delete_prompt,
57
- search_and_display_items as sqlite_search_and_display_items,
58
- get_conversation_name as sqlite_get_conversation_name,
59
- add_media_with_keywords as sqlite_add_media_with_keywords,
60
- check_media_and_whisper_model as sqlite_check_media_and_whisper_model,
61
- DatabaseError
62
- )
63
-
64
- class Database:
65
- def __init__(self, db_path=None):
66
- self.db_path = db_path or os.getenv('DB_NAME', 'media_summary.db')
67
- self.pool = []
68
- self.pool_size = 10
69
-
70
- @contextmanager
71
- def get_connection(self):
72
- retry_count = 5
73
- retry_delay = 1
74
- conn = None
75
- while retry_count > 0:
76
- try:
77
- conn = self.pool.pop() if self.pool else sqlite3.connect(self.db_path, check_same_thread=False)
78
- yield conn
79
- self.pool.append(conn)
80
- return
81
- except sqlite3.OperationalError as e:
82
- if 'database is locked' in str(e):
83
- logging.warning(f"Database is locked, retrying in {retry_delay} seconds...")
84
- retry_count -= 1
85
- sleep(retry_delay)
86
- else:
87
- raise DatabaseError(f"Database error: {e}")
88
- except Exception as e:
89
- raise DatabaseError(f"Unexpected error: {e}")
90
- finally:
91
- # Ensure the connection is returned to the pool even on failure
92
- if conn and conn not in self.pool:
93
- self.pool.append(conn)
94
- raise DatabaseError("Database is locked and retries have been exhausted")
95
-
96
- def execute_query(self, query: str, params: Tuple = ()) -> None:
97
- with self.get_connection() as conn:
98
- try:
99
- cursor = conn.cursor()
100
- cursor.execute(query, params)
101
- conn.commit()
102
- except sqlite3.Error as e:
103
- raise DatabaseError(f"Database error: {e}, Query: {query}")
104
-
105
- def close_all_connections(self):
106
- for conn in self.pool:
107
- conn.close()
108
- self.pool.clear()
109
-
110
- def get_db_config():
111
- config = configparser.ConfigParser()
112
- config.read('config.txt')
113
- return {
114
- 'type': config['Database']['type'],
115
- 'sqlite_path': config.get('Database', 'sqlite_path', fallback='media_summary.db'),
116
- 'elasticsearch_host': config.get('Database', 'elasticsearch_host', fallback='localhost'),
117
- 'elasticsearch_port': config.getint('Database', 'elasticsearch_port', fallback=9200)
118
- }
119
-
120
- db_config = get_db_config()
121
- db_type = db_config['type']
122
-
123
- if db_type == 'sqlite':
124
- # Use the config path if provided, otherwise fall back to default
125
- db = Database(db_config.get('sqlite_path'))
126
- elif db_type == 'elasticsearch':
127
- es = Elasticsearch([{
128
- 'host': db_config['elasticsearch_host'],
129
- 'port': db_config['elasticsearch_port']
130
- }])
131
- else:
132
- raise ValueError(f"Unsupported database type: {db_type}")
133
-
134
- db_path = db_config['sqlite_path']
135
-
136
- # Update this path to the directory where you want to store the database backups
137
- backup_dir = os.environ.get('DB_BACKUP_DIR', 'path/to/backup/directory')
138
-
139
-
140
-
141
-
142
- if db_type == 'sqlite':
143
- conn = sqlite3.connect(db_config['sqlite_path'])
144
- cursor = conn.cursor()
145
- elif db_type == 'elasticsearch':
146
- es = Elasticsearch([{
147
- 'host': db_config['elasticsearch_host'],
148
- 'port': db_config['elasticsearch_port']
149
- }])
150
- else:
151
- raise ValueError(f"Unsupported database type: {db_type}")
152
-
153
- ############################################################################################################
154
- #
155
- # DB-Searching functions
156
-
157
- def view_database(*args, **kwargs):
158
- if db_type == 'sqlite':
159
- return sqlite_view_database(*args, **kwargs)
160
- elif db_type == 'elasticsearch':
161
- # Implement Elasticsearch version
162
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
163
-
164
- def search_and_display_items(*args, **kwargs):
165
- if db_type == 'sqlite':
166
- return sqlite_search_and_display_items(*args, **kwargs)
167
- elif db_type == 'elasticsearch':
168
- # Implement Elasticsearch version
169
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
170
-
171
- def search_and_display(*args, **kwargs):
172
- if db_type == 'sqlite':
173
- return sqlite_search_and_display(*args, **kwargs)
174
- elif db_type == 'elasticsearch':
175
- # Implement Elasticsearch version
176
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
177
-
178
- #
179
- # End of DB-Searching functions
180
- ############################################################################################################
181
-
182
- ############################################################################################################
183
- #
184
- # Transcript-related Functions
185
-
186
- def get_transcripts(*args, **kwargs):
187
- if db_type == 'sqlite':
188
- return sqlite_get_transcripts(*args, **kwargs)
189
- elif db_type == 'elasticsearch':
190
- # Implement Elasticsearch version
191
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
192
-
193
- #
194
- # End of Transcript-related Functions
195
- ############################################################################################################
196
-
197
- ############################################################################################################
198
- #
199
- # DB-Ingestion functions
200
-
201
- def add_media_to_database(*args, **kwargs):
202
- if db_type == 'sqlite':
203
- return sqlite_add_media_to_database(*args, **kwargs)
204
- elif db_type == 'elasticsearch':
205
- # Implement Elasticsearch version
206
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
207
-
208
-
209
- def import_obsidian_note_to_db(*args, **kwargs):
210
- if db_type == 'sqlite':
211
- return sqlite_import_obsidian_note_to_db(*args, **kwargs)
212
- elif db_type == 'elasticsearch':
213
- # Implement Elasticsearch version
214
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
215
-
216
- def update_media_content(*args, **kwargs):
217
- if db_type == 'sqlite':
218
- return sqlite_update_media_content(*args, **kwargs)
219
- elif db_type == 'elasticsearch':
220
- # Implement Elasticsearch version
221
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
222
-
223
- def add_media_with_keywords(*args, **kwargs):
224
- if db_type == 'sqlite':
225
- return sqlite_add_media_with_keywords(*args, **kwargs)
226
- elif db_type == 'elasticsearch':
227
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
228
-
229
- def check_media_and_whisper_model(*args, **kwargs):
230
- if db_type == 'sqlite':
231
- return sqlite_check_media_and_whisper_model(*args, **kwargs)
232
- elif db_type == 'elasticsearch':
233
- raise NotImplementedError("Elasticsearch version of check_media_and_whisper_model not yet implemented")
234
-
235
- def ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt):
236
- if db_type == 'sqlite':
237
- return sqlite_ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt)
238
- elif db_type == 'elasticsearch':
239
- # Implement Elasticsearch version
240
- raise NotImplementedError("Elasticsearch version of ingest_article_to_db not yet implemented")
241
- else:
242
- raise ValueError(f"Unsupported database type: {db_type}")
243
-
244
- #
245
- # End of DB-Ingestion functions
246
- ############################################################################################################
247
-
248
-
249
- ############################################################################################################
250
- #
251
- # Prompt-related functions
252
-
253
- def list_prompts(*args, **kwargs):
254
- if db_type == 'sqlite':
255
- return sqlite_list_prompts(*args, **kwargs)
256
- elif db_type == 'elasticsearch':
257
- # Implement Elasticsearch version
258
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
259
-
260
-
261
- def fetch_prompt_details(*args, **kwargs):
262
- if db_type == 'sqlite':
263
- return sqlite_fetch_prompt_details(*args, **kwargs)
264
- elif db_type == 'elasticsearch':
265
- # Implement Elasticsearch version
266
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
267
-
268
- def add_prompt(*args, **kwargs):
269
- if db_type == 'sqlite':
270
- return sqlite_add_prompt(*args, **kwargs)
271
- elif db_type == 'elasticsearch':
272
- # Implement Elasticsearch version
273
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
274
-
275
-
276
- def add_or_update_prompt(*args, **kwargs):
277
- if db_type == 'sqlite':
278
- return sqlite_add_or_update_prompt(*args, **kwargs)
279
- elif db_type == 'elasticsearch':
280
- # Implement Elasticsearch version
281
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
282
-
283
- def load_prompt_details(*args, **kwargs):
284
- if db_type == 'sqlite':
285
- return sqlite_load_prompt_details(*args, **kwargs)
286
- elif db_type == 'elasticsearch':
287
- # Implement Elasticsearch version
288
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
289
-
290
- def load_preset_prompts(*args, **kwargs):
291
- if db_type == 'sqlite':
292
- return sqlite_load_preset_prompts(*args, **kwargs)
293
- elif db_type == 'elasticsearch':
294
- # Implement Elasticsearch version
295
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
296
-
297
- def insert_prompt_to_db(*args, **kwargs):
298
- if db_type == 'sqlite':
299
- return sqlite_insert_prompt_to_db(*args, **kwargs)
300
- elif db_type == 'elasticsearch':
301
- # Implement Elasticsearch version
302
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
303
-
304
- def delete_prompt(*args, **kwargs):
305
- if db_type == 'sqlite':
306
- return sqlite_delete_prompt(*args, **kwargs)
307
- elif db_type == 'elasticsearch':
308
- # Implement Elasticsearch version
309
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
310
-
311
- #
312
- # End of Prompt-related functions
313
- ############################################################################################################
314
-
315
- ############################################################################################################
316
- #
317
- # Keywords-related Functions
318
-
319
- def keywords_browser_interface(*args, **kwargs):
320
- if db_type == 'sqlite':
321
- return sqlite_keywords_browser_interface(*args, **kwargs)
322
- elif db_type == 'elasticsearch':
323
- # Implement Elasticsearch version
324
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
325
-
326
- def add_keyword(*args, **kwargs):
327
- if db_type == 'sqlite':
328
- with db.get_connection() as conn:
329
- cursor = conn.cursor()
330
- return sqlite_add_keyword(*args, **kwargs)
331
- elif db_type == 'elasticsearch':
332
- # Implement Elasticsearch version
333
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
334
-
335
- def delete_keyword(*args, **kwargs):
336
- if db_type == 'sqlite':
337
- return sqlite_delete_keyword(*args, **kwargs)
338
- elif db_type == 'elasticsearch':
339
- # Implement Elasticsearch version
340
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
341
-
342
- def export_keywords_to_csv(*args, **kwargs):
343
- if db_type == 'sqlite':
344
- return sqlite_export_keywords_to_csv(*args, **kwargs)
345
- elif db_type == 'elasticsearch':
346
- # Implement Elasticsearch version
347
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
348
-
349
- #
350
- # End of Keywords-related Functions
351
- ############################################################################################################
352
-
353
- ############################################################################################################
354
- #
355
- # Chat-related Functions
356
-
357
- def delete_chat_message(*args, **kwargs):
358
- if db_type == 'sqlite':
359
- return sqlite_delete_chat_message(*args, **kwargs)
360
- elif db_type == 'elasticsearch':
361
- # Implement Elasticsearch version
362
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
363
-
364
- def update_chat_message(*args, **kwargs):
365
- if db_type == 'sqlite':
366
- return sqlite_update_chat_message(*args, **kwargs)
367
- elif db_type == 'elasticsearch':
368
- # Implement Elasticsearch version
369
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
370
-
371
- def add_chat_message(*args, **kwargs):
372
- if db_type == 'sqlite':
373
- return sqlite_add_chat_message(*args, **kwargs)
374
- elif db_type == 'elasticsearch':
375
- # Implement Elasticsearch version
376
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
377
-
378
- def get_chat_messages(*args, **kwargs):
379
- if db_type == 'sqlite':
380
- return sqlite_get_chat_messages(*args, **kwargs)
381
- elif db_type == 'elasticsearch':
382
- # Implement Elasticsearch version
383
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
384
-
385
- def search_chat_conversations(*args, **kwargs):
386
- if db_type == 'sqlite':
387
- return sqlite_search_chat_conversations(*args, **kwargs)
388
- elif db_type == 'elasticsearch':
389
- # Implement Elasticsearch version
390
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
391
-
392
- def create_chat_conversation(*args, **kwargs):
393
- if db_type == 'sqlite':
394
- return sqlite_create_chat_conversation(*args, **kwargs)
395
- elif db_type == 'elasticsearch':
396
- # Implement Elasticsearch version
397
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
398
-
399
- def save_chat_history_to_database(*args, **kwargs):
400
- if db_type == 'sqlite':
401
- return sqlite_save_chat_history_to_database(*args, **kwargs)
402
- elif db_type == 'elasticsearch':
403
- # Implement Elasticsearch version
404
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
405
-
406
- def get_conversation_name(*args, **kwargs):
407
- if db_type == 'sqlite':
408
- return sqlite_get_conversation_name(*args, **kwargs)
409
- elif db_type == 'elasticsearch':
410
- # Implement Elasticsearch version
411
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
412
-
413
- #
414
- # End of Chat-related Functions
415
- ############################################################################################################
416
-
417
- ############################################################################################################
418
- #
419
- # Trash-related Functions
420
-
421
- def get_trashed_items(*args, **kwargs):
422
- if db_type == 'sqlite':
423
- return sqlite_get_trashed_items(*args, **kwargs)
424
- elif db_type == 'elasticsearch':
425
- # Implement Elasticsearch version
426
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
427
-
428
- def user_delete_item(*args, **kwargs):
429
- if db_type == 'sqlite':
430
- return sqlite_user_delete_item(*args, **kwargs)
431
- elif db_type == 'elasticsearch':
432
- # Implement Elasticsearch version
433
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
434
-
435
- def empty_trash(*args, **kwargs):
436
- if db_type == 'sqlite':
437
- return sqlite_empty_trash(*args, **kwargs)
438
- elif db_type == 'elasticsearch':
439
- # Implement Elasticsearch version
440
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
441
-
442
- #
443
- # End of Trash-related Functions
444
- ############################################################################################################
445
-
446
- ############################################################################################################
447
- #
448
- # DB-Backup Functions
449
-
450
- def create_automated_backup(*args, **kwargs):
451
- if db_type == 'sqlite':
452
- return sqlite_create_automated_backup(*args, **kwargs)
453
- elif db_type == 'elasticsearch':
454
- # Implement Elasticsearch version
455
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
456
-
457
- #
458
- # End of DB-Backup Functions
459
- ############################################################################################################
460
-
461
- ############################################################################################################
462
- #
463
- # Function to close the database connection for SQLite
464
-
465
- def close_connection():
466
- if db_type == 'sqlite':
467
- db.close_all_connections()
468
- # Elasticsearch doesn't need explicit closing
469
-
470
- #
471
- # End of file
472
- ############################################################################################################