forked from shelari/prereform_to_contemporary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_interface.py
296 lines (273 loc) · 12.6 KB
/
web_interface.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
__author__ = 'ElenaSidorova'
from flask import Flask, render_template, request, make_response, Response
from werkzeug.utils import secure_filename
import os
from flask_recaptcha import ReCaptcha
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import codecs
from zipfile import ZIP_DEFLATED
import time
import zipstream
import shutil
from process import Processor
from meta_data import META
from load_data import LoadData
# BEGIN: UGLY MONKEYPATCH
import pkgutil
orig_get_loader = pkgutil.get_loader
def get_loader(name):
try:
return orig_get_loader(name)
except AttributeError:
pass
pkgutil.get_loader = get_loader
# END: UGLY MONKEYPATCH
app = Flask(__name__)
app.config['RECAPTCHA_SITE_KEY'] = "6LeAcggTAAAAALCNTpHX5Qpa0DnHExVZrFMXiESF"
app.config['RECAPTCHA_SECRET_KEY'] = "6LeAcggTAAAAAAPVJ8qalCwkvSBLOpy8Yg2bswwM"
recaptcha = ReCaptcha(app)
#@app.route('/')
def index():
new_text = ''
input_text = ''
return render_template("prereform_to_contemporary.html", output_text=new_text, input_text=input_text)
@app.route("/", methods=['GET', 'POST'])
def web_converter():
input_text = ''
output_text = ''
new_text = ''
if request.method == 'POST':
if META['tmp_folder'] != '':
shutil.rmtree(META['tmp_folder'])
META['tmp_folder'] = ''
input_text = request.values.get('inp_text')
#app.logger.info(input_text)
both = request.form.getlist('both')
if 'go' in request.values:
if 'spell' in META['current_delimiters_text']:
check_brackets = 1
else:
check_brackets = 0
if 'show' in both:
output_text, changes, wrong_changes, _ = Processor.process_text(input_text, 1, META['old_new_delimiters'][META['current_delimiters_text']], check_brackets)
else:
output_text, changes, wrong_changes, _ = Processor.process_text(input_text, 0, META['old_new_delimiters'][META['current_delimiters_text']], check_brackets)
if 'clean' in request.values:
input_text = ''
output_text = ''
if 'download_txt' in request.values:
ftxt = request.files.getlist("f_txt")
errors = []
curr_time = time.time()
clean_store(curr_time)
tmp_folder = str(curr_time) + '_ptc'
META['tmp_folder'] = META['tmp_store'] + tmp_folder
os.mkdir(tmp_folder)
for el in ftxt:
try:
META['filename'] = secure_filename(el.filename)
input_text = el.read().decode('utf-8')
if 'spell' in META['current_delimiters_text']:
check_brackets = 1
else:
check_brackets = 0
if 'show' in both:
new_text, changes, wrong_changes, _ = Processor.process_text(input_text, 1, META['old_new_delimiters'][META['current_delimiters_text']], check_brackets)
else:
new_text, changes, wrong_changes, _ = Processor.process_text(input_text, 0, META['old_new_delimiters'][META['current_delimiters_text']], check_brackets)
with codecs.open('log', 'w', 'utf-8') as ou:
ou.write(changes)
print changes, 'THIS'
name = os.path.splitext(META['filename'])[0]
suffix = os.path.splitext(META['filename'])[1]
print 'GET NAME'
if suffix == '':
suffix = '.txt'
new_filename = name + "_transliterated" + suffix
log_filename = name + '_log.txt'
fnpath = tmp_folder + '/' + new_filename
lpath = tmp_folder + '/' + log_filename
print 'GET PATH'
with codecs.open(fnpath, 'w', 'utf-8') as ou1:
ou1.write(new_text)
print 'WRITE DATA'
with codecs.open(lpath, 'w', 'utf-8') as ou2:
ou2.write(changes)
print changes, 'WRITE LOG'
except:
m = 'Error: file ' + secure_filename(el.filename)
errors.append(m)
if errors:
errors = u'\n'.join(errors)
fer = tmp_folder + '/' + 'errors.txt'
with codecs.open(fer, 'w', 'utf-8') as ou3:
ou3.write(errors)
response = Response(generator(tmp_folder), mimetype='application/zip')
response.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')
return response
if 'download_xml' in request.values:
fxml = request.files.getlist("f_xml")
errors = []
tmp_folder = str(time.time()) + '_ptc'
META['tmp_folder'] = tmp_folder
os.mkdir(tmp_folder)
for el in fxml:
try:
META['filename'] = secure_filename(el.filename)
temp_filename = LoadData.get_temp_web(tmp_folder, el)
new_text, changes = LoadData.iterate_root_web(temp_filename)
os.remove(temp_filename)
name = os.path.splitext(META['filename'])[0]
suffix = os.path.splitext(META['filename'])[1]
if suffix == '':
suffix = '.txt'
new_filename = name + "_transliterated" + suffix
log_filename = name + '_log.txt'
fnpath = tmp_folder + '/' + new_filename
lpath = tmp_folder + '/' + log_filename
with codecs.open(fnpath, 'w') as ou1:
ou1.write(new_text)
if changes != u'':
with codecs.open(lpath, 'w', 'utf-8') as ou2:
ou2.write(changes)
except:
m = 'Error: file ' + secure_filename(el.filename)
errors.append(m)
if errors:
errors = u'\n'.join(errors)
fer = tmp_folder + '/' + 'errors.txt'
with codecs.open(fer, 'w', 'utf-8') as ou3:
ou3.write(errors)
response = Response(generator(tmp_folder), mimetype='application/zip')
response.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')
return response
return render_template("prereform_to_contemporary.html", output_text=output_text, input_text=input_text)
return render_template("prereform_to_contemporary.html", output_text=output_text, input_text=input_text)
def generator(tmp_folder):
z = zipstream.ZipFile(mode='w', compression=ZIP_DEFLATED)
files = os.listdir(tmp_folder)
for f in files:
if u'~' in f:
continue
p = tmp_folder + '/' + f
z.write(p)
for chunk in z:
yield chunk
def clean_store(curr_time):
sended_responces = os.listdir(META['tmp_store'])
for sr in sended_responces:
try:
test = sr[:-4]
prev = int(test)
if curr_time - prev > 15:
p = META['tmp_store'] + sr
shutil.rmtree(p)
except:
pass
@app.route("/feedback", methods=['GET', 'POST'])
def send_mail():
input_mail = ''
alert = ''
if recaptcha.verify():
user = "[email protected]"
pwd = "**********"
FROM = '[email protected]'
TO = ['[email protected]']
SUBJECT = "Feedback from old2new converter"
TEXT = request.values.get('mail')
if TEXT.strip() != u'':
msg = MIMEMultipart(encoding='utf-8')
attachment = MIMEText(TEXT.encode('utf-8'), 'plain', _charset='utf-8')
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['To'] = ', '.join(TO)
msg.attach(attachment)
server = smtplib.SMTP("smtp.yandex.com", 587)
server.ehlo()
server.starttls()
server.login(user, pwd)
server.sendmail(FROM, TO, msg.as_string().encode('utf-8'))
alert = u'Сообщение отправлено.'
server.close()
return render_template("feedback.html", input_mail = input_mail, alert = alert)
else:
if request.method == 'POST':
input_mail = request.values.get('mail')
return render_template("feedback.html", input_mail = input_mail, alert = u'Кажется, вы робот. Попробуйте ещё раз!')
return render_template("feedback.html", input_mail = input_mail, alert = alert)
@app.route("/settings", methods=['GET', 'POST'])
def get_settings():
if META['current_delimiters_text'] == 'tei':
check_txt = 'tei_txt'
uncheck_txt1 = 's_txt'
uncheck_txt2 = 'm_txt'
elif META['current_delimiters_text'] == 'simple':
check_txt = 's_txt'
uncheck_txt1 = 's_txt'
uncheck_txt2 = 'tei_txt'
elif META['current_delimiters_text'] == 'manual_text':
check_txt = 'm_txt'
uncheck_txt1 = 's_txt'
uncheck_txt2 = 'tei_txt'
if META['current_delimiters_xml'] == 'tei':
check_xml = 'tei_xml'
uncheck_xml1 = 's_xml'
uncheck_xml2 = 'm_xml'
elif META['current_delimiters_xml'] == 'simple':
check_xml = 's_xml'
uncheck_xml1 = 'tei_xml'
uncheck_xml2 = 'm_xml'
elif META['current_delimiters_xml'] == 'manual_xml':
check_xml = 'm_xml'
uncheck_xml1 = 's_xml'
uncheck_xml2 = 'tei_xml'
# print 'CURRENT BEFORE', META['current_delimiters_text'], META['current_delimiters_xml']
if request.method == 'POST':
if 'save_settings' in request.values:
checked_text = request.form['text_settings']
META['current_delimiters_text'] = checked_text
checked_xml = request.form['xml_settings']
META['current_delimiters_xml'] = checked_xml
if checked_text == 'tei':
check_txt = 'tei_txt'
uncheck_txt1 = 's_txt'
uncheck_txt2 = 'm_txt'
elif checked_text == 'simple':
check_txt = 's_txt'
uncheck_txt1 = 'tei_txt'
uncheck_txt2 = 'm_txt'
else:
check_txt = 'm_txt'
uncheck_txt1 = 's_txt'
uncheck_txt2 = 'tei_txt'
t1 = request.values.get('br_text20')
t2 = request.values.get('br_text21')
t3 = request.values.get('br_text22')
META['old_new_delimiters']['manual_text'] = [t1, t2, t3]
if checked_xml == 'tei':
check_xml = 'tei_xml'
uncheck_xml1 = 's_xml'
uncheck_xml2 = 'm_xml'
elif checked_xml == 'simple':
check_xml = 's_xml'
uncheck_xml1 = 'tei_xml'
uncheck_xml2 = 'm_xml'
else:
check_xml = 'm_xml'
uncheck_xml1 = 's_xml'
uncheck_xml2 = 'tei_xml'
x1 = request.values.get('br_xml20')
x2 = request.values.get('br_xml21')
x3 = request.values.get('br_xml22')
META['old_new_delimiters']['manual_xml'] = [x1, x2, x3]
return render_template("settings.html", check_txt=check_txt, uncheck_txt1=uncheck_txt1, uncheck_txt2=uncheck_txt2, check_xml=check_xml, uncheck_xml1=uncheck_xml1, uncheck_xml2=uncheck_xml2, v1_txt=META['old_new_delimiters']['manual_text'][0], v2_txt=META['old_new_delimiters']['manual_text'][1], v3_txt=META['old_new_delimiters']['manual_text'][2], v1_xml=META['old_new_delimiters']['manual_xml'][0], v2_xml=META['old_new_delimiters']['manual_xml'][1], v3_xml=META['old_new_delimiters']['manual_xml'][2])
return render_template("settings.html", check_txt=check_txt, uncheck_txt1=uncheck_txt1, uncheck_txt2=uncheck_txt2, check_xml=check_xml, uncheck_xml1=uncheck_xml1, uncheck_xml2=uncheck_xml2, v1_txt=META['old_new_delimiters']['manual_text'][0], v2_txt=META['old_new_delimiters']['manual_text'][1], v3_txt=META['old_new_delimiters']['manual_text'][2], v1_xml=META['old_new_delimiters']['manual_xml'][0], v2_xml=META['old_new_delimiters']['manual_xml'][1], v3_xml=META['old_new_delimiters']['manual_xml'][2])
def save_log(m):
with codecs.open('log', 'a', 'utf-8') as lf:
lf.write(m)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=2131, debug=True)
# app.run(debug=True)