-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_ideas.py
157 lines (130 loc) · 4.21 KB
/
py_ideas.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
# Copyright (c) 2021.
import configparser
import os
version = "1.2.0"
config_dir = os.path.expanduser('~') + "/.config/mydea"
os.makedirs(config_dir, 0o777, True)
config_path = config_dir + "/conf.ini"
config = configparser.ConfigParser()
config["DEFAULT"] = {
"IPath": config_dir + "/py_idea_list.txt",
"IsSilent": "y"
}
default_config = config["DEFAULT"]
def toggle_silent():
if config["DEFAULT"]["IsSilent"] == "y":
config["DEFAULT"]["IsSilent"] = "n"
else:
config["DEFAULT"]["IsSilent"] = "y"
def is_silent():
return config["DEFAULT"]["IsSilent"] == "y"
def update_path():
config["DEFAULT"]["IPath"] = path
def save_config():
f = open(config_path, "w")
config.write(f)
f.flush()
f.close()
print("Config saved.")
def init_config():
if not os.path.exists(config_path):
save_config()
print("The configuration file has been created at:\n\t" + config_path)
else:
print("Loading config...")
config.read(config_path)
init_config()
path = config["DEFAULT"]["IPath"]
if not os.path.exists(path):
open(path, "w").close()
print("Idea text file has been created at: \n\t" + path)
def read_ideas():
return open(path, "r").readlines()
def wipe_ideas():
if os.path.exists(path):
os.remove(path)
def add_idea(text):
f = open(path, "a+")
f.write(text + "\n")
f.flush()
f.close()
if not is_silent():
print("-\nIdea added\n\n" + text + "\n-")
print("\nWelcome to mydea " + version + "!\nh for help\n\n")
while True:
while not os.path.exists(path):
c = input("The file could not be found (" + path + ")."
+ "\nType c to create a blank replacement with the same path"
+ " or q to quit.\n:")
c = c.lower()
if c == "q":
break
elif c == "c":
open(path, "w").close()
print("The file has been created and it is empty. Entering default idea space. Type h for help.")
elif c == "h":
print("- Options -\n* q to quit."
+ "\n* c to create the file.")
else:
print("Tou entered an invalid option.")
continue
c = input(":")
if not len(c) < 3:
add_idea(c)
continue
c = c.lower()
if c in ["q", "quit", "e", "exit"]:
break
elif c in ["h", "help", "-h", "-help", "--h", "--help"]:
silentPlaceholder = "On"
if not is_silent():
silentPlaceholder = "Off"
print("-Help-\nType a sentence to add it as an idea, or type a"
+ "\nletter from the list below to execute a command."
+ "\n-Commands-\n* r to read ideas"
+ "\n* p to change the global file path for ideas"
+ "\n* s to toggle silent mode (" + silentPlaceholder
+ ")\n* c! to delete the file."
+ "\n* q to quit")
continue
elif c == "p":
print("Changing the system-wide filepath to store ideas in..."
+ "\nThe current path is " + path)
c = input("Enter the new path in full or type c to cancel.")
c = c.lower()
if c == "c":
print("Operation cancelled.")
else:
if not os.path.exists(c):
try:
open(c, "w")
print("Successfully created the file!"
+ " You are now using the file " + c)
path = c
update_path()
except OSError:
print("Error! Could not create the file!"
+ " Make sure any directories exist.")
else:
print("Successfully switched to " + c)
path = c
update_path()
continue
elif c == "c!":
print("Clearing file...")
wipe_ideas()
elif c == "r":
print("")
for idea in read_ideas():
print(" " + idea)
continue
elif c == "s":
toggle_silent()
elif len(c) > 0:
print(" Invalid command. Your idea should have a minimum length of 3 characters.")
continue
else:
continue
print(" - Done!")
print("Saving settings...")
save_config()