-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_case.py
executable file
·68 lines (51 loc) · 2.1 KB
/
new_case.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
#!/usr/bin/env python3
# use inside chapter folders (eq. data_structures)
# takes 1 argument: assignment_name [singular](eg. hash_table)
import sys, os, pathlib
from distutils.dir_util import copy_tree
import itertools
import fileinput
from datetime import datetime
assignment_name = sys.argv[1]
assignment_dir = os.getcwd()
script_dir = os.path.dirname(os.path.realpath(__file__))
current_stamp_time = datetime.now().strftime('%Y-%m-%d %H:%M')
assignemnt_name_plural = assignment_name
assignment_name_uppercase = assignment_name.upper()
assignment_name_class_name = assignment_name.title().replace("_", "")
replacemenets = {
"$ASSIGNMENT_NAME_PLURAL$": assignemnt_name_plural,
"$ASSIGNMENT_NAME$": assignment_name,
"$ASSIGNEMNT_NAME_UPERCASE$": assignment_name_uppercase,
"$ASSIGNMENT_NAME_CLASS_NAME$": assignment_name_class_name,
"$START_DATE$": current_stamp_time
}
assignemnt_path = "{}/{}".format(assignment_dir, assignemnt_name_plural)
template_path = "{}/code/templates".format(script_dir)
def list_template_files_paths():
filelists = [["{}/{}".format(root, file) for file in files]
for root, dirs, files in os.walk(assignemnt_path)]
return list(itertools.chain.from_iterable(filelists))
def replace_in_line(line):
retline = line
for key, value in replacemenets.items():
retline = retline.replace(key, value)
return retline
def main():
# make new folder
pathlib.Path(assignemnt_path).mkdir(parents=True, exist_ok=True)
# copy template files
copy_tree(template_path, assignemnt_path)
files = list_template_files_paths()
# replace template contents
with fileinput.FileInput(files=files, inplace=True) as file:
for line in file:
print(replace_in_line(line), end='')
for file in files:
os.rename(file, file.replace("placeholder", assignment_name))
# todo: fill task
# future-todo: trigger make on c and c++ folders
# future-todo: autoinsert propper tasklist (if possible)
# future-todo: allow more commands (copy cpp to cpp_builtin, end assigment [stamp enddate, commit, etc.])
if __name__ == "__main__":
main()