pydoto-cli

Simple ToDo-like application, that just works. Add any tasks without closing your terminal.
git clone https://git.nfu.su/pydoto-cli.git
Log | Files | Refs | README

commit 25119dfcf0493d7089a54fce8d4eec9280df4e4a
parent 9989805df2c44b35585cf98611c92b0aeb144cfc
Author: Pr0fiTX <uventik999@gmail.com>
Date:   Wed, 11 Jun 2025 13:31:45 +0300

[REFACTORING]: Delite repiting pices of code

Diffstat:
Mpydoto.py | 21++++++++++++++++-----
Mtasksmanage.py | 82++++++++++++++++++++++++++++++-------------------------------------------------
2 files changed, 47 insertions(+), 56 deletions(-)

diff --git a/pydoto.py b/pydoto.py @@ -6,19 +6,30 @@ def main(): # Parse CLI flags args = ps.parseit() - if args.create: - # task = tm.Task(args.create[0]) + if args.create: # -c NAME DESCRIPTION EXPIRATION_DATE task = tm.Task() + # Chacking Arguments Count task.name = args.create[0] if len(args.create) > 0 else "Defaul Task" task.description = args.create[1] if len(args.create) > 1 else None task.expiration_date = ps.parse_expiration_date(args.create[2]) if len(args.create) > 2 else None - task.save_to_json() + # Saving Task to DB + task.db_append() return - if args.remove: + if args.remove: # -r ID pass - if args.done: + if args.done: # -d ID + pass + if args.print_tasks: # -a + pass + if args.print_all: # -A + pass + if args.id: # -i + pass + if args.all_id: # -I pass else: + # Execute witheout any flags + # Prints only ACTIVE tasks tm.Task.print_active() return diff --git a/tasksmanage.py b/tasksmanage.py @@ -21,22 +21,15 @@ class Task: self.expiration_date = expiration_date self.status = TaskStatus.ACTIVE - def check_expiration(self): # WARN: - if (self.expiration_date and self.expiration_date < datetime.now() and self.status == TaskStatus.ACTIVE): - self.status = TaskStatus.EXPIRED - - return self.status - def complete(self): - self.check_expiration() if (self.status == TaskStatus.DELETED): - raise ValueError("!> You can't complite deleted task") + raise ValueError("!> You can't complete deleted task") else: self.status = TaskStatus.COMPLETED return self.status - def to_dict(self): + def to_dict(self) -> dict: return { "name": self.name, "description": self.description, @@ -45,56 +38,31 @@ class Task: "expiration_date": self.expiration_date.isoformat() if self.expiration_date else None } - def save_to_json(self): - """Load and save to json""" + def db_append(self): + """Append task to DB""" tasks = [] - - if os.path.exists(DB_NAME): - try: - with open(DB_NAME, 'r', encoding='utf-8') as f: - tasks = js.load(f) - if not isinstance(tasks, list): - tasks = [tasks] - except (js.JSONDecodeError, IOError) as e: - print(f"!> Can't read the file {DB_NAME}: {e}") - - tasks.append(self.to_dict()) - - try: - with open(DB_NAME, 'w', encoding='utf-8') as f: - js.dump(tasks, f, ensure_ascii=False, indent=4) - except IOError as e: - print(f"!> Can't write to the file {DB_NAME}: {e}") + tasks = Task.db_load() + tasks.append(self) + Task.db_dump(tasks) @classmethod def update_expiration_statuses(cls): tasks = [] tasks_new = [] - if os.path.exists(DB_NAME): - try: - with open(DB_NAME, 'r', encoding='utf-8') as f: - tasks = js.load(f) - if not isinstance(tasks, list): - tasks = [tasks] - except (js.JSONDecodeError, IOError) as e: - print(f"!> Can't read the file {DB_NAME}: {e}") + tasks = cls.db_load() - for task in tasks: - task = cls.from_dict(task) - if (task.expiration_date and task.status == TaskStatus.ACTIVE and task.expiration_date < datetime.now()): - task.status = TaskStatus.EXPIRED + for task in tasks: + if (task.expiration_date and task.status == TaskStatus.ACTIVE and task.expiration_date < datetime.now()): + task.status = TaskStatus.EXPIRED - tasks_new.append(task.to_dict()) + tasks_new.append(task) - try: - with open(DB_NAME, 'w', encoding='utf-8') as f: - js.dump(tasks_new, f, ensure_ascii=False, indent=4) - except IOError as e: - print(f"!> Can't write to the file {DB_NAME}: {e}") + cls.db_dump(tasks_new) @classmethod def from_dict(cls, data): + """Convert ONE dict -> Task""" expiration_date = datetime.fromisoformat(data['expiration_date']) if data['expiration_date'] else None task = cls(data['name'], data['description'], expiration_date) task.creation_date = datetime.fromisoformat(data['creation_date']) @@ -103,7 +71,7 @@ class Task: return task @classmethod - def load_from_json(cls): + def db_load(cls) -> list: if not os.path.exists(DB_NAME): print(f"!> File {DB_NAME} doesn't exist") @@ -123,20 +91,32 @@ class Task: return [] @classmethod + def db_dump(cls, data): + data_dict = [] + + for task in data: + data_dict.append(cls.to_dict(task)) + + if os.path.exists(DB_NAME): + try: + with open(DB_NAME, 'w', encoding='utf-8') as f: + js.dump(data_dict, f, ensure_ascii=False, indent=4) + except IOError as e: + print(f"!> Can't write to the file {DB_NAME}: {e}") + + @classmethod def print_active(cls): # TODO: Prettier Output - tasks = cls.load_from_json() + cls.update_expiration_statuses() + tasks = cls.db_load() if not tasks: print("!> You don't have any active tasks") return print("=== ACTIVE TASKS ===") for i, task in enumerate(tasks, 1): - cls.update_expiration_statuses() if (task.status != TaskStatus.ACTIVE): continue print(f"{task.name}\t", f"{task.description}\t" if task.description else "-\t", f"{task.expiration_date}\t" if task.expiration_date else "-\t") - - return