-
Notifications
You must be signed in to change notification settings - Fork 0
/
RUNME.py
224 lines (172 loc) · 6.12 KB
/
RUNME.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
import re
import sys
where_potential_urls_can_be_proposed_one_per_line = 'FEEDME.txt'
where_new_valid_urls_are_stored_alphabetically = 'README.md'
class Pattern: #parent
@staticmethod
def __init__():
Pattern.regex = r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+[\w\-/?=&.:]*'
@staticmethod
def clean(string):
return string.strip(' /')
@staticmethod
def valid_url(string):
try:
match = re.search(Pattern.regex, string).group(0)
return Pattern.clean(match)
except:
return None
class List: #parent
def __init__(instance):
instance.addables = list()
instance.readables = list()
instance.deletables = list()
@staticmethod
def get_unique(entries, alphabetical = True):
entries = list(set(entries))
if alphabetical:
return sorted(entries)
return entries
class Readme: #parent
@staticmethod
def __init__():
Readme.line_break = '<br />'
@staticmethod
def untag(string):
return string.replace(Readme.line_break, '')
class File: #parent
@staticmethod
def rewrite(file, prefix = '', entries = False, line_feed = False):
with open(file, 'w') as opened:
if entries:
for each in entries:
opened.write(prefix + each + '\n' if line_feed else '')
else:
opened.write('')
opened.close()
@staticmethod
def get_processed_entries(file, invokable_process):
entries = list()
opened = open(file)
for each in opened:
each = invokable_process(each.strip('\n'))
if each:
entries.append(each)
opened.close()
return entries
class Interface: #parent
@staticmethod
def __init__():
Interface.main_divider = Interface.get_divider(64, '=')
Interface.sub_divider = Interface.get_divider(64, '-')
Interface.margin = Interface.get_margin(4)
def get_divider(length, symbol, string = '', i = 0):
while i < length:
string += symbol
i += 1
return string
def get_margin(length, string = '', i = 0):
while i < length:
string += ' '
i += 1
return string
@staticmethod
def help(text):
print('\n' + Interface.main_divider)
print(Interface.margin + text)
print(Interface.main_divider + '\n')
@staticmethod
def report(text):
print('\n' + Interface.margin + text + '\n')
class Persistence(File, Readme): #child
def of(instance):
File.rewrite(where_potential_urls_can_be_proposed_one_per_line)
File.rewrite(
where_new_valid_urls_are_stored_alphabetically,
Readme.line_break,
instance.readables,
True
)
class Interactive(Interface, Persistence): #grandchild
def run(instance):
Interface.help('"q" ends, "example\.io" searches')
while True:
it = Interactive.ask('Something to delete? ')
if 'q' == it:
Interactive.end()
for each in instance.readables:
try:
instance.deletables.append(
re.search(f'(.*)?{it}(.*)?', each).group(0)
)
except:
continue
if len(instance.deletables):
print(Interface.sub_divider)
for each in instance.deletables:
print(Interface.margin + each)
Interface.help('"q" ends, "y" confirms, any other choice cancels')
it = Interactive.ask('Do you confirm deletion? ')
if 'y' == it:
for each in instance.deletables:
instance.readables.remove(each)
Persistence.of(instance)
Interface.report('Deletion done')
else:
Interface.report('Deletion canceled')
if 'q' == it:
Interactive.end()
instance.deletables = list()
else:
Interface.report('No match')
def ask(question):
return input(Interface.margin + question)
def end():
Interface.report('End of execution')
quit()
class Boot(Interactive, Persistence, List, Pattern): #great-grandchild
def __init__(self):
self.p = len(sys.argv) > 1
List.__init__(self)
Pattern.__init__()
Readme.__init__()
self.fetch_data()
self.process_data()
Persistence.of(self)
Interface.__init__()
self.display_state()
Interactive.run(self)
def fetch_data(self):
if self.p:
for each in sys.argv[1:]:
each = Pattern.valid_url(each)
if each:
self.addables.append(each)
else:
self.addables = File.get_processed_entries(
where_potential_urls_can_be_proposed_one_per_line,
Pattern.valid_url
)
self.readables = File.get_processed_entries(
where_new_valid_urls_are_stored_alphabetically,
Readme.untag
)
def process_data(self):
self.previous = len(self.readables)
self.readables = List.get_unique(self.readables + self.addables)
self.updated = len(self.readables)
def display_state(self):
if self.updated > self.previous:
if self.p:
i = f'Command line argument{"s" if self.updated - self.previous > 1 else ""}'
i += f' moved in {where_new_valid_urls_are_stored_alphabetically}'
else:
i = f'Content of {where_potential_urls_can_be_proposed_one_per_line}'
i += f' moved to {where_new_valid_urls_are_stored_alphabetically}'
else:
if self.p:
i = f'There was no valid new URL passed as an p'
else:
i = f'There was no valid new URL in {where_potential_urls_can_be_proposed_one_per_line}'
Interface.report(i)
start = Boot()