-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
147 lines (133 loc) · 3.52 KB
/
tools.py
File metadata and controls
147 lines (133 loc) · 3.52 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""Some tools for the project"""
import platform
import os
import getpass
import sys
import json
from decorators import *
import config
# ================================
# Input
# ================================
# ================================
# ================================
def _sinput(ps="~> "):
"""Smart Input handles errors"""
try:
npt = input(ps).strip()
return npt
except (KeyboardInterrupt, EOFError):
sys.exit()
def _secured_sinput(ps="~> "):
try:
npt = getpass.getpass(ps).strip()
return npt
except (KeyboardInterrupt, EOFError):
sys.exit()
# ================================
# Clear console
# ================================
# ================================
# ================================
if platform.system().lower() == "windows":
def _clear_console():
"""Clears the console"""
os.system("cls")
else:
def _clear_console():
"""Clears the console"""
os.system("clear")
# ================================
# Help function
# ================================
# ================================
# ================================
def _print_help_info():
"""Prints help pages"""
print("Project on github: https://github.com/javid-aliyev/Todo-list-application-Python")
print("The documentation is in doc/ repo's dir")
for index, command in enumerate(config.COMMANDS, 1):
print(f"{index}. {command}")
# ================================
# JSON
# ================================
# ================================
# ================================
def get_json_from_file(path):
"""Fetches data from a json file
param path is path to file
:param path: str
:return: str
"""
with open(path, "rt") as jfl:
return json.load(jfl)
def save_json_to_file(path, data):
"""Saves new data to a json file
param path is path to file
param data is json
:param path: str
:param data: str
"""
with open(path, "wt") as jfl:
json.dump(data, jfl, indent=4)
# ================================
# Messages
# ================================
# ================================
# ================================
@red_output
def warn(*args, **kwargs):
"""Displays a warning
:param args: list (stores str elements)
:param kwargs: dict (stores extra keyword params of the print function)
"""
print(*args, **kwargs)
error = warn # error function
@blue_output
def info(*args, **kwargs):
"""Displays an information
:param args: list (stores str elements)
:param kwargs: dict (stores extra keyword params of the print function)
"""
print(*args, **kwargs)
@green_output
def success(*args, **kwargs):
"""Displays a success message
:param args: list (stores str elements)
:param kwargs: dict (stores extra keyword params of the print function)
"""
print(*args, **kwargs)
# ================================
# Other
# ================================
# ================================
# ================================
def db_exists():
"""Returns True if a db exists, otherwise False"""
return os.path.exists(config.DB_PATH)
def process_task_or_index(id2task, command, account, fn, done=None):
# FIX: THIS FUNCTION IS NOT READABLE
if command == "rm":
task = _sinput("task to remove? ").strip()
try:
if task[0] == "\\":
fn(task[1:], account)
else:
try:
fn(id2task.get(int(task))[0], account)
except (ValueError, TypeError):
return
except IndexError:
pass
else:
task = _sinput("task? ").strip()
try:
if task[0] == "\\":
fn(task[1:], account, done=done)
else:
try:
fn(id2task.get(int(task))[0], account, done=done)
except ValueError:
pass
except IndexError:
pass