-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_storage.py
More file actions
69 lines (59 loc) · 1.86 KB
/
Copy pathtask_storage.py
File metadata and controls
69 lines (59 loc) · 1.86 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
from abc import ABC, abstractmethod
import csv
from typing import List
from task import Task
class IStorage(ABC):
"""
Interfaz para guardar las tareas.
"""
@abstractmethod
def save(self, tasks: List[Task]) -> None:
pass
class IFileStorage(IStorage):
"""
Guarda las tareas en un archivo.
"""
def __init__(self, filename: str) -> None:
self.filename = filename
class IDBStorage(IStorage):
"""
Guarda las tareas en una base de datos.
"""
@abstractmethod
def health_check(self) -> bool:
pass
class TextFileStorage(IFileStorage):
"""
Guarda las tareas en un archivo de texto.
"""
def save(self, tasks: List[Task]) -> None:
with open(self.filename, "w") as f:
for task in tasks:
f.write(f"{task.id} - {task.name} - {task.user} - {task.completed}\n")
class CSVFileStorage(IFileStorage):
"""
Guarda las tareas en un archivo CSV.
"""
def save(self, tasks: List[Task]) -> None:
with open(self.filename, mode="w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["id", "name", "user", "completed"])
for task in tasks:
writer.writerow([task.id, task.name, task.user, task.completed])
class SQLiteDatabaseStorage(IFileStorage):
"""
Guarda las tareas en una base de datos tipo archivo.
"""
def save(self, tasks: List[Task]) -> None:
print(f"Guardando en base de datos {self.filename}...")
class MySQLDatabaseStorage(IDBStorage):
"""
Guarda las tareas en una base de datos tipo MySQL.
"""
def save(self, tasks: List[Task]) -> None:
print(f"Guardando en base de datos MYSQL...")
def health_check(self) -> bool:
"""
Verifica si el motor de la base de datos está funcionando.
"""
return True