Matrix "Upload Choices" code

Hi All,

Im working on sorting through some bugs on the python3 version of the grammar matrix, but I’m having some trouble making sense of some of the code.

Specifically I see in gmcs/deffile.py where the “upload choices file” button is created. and I see in matrix.cgi where different options for evaluating the choices file exist, but I’m having trouble distinguishing where in that process the file is actually dealt with.

It seems like it should either be

else: # Uploaded choices data
data = choices

or:

if data or choices.endswith(’/empty’):
f = open(os.path.join(session_path, ‘choices’), ‘w’)
f.write(data)
f.close()

but I put debug statements in to preempt the error that occures when you click “upload choices”, and it doesn’t seem to trigger in either case, which suggests either i’m very wrong, or there is a process that goes off inbetween deffile.py and the post statement back into matrix.cgi… Any thoughts or insights would be appreciated.

Hi @bmgraves,

Is this related to https://github.com/delph-in/matrix/issues/553? What is the error you’re seeing? I’m not getting an error when I upload a choices file at https://matrix.ling.washington.edu/customize/.

It seems like it should either be

else: # Uploaded choices data
    data = choices

This is just the variable assignment. Each branch of the if block this else belongs to will assign something to data, depending on what kind of ‘choices’ the user selected (e.g., a predefined one or an uploaded one). This would not fail unless the choices variable itself was not assigned anywhere (which would raise a NameError), but this is not the case.

or:

if data or choices.endswith(’/empty’):
    f = open(os.path.join(session_path, ‘choices’), ‘w’)
    f.write(data)
    f.close()

This writes the choices content assigned to data to the choices file inside the session directory. This open() call should probably have an encoding specified. Also, the with syntax for file operations is safer as it ensures that file handles are properly closed:

if data or choices.endswith('/empty'):
    path = os.path.join(session_path, 'choices')
    with open(path, 'w', encoding='utf-8') as f:
        f.write(data)

Right after these lines in matrix.cgi is a function call to deffile.py:

# if the 'section' field is defined, we have submitted values to save
if 'section' in form_data:
    matrixdef.save_choices(form_data, os.path.join(session_path, 'choices'))

If you’re getting errors from deffile.py when uploading choices, I’d look into this call (I note that the MatrixDefFile.save_choices() function also has an open() call without an encoding specified, but if that open fails I’d think the one in matrix.cgi would have failed first).

I hope this helps, but since I can’t repro the error it’s hard to guess where it might be going wrong.

1 Like

@goodmami, I think @bmgraves is using https://matrix.ling.washington.edu/trunkpy3/matrix.cgi at the moment. The main site still has an old version, I believe (probably still python2).

@goodmami thanks for the info. I will use that to try to find some errors. @olzama is correct /customize is still running the old python2 code /trunkpy3 is the running test branch.

The error is again another random apache internal server error “invalid character” message. I’m trying to get some debug statements to work to give me some better errors when it executes but I haven’t found it yet. Hopefully today will be the day.

For those following here, it looks like discussion will continue on the GitHub issue: https://github.com/delph-in/matrix/issues/553