| 1 |
|
| 2 |
|
| 3 |
from CifFile import CifDic,CifFile |
| 4 |
from CifFile.StarFile import CIFStringIO |
| 5 |
import cStringIO,os,re |
| 6 |
|
| 7 |
def make_asciidoc(indic): |
| 8 |
"""Make a nice asciidoc document from a CIF dictionary""" |
| 9 |
template_files = {} |
| 10 |
base_directory = os.path.dirname(indic) |
| 11 |
sem_dic = CifDic(indic,grammar="2.0",do_imports="Contents",do_dREL=False) |
| 12 |
dep_dics = analyse_deps(sem_dic,in_directory=base_directory) |
| 13 |
blockorder = sem_dic.get_full_child_list() |
| 14 |
outstring = CIFStringIO() |
| 15 |
categories = sem_dic.get_categories() |
| 16 |
|
| 17 |
top_block = sem_dic.master_block |
| 18 |
dicname = top_block['_dictionary.title'] |
| 19 |
dicversion = top_block['_dictionary.version'] |
| 20 |
dicdate = top_block['_dictionary.date'] |
| 21 |
dicdes = prepare_text(top_block['_description.text']) |
| 22 |
outstring.write(":toc-placement: manual\n") |
| 23 |
outstring.write(":toc:\n") |
| 24 |
outstring.write("= " + dicname + "\n") |
| 25 |
outstring.write(":date: " + dicdate + "\n") |
| 26 |
outstring.write(":version: " + dicversion + "\n\n") |
| 27 |
outstring.write(dicdes + "\n\n") |
| 28 |
if len(dep_dics)>0: |
| 29 |
outstring.write("This dictionary builds on definitions found in ") |
| 30 |
for pos,one_dic in enumerate(dep_dics): |
| 31 |
outstring.write(one_dic.master_block["_dictionary.title"]) |
| 32 |
if pos < len(dep_dics)-2: |
| 33 |
outstring.write(", ") |
| 34 |
elif pos == len(dep_dics)-2: |
| 35 |
outstring.write(" and ") |
| 36 |
else: |
| 37 |
outstring.write(".") |
| 38 |
outstring.write("\n\n") |
| 39 |
outstring.write("Generated from %s, version {version}\n\n" % indic) |
| 40 |
outstring.write(""" |
| 41 |
Categories that may have more than one row are called loop categories. |
| 42 |
Datanames making up the keys for these categories have a dot suffix.""") |
| 43 |
outstring.write("\n\n"); |
| 44 |
outstring.write("toc::[]\n") |
| 45 |
|
| 46 |
section_ids = map(make_id,blockorder[1:]) |
| 47 |
for adef in blockorder[1:]: |
| 48 |
onedef = sem_dic[adef] |
| 49 |
def_type = onedef.get('_definition.scope','Item') |
| 50 |
def_text = onedef.get('_description.text','Empty') |
| 51 |
out_def_header = def_header = onedef.get('_definition.id','None') |
| 52 |
def_id = make_id(def_header) |
| 53 |
aliases = onedef.get('_alias.definition_id',[]) |
| 54 |
cat_id = onedef.get('_name.category_id',None) |
| 55 |
if def_header in aliases: |
| 56 |
aliases.remove(def_header) |
| 57 |
alternates = onedef.get('_enumeration_set.state',[]) |
| 58 |
alt_defs = [prepare_text(a,section_ids) for a in onedef.get('_enumeration_set.detail',[])] |
| 59 |
if len(alt_defs) != len(alternates): |
| 60 |
|
| 61 |
alt_defs = [None]*len(alternates) |
| 62 |
|
| 63 |
examples = onedef.get('_description_example.case',[]) |
| 64 |
example_explanation = onedef.get('_description_example.detail',[]) |
| 65 |
if len(example_explanation) != len(examples): |
| 66 |
example_explanation = ['']*len(examples) |
| 67 |
all_examples = zip(examples,example_explanation) |
| 68 |
if def_type == 'Category': |
| 69 |
outstring.write("[[%s]]\n== " % def_id) |
| 70 |
current_cat = def_header |
| 71 |
out_def_header = def_header.upper() |
| 72 |
elif def_type == 'Item': |
| 73 |
if cat_id.lower() != current_cat.lower(): |
| 74 |
|
| 75 |
|
| 76 |
dic_source = locate_category(dep_dics,cat_id) |
| 77 |
outstring.write("\n== " + cat_id.upper() + " (Original category from: %s) \n" % dic_source) |
| 78 |
outstring.write("This category is defined in external dictionary %s\n" % dic_source) |
| 79 |
current_cat = cat_id |
| 80 |
outstring.write("[[%s]]\n=== " % def_id) |
| 81 |
if is_key(sem_dic,def_header): |
| 82 |
out_def_header = def_header + r"•" |
| 83 |
else: |
| 84 |
continue |
| 85 |
outstring.write(out_def_header + "\n") |
| 86 |
if def_text == 'Empty': |
| 87 |
def_text = get_template_text(onedef,indic,template_files) |
| 88 |
else: |
| 89 |
def_text = prepare_text(def_text,section_ids) |
| 90 |
outstring.write("\n"+def_text+"\n") |
| 91 |
|
| 92 |
if len(alternates)>0: |
| 93 |
|
| 94 |
default = onedef.get('_enumeration.default',None) |
| 95 |
outstring.write("\n.Possible values") |
| 96 |
if default is not None: |
| 97 |
outstring.write('(default in bold)') |
| 98 |
outstring.write(":\n\n[horizontal]\n*****\n") |
| 99 |
for n,d in zip(alternates,alt_defs): |
| 100 |
if n == default: |
| 101 |
outstring.write("*"+vb_text(n)+"*") |
| 102 |
else: |
| 103 |
outstring.write(vb_text(n)) |
| 104 |
outstring.write("::\n\n") |
| 105 |
if d is not None: |
| 106 |
outstring.write(d+"\n") |
| 107 |
outstring.write("*****\n") |
| 108 |
if len(aliases)>0: |
| 109 |
outstring.write("\n.Aliases\n*****\n") |
| 110 |
for a in aliases: |
| 111 |
outstring.write("`"+a+"` +\n") |
| 112 |
outstring.write("*****\n") |
| 113 |
if len(all_examples)>0: |
| 114 |
for e,caption in all_examples: |
| 115 |
outstring.write("\n.Example:%s\n----\n" % prepare_text(caption)) |
| 116 |
|
| 117 |
if def_type == 'Item': |
| 118 |
onedef.format_value(e,outstring) |
| 119 |
else: |
| 120 |
outstring.write(e) |
| 121 |
outstring.write("\n----\n\n") |
| 122 |
outfile = open(indic+".adoc","w") |
| 123 |
outfile.write(outstring.getvalue()) |
| 124 |
|
| 125 |
def prepare_text(textstring,link_ids=[]): |
| 126 |
"""Transform text for better presentation""" |
| 127 |
|
| 128 |
outstring = re.sub(r"\n +",r"\n",textstring.strip()) |
| 129 |
|
| 130 |
outstring = re.sub(r"\\alpha",r"α",outstring) |
| 131 |
outstring = re.sub(r"\\\\a",r"α",outstring) |
| 132 |
outstring = re.sub(r"\\a",r"α",outstring) |
| 133 |
outstring = re.sub(r"\\b",r"β",outstring) |
| 134 |
outstring = re.sub(r"\\\\q",r"θ",outstring) |
| 135 |
outstring = re.sub(r"\\q",r"θ",outstring) |
| 136 |
outstring = re.sub(r"\\l",r"λ",outstring) |
| 137 |
outstring = re.sub(r"\\\\m",r"μ",outstring) |
| 138 |
outstring = re.sub(r"\\\\n",r"ν",outstring) |
| 139 |
outstring = re.sub(r"\\n",r"ν",outstring) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
outstring = re.sub(r"([\s(,]|^)(_[A-Za-z0-9.%_-]+)",match_to_id,outstring) |
| 144 |
|
| 145 |
outstring = re.sub(r"'s",r"\'s",outstring) |
| 146 |
outstring = re.sub(r"'([\S])'",r'"\1"',outstring) |
| 147 |
|
| 148 |
outstring = re.sub(r"(\*_[\w]+|[\w]+_\*)",r"`\1`",outstring) |
| 149 |
|
| 150 |
outstring = re.sub(r"\*(?!_)",r"\*",outstring) |
| 151 |
return outstring |
| 152 |
|
| 153 |
def match_to_id(one_match): |
| 154 |
""" Used by prepare_text to format cross-references correctly""" |
| 155 |
to_id = make_id(one_match.group(2)) |
| 156 |
return one_match.group(1)+"xref:"+to_id+"[`"+one_match.group(2)+"`]" |
| 157 |
|
| 158 |
def vb_text(textstring): |
| 159 |
"""Escape characters that might be interpreted by asciidoc""" |
| 160 |
import re |
| 161 |
outstring = re.sub(r"~(.+)",r"\~\1",textstring) |
| 162 |
return "+" + outstring + "+" |
| 163 |
|
| 164 |
def make_id(textstring): |
| 165 |
"""Convert the provided text into an ID for cross-referencing. |
| 166 |
Substitute all non-alphanumerics with underscore""" |
| 167 |
outstring = re.sub(r"\W","_",textstring) |
| 168 |
return outstring |
| 169 |
|
| 170 |
def is_key(dic,name): |
| 171 |
"""Check whether or not name is a key in its category""" |
| 172 |
cat_name = dic[name]['_name.category_id'] |
| 173 |
cat_block = dic.get(cat_name) |
| 174 |
if cat_block is not None: |
| 175 |
cat_keys = cat_block.get('_category_key.name',[]) |
| 176 |
single_key = cat_block.get('_category.key_id','') |
| 177 |
return name in cat_keys or name == single_key |
| 178 |
print 'Warning: no block found for %s' % cat_name |
| 179 |
return False |
| 180 |
|
| 181 |
def get_template_text(onedef,mainfilename,template_cache={}): |
| 182 |
"""Return some text stating that the definition comes from a template""" |
| 183 |
import os.path |
| 184 |
def_text = 'Empty' |
| 185 |
if onedef.has_key('_import.get'): |
| 186 |
filename = onedef['_import.get'][0]['file'] |
| 187 |
blockname = onedef['_import.get'][0]['save'] |
| 188 |
if filename not in template_cache: |
| 189 |
basename = os.path.join(os.path.dirname(mainfilename),filename) |
| 190 |
template_cache[filename] = CifFile(basename,grammar="2.0") |
| 191 |
def_text = '(Generic definition) '+ prepare_text(template_cache[filename][blockname]['_description.text']) |
| 192 |
else: |
| 193 |
print 'No import found' |
| 194 |
return def_text |
| 195 |
|
| 196 |
def analyse_deps(indic,in_directory="."): |
| 197 |
"""Determine which dictionaries this builds on by looking for imports in |
| 198 |
the top category""" |
| 199 |
import os |
| 200 |
cats = indic.get_categories() |
| 201 |
head_cat = [a for a in cats if indic[a].get('_definition.class',None)=='Head'][0] |
| 202 |
imports = indic[head_cat].get('_import.get') |
| 203 |
if imports is None: |
| 204 |
return [] |
| 205 |
full_imports = [d['file'] for d in imports if d.get("mode") == 'Full'] |
| 206 |
return [CifDic(os.path.join(in_directory,i),grammar="2.0",do_minimum=True) for i in full_imports] |
| 207 |
|
| 208 |
def locate_category(diclist,catname): |
| 209 |
"""Find out which dictionary has catname as a category""" |
| 210 |
found_id = [d for d in diclist if catname.lower() in d.get_categories()] |
| 211 |
found_id = [d.master_block['_dictionary.title'] for d in found_id] |
| 212 |
if len(found_id)==1: |
| 213 |
return found_id[0] |
| 214 |
elif len(found_id) == 0: |
| 215 |
return 'External category not found' |
| 216 |
else: |
| 217 |
return 'Ambiguous external dictionary' |
| 218 |
|
| 219 |
if __name__ == "__main__": |
| 220 |
import sys |
| 221 |
if len(sys.argv) > 1: |
| 222 |
indic = sys.argv[1] |
| 223 |
final_doc = make_asciidoc(indic) |
| 224 |
else: |
| 225 |
print 'Usage: output_asciidoc <dictionary_name>. Output file will be <dictionary_name>.adoc' |