-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwish.py
executable file
·346 lines (271 loc) · 10.6 KB
/
wish.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
import random
from typing import Optional
from rich.console import Console
import typer
from rich.table import Column, Table
from rich.theme import Theme
from pathlib import Path
import os
from typing_extensions import Annotated
# Define a color scheme for the console
color_scheme = Theme({
"#FF6B66": "#af00ff",
"warning": "magenta",
"danger": "bold #FF6B66"
})
# Create a Typer app instance, a Rich console instance, and a Rich table for displaying wish list data
app = typer.Typer()
console = Console(color_system="truecolor", theme=color_scheme)
table = Table(
Column("Wish", justify="left", width=30, style="#FFD166"),
Column("Category", justify="center", style="#06D6A0"),
Column("Completed?", justify="center", width=10),
header_style="#66C2FF bold",
title="My Wish List",
)
# Define metadata for the application
__app_name__ = "Wish"
__version__ = "0.0.2"
__save__ = Path.home() / "wish" / "wishlist"
# Ensure that the directory for saving wishlist files exists
os.makedirs(Path.home() / "wish", exist_ok=True)
# Function to convert table columns to rows
def get_rows():
rows = table.columns
vertical = []
for row in rows:
vertical.append([str(cell) for cell in row.cells])
out = []
try:
for count in range(len(vertical[0])):
out.append([vertical[row][count] for row in range(len(rows))])
except Exception:
return []
return out
# Function to load wishlist data from a file
def load():
try:
with open(__save__) as f:
lines = f.readlines()
except Exception:
with open(__save__, "w") as f:
pass
with open(__save__) as f:
lines = f.readlines()
for line in lines:
try:
line = line.replace("\n", "").split(" ")
category = line.pop(-2)
completed = line.pop(-1)
wish = " ".join(word for word in line)
if completed != "True" and completed != "False":
completed = "False"
table.add_row(wish, category, completed)
except Exception:
continue
# Function to save wishlist data to a file
def save(ignore: list = None, edit: list = None):
if ignore is None:
ignore = []
with open(__save__, "w") as f:
for row in get_rows():
if row[0] in ignore:
continue
if edit is not None:
if row[0] == edit[0]:
row[edit[1]] = edit[2]
f.write(" ".join(row) + "\n")
# Function to determine the sorting key for category
def sort_category(key, categories):
key = key[-2]
for idx, category in enumerate(categories):
if key == category:
return idx
return 1
# Function to determine the sorting key for completion completed
def sort_completed(key, categories):
return int(key)
# Function to get wishes
def get_wish(name: str):
rows = get_rows()
finds = []
name = name.lower()
for row in rows:
row = row[0]
if name in row.lower():
finds.append(row)
if len(finds) == 0:
return None
if len(finds) == 1:
return finds[0]
if len(finds) > 1:
for find in finds:
if find == name:
return find
return finds[0]
def random_prefix(prefix_list):
return random.choice(prefix_list)
def version_callback(value: bool):
if value:
console.print(f"\n👋 Hi, I am [#66C2FF bold]wish v{__version__}[/#66C2FF bold] created by [bold #66C2FF]levkush[/bold #66C2FF].\n\n🪄 A program that will make your wish list! 🔮\n")
raise typer.Exit()
# Load and save wishlist data
load()
save()
# Define the main command for the application
@app.callback()
def main(
version: Annotated[
Optional[bool], typer.Option("--version", "-v", callback=version_callback)
] = None,
):
return
# Define the 'add' command to create a new wish
@app.command(name="add", help="Add a new wish to the wish list.", no_args_is_help=True)
def add(name: str, category: str):
name = name.capitalize()
if get_wish(name) is not None:
print(f"\n\n🛑 Sorry, but wish '{name}' already exists in your wish list! 😔\n\n")
return
table.add_row(name, category, "False")
save()
print(f"\n✨ Wish '{name}' in category '{category}' added successfully! 🌟\n")
# Define the 'delete' command to delete a wish
@app.command(name="delete", help="Delete a wish from the wish list.", no_args_is_help=True)
def delete(name: str):
name = name.capitalize()
target = get_wish(name)
if target is None:
prefix = random_prefix(["[bold #FF6B66]Can't find it![/bold #FF6B66]", "[bold #FF6B66]Where is it?[/bold #FF6B66]", "[bold #FF6B66]Lost forever![/bold #FF6B66]", "[bold #FF6B66]It's gone![/bold #FF6B66]"])
console.print(f"\n🛑 {prefix} Wish [#66C2FF]'{name}'[/#66C2FF] is not in your wish list! 😔\n")
return
save(ignore=get_wish(name))
load()
prefix = random_prefix(["[bold #FF6B66]Throw away![/bold #FF6B66]", "[bold #FF6B66]Flaming hot![/bold #FF6B66]", "[bold #FF6B66]Into the bin![/bold #FF6B66]", "[bold #FF6B66]It belongs there![/bold #FF6B66]"])
console.print(f"\n🗑 {prefix} Wish [#66C2FF]'{target}'[/#66C2FF] deleted successfully! 🔥\n")
# Define the 'set' command to assign wish property to a value.
@app.command(name="set", help="Assign wish property to a value.", no_args_is_help=True)
def set_(
name: Annotated[str, typer.Argument(help="The name of the wish to edit")],
property: Annotated[str, typer.Argument(help="The edit property. Properties: category, completed, name.")],
value: Annotated[str, typer.Argument(help="The edit value. Values: <name>, <category>, yes, no.")]
):
name = name.capitalize()
target = get_wish(name)
value = value.capitalize()
property = property.lower()
if target is None:
prefix = random_prefix(["[bold #FF6B66]Can't find it![/bold #FF6B66]", "[bold #FF6B66]Where is it?[/bold #FF6B66]", "[bold #FF6B66]Lost forever![/bold #FF6B66]", "[bold #FF6B66]It's gone![/bold #FF6B66]"])
console.print(f"\n🛑 {prefix} Wish [#66C2FF]'{name}'[/#66C2FF] is not in your wish list! 😔\n")
return
if property == "completed":
if value == "Yes":
value = 'True'
elif value == "No":
value = 'False'
if value != "True" and value != "False":
value = "False"
save(edit=[target, 2, value])
elif property == "name":
save(edit=[target, 0, value])
elif property == "category":
save(edit=[target, 1, value.capitalize()])
else:
prefix = random_prefix(["[bold #FF6B66]Can't find it![/bold #FF6B66]", "[bold #FF6B66]Where is it?[/bold #FF6B66]", "[bold #FF6B66]Lost forever![/bold #FF6B66]", "[bold #FF6B66]It's gone![/bold #FF6B66]"])
console.print(f"\n🛑 {prefix} Property [#66C2FF]'{property}'[/#66C2FF] doesn't exist! 😔\n")
load()
prefix = random_prefix(["[bold #06D6A0]Edit Successful![/bold #06D6A0]", "[bold #06D6A0]Good as new![/bold #06D6A0]", "[bold #06D6A0]Changes![/bold #06D6A0]", "[bold #06D6A0]Misspelled?[/bold #06D6A0]"])
console.print(f"\n📝 {prefix} Wish [#66C2FF]'{target}'[/#66C2FF] property [#66C2FF]'{property}'[/#66C2FF] set to [#66C2FF]'{value}'[/#66C2FF] successfully! 🖊️\n")
# Define the 'list' command for displaying the wish list
@app.command(name="list", help="Display the wish list.")
def list_(
alpha: Optional[bool] = typer.Option(
True,
"--sort",
"-w",
help="Sort alphabetically by wish names.",
),
category: Optional[bool] = typer.Option(
False,
"--category",
"-c",
help="Sort alphabetically by category names.",
),
reverse: Optional[bool] = typer.Option(
False,
"--reverse",
"-r",
help="Sort in reverse.",
),
all: Optional[bool] = typer.Option(
None,
"--all",
"-a",
help="Show already completed wishes.",
)
):
rows_old = get_rows()
rows = []
if not all:
for row in rows_old:
if row[-1] == "False":
rows.append(row)
else:
rows = rows_old
sorted_rows = []
if all and category:
categories = sorted(set(row[-2] for row in rows))
sorted_rows = sorted(rows, key=lambda x: (int(x[-1] == "True"), sort_category(x, categories)), reverse=reverse)
elif all and alpha:
sorted_rows = sorted(rows, key=lambda x: (int(x[-1] == "True"), x), reverse=reverse)
elif category:
categories = sorted(set(row[-2] for row in rows))
sorted_rows = sorted(rows, key=lambda x: sort_category(x, categories), reverse=reverse)
elif alpha:
sorted_rows = sorted(rows, reverse=reverse)
# Create a new table for sorted data
sorted_table = Table(
Column("Wish", justify="left", width=30, style="#FFD166"),
Column("Category", justify="center", style="#06D6A0"),
Column("Completed?", justify="center", width=10),
header_style="#66C2FF bold",
title="My Wish List",
)
for row in sorted_rows:
category = row.pop(-2)
completed = row.pop(-1)
if completed == 'True':
completed = "✅"
else:
completed = "❌"
# Add a row to the sorted table
sorted_table.add_row(" ".join(row), category, completed)
# Display the sorted table
print("\n")
console.print(sorted_table)
print("\n")
# stats command
@app.command(name="stats", help="Display statistics about the wish list.")
def stats():
total_wishes = len(get_rows())
completed_wishes = sum(1 for row in get_rows() if row[2] == "True")
categories = {}
for row in get_rows():
category = row[1]
categories[category] = categories.get(category, 0) + 1
display_stats(total_wishes, completed_wishes, categories)
# stats func
def display_stats(total_wishes: int, completed_wishes: int, categories: dict):
stats_table = Table(title="Wish List Statistics")
stats_table.add_column("Statistic", justify="center", style="cyan", no_wrap=True)
stats_table.add_column("Value", justify="center", style="bold", no_wrap=True)
stats_table.add_row("Total number of wishes", str(total_wishes))
stats_table.add_row("Number of completed wishes", str(completed_wishes))
stats_table.add_row("Number of wishes per category", "")
for category, count in categories.items():
stats_table.add_row(f"- {category}", str(count))
console.print(stats_table)
# run
if __name__ == "__main__":
app()