-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (163 loc) · 7.53 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
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
import json
import sys
from antlr4 import *
from ExpressoesLexer import ExpressoesLexer
from ExpressoesParser import ExpressoesParser
NEW_FORM = {}
def executeStmt(stmt):
if stmt.titleStmt():
executeTitle(stmt.titleStmt())
elif stmt.descriptionStmt():
executeDescription(stmt.descriptionStmt())
elif stmt.insertItemTextStmt():
executeInsertText(stmt.insertItemTextStmt())
elif stmt.insertItemMultipleChoicetStmt():
executeInsertMultipleChoice(stmt.insertItemMultipleChoicetStmt())
elif stmt.insertItemSectionHeadertStmt():
executeInsertSectionHeader(stmt.insertItemSectionHeadertStmt())
elif stmt.showStmt():
executeShow(stmt.showStmt())
elif stmt.exportStmt():
executeExport(stmt.exportStmt())
else:
raise Exception("Nao identificado")
# OK
def executeTitle(stmt):
title = stmt.value().getText().replace("'", "")
if 'info' not in NEW_FORM.keys():
dict_aux = {'title' : title}
NEW_FORM['info'] = dict_aux
print(f"Titulo criado: {title}")
else:
raise Exception(f"O formulario ja possui um titulo: {title}")
def executeDescription(stmt):
description = stmt.value().getText().replace("'", "")
if 'info' not in NEW_FORM.keys():
dict_aux = {'description': description}
NEW_FORM['info'] = dict_aux
print(f"Descricao criada: {description}")
elif 'description' not in NEW_FORM['info'].keys():
NEW_FORM['info']['description'] = description
print(f"Descricao criada: {description}")
else:
raise Exception(f"O formulario ja possui uma descricao: {description}")
# OK
def executeInsertText(stmt):
itemKeys = [col.getText() for col in stmt.itemKeys().ID()]
values = [val.getText() for val in stmt.values().value()]
if 'info' in NEW_FORM:
if 'items' in NEW_FORM.keys():
NEW_FORM['items'].append({"type": "TEXT",
itemKeys[0].replace("'", ""): values[0].replace("'", ""),
itemKeys[1].replace("'", ""): values[1].replace("'", ""),
"index": len(NEW_FORM['items'])
})
print(f"Item criado: {values[0].replace("'", "")}")
else:
NEW_FORM['items'] = []
NEW_FORM['items'].append({"type": "TEXT",
itemKeys[0].replace("'", ""): values[0].replace("'", ""),
itemKeys[1].replace("'", ""): values[1].replace("'", ""),
"index": len(NEW_FORM['items'])
})
print(f"Item criado: {values[0].replace("'", "")}")
else:
raise Exception(f"Formulario nao iniciado, e necessario um titulo")
# OK
def executeInsertMultipleChoice(stmt):
itemKeys = [col.getText() for col in stmt.itemKeys().ID()]
values = [val.getText() for val in stmt.values().value()]
if 'info' in NEW_FORM:
if 'items' in NEW_FORM.keys():
choices = []
for i in range(0, len(values)):
if i != 0 and i != 1:
choices.append(values[i].replace("'", ""))
NEW_FORM['items'].append({"type": "MULTIPLE_CHOICE",
itemKeys[0].replace("'", ""): values[0].replace("'", ""),
itemKeys[1].replace("'", ""): values[1].replace("'", ""),
itemKeys[2].replace("'", ""): choices,
"index": len(NEW_FORM['items'])
})
print(f"Item criado: {values[0].replace("'", "")}")
else:
NEW_FORM['items'] = []
choices = []
for i in range(0, len(values)):
if i != 0 and i != 1:
choices.append(values[i].replace("'", ""))
NEW_FORM['items'].append({"type": "MULTIPLE_CHOICE",
itemKeys[0].replace("'", ""): values[0].replace("'", ""),
itemKeys[1].replace("'", ""): values[1].replace("'", ""),
itemKeys[2].replace("'", ""): choices, "index": len(NEW_FORM['items'])
})
print(f"Item criado: {values[0].replace("'", "")}")
else:
raise Exception(f"Formulario nao iniciado, e necessario um titulo")
# OK
def executeInsertSectionHeader(stmt):
itemKeys = [col.getText() for col in stmt.itemKeys().ID()]
values = [val.getText() for val in stmt.values().value()]
if 'info' in NEW_FORM:
if 'items' in NEW_FORM.keys():
NEW_FORM['items'].append({"type": "SECTION_HEADER",
itemKeys[0].replace("'", ""): values[0].replace("'", ""),
itemKeys[1].replace("'", ""): values[1].replace("'", ""),
"index": len(NEW_FORM['items'])})
print(f"Item criado: {values[0].replace("'", "")}")
else:
NEW_FORM['items'] = []
NEW_FORM['items'].append({"type": "SECTION_HEADER",
itemKeys[0].replace("'", ""): values[0].replace("'", ""),
itemKeys[1].replace("'", ""): values[1].replace("'", ""),
"index": len(NEW_FORM['items'])})
print(f"Item criado: {values[0].replace("'", "")}")
else:
raise Exception(f"Formulario nao iniciado, e necessario um titulo")
# OK
def executeShow(stmt):
if bool(NEW_FORM):
json_dict = json.dumps(NEW_FORM, indent=4)
print(json_dict)
else:
raise Exception(f"O formulario vazio")
# OK
def executeExport(stmt):
if bool(NEW_FORM):
json_dict = json.dumps(NEW_FORM, indent=4)
with open('output.json', 'w') as f:
f.write(json_dict)
print(f"Arquivo exportado com sucesso")
else:
raise Exception(f"O formulario vazio")
# COMMANDS EXAMPLES:
# TITLE 'Cadastro pessoal';
# ITEM TEXT (title, isRequired) VALUES ('Insira seu nome', 'true');
# ITEM PARAGRAPH_TEXT (title, isRequired) VALUES ('Endereco', 'true');
# ITEM MULTIPLE_CHOICE (title, isRequired, choices) VALUES ('Escolha uma opcao', 'false', 'Option 1', 'Option 2', 'Option 3');
# ITEM SECTION_HEADER (title, helpText) VALUES ('This is a title', 'No questions here');
# SHOW;
# EXPORT;
input_text = """
TITLE 'Cadastro pessoal';
DESCRIPTION 'Testo descricao';
ITEM TEXT (title, isRequired) VALUES ('Insira seu nome', 'true');
ITEM TEXT (title, isRequired) VALUES ('Insira seu sobrenome', 'false');
ITEM TEXT (title, isRequired) VALUES ('Insira seu email', 'true');
ITEM MULTIPLE_CHOICE (title, choices, isRequired) VALUES ('Escolha uma opcao', 'false', 'Option 1', 'Option 2', 'Option 3');
ITEM SECTION_HEADER (title, text) VALUES ('Titulo do section header', 'Sem perguntas, apenas anotacoes aqui');
SHOW;
EXPORT;
"""
input_stream = InputStream(input_text)
lexer = ExpressoesLexer(input_stream)
stream = CommonTokenStream(lexer)
parser = ExpressoesParser(stream)
tree = parser.prog()
# Verifica se houve erro sintático
if parser.getNumberOfSyntaxErrors() > 0:
print("erro sintático")
sys.exit(1)
# print(tree.toStringTree(recog=parser))
for stmt in tree.stmt():
executeStmt(stmt)