| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import print_function |
| 6 |
from CifFile import CifDic |
| 7 |
|
| 8 |
def update_one_category(dic,cat_name,hub_id,obj_id,extra_text=""): |
| 9 |
"""Add a dataname <cat_name.obj_id> to category cat_name which is |
| 10 |
a child_item of category hub_id and an additional key for cat_name""" |
| 11 |
blockname = dic.block_from_catname(cat_name) |
| 12 |
hub_blockname = dic.block_from_catname(hub_id) |
| 13 |
hub_keyname = dic[hub_blockname]['_category_key.name'][0] |
| 14 |
def_text = "This dataname indicates which member of the " + hub_id.upper() |
| 15 |
def_text += \ |
| 16 |
""" category a member of this category belongs to. It does not need to be |
| 17 |
explicitly included in datafiles if the key for the """ + hub_id + \ |
| 18 |
""" category takes a single (or default) value. """ + extra_text |
| 19 |
new_bname = dic.add_definition(obj_id,blockname,def_text) |
| 20 |
dic[new_bname]['_name.linked_item_id'] = hub_keyname |
| 21 |
dic[new_bname]['_type.purpose'] = 'Link' |
| 22 |
|
| 23 |
current_keys = dic[blockname].get('_category_key.name',[]) |
| 24 |
dic[blockname]['_category_key.name'] = current_keys + ["_"+new_bname] |
| 25 |
dic[blockname].CreateLoop(['_category_key.name']) |
| 26 |
return new_bname |
| 27 |
|
| 28 |
def add_spoke(sem_dic,hub_cat,specific_cats = set(),extra_text=''): |
| 29 |
all_cats = set([a.get('_definition.id','').lower() for a in sem_dic if a.get('_definition.scope','Item'=='Category') and a.get('_definition.class','') != 'Head']) |
| 30 |
if specific_cats is None: |
| 31 |
specific_cats = all_cats |
| 32 |
actual_cats = (all_cats & set(specific_cats)) - set([hub_cat]) |
| 33 |
print('Will update categories in following list') |
| 34 |
print(repr(actual_cats)) |
| 35 |
for one_cat in actual_cats: |
| 36 |
print('Updating %s' % one_cat) |
| 37 |
new_bname = update_one_category(sem_dic,one_cat,hub_cat,hub_cat+'_id',extra_text) |
| 38 |
print('New definition:\n') |
| 39 |
print(str(sem_dic[new_bname])) |
| 40 |
|
| 41 |
def add_hub(indic,hub_cat,key_id,extra_text=''): |
| 42 |
"""Add a hub category with hub key id <key_id>""" |
| 43 |
def_text = \ |
| 44 |
"""This category is a central category for logically connecting together a |
| 45 |
group of categories.""" |
| 46 |
blockname = indic.add_category(hub_cat) |
| 47 |
indic[blockname].overwrite = True |
| 48 |
indic[blockname]['_description.text'] = def_text |
| 49 |
indic[blockname]['_definition.class'] = 'Loop' |
| 50 |
indic[blockname]['_category_key.name'] = ['_'+hub_cat.lower()+"."+key_id] |
| 51 |
indic[blockname].CreateLoop(['_category_key.name']) |
| 52 |
|
| 53 |
new_def_text = """This dataname must have a unique value for each distinct |
| 54 |
row in this category. As a default value is defined, it need not explicitly |
| 55 |
appear in datafiles unless there is more than entry in the category.""" |
| 56 |
new_def = indic.add_definition(key_id,hub_cat,new_def_text) |
| 57 |
indic[new_def]['_enumeration.default'] = "." |
| 58 |
|
| 59 |
def add_hub_spoke(indic,hub_cat,only_cats=None,force=False): |
| 60 |
"""Edit <indic> to add a new hub category, with linked ids in all |
| 61 |
other categories, unless only_cats is specified""" |
| 62 |
sem_dic = CifDic(indic,grammar="2.0",do_minimum=True) |
| 63 |
if hub_cat.lower() not in sem_dic.cat_map.keys() or force==True: |
| 64 |
print(hub_cat + ' not found in dictionary, adding') |
| 65 |
add_hub(sem_dic, hub_cat, 'id') |
| 66 |
add_spoke(sem_dic,hub_cat,only_cats) |
| 67 |
outfile = indic + '.updated' |
| 68 |
outfile = open(outfile,"w") |
| 69 |
outfile.write(str(sem_dic)) |
| 70 |
return sem_dic |
| 71 |
|
| 72 |
if __name__ == '__main__': |
| 73 |
import sys |
| 74 |
if len(sys.argv)>2: |
| 75 |
indic = sys.argv[1] |
| 76 |
hub_cat = sys.argv[2].lower() |
| 77 |
if len(sys.argv)>3: |
| 78 |
all_cats = [a.lower() for a in sys.argv[3:]] |
| 79 |
else: |
| 80 |
all_cats = None |
| 81 |
new_dic = add_hub_spoke(indic,hub_cat,all_cats) |
| 82 |
else: |
| 83 |
print('Usage: add_spoke <dic name> <hub category name> <category name> ...') |