|
1 | | -""" |
2 | | -scheduler.py (v0.4) |
3 | | -------------------- |
4 | | -Core scheduler logic for SchedPlus (v0.4). |
5 | | -
|
6 | | -This module provides the `Task` dataclass and the `Scheduler` class. |
7 | | -The `Scheduler` is responsible for in-memory task management and |
8 | | -exposes small wrapper methods to persist/load tasks via the storage |
9 | | -layer so UIs (Tkinter/PyQt) do not need to know storage paths. |
10 | | -""" |
11 | | - |
12 | | -import uuid |
13 | | -from dataclasses import dataclass, field |
14 | | -from typing import List |
15 | | -from datetime import datetime |
16 | | - |
17 | | -@dataclass |
18 | | -class Task: |
19 | | - id: str = field(default_factory=lambda: str(uuid.uuid4())) |
20 | | - date: str = "" |
21 | | - time: str = "" |
22 | | - text: str = "" |
23 | | - createdAt: str = field(default_factory=lambda: datetime.utcnow().isoformat()) |
24 | | - updatedAt: str = field(default_factory=lambda: datetime.utcnow().isoformat()) |
25 | | - |
26 | | - def to_dict(self): |
27 | | - return { |
28 | | - "id": self.id, |
29 | | - "date": self.date, |
30 | | - "time": self.time, |
31 | | - "text": self.text, |
32 | | - "createdAt": self.createdAt, |
33 | | - "updatedAt": self.updatedAt, |
34 | | - } |
35 | | - |
36 | | - @classmethod |
37 | | - def from_dict(cls, data: dict): |
38 | | - return cls( |
39 | | - id=data.get("id", str(uuid.uuid4())), |
40 | | - date=data.get("date", ""), |
41 | | - time=data.get("time", ""), |
42 | | - text=data.get("text", ""), |
43 | | - createdAt=data.get("createdAt", datetime.utcnow().isoformat()), |
44 | | - updatedAt=data.get("updatedAt", datetime.utcnow().isoformat()), |
45 | | - ) |
46 | | -class Scheduler: |
47 | | - """ |
48 | | - The Scheduler class manages a list of tasks. |
49 | | - In v0.4, tasks are stored in memory and the class exposes |
50 | | - simple `save_tasks`/`load_tasks` helpers that delegate to the |
51 | | - storage layer. This keeps UIs decoupled from storage details. |
52 | | - """ |
53 | | - |
54 | | - def __init__(self): |
55 | | - self.tasks: List[Task] = [] |
56 | | - |
57 | | - def add_task(self, date: str, time: str, text: str): |
58 | | - """ |
59 | | - Add a new task to the scheduler. |
60 | | - No validation yet — this will be added in v0.2+. |
61 | | - """ |
62 | | - task = Task(date=date, time=time, text=text) |
63 | | - self.tasks.append(task) |
64 | | - |
65 | | - def save_tasks(self, filepath: str = None): |
66 | | - """ |
67 | | - Persist current tasks using the storage layer. |
68 | | -
|
69 | | - This method performs a local import to avoid import cycles |
70 | | - between `logic.storage` and `logic.scheduler`. |
71 | | - """ |
72 | | - try: |
73 | | - from . import storage as _storage |
74 | | - |
75 | | - _storage.save_tasks(self.tasks, filepath) if filepath else _storage.save_tasks(self.tasks) |
76 | | - except Exception: |
77 | | - # Intentionally swallow errors here; storage will log on failure. |
78 | | - pass |
79 | | - |
80 | | - def load_tasks(self, filepath: str = None): |
81 | | - """ |
82 | | - Load tasks from the storage layer into `self.tasks`. |
83 | | - Returns the loaded list of tasks. |
84 | | - """ |
85 | | - try: |
86 | | - from . import storage as _storage |
87 | | - |
88 | | - self.tasks = _storage.load_tasks(filepath) |
89 | | - except Exception: |
90 | | - self.tasks = [] |
91 | | - |
92 | | - return self.tasks |
93 | | - |
94 | | - def get_tasks(self) -> List[Task]: |
95 | | - """Return the list of tasks.""" |
96 | | - return self.tasks |
| 1 | +""" |
| 2 | +scheduler.py (v0.4) |
| 3 | +------------------- |
| 4 | +Core scheduler logic for SchedPlus (v0.4). |
| 5 | +
|
| 6 | +This module provides the `Task` dataclass and the `Scheduler` class. |
| 7 | +The `Scheduler` is responsible for in-memory task management and |
| 8 | +exposes small wrapper methods to persist/load tasks via the storage |
| 9 | +layer so UIs (Tkinter/PyQt) do not need to know storage paths. |
| 10 | +""" |
| 11 | + |
| 12 | +import uuid |
| 13 | +from dataclasses import dataclass, field |
| 14 | +from typing import List |
| 15 | +from datetime import datetime |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class Task: |
| 19 | + id: str = field(default_factory=lambda: str(uuid.uuid4())) |
| 20 | + date: str = "" |
| 21 | + time: str = "" |
| 22 | + text: str = "" |
| 23 | + createdAt: str = field(default_factory=lambda: datetime.utcnow().isoformat()) |
| 24 | + updatedAt: str = field(default_factory=lambda: datetime.utcnow().isoformat()) |
| 25 | + |
| 26 | + def to_dict(self): |
| 27 | + return { |
| 28 | + "id": self.id, |
| 29 | + "date": self.date, |
| 30 | + "time": self.time, |
| 31 | + "text": self.text, |
| 32 | + "createdAt": self.createdAt, |
| 33 | + "updatedAt": self.updatedAt, |
| 34 | + } |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def from_dict(cls, data: dict): |
| 38 | + return cls( |
| 39 | + id=data.get("id", str(uuid.uuid4())), |
| 40 | + date=data.get("date", ""), |
| 41 | + time=data.get("time", ""), |
| 42 | + text=data.get("text", ""), |
| 43 | + createdAt=data.get("createdAt", datetime.utcnow().isoformat()), |
| 44 | + updatedAt=data.get("updatedAt", datetime.utcnow().isoformat()), |
| 45 | + ) |
| 46 | +class Scheduler: |
| 47 | + """ |
| 48 | + The Scheduler class manages a list of tasks. |
| 49 | + In v0.4, tasks are stored in memory and the class exposes |
| 50 | + simple `save_tasks`/`load_tasks` helpers that delegate to the |
| 51 | + storage layer. This keeps UIs decoupled from storage details. |
| 52 | + """ |
| 53 | + |
| 54 | + def __init__(self): |
| 55 | + self.tasks: List[Task] = [] |
| 56 | + |
| 57 | + def add_task(self, date: str, time: str, text: str): |
| 58 | + """ |
| 59 | + Add a new task to the scheduler. |
| 60 | + No validation yet — this will be added in v0.2+. |
| 61 | + """ |
| 62 | + task = Task(date=date, time=time, text=text) |
| 63 | + self.tasks.append(task) |
| 64 | + |
| 65 | + def save_tasks(self, filepath: str = None): |
| 66 | + """ |
| 67 | + Persist current tasks using the storage layer. |
| 68 | +
|
| 69 | + This method performs a local import to avoid import cycles |
| 70 | + between `logic.storage` and `logic.scheduler`. |
| 71 | + """ |
| 72 | + try: |
| 73 | + from . import storage as _storage |
| 74 | + |
| 75 | + _storage.save_tasks(self.tasks, filepath) if filepath else _storage.save_tasks(self.tasks) |
| 76 | + except Exception: |
| 77 | + # Intentionally swallow errors here; storage will log on failure. |
| 78 | + pass |
| 79 | + |
| 80 | + def load_tasks(self, filepath: str = None): |
| 81 | + """ |
| 82 | + Load tasks from the storage layer into `self.tasks`. |
| 83 | + Returns the loaded list of tasks. |
| 84 | + """ |
| 85 | + try: |
| 86 | + from . import storage as _storage |
| 87 | + |
| 88 | + self.tasks = _storage.load_tasks(filepath) |
| 89 | + except Exception: |
| 90 | + self.tasks = [] |
| 91 | + |
| 92 | + return self.tasks |
| 93 | + |
| 94 | + def get_tasks(self) -> List[Task]: |
| 95 | + """Return the list of tasks.""" |
| 96 | + return self.tasks |
0 commit comments