| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import print_function |
| 7 |
from __future__ import unicode_literals |
| 8 |
from __future__ import division |
| 9 |
from __future__ import absolute_import |
| 10 |
|
| 11 |
|
| 12 |
try: |
| 13 |
from urllib2 import urlopen |
| 14 |
from urllib.parse import url2pathname |
| 15 |
except: |
| 16 |
from urllib.request import urlopen,url2pathname |
| 17 |
|
| 18 |
|
| 19 |
from optparse import OptionParser |
| 20 |
|
| 21 |
import CifFile |
| 22 |
import os |
| 23 |
import urllib |
| 24 |
import traceback |
| 25 |
|
| 26 |
import CifFile.yapps3_compiled_rt as yappsrt |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
def cif_by_ftp(ftp_ptr,store=True,directory="."): |
| 31 |
|
| 32 |
if store: |
| 33 |
new_fn = os.path.split(url2pathname(ftp_ptr))[1] |
| 34 |
target = os.path.abspath(os.path.join(directory,new_fn)) |
| 35 |
if target != ftp_ptr: |
| 36 |
response = urlopen(ftp_ptr) |
| 37 |
contents = response.read() |
| 38 |
open(target,"wb").write(contents) |
| 39 |
print("Stored {} as {}".format(ftp_ptr,target)) |
| 40 |
print('Reading ' + target) |
| 41 |
ret_cif = CifFile.CifFile(target) |
| 42 |
else: |
| 43 |
ret_cif = CifFile.ReadCif(ftp_ptr) |
| 44 |
return ret_cif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
def locate_dic(dicname,dicversion,regloc="cifdic.register",store_dir = "."): |
| 50 |
register = cif_by_ftp(regloc,directory=store_dir) |
| 51 |
good_gen = register["validation_dictionaries"] |
| 52 |
dataloop = good_gen.GetLoop("_cifdic_dictionary.version") |
| 53 |
matches = [a for a in dataloop if getattr(a,"_cifdic_dictionary.name")==dicname and \ |
| 54 |
getattr(a,"_cifdic_dictionary.version")==dicversion] |
| 55 |
if len(matches)==0: |
| 56 |
print( "Unable to find any matches for {} version {}".format(dicname,dicversion)) |
| 57 |
return "" |
| 58 |
elif len(matches)>1: |
| 59 |
print( "Warning: found more than one candidate, choosing first.") |
| 60 |
print( map(str,matches)) |
| 61 |
return getattr(matches[0],"_cifdic_dictionary.URL") |
| 62 |
|
| 63 |
|
| 64 |
def parse_options(): |
| 65 |
|
| 66 |
op = OptionParser(usage="%prog [options] ciffile", version="%prog 0.7") |
| 67 |
op.add_option("-d","--dict_dir", dest = "dirname", default = ".", |
| 68 |
help = "Directory where locally stored dictionaries are located") |
| 69 |
op.add_option("-f","--dict_file", dest = "dictnames", |
| 70 |
action="append", |
| 71 |
help = "A dictionary name stored locally") |
| 72 |
op.add_option("-u","--dict-version", dest = "versions", |
| 73 |
action="append", |
| 74 |
help = "A dictionary version") |
| 75 |
op.add_option("-n","--name", dest = "iucr_names",action="append", |
| 76 |
help = "Dictionary name as registered by IUCr") |
| 77 |
op.add_option("-s","--store", dest = "store_flag",action="store_true", |
| 78 |
help = "Store this dictionary locally", default=True) |
| 79 |
op.add_option("-c","--canon-reg", dest = "registry",action="store_const", |
| 80 |
const = "ftp://ftp.iucr.org/pub/cifdics/cifdic.register", |
| 81 |
help = "Fetch and use canonical dictionary registry from IUCr") |
| 82 |
op.add_option("-m","--markup", dest = "use_html",action="store_true", |
| 83 |
help = "Output result in HTML",default=False) |
| 84 |
op.add_option("-t","--is_dict", dest = "dict_flag", action="store_true",default=False, |
| 85 |
help = "CI y") |
| 86 |
op.add_option("-r","--registry-loc", dest = "registry", |
| 87 |
default = "file:cifdic.register", |
| 88 |
help = "Location of global dictionary registry (see also -c option)") |
| 89 |
op.add_option("-v", "--verbose_validation", action="store_true", default=False, |
| 90 |
help="Log information in the validation process.") |
| 91 |
op.add_option("-w", "--verbose_import", action="store_true", default=False, |
| 92 |
help="Log information in the dictionary importing process.") |
| 93 |
(options,args) = op.parse_args() |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
import sys |
| 100 |
if len(sys.argv) <= 1: |
| 101 |
print( "No arguments given: use option --help to get a help message\n") |
| 102 |
exit |
| 103 |
|
| 104 |
return options,args |
| 105 |
|
| 106 |
def print_cif_syntax_error(parsing_result, cif_file_name): |
| 107 |
error = parsing_result[1] |
| 108 |
|
| 109 |
|
| 110 |
if isinstance(error, yappsrt.YappsSyntaxError): |
| 111 |
parser = parsing_result[2] |
| 112 |
Y = parsing_result[3] |
| 113 |
|
| 114 |
scanner = parser._scanner |
| 115 |
input = parser._scanner.input |
| 116 |
pos = error.charpos |
| 117 |
|
| 118 |
line_number = scanner.get_line_number_with_pos(pos) |
| 119 |
|
| 120 |
out_str = "\n" |
| 121 |
out_str += "======================================================================\n" |
| 122 |
out_str += "\n" |
| 123 |
out_str += "SYNTAX ERROR AT LINE " + str(line_number) + " WHEN PARSING INPUT FILE:" + str(cif_file_name) + ":\n" |
| 124 |
out_str += str(error.msg) + "\n" |
| 125 |
out_str += "\n" |
| 126 |
out_str += "ERROR NEAR THE FOLLOWING INPUT TEXT:\n" |
| 127 |
|
| 128 |
text_error = Y.yappsrt.print_line_with_pointer(input, pos) |
| 129 |
|
| 130 |
out_str += text_error |
| 131 |
print(out_str) |
| 132 |
|
| 133 |
|
| 134 |
if isinstance(error, CifFile.StarError): |
| 135 |
out_str = "\n" |
| 136 |
out_str += "======================================================================\n" |
| 137 |
out_str += "\n" |
| 138 |
out_str += "ERROR ENCOUNTERED WHEN PARSING INPUT FILE:" + str(cif_file_name) + ":\n\n" |
| 139 |
out_str += str(error.value) + "\n" |
| 140 |
|
| 141 |
print(out_str) |
| 142 |
|
| 143 |
return out_str |
| 144 |
|
| 145 |
def execute_with_options(options,args): |
| 146 |
print(args) |
| 147 |
print() |
| 148 |
print(options) |
| 149 |
|
| 150 |
verbose_import = options.verbose_import |
| 151 |
verbose_validation = options.verbose_validation |
| 152 |
|
| 153 |
if options.dictnames: |
| 154 |
diclist = list(map(lambda a:os.path.join(options.dirname,a),options.dictnames)) |
| 155 |
print( "Using following local dictionaries to validate:") |
| 156 |
for dic in diclist: print( "{}".format(dic)) |
| 157 |
|
| 158 |
fulldic = CifFile.CifFile_module.merge_dic(diclist, verbose_import=verbose_import, verbose_validation=verbose_validation) |
| 159 |
else: |
| 160 |
|
| 161 |
dics = zip(options.iucr_names,options.versions) |
| 162 |
dicurls = map(lambda a:locate_dic(a[0],a[1],regloc=options.registry,store_dir=options.dirname),dics) |
| 163 |
diccifs = map(lambda a:cif_by_ftp(a,options.store_flag,options.dirname),dicurls) |
| 164 |
fulldic = CifFile.CifFile_module.merge_dic(diccifs, verbose_import=verbose_import, verbose_validation=verbose_validation) |
| 165 |
diclist = dicurls |
| 166 |
|
| 167 |
f = CifFile.CifFile(fulldic) |
| 168 |
|
| 169 |
cif_file_name = args[0] |
| 170 |
|
| 171 |
with open(cif_file_name, "r") as f: |
| 172 |
cif_text = f.read() |
| 173 |
|
| 174 |
cf = CifFile.CifFile(cif_text,grammar="auto", from_str=True, allow_partial = True) |
| 175 |
|
| 176 |
result = cf.get_parsing_result() |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if result[0] < 0: |
| 181 |
error_str = print_cif_syntax_error(result, cif_file_name) |
| 182 |
cc = (False, None) |
| 183 |
|
| 184 |
return cc, error_str |
| 185 |
|
| 186 |
output_header(options.use_html,args[0],diclist) |
| 187 |
|
| 188 |
cc = CifFile.Validate(cf,dic= fulldic,isdic=options.dict_flag) |
| 189 |
report_str, dict_summary = CifFile.validate_report(cc,use_html=options.use_html) |
| 190 |
|
| 191 |
print(report_str) |
| 192 |
output_footer(options.use_html) |
| 193 |
|
| 194 |
return cc, report_str |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
def output_header(use_html,filename,dictionaries): |
| 201 |
prog_info = "Validate_cif version 0.7, Copyright ANSTO 2005-2020\n" |
| 202 |
if use_html: |
| 203 |
print( "<html><head><title>PyCIFRW validation report</title></head>") |
| 204 |
print( '<style type="text/css">') |
| 205 |
print( " body {font-family: verdana, sans-serif;}") |
| 206 |
print( " body {margin-left: 5%; margin-right: 5%;}") |
| 207 |
print( " table{background: #f0f0f8;}") |
| 208 |
print( " h4 {background: #f0f8f0;}") |
| 209 |
print( "</style><body>") |
| 210 |
print( "<h1>Validation results for {}</h1>".format(filename)) |
| 211 |
print( "<p>Validation performed by {}</p>".format(prog_info)) |
| 212 |
print( "<p>Dictionaries used:<ul>") |
| 213 |
for one_dic in dictionaries: |
| 214 |
print( "<li>{}".format(one_dic)) |
| 215 |
print( "</ul>") |
| 216 |
else: |
| 217 |
print( "Validation results for {}\n".format(filename)) |
| 218 |
print( "Validation performed by {}".format(prog_info)) |
| 219 |
print( "File validated against following dictionaries:") |
| 220 |
|
| 221 |
for one_dic in dictionaries: |
| 222 |
print( " {}".format(one_dic)) |
| 223 |
|
| 224 |
def output_footer(use_html): |
| 225 |
if use_html: |
| 226 |
print( "</body></html>") |
| 227 |
|
| 228 |
def main (): |
| 229 |
execute_with_options(*parse_options()) |
| 230 |
|
| 231 |
if __name__ == "__main__": |
| 232 |
main() |