mkjson.py 2.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
from __future__ import print_function

# TEMPLATES TAKEN FROM OCTOPUS.  KEEP SYNCHRONIZED.
json_header = """{
  "type": "nomad_meta_info_1_0",
  "description": "autogenerated nomad meta info for %(parser)s parser.  The file which generates this maintained in the parser's git repository.",
  "dependencies": [ {
      "relativePath": "common.nomadmetainfo.json"
    }],
  "metaInfos": [ %(info)s]
}"""


json_template = """{
      "description": "%(description)s",
      "dtypeStr": "%(dtypeStr)s",
      "name": "%(name)s",
      "repeats": false,
      "shape": [],
      "superNames": [
        "%(supername)s"
      ]
    }"""

json_section_template = """{
      "description": "%(description)s",
      "kindStr": "type_abstract_document_content",
      "name": "%(name)s",
29 30 31 32 33 34 35
      "superNames": [
        "x_siesta_section_input"
      ]
    }, {
      "description": "input section",
      "kindStr": "type_section",
      "name": "x_siesta_section_input",
36 37 38 39 40 41 42
      "superNames": [
        "section_run"
      ]
    }"""
# END OF TEMPLATES FROM OCTOPUS

import sys
43
#fname = sys.argv[1]
44

Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
45
inputvars_fd = open('inputvars.py', 'w')
46
print('varlist = [', file=inputvars_fd)
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
47

48 49
def getlines():
    fd = open('test/H2O/fdf-95611.log')
50 51 52
    for line in fd:
        if not line[:1].isalpha():  # Get rid of blocks
            continue
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        yield line

    withinblock = False
    fd = open('test/smeagol-Au-leads/out.fdf')
    for line in fd:
        if line.startswith('%block'):
            withinblock = True
        elif line.startswith('%endblock'):
            withinblock = False
        if withinblock:
            continue
        yield line

seen = set()

#with open(fname) as fd:
#for line in getlines():
if 1:
    varnames = []
    for line in getlines():
73 74

        # Get rid of comments
75 76 77
        line = line.strip().rsplit('#', 1)[0].strip()
        if not line:
            continue
78
        varname = line.split()[0]
79 80 81 82 83
        lowervarname = varname.lower()
        if lowervarname in seen:
            continue  # Already registered

        seen.add(lowervarname)
84 85 86 87 88 89
        varnames.append(varname)
    varnames = list(set(varnames))
    varnames.sort()

    jsontokens = []
    for var in varnames:
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
90
        print("    '%s'," % var, file=inputvars_fd)
91 92 93 94 95 96 97 98 99 100 101 102 103 104
        json = json_template % dict(description=r'siesta input variable \"%s\"'
                                    % var,
                                    name='x_siesta_input_%s' % var,
                                    dtypeStr='C',
                                    supername='x_siesta_input')
        jsontokens.append(json)
    section = json_section_template % dict(description='siesta input '
                                           'variables',
                                           name='x_siesta_input')
    jsontokens.append(section)

    txt = json_header % dict(parser='siesta',
                             info=', '.join(jsontokens))
    print(txt)
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
105 106

print(']', file=inputvars_fd)