commit 57b4906763c4957106bd4b445fee2cc21b5ef826
parent 228ff8ba2308be3394cfcc71e173cef5ce86b318
Author: Pr0fiTX <uventik999@gmail.com>
Date: Sun, 6 Jul 2025 13:49:52 +0300
[NEW]: Implement ability to create DB file with flag (-f).
Diffstat:
3 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/parser.py b/parser.py
@@ -10,6 +10,7 @@ def parseit():
parser.add_argument("-c", "--create", nargs='*', metavar=('ARGS'),
help="You can provide NAME, DESCRIPTION, EXPIRATION_DATE to create a new task")
+ parser.add_argument("-f", "--db-create", action="store_true", help="Create a new DB file (if doesn't exist yet)")
# parser.add_argument("-c", "--create", nargs='*', metavar=('NAME',
# 'DESCRIPTION', 'EXPIRATION_DATE'), help="Provide NAME, DESCRIPTION, EXPIRATION_DATE to create a new task")
parser.add_argument('-r', '--remove', nargs=1,
diff --git a/pydoto.py b/pydoto.py
@@ -15,6 +15,9 @@ def main():
# Saving Task to DB
task.db_append()
return
+ if args.db_create: # -f
+ tm.Task.create_db()
+ return
if args.remove: # -r ID
tm.Task.mark_deleted(args.remove[0])
return
diff --git a/tasksmanage.py b/tasksmanage.py
@@ -99,22 +99,18 @@ class Task:
def db_load(cls) -> list:
"""Load tasks from .json into list (AS CLASSES)"""
if not os.path.exists(DB_NAME):
- print(f"!> File {DB_NAME} doesn't exist")
-
- return []
+ print(f"!> File {DB_NAME} doesn't exist. Check --help menu.")
+ exit(1)
try:
with open(DB_NAME, "r", encoding='utf-8') as f:
tasks_data = js.load(f)
if not isinstance(tasks_data, list):
tasks_data = [tasks_data]
-
return [cls.from_dict(task_data) for task_data in tasks_data]
-
except (js.JSONDecodeError, IOError) as e:
print(f"!> Can't read {DB_NAME}: {e}")
-
- return []
+ exit(1)
@classmethod
def db_dump(cls, data):
@@ -124,12 +120,16 @@ class Task:
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}")
+ if not os.path.exists(DB_NAME):
+ print(f"!> File {DB_NAME} doesn't exist. Check --help menu.")
+ exit(1)
+
+ 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}")
+ exit(1)
@classmethod
def print_active(cls):
@@ -171,6 +171,17 @@ class Task:
if not cls.check_for_emptiness(tasks):
tp.TaskPrinter.print_tasks_all(tasks)
+ @classmethod
+ def create_db(cls):
+ """Creates DB file (if doesn't exist)"""
+ with open(DB_NAME, "w"):
+ pass
+
+ if os.path.exists(DB_NAME):
+ print("=> DB succesfuly created")
+ else:
+ print("!> Can't create DB")
+
@staticmethod
def check_for_emptiness(tasks) -> bool:
"""Is .json empty?"""