forked from amir-zeldes/rstWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.py
230 lines (188 loc) · 7.25 KB
/
segment.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Interface to segment a document into elementary discourse units (EDUs). These are the basis for
the EDU nodes in the structurer script's layout. Dynamic editing of the segments is handled
by script/segment.js, and once saved this script updates the database and renders the saved
segmentation.
Author: Amir Zeldes
"""
import _version
import cgitb
from modules.rstweb_sql import *
import codecs
import sys
import re
from modules.configobj import ConfigObj
from modules.pathutils import *
import cgi
import os
from modules.logintools import login
import datetime
def segment_main(user, admin, mode, **kwargs):
cpout = ""
theform = kwargs
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
cgitb.enable()
config = ConfigObj(userdir + 'config.ini')
templatedir = scriptpath + config['controltemplates'].replace("/",os.sep)
template = "main_header.html"
header = readfile(templatedir+template)
header = header.replace("**page_title**","Segmentation editor")
header = header.replace("**user**",user)
header = header.replace("**open_disabled**",'')
if admin == "0":
header = header.replace("**admin_disabled**",'disabled="disabled"')
else:
header = header.replace("**admin_disabled**",'')
cpout = ""
if mode == "server":
cpout += "Content-Type: text/html\n\n\n"
header = header.replace("**logout_control**",'(<a href="logout.py">log out</a>)')
else:
header = header.replace("**logout_control**",'')
cpout += header
if "current_doc" in theform:
current_doc = theform["current_doc"]
current_project = theform["current_project"]
current_guidelines = get_guidelines_url(current_project)
else:
current_doc = ""
current_project = ""
current_guidelines = ""
edit_bar = "edit_bar.html"
edit_bar = readfile(templatedir+edit_bar)
edit_bar = edit_bar.replace("**doc**",current_doc)
edit_bar = edit_bar.replace("**project**",current_project)
edit_bar = edit_bar.replace("**structure_disabled**",'')
edit_bar = edit_bar.replace("**quickexp_disabled**",'')
edit_bar = edit_bar.replace("**screenshot_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace("**save_disabled**",'')
edit_bar = edit_bar.replace("**reset_disabled**",'')
edit_bar = edit_bar.replace("**segment_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace("**submit_target**",'segment.py')
edit_bar = edit_bar.replace("**action_type**",'seg_action')
edit_bar = edit_bar.replace("**current_guidelines**",current_guidelines)
edit_bar = edit_bar.replace("**serve_mode**",mode)
edit_bar = edit_bar.replace("**open_disabled**",'')
edit_bar = edit_bar.replace('id="nav_segment" class="nav_button"','id="nav_segment" class="nav_button nav_button_inset"')
if admin == "0":
edit_bar = edit_bar.replace("**admin_disabled**",'disabled="disabled"')
else:
edit_bar = edit_bar.replace("**admin_disabled**",'')
cpout += edit_bar
help = "help.html"
help = readfile(templatedir+help)
help_match = re.search(r'(<div.*?help_seg.*?</div>)',help,re.MULTILINE|re.DOTALL)
help = help_match.group(1)
cpout += help
about = "about.html"
about = readfile(templatedir+about)
about = about.replace("**version**", _version.__version__)
cpout += about
if current_doc =="":
cpout += '<p class="warn">No file found - please select a file to open</p>'
return cpout
cpout += '<input id="undo_log" type="hidden" value=""/>'
cpout += '<input id="redo_log" type="hidden" value=""/>'
cpout += '<input id="undo_state" type="hidden" value=""/>'
cpout += '''<div class="canvas">
<div id="inner_canvas">'''
# Remove floating non-terminal nodes if found
# (e.g. due to browsing back and re-submitting old actions or other data corruption)
clean_floating_nodes(current_doc, current_project, user)
timestamp = ""
if "timestamp" in theform:
if len(theform["timestamp"]) > 1:
timestamp = theform["timestamp"]
refresh = check_refresh(user, timestamp)
if "reset" in theform or user=="demo":
if len(theform["reset"]) > 1 or user=="demo":
reset_rst_doc(current_doc,current_project,user)
if "logging" in theform and not refresh:
if len(theform["logging"]) > 1:
if get_setting("logging") == "on":
logging = theform["logging"]
if len(logging) > 0:
update_log(current_doc,current_project,user,logging,"segment",str(datetime.datetime.now()))
if "seg_action" in theform and not refresh:
if len(theform["seg_action"]) > 1:
action_log = theform["seg_action"]
if len(action_log) > 0:
actions = action_log.split(";")
set_timestamp(user,timestamp)
for action in actions:
action_type = action.split(":")[0]
action_params = action.split(":")[1]
if action_type =="ins":
insert_seg(int(action_params.replace("tok","")),current_doc,current_project,user)
elif action_type =="del":
merge_seg_forward(int(action_params.replace("tok","")),current_doc,current_project,user)
segs={}
rows = get_rst_doc(current_doc,current_project,user)
if current_guidelines != "":
cpout += '<script>enable_guidelines();</script>'
cpout += '\t<script src="script/segment.js"></script>'
cpout += '<h2>Edit segmentation</h2>'
cpout += '\t<div id="control">'
cpout += '\t<p>Document: <b>'+current_doc+'</b> (project: <i>'+current_project+'</i>)</p>'
cpout += '\t<div id="segment_canvas">'
for row in rows:
if row[5] =="edu":
segs[int(row[0])] = SEGMENT(row[0],row[6])
seg_counter=0
tok_counter=0
segs = collections.OrderedDict(sorted(segs.items()))
first_seg = True
for seg_id in segs:
first_tok = True
seg = segs[seg_id]
seg_counter+=1
if first_seg:
first_seg = False
else:
cpout += '<div class="tok_space" id="tok'+str(tok_counter)+'" style="display:none" onclick="act('+"'ins:"+'tok'+str(tok_counter)+"'"+')"> </div>'
cpout += '\t\t\t<div id="segend_post_tok'+str(tok_counter)+'" class="seg_end" onclick="act('+"'del:"+'tok'+str(tok_counter)+"'"+')">||</div>'
cpout += '\t\t</div>'
cpout += '\t\t<div id="seg'+ str(seg_counter) +'" class="seg">'
for token in seg.tokens:
tok_counter+=1
if first_tok:
first_tok = False
else:
cpout += '<div class="tok_space" id="tok'+str(tok_counter-1)+'" onclick="act('+"'ins:"+'tok'+str(tok_counter-1)+"'"+')"> </div>'
cpout += '\t\t\t<div class="token" id="string_tok'+str(tok_counter)+'">' + token + '</div>'
cpout += '\t\t</div>'
cpout += '''\t</div></div>
</body>
</html>
'''
if mode != "server":
cpout = cpout.replace(".py","")
return cpout
# Main script when running from Apache
def segment_main_server():
thisscript = os.environ.get('SCRIPT_NAME', '')
action = None
theform = cgi.FieldStorage()
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
action, userconfig = login(theform, userdir, thisscript, action)
user = userconfig["username"]
admin = userconfig["admin"]
kwargs={}
for key in theform:
kwargs[key] = theform[key].value
print(segment_main(user, admin, 'server', **kwargs))
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
config = ConfigObj(userdir + 'config.ini')
if "/" in os.environ.get('SCRIPT_NAME', ''):
mode = "server"
else:
mode = "local"
if mode == "server":
segment_main_server()