Debian Codegraph @

src/Programs/process-validate.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # This script processes the input from the web form requesting validation
3 # against one or more CIF dictionaries
4 # Note it requires the legacy-cgi package to be installed for Python >= 3.13
5
6 from __future__ import print_function
7
8 import CifFile
9 import validate_cif
10 import tempfile
11
12 import cgi #requires installation of package legacy-cgi for Python >= 3.13
13 import os
14 import cgitb; cgitb.enable() # for debugging
15
16 # output header
17 print("Content-Type: text/html\n\n")
18
19 formdata = cgi.FieldStorage()
20
21 # some constants
22 dic_directory = "/usr/local/lib/cif/"
23 input_cif = formdata["cif_file"]
24 if input_cif.file:
25 filename = input_cif.filename
26 else: filename = ""
27 is_dic = formdata.has_key("is_dic")
28 input_dics = formdata.getlist("dic_list")
29
30 # Save file data to temporary file
31 tmpfile = tempfile.mkstemp()[1]
32 jj = open(tmpfile,"w")
33 jj.write(input_cif.file.read())
34 jj.close()
35 try:
36 cf = CifFile.ReadCif(tmpfile,scantype="flex")
37 os.remove(tmpfile)
38 except CifFile.StarFile.StarError:
39 os.remove(tmpfile)
40 import sys
41 print("<h3>File reading error</h3>")
42 print("<p>File %s appears to have one or more syntax errors</p>" % input_cif.filename)
43 print("<p>The detailed error message is as follows:</p><p><pre>")
44 etype,eval,trace = sys.exc_info()
45 unsafe_str = str(eval)
46 unsafe_str = unsafe_str.replace("&","&amp;")
47 unsafe_str = unsafe_str.replace("<","&lt;")
48 safe_str = unsafe_str.replace(">","&gt;")
49 print(safe_str)
50 print("</pre>")
51 except:
52 os.remove(tmpfile)
53 print("Unspecified error reading file %s. This is most likely a CIF syntax error.")
54 # Now do the validation...
55 else:
56 diclist = map(lambda a:os.path.join(dic_directory,a),input_dics)
57 merged_dics = CifFile.merge_dic(diclist)
58 validate_cif.output_header(True,filename,input_dics)
59 print(CifFile.validate_report(CifFile.Validate(cf,dic= merged_dics,isdic=is_dic),use_html=True))
60 validate_cif.output_footer(True)