-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
executable file
·80 lines (69 loc) · 2.34 KB
/
main.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
'''
This program converts all the comments in your code to a README.md file and saves a lot of your time provided you write comments in your code. It is obvious how easy it is to actually generate the README.md file and save your time.Using the argparse module to get the file path. Note that to run the file
```bash
python main.py -p <path to file>
```
'''
#The readme for this program is also generated by the code itself except the images added.
import argparse
import re
#adds arguments
parser = argparse.ArgumentParser()
parser.add_argument("-p", type=str, help="enter the file path")
args = parser.parse_args()
file = open(args.p, 'r')
f = file.readlines()
#To find Block Comments
def blockComm():
p = [(x, f[x][0]) for x in range(len(f))]
fip = []
for a in range(len(p)):
try:
if p[a][1] == "'":
fip.append(a)
except IndexError:
pass
fip = [[fip[x], fip[x + 1]] for x in range(0, len(fip) - 1, 2)]
contents = []
sidecomm = []
blockcomm = [''.join(f[x[0] + 1:x[1]]).strip() for x in fip]
return blockcomm
'''
For now this program only supports python.
'''
# find the side comments and create contents list
def content_sidecomm():
contents = []
sidecomm = []
for a in range(0, len(f)):
finder = f[a].find('#')
try:
if finder != -1:
if finder == 0:
# fin_out.write('### {}\n'.format(f[a][1::]).upper())
contents.append(f[a][1::].upper())
else:
# fin_out.write('> {}\n'.format(f[a][finder+1::]).capitalize())
sidecomm.append(f[a][finder + 1::].capitalize())
except IndexError:
pass
return [contents, sidecomm]
# Format the file and save
def format_and_save():
#initial file
fin_out = open('README.md', 'w+') #output file
fin_out.write('# README\n')
fin_out.write('## Contents\n')
cs = content_sidecomm()
for a in range(len(cs[0])):
fin_out.write('{}. {}\n'.format(a+1,cs[0][a]))
fin_out.write('## Docstrings\n')
bc = blockComm()
for a in range(len(bc)):
fin_out.write('>{}\n'.format(bc[a]))
fin_out.write('## Side comments\n')
for a in range(len(cs[1])):
fin_out.write('- {}\n'.format(cs[1][a]))
fin_out.close()
# Main function
format_and_save()