-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkSlayerRemnote.py
More file actions
171 lines (142 loc) · 5.04 KB
/
LinkSlayerRemnote.py
File metadata and controls
171 lines (142 loc) · 5.04 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Welcome to Link Slayer. Please read README.md for more information
import os
import sys
from typing import List
MAIN_EMOJI = "\U0001F525" * ("--no-emojis" not in sys.argv)
def bidirectional_pad(text: str, pad: str, num: int = 1) -> str:
return (pad * num) + text + (pad * num)
def menu_select() -> str:
return input(
bidirectional_pad("What would you like to do?", MAIN_EMOJI, 3)
+ "\n"
+ bidirectional_pad("1. Read a file", MAIN_EMOJI)
+ "\n"
+ bidirectional_pad("2. Read a text from the stdin", MAIN_EMOJI)
+ "\n"
+ bidirectional_pad(
"3. Read a file and append it to another file",
MAIN_EMOJI,
)
+ "\n"
+ bidirectional_pad("4. Exit", MAIN_EMOJI)
+ "\n"
)
def intake_name(filename: str):
"""
Function to intake a filename and open the file in read mode.
If the file does not exist the function
will be repeatedly ask for a valid filename.
It return a list, with each element being a line from the file
"""
while not os.path.isfile(filename):
filename = input(
bidirectional_pad(
"File not found, please enter the name of the file",
MAIN_EMOJI,
)
)
with open(filename, "r", encoding="utf-8") as readfile:
lines = readfile.readlines()
return lines
def file_amalgam(lines):
"""
Function to intake a filename and open the file in append mode.
If the file does not exist the function
will be repeatedly ask for a valid filename.
"""
path = input(
bidirectional_pad(
"Please enter the name of the file you wish to add to",
MAIN_EMOJI,
)
+ "\n (This will overwrite any existing file of the same name) \n"
)
while not os.path.isfile(path):
path = input(
bidirectional_pad(
"Please enter the name of the file you wish to add to",
MAIN_EMOJI,
)
+ "\n (This will overwrite any existing file of the same name) \n"
)
with open(
path,
"a",
encoding="utf-8",
) as destination:
new_data = slayer(lines)
destination.writelines(new_data)
# This should return the text, not write it to a file.
def slayer(lines) -> List[str]:
"""
Intake a file object and a list of elements making of the contents of a previously read file.
Writes the contents of the list to the new file, unless a line contains a keyphrase that is meant to be excluded
Returns the number of links removed
"""
key = "%LOCAL_FILE%"
key2 = "remnote-user"
link_count = 0
new_text = []
for line in lines:
line = str(line)
if key not in line:
if key2 not in line:
new_text.append(line)
else:
link_count += 1
else:
link_count += 1
print(bidirectional_pad(f"{link_count} link(s) have been slain!", MAIN_EMOJI))
return new_text
def write_new_file(data: str):
filename = input(
bidirectional_pad(
"Please enter the name of the new text file that will be summoned",
MAIN_EMOJI,
)
)
while os.path.isfile(filename):
filename = input("\nFile already exists. Pick a different one.\n")
with open(filename, "w", encoding="utf-8") as newfile:
new_data = slayer(data)
newfile.writelines(new_data)
def main():
print(bidirectional_pad("WELCOME TO LINK SLAYER", MAIN_EMOJI))
while (menu := menu_select()) != "4":
match menu:
case "1":
# intakes name of file to be read
filename = input(
bidirectional_pad(
"Please enter the name of the text file you wish to cleanse of its sins",
MAIN_EMOJI,
)
)
# repetition in case 2
lines = intake_name(filename)
write_new_file(lines)
case "2":
# intakes text to be read
# repetition in case 2
lines = str(
input(
"\n\U0001F525 Please paste the text you wish to vanquish links from into the terminal (Right Click) \U0001F525\n"
)
)
write_new_file(lines)
case "3":
# appends new file to current file
filename = input(
bidirectional_pad(
"Please enter the name of the text file you wish to cleanse of its sins and join in union with another cleansed file",
MAIN_EMOJI,
)
)
lines = intake_name(filename)
case "4":
print(bidirectional_pad("Fare thee well, Hero", MAIN_EMOJI))
break
case _:
print(bidirectional_pad("Not a valid menu option", MAIN_EMOJI))
if __name__ == "__main__":
main()