@@ -24,6 +24,13 @@ from gmcs.choices import ChoicesFile
24
24
from gmcs .linglib .toolboximport import import_toolbox_lexicon
25
25
26
26
27
+ # Sometimes UTF-8 files have a (gratuitous) BOM. The utf-8-sig
28
+ # encoding will strip the BOM, but we want to always write files
29
+ # without it, so use regular utf-8 on write.
30
+ READ_ENCODING = 'utf-8-sig'
31
+ WRITE_ENCODING = 'utf-8'
32
+
33
+
27
34
cgitb .enable ()
28
35
29
36
# Production Check
@@ -106,23 +113,30 @@ for s in sessions:
106
113
# figure out the path to the current session's directory, creating it
107
114
# if necessary
108
115
session_path = 'sessions/' + cookie
116
+ choices_path = os .path .join (session_path , 'choices' )
117
+
109
118
if cookie and not os .path .exists (session_path ):
110
119
os .mkdir (session_path )
111
120
# create a blank choices file
112
- open (os .path .join (session_path , 'choices' ), 'w' ).close ()
121
+ with open (choices_path , 'w' , encoding = WRITE_ENCODING ):
122
+ pass
113
123
114
124
# if the 'choices' field is defined, we have either the contents of an
115
125
# uploaded choices file or the name of a sample choices file (which
116
126
# will begin with 'sample-choices/') to replace the current choices.
117
127
# TJT 2014-09-18: Get choices files from Language CoLLAGE links
118
128
if 'choices' in form_data :
119
- choices = form_data ['choices' ].value
129
+ choices_item = form_data ['choices' ]
130
+ if choices_item .file :
131
+ # TODO: handle encoding problems
132
+ choices = choices_item .value .decode (READ_ENCODING )
133
+ else :
134
+ choices = choices_item .value
120
135
if choices :
121
136
data = ''
122
137
if choices .startswith ('web/sample-choices/' ):
123
- f = open (choices , 'r' )
124
- data = f .read ()
125
- f .close ()
138
+ with open (choices , 'r' , encoding = READ_ENCODING ) as f :
139
+ data = f .read ()
126
140
elif choices .startswith ('collage/' ):
127
141
# Get choices files from CoLLAGE
128
142
# should be 3 or 7 letter keys... doesn't work for other length keys
@@ -150,13 +164,12 @@ if 'choices' in form_data:
150
164
else : # Uploaded choices data
151
165
data = choices
152
166
if data or choices .endswith ('/empty' ):
153
- f = open (os .path .join (session_path , 'choices' ), 'w' )
154
- f .write (data )
155
- f .close ()
167
+ with open (choices_path , 'w' , encoding = WRITE_ENCODING ) as f :
168
+ f .write (data )
156
169
157
170
# if the 'section' field is defined, we have submitted values to save
158
171
if 'section' in form_data :
159
- matrixdef .save_choices (form_data , os . path . join ( session_path , 'choices' ) )
172
+ matrixdef .save_choices (form_data , choices_path )
160
173
161
174
# if we have recieved toolbox files, then we want to add these lexical items after saving the toolbox configuration (done above).
162
175
if 'import_toolbox' in form_data :
@@ -167,8 +180,8 @@ if 'import_toolbox' in form_data:
167
180
fout .write (form_data [key ].value )
168
181
toolbox_files .append (fout )
169
182
form_data [key ].value = fout .name
170
- matrixdef .save_choices (form_data , os . path . join ( session_path , 'choices' ) )
171
- import_toolbox_lexicon (os . path . join ( session_path , 'choices' ) )
183
+ matrixdef .save_choices (form_data , choices_path )
184
+ import_toolbox_lexicon (choices_path )
172
185
for tbfile in toolbox_files :
173
186
tbfile .close ()
174
187
@@ -184,10 +197,10 @@ if 'verbpred' in form_data:
184
197
# no longer true, there can now be validation info messages.
185
198
# nothing seems to depend on the list being empty #14 feb 2012
186
199
try :
187
- vr = validate_choices (os . path . join ( session_path , 'choices' ) )
200
+ vr = validate_choices (choices_path )
188
201
except :
189
202
exc = sys .exc_info ()
190
- matrixdef .choices_error_page (os . path . join ( session_path , 'choices' ) , exc )
203
+ matrixdef .choices_error_page (choices_path , exc )
191
204
sys .exit ()
192
205
193
206
# modified to support captcha
@@ -208,7 +221,7 @@ elif 'customize' in form_data:
208
221
matrixdef .error_page (vr )
209
222
else :
210
223
# If the user said it's OK, archive the choices file
211
- choices = ChoicesFile (os . path . join ( session_path , 'choices' ) )
224
+ choices = ChoicesFile (choices_path )
212
225
if choices .get ('archive' ) == 'yes' :
213
226
# create the saved-choices directory
214
227
if not os .path .exists ('saved-choices' ):
@@ -224,15 +237,15 @@ elif 'customize' in form_data:
224
237
num = f [i + 1 :]
225
238
if num .isdigit ():
226
239
serial = max (serial , int (num ) + 1 )
227
- shutil .copy (os . path . join ( session_path , 'choices' ) ,
240
+ shutil .copy (choices_path ,
228
241
'saved-choices/choices.' + str (serial ))
229
242
230
243
# Create the customized grammar
231
244
try :
232
245
grammar_dir = customize_matrix (session_path , arch_type )
233
246
except :
234
247
exc = sys .exc_info ()
235
- matrixdef .customize_error_page (os . path . join ( session_path , 'choices' ) ,
248
+ matrixdef .customize_error_page (choices_path ,
236
249
exc )
237
250
sys .exit ()
238
251
0 commit comments