-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmyscript.py
More file actions
317 lines (252 loc) · 10 KB
/
myscript.py
File metadata and controls
317 lines (252 loc) · 10 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
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
import os
import sys
import argparse
import configparser
import re
class ScriptScribe(object):
def __init__(self):
dirCalled = os.path.dirname(__file__)
self.dbFile = 'scripts.db'
self.dbFile = os.path.join(dirCalled, self.dbFile)
if os.path.exists(self.dbFile):
self.database = configparser.ConfigParser()
self.database.read(self.dbFile, encoding='utf-8')
else:
print('{} is not found.'.format(self.dbFile))
sys.exit()
self.parse_args()
if self.section:
if self.args.update_bool:
self.update()
elif self.args.insert_bool:
self.insert_new()
elif self.args.remove_bool:
self.remove()
else:
self.pick_script()
else:
if self.args.burst_bool:
self.burst_database()
else:
self.parser.print_help()
self.enumerate_scripts()
def parse_args(self) -> None:
self.parser = argparse.ArgumentParser(
add_help=False,
description = 'Extract a script from "scripts.db" to run it.'
)
self.parser.add_argument(
'script',
nargs = '*'
)
self.parser.add_argument(
'-I',
dest = 'insert_bool',
action = 'store_true',
default = False,
help = 'Insert a new script file into the database file.'
)
self.parser.add_argument(
'-U',
dest = 'update_bool',
action = 'store_true',
default = False,
help = 'Update the database with the file being in the current directory.'
)
self.parser.add_argument(
'-R',
dest = 'remove_bool',
action = 'store_true',
default = False,
help = 'Remove the specified script from the database file.'
)
self.parser.add_argument(
'-B',
dest = 'burst_bool',
action = 'store_true',
default = False,
help = 'Take out every script.'
)
self.parser.add_argument(
'-C',
dest = 'clear_bool',
action = 'store_true',
default = False,
help = 'Delete the extracted script file after a run.'
)
self.args, unknown_options = self.parser.parse_known_args()
if len(self.args.script) > 0:
self.section = self.args.script[0]
self.script_arguments = self.args.script
del self.script_arguments[0]
self.script_arguments = unknown_options + self.script_arguments
else:
self.section = None
def check_section(self) -> bool:
if self.database.has_section(self.section):
return True
else:
print('"{}" is not included in the database.'.format(self.section))
return False
def confirm_to_overwrite(self, file) -> bool:
if file is None:
print('Ensure the script database file is set properly.')
return False
else:
if os.path.exists(file):
answer = input('"{}" already exists. Are you sure to overwrite it? [y/N] '.format(file))
if answer.lower() == 'y':
os.remove(file)
return True
else:
return False
else:
return True
def write_from_database(self, filename, source) -> bool:
if self.confirm_to_overwrite(filename):
try:
content = self.database.get(self.section, source)
except:
print('Ensure the script database is set properly.')
return False
ext = os.path.splitext(filename)[1].lower()
if ext != '.cmd':
content = content.replace('```', '')
if ext == '.tsv':
content = re.sub("$", "\t", content, flags=re.MULTILINE)
with open(filename, mode='w', encoding='utf-8') as f:
f.write(content)
return True
else:
return False
def pick_script(self) -> None:
if not self.check_section():
return
options = self.database.options(self.section)
for option in options:
if 'output' in option:
filename = self.database.get(self.section, option)
if option == 'code_output':
codefile = filename
source = option.split('_')[0]
if not self.write_from_database(filename, source):
return
def if_exist(self, filename) -> bool:
if os.path.exists(filename):
return True
else:
print('"{}" does not exist in the current directory.'.format(filename))
return False
def read_script_file(self, filename) -> tuple[str, str]:
ext = os.path.splitext(filename)[1].lower()
if ext == '.py':
script_type = '[Python]'
elif ext == '.ps1':
script_type = '[PowerShell]'
elif ext == '.cmd':
script_type = '[cmd]'
elif ext == '.jsx':
script_type = '[JavaScript]'
elif ext == '.bas':
script_type = '[Visual Basic]'
elif ext == '.tsv':
script_type = '[Tab-separated values]'
else:
script_type = '[Unknown]'
with open(filename, mode='r', encoding='utf-8') as f:
code = f.read()
code = re.sub('%', '%%', code)
if ext != '.cmd':
code = re.sub('^', '```', code, flags=re.MULTILINE)
found = re.search('(?<= description = ).*$', code, flags=re.MULTILINE)
if found:
description = found.group(0)
description = re.sub("^'", "", description)
description = re.sub("'$", "", description)
description = re.sub('^"', '', description)
description = re.sub('"$', '', description)
description = '{} {}'.format(script_type, description)
else:
description = script_type
return description, code
#>myscript.py -I foo.py conf_output=foo.conf
def insert_new(self) -> None:
filename = os.path.basename(self.section)
if not self.if_exist(filename):
return
self.section = os.path.splitext(filename)[0]
if self.section in self.database.sections():
print('"{}" is already included in the database.'.format(self.section))
return
description, code = self.read_script_file(filename)
self.database.add_section(self.section)
self.database.set(self.section, 'description', description)
self.database.set(self.section, 'code_output', filename)
self.database.set(self.section, 'code', code)
self.check_auxiliary_files()
with open(self.dbFile, mode='w', encoding='utf-8') as f:
self.database.write(f)
print('Successfully inserted.')
# Read auxiliary files if specified
def check_auxiliary_files(self) -> None:
if len(self.args.script) > 0:
for i in self.args.script:
option_filename, filename = i.split('=')
if not self.if_exist(filename):
continue
option_content = re.sub('_output', '', option_filename)
with open(filename, mode='r', encoding='utf-8') as f:
content = f.read()
content = re.sub('%', '%%', content)
content = re.sub('^', '```', content, flags=re.MULTILINE)
self.database.set(self.section, option_filename, filename)
self.database.set(self.section, option_content, content)
def update(self) -> None:
if not self.check_section():
return
filename = self.database.get(self.section, 'code_output')
if not self.if_exist(filename):
return
description, code = self.read_script_file(filename)
self.database.set(self.section, 'description', description)
self.database.set(self.section, 'code', code)
# Update auxiliary files if any.
options = self.database.options(self.section)
for option in options:
if 'output' in option:
filename = self.database.get(self.section, option)
if not self.if_exist(filename):
continue
with open (filename, mode='r', encoding='utf-8') as f:
content = f.read()
content = re.sub('%', '%%', content)
content = re.sub('^', '```', content, flags=re.MULTILINE)
option_content = re.sub('_output', '', option)
self.database.set(self.section, option_content, content)
with open(self.dbFile, mode='w', encoding='utf-8') as f:
self.database.write(f)
print('Successfully updated.')
def remove(self) -> None:
if not self.check_section():
return
answer = input('Are you sure to remove "{}" from the database? [y/N] '.format(self.section))
if answer.lower() == 'y':
self.database.remove_section(self.section)
with open(self.dbFile, mode='w', encoding='utf-8') as f:
self.database.write(f)
print('Successfully removed.')
def enumerate_scripts(self) -> None:
scripts = sorted(self.database.sections(), key=str.casefold)
print()
for i in scripts:
description = self.database.get(i, 'description', fallback=None)
if description is None:
description = ''
print('{:12} {}'.format(i,description))
def burst_database(self) -> None:
scripts = sorted(self.database.sections(), key=str.casefold)
for i in scripts:
self.script = i
self.pick_script()
if __name__ == '__main__':
ScriptScribe()