-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbaseHandler.py
More file actions
73 lines (60 loc) · 2.15 KB
/
Copy pathbaseHandler.py
File metadata and controls
73 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from pathlib import Path
from typing import Optional
class Handler:
def __init__(self) -> None:
self.dbPathOrUrl: str = ""
def getDbPathOrUrl(self) -> str:
return self.dbPathOrUrl
def setDbPathOrUrl(self, pathOrUrl: str) -> bool:
if pathOrUrl is None:
return False
# small safety improvement
self.dbPathOrUrl = str(pathOrUrl).strip()
return True
class UploadHandler(Handler):
"""
Generic upload dispatcher.
- Verifies that a DB path/URL is set.
- Chooses the correct concrete upload handler based on file extension.
- Imports concrete handlers dynamically to avoid circular imports.
- Returns True on success, False on failure.
"""
def __init__(self) -> None:
super().__init__()
def _choose_handler_class(self, suffix: str):
"""
Map file suffix to the concrete handler class.
Import modules dynamically to avoid circular imports.
"""
s = suffix.lower() # safer
if s == ".csv":
from li import JournalUploadHandler
return JournalUploadHandler
if s == ".json":
from daniele import CategoryUploadHandler
return CategoryUploadHandler
return None
def pushDataToDb(self, path: str) -> bool:
db_path = self.getDbPathOrUrl()
if not db_path:
print("Error: No database path or URL provided. Call setDbPathOrUrl() first.")
return False
if not path:
print("Error: No file path provided.")
return False
p = Path(path)
if not p.exists():
print(f"Error: File not found: {path}")
return False
handler_cls = self._choose_handler_class(p.suffix)
if handler_cls is None:
print(f"Error: Unsupported file format: {p.suffix}")
return False
try:
handler = handler_cls()
handler.setDbPathOrUrl(db_path)
result = handler.pushDataToDb(path)
return bool(result)
except Exception as e:
print(f"Error while pushing data to DB: {e}")
return False