forked from sandeepraju/git-talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_comment.py
48 lines (42 loc) · 1.27 KB
/
insert_comment.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
import os
import sys
comment_syntax_dict = {
'.py': '# ',
'.pl': '# ',
'.rb': '# ',
'.php': '# ',
'.c': '// ',
'.h': '// ',
'.cpp': '// ',
'.cs': '// ',
'.go': '// ',
'.java': '// ',
'.js': '// ',
'.m': '// ',
'.mm': '// ',
'.swift': '// ',
'.sass': '// ',
'.scss': '// ',
'.lisp': '; '
}
def get_comment_syntax(filepath):
_, extension = os.path.splitext(filepath)
if extension in comment_syntax_dict:
return comment_syntax_dict[extension]
# TODO: This needs to replaced so that users can input the right inline comment
# syntax for unknown file types.
return '#'
def insert_comment(filepath, line_number, comment):
comment_syntax = get_comment_syntax(filepath)
with open(filepath, 'r') as f:
lines = f.readlines()
lines.insert(line_number - 1, comment_syntax + comment + '\n')
with open(filepath, 'w') as f:
f.writelines(lines)
def main():
if len(sys.argv) < 4:
print 'USAGE:\npython insert_comment.py [file path] [line number] [comment]\n\t[file path]: path of file to insert comments into\n\t[line number]: line to insert comment before\n\t[comment]: comment to insert (wrap in quotes if multiple words)\n'
else:
insert_comment(sys.argv[1], int(sys.argv[2]), sys.argv[3])
if __name__ == "__main__":
main()