-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton.py
187 lines (149 loc) · 4.5 KB
/
skeleton.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
#! /usr/bin/python
from argparse import ArgumentParser
import datetime
from os import path
import subprocess
def run_git(name, automatic):
result = subprocess.run(["git", "status"], capture_output=True)
if result.returncode == 0:
if automatic:
subprocess.run(["git", "add", name], capture_output=True)
else:
ans = input(f"add {name} to git tracked files?[Y/n] ")
if ans.lower() != "n":
subprocess.run(["git", "add", name], capture_output=True)
def get_comment():
output = ""
print("Add Comment,# is added automatically when needed, empty line to end")
while line := input():
if line.startswith("#"):
output += line
else:
output += "# " + line
output += "\n"
return output
def wrap_comment(line):
if line.startswith("#"):
output = ""
else:
output = "# "
size = 0
for token in line.split():
size += len(token) + 1
if size > 80:
size = len(token) + 1
output += "\n"
output += "# "
output += token + " "
output += "\n"
return output
def add_empty_line(file, count):
for _ in range(count):
print(file=file)
def output_skeleton(file, comment, matplot, time, space):
print("#! /usr/bin/python", file=file)
add_empty_line(file, 1)
indent = " " * space
if time:
now = datetime.datetime.now()
time_stamp = now.strftime("%Y-%m-%d %H:%M")
info = f"# autogenerated on {time_stamp}\n"
print(info, file=file)
if comment:
print(comment, file=file)
print("import cv2", file=file)
print("import numpy as np", file=file)
if matplot:
print("from matplotlib import pyplot as plt", file=file)
add_empty_line(file, 1)
print("from utils import *", file=file)
add_empty_line(file, 2)
print("def main():", file=file)
print(f"{indent}pass", file=file)
add_empty_line(file, 2)
print("if __name__ == '__main__':", file=file)
print(f"{indent}main()", file=file)
def parse_args():
parser = ArgumentParser(
description="Create a new skeleton file for the DIP laboratory"
)
parser.add_argument(
"name",
help="output file name (.py extension is added automatically if missing)",
)
parser.add_argument(
"-m",
"--matplot",
action="store_true",
default=False,
help="add matplotlib import",
)
comment_mutex_group = parser.add_mutually_exclusive_group()
comment_mutex_group.add_argument(
"-c",
"--comment",
help="a comment to add at the beginning of the file, line is wrapped automatically",
default=None,
)
comment_mutex_group.add_argument(
"-C",
"--no-comment",
help="stop the program from asking if the user wants to add comment to the new file",
default=False,
action="store_true",
)
git_mutex_group = parser.add_mutually_exclusive_group()
git_mutex_group.add_argument(
"-g",
"--git",
help="automatically add new file to git tracked files",
default=False,
action="store_true",
)
git_mutex_group.add_argument(
"-G",
"--no-git",
help="stop the program from asking if the user wants to track the new file",
default=False,
action="store_true",
)
parser.add_argument(
"-t",
"--time-info",
default=False,
help="add creation time info ad the beginning of the file",
action="store_true",
)
parser.add_argument(
"-i",
"--indent",
default=4,
help="set indent size in spaces, default 4",
type=int,
)
return parser.parse_args()
def main():
args = parse_args()
if args.name.endswith(".py"):
name = args.name
else:
name = args.name + ".py"
if path.exists(name):
ans = input(f"{name} already exists, continue anyway?[y/N]")
if ans.lower() != "y":
print("Abort")
exit()
if not args.no_comment:
if args.comment is None:
comment = get_comment()
else:
comment = args.comment
comment = wrap_comment(comment)
else:
comment = None
with open(name, "w+") as file:
output_skeleton(file, comment, args.matplot, args.time_info, args.indent)
if not args.no_git:
run_git(name, args.git)
if __name__ == "__main__":
main()