mkjson.py 2.08 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 29 30 31 32 33 34 35 36 37
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",
      "superNames": [
        "section_run"
      ]
    }"""
# END OF TEMPLATES FROM OCTOPUS

import sys
fname = sys.argv[1]

Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
38 39 40
inputvars_fd = open('inputvars.py', 'w')
print('inputvars = [', file=inputvars_fd)

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
with open(fname) as fd:
    varnames = []
    for line in fd:
        if not line[:1].isalpha():  # Get rid of blocks
            continue

        # Get rid of comments
        line = line.strip().rsplit('#', 1)[0]
        varname = line.split()[0]
        varnames.append(varname)
    varnames = list(set(varnames))
    varnames.sort()

    jsontokens = []
    for var in varnames:
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
56
        print("    '%s'," % var, file=inputvars_fd)
57 58 59 60 61 62 63 64 65 66 67 68 69 70
        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
71 72

print(']', file=inputvars_fd)