-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
174 lines (138 loc) · 6.33 KB
/
main.py
File metadata and controls
174 lines (138 loc) · 6.33 KB
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
from gui import GUI
from tokenizer import Tokenizer
from parser import Parser
import requests
import threading
import os
import time
import git # pip install GitPython
# Global variables
update_result = "Not checked yet"
tests_list = []
current_test_index = 0
SentMessages = []
# make me funciton to check if this string hsa content other than spaces
def has_content(string):
return any(c != ' ' and c != '\n' for c in string)
def formate_message(inputText, outputTokens, outputSyntax, outputGrammar):
if not has_content(inputText): inputText = "Empty input\n"
if not has_content(outputTokens): outputTokens = "No tokens\n"
if not has_content(outputSyntax): outputSyntax = "No syntax\n"
if not has_content(outputGrammar): outputGrammar = "No grammar\n"
return f"\n\nInput from {os.getenv("USERNAME")} : `Update-Status: {update_result}`\n```{inputText}\n```\nTokens:\n```\n{outputTokens}\n```\nSyntax:\n```\n{outputSyntax}\n```\nGrammar:```\n{outputGrammar}```\n═════════════════════════════════════════════════════════════════════════════════════════════════"
def send_message_to_bot(message):
if message in SentMessages: return
SentMessages.append(message)
url = 'http://ro05.pylex.me:10337/send-message'
data = {'message': message}
# Define a function to send the request in a separate thread
def send_request():
requests.post(url, json=data)
# Start a new thread to send the request
threading.Thread(target=send_request).start()
def run_with_timeout(func):
result = [None] # Using a list to store the result as it's mutable
def target():
result[0] = func()
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout=2) # Wait for 2 seconds
if thread.is_alive():
print(f"Function {func.__name__} exceeded 2 seconds, Invalid Syntax!")
return result[0] if not thread.is_alive() else "TLE"
input_output_cache = {}
# Main output function
def main_output(inputText):
if inputText in input_output_cache:
return input_output_cache[inputText]
tokens = run_with_timeout(lambda: Tokenizer().tokenize(inputText))
outputTokens = "\n".join([f"{token.type}: {token.value}" for token in tokens])
syntax = run_with_timeout(lambda: Parser(tokens).parse()[0])
if "TLE" in syntax:
input_output_cache[inputText] = (outputTokens, "Invalid Syntax!", "Invalid Syntax!")
return (outputTokens, "Invalid Syntax!", "Invalid Syntax!")
outputSyntax = "\n".join(syntax) if syntax else "Parsing successful"
grammar = run_with_timeout(lambda: Parser(tokens).parse()[1])
outputGrammar = " -->\n".join(grammar) if grammar else "No grammar found"
input_output_cache[inputText] = (outputTokens, outputSyntax, outputGrammar)
return (outputTokens, outputSyntax, outputGrammar)
# Parse input function
def parse_input():
global tests_list, current_test_index
input_text = gui.get_input_text()
update_test_list(input_text)
tokens_output, syntax_output, grammar_output = main_output(input_text)
gui.set_output_text(syntax_output)
send_message_to_bot(formate_message(input_text, tokens_output, syntax_output, grammar_output))
# Tokenize input function
def tokenize_input():
global tests_list, current_test_index
input_text = gui.get_input_text()
update_test_list(input_text)
tokens_output, syntax_output, grammar_output = main_output(input_text)
gui.set_output_text(tokens_output)
send_message_to_bot(formate_message(input_text, tokens_output, syntax_output, grammar_output))
def get_grammar_rules():
global tests_list, current_test_index
input_text = gui.get_input_text()
update_test_list(input_text)
tokens_output, syntax_output, grammar_output = main_output(input_text)
gui.set_output_text(grammar_output)
send_message_to_bot(formate_message(input_text, tokens_output, syntax_output, grammar_output))
# Show next test function
def show_next_test():
if not tests_list: return
global current_test_index
current_test_index = (current_test_index + 1) % len(tests_list)
gui.set_input_text(tests_list[current_test_index])
# Show previous test function
def show_previous_test():
if not tests_list: return
global current_test_index
current_test_index = (current_test_index - 1) % len(tests_list)
gui.set_input_text(tests_list[current_test_index])
# Clear boxes function
def clear_boxes():
gui.clear_boxes()
def check_for_updates_async():
# Define a function to be executed in the new thread
def check_updates(branch_name='master'):
global update_result
repo = git.Repo('.')
origin = repo.remotes.origin
origin.fetch()
current_commit = repo.head.commit
latest_remote_commit = origin.refs[branch_name].commit
if current_commit == latest_remote_commit:
update_result = f"Up to date with {branch_name} ✅"
else:
update_result = f"Warning: Outdated with {branch_name} 🟥"
# Create a new thread and start it
update_thread = threading.Thread(target=check_updates)
update_thread.start()
# Function to read tests from file and populate the global list
def read_tests_from_file():
try:
with open("Tests.txt", "r", encoding="UTF-8") as file:
tests = file.read().split("###")
tests = [test.strip() for test in tests if test.strip()]
return tests
except FileNotFoundError:
print("Tests file not found.")
return []
# Update test list function
def update_test_list(new_test):
if new_test == "": return
global tests_list
global current_test_index
if new_test not in tests_list:
tests_list.append(new_test)
with open("Tests.txt", "a", encoding="UTF-8") as file:
file.write(f"{new_test}\n###\n")
current_test_index = len(tests_list) - 1
check_for_updates_async()
tests_list = read_tests_from_file()
# Initialize GUI with the new function for the grammar button
gui = GUI(tokenize_command=tokenize_input, parse_command=parse_input, show_grammar_rules=get_grammar_rules, clear_command=clear_boxes,
show_next_command=show_next_test, show_previous_command=show_previous_test)
gui.run()