-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_comments_fetcher.py
198 lines (170 loc) · 7.39 KB
/
code_comments_fetcher.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
188
189
190
191
192
193
194
195
196
197
198
# Code Comments Fetcher
# imported necessary library
import tkinter
from tkinter import *
import tkinter as tk
import tkinter.messagebox as mbox
import re
# created main window
window = Tk()
window.geometry("1000x700")
window.title("Code Comments Fetcher")
# extracting url -------------------------
def fetch_comments():
global detected_language
input_text = str(text_enter.get("1.0", "end-1c"))
if re.search(r'public static void main\(String args\[\]\)|System\.out\.println|import java\..+?;', input_text):
detected_language = 'Java'
elif re.search(r'#include<stdio.h>', input_text):
detected_language = 'C'
elif re.search(r'#include<iostream.h>', input_text):
detected_language = 'C++'
elif re.search(r'using System;', input_text):
detected_language = 'C#'
elif re.search(r'<!DOCTYPE html>', input_text):
detected_language = 'HTML'
elif re.search(r'<html>', input_text):
detected_language = 'HTML'
elif re.search(r'fun ', input_text):
detected_language = 'Kotlin'
elif re.search(r'<script>', input_text):
detected_language = 'JavaScript'
else:
detected_language = 'Python'
comments = ""
res_list = (input_text.rstrip().split('\n'))
if(detected_language == "Python"):
mul = False
for line in res_list:
# print (line)
if mul:
# print(mul)
texts1 = line.strip().split("'''")
if len(texts1) == 2:
# print("%s*/" % texts1[0])
comments = comments + texts1[0] + "'''\n\n"
mul = False
else:
comments = comments + texts1[0]
# print(texts1[0])
else:
# print(mul)
texts = line.strip().split("'''")
if len(texts) == 2:
comments = comments + "'''" + texts[1] + "\n\n"
# print("//%s" % texts[1])
# ----------------- python single ---------
texts = line.strip().split("#")
if len(texts) == 2:
comments = comments + "#" + texts[1] + "\n\n"
# print("//%s" % texts[1])
# -------------------------------------
else:
texts2 = line.strip().split("'''")
if len(texts2) == 2:
texts3 = texts2[1].split("'''")
if len(texts3) == 2:
comments = comments + "'''" + texts3[0] + "'''\n\n"
# print("/*%s*/" % texts3[0])
else:
comments = comments + "'''" + texts2[1] + "\n\n"
# print("/*%s" % texts2[1])
mul = True
elif(detected_language=="HTML" or detected_language=="JavaScript"):
mul = False
for line in res_list:
# print (line)
if mul:
# -------------------- html -----------
texts1 = line.strip().split("-->")
if len(texts1) == 2:
# print("%s*/" % texts1[0])
comments = comments + texts1[0] + "-->\n\n"
mul = False
else:
comments = comments + texts1[0]
# print(texts1[0])
# -------------------------------------
else:
# print(mul)
texts = line.strip().split("<!--")
if len(texts) == 2:
comments = comments + "<!--" + texts[1] + "\n\n"
# print("//%s" % texts[1])
else:
# ----------------------------html -------------
texts2 = line.strip().split("<!--")
if len(texts2) == 2:
texts3 = texts2[1].split("-->")
if len(texts3) == 2:
comments = comments + "<!--" + texts3[0] + "-->\n\n"
# print("/*%s*/" % texts3[0])
else:
comments = comments + "-->" + texts2[1] + "\n\n"
# print("/*%s" % texts2[1])
mul = True
# ---------------------------------------------
else:
mul = False
for line in res_list:
# print (line)
if mul:
# print(mul)
texts1 = line.strip().split("*/")
if len(texts1) == 2:
# print("%s*/" % texts1[0])
comments = comments + texts1[0] + "*/\n\n"
mul = False
else:
comments = comments + texts1[0]
# print(texts1[0])
else:
# print(mul)
texts = line.strip().split("//")
if len(texts) == 2:
comments = comments + "//" + texts[1] + "\n\n"
# print("//%s" % texts[1])
else:
texts2 = line.strip().split("/*")
if len(texts2) == 2:
texts3 = texts2[1].split("*/")
if len(texts3) == 2:
comments = comments + "/*" + texts3[0] + "*/\n\n"
# print("/*%s*/" % texts3[0])
else:
comments = comments + "/*" + texts2[1] + "\n\n"
# print("/*%s" % texts2[1])
mul = True
mbox.showinfo("Comments Fetch", "Language : " + detected_language + "\n\nComments :\n\n" + comments)
def lang_support():
mbox.showinfo("Supported Programming Language", "Supported Programming Language :\n\nC\nC++\nJava\nPython\nHTML\nKolin\nJavaScript\nC#")
# top label
start1 = tk.Label(text="Code Comments Fetcher", font=("Arial", 50), fg="magenta") # same way bg
start1.place(x=110, y=10)
# top second label
enter_label = Label(window, text="Write code snippets and fetch comments...", font=("Arial", 30), fg="brown")
enter_label.place(x=110, y=100)
# created text area
text_enter = tk.Text(window, height=18, width=80, font=("Arial", 15), bg="light yellow", fg="brown", borderwidth=3,relief="solid")
text_enter.place(x=50, y=150)
# created Language button
domainb = Button(window, text="LANG. SUPPORT", command=lang_support, font=("Arial", 20), bg="light green", fg="blue",borderwidth=3, relief="raised")
domainb.place(x=50, y=600)
# created FETCH COMMENTS button
extractb = Button(window, text="FETCH COMMENTS", command=fetch_comments, font=("Arial", 20), bg="light green", fg="blue",borderwidth=3, relief="raised")
extractb.place(x=330, y=600)
# function for clearing the text area
def clear_text():
text_enter.delete("1.0", "end")
# created a clear button
clearb = Button(window, text="CLEAR", command=clear_text, font=("Arial", 20), bg="orange", fg="blue", borderwidth=3,relief="raised")
clearb.place(x=660, y=600)
# function for exiting
def exit_win():
if mbox.askokcancel("Exit", "Do you want to exit?"):
window.destroy()
# created exit button
exitb = Button(window, text="EXIT", command=exit_win, font=("Arial", 20), bg="red", fg="blue", borderwidth=3,relief="raised")
exitb.place(x=850, y=600)
window.protocol("WM_DELETE_WINDOW", exit_win)
window.mainloop()