10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-04 05:03:54 +01:00
quantum_package/scripts/ezfio_interface.py

163 lines
4.3 KiB
Python
Raw Normal View History

2014-12-01 16:26:34 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
from collections import defaultdict
2014-12-02 16:09:16 +01:00
2014-12-02 15:13:34 +01:00
def create_dic_fancy_type():
fancy_type = defaultdict(dict)
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
ocaml_to_fortran = {"int": "integer",
"float": "double precision",
"logical": "logical",
"string": "character*60"}
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
fancy_type['integer'] = {
"ocaml": "int",
"fortran": "integer"}
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
fancy_type['double precision'] = {
"ocaml": "float",
"fortran": "double precision"}
fancy_type['logical'] = {
"ocaml": "logical",
"fortran": "logical"}
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
src = os.environ.get('QPACKAGE_ROOT') + "/ocaml/qptypes_generator.ml"
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
with open(src, "r") as f:
l = f.read().splitlines()
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
l = [i for i in l if i.strip().startswith("*")]
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
for i in l:
ocaml_fancy_type = i.split()[1].strip()
ocaml_type = i.split()[3]
fortran_type = ocaml_to_fortran[ocaml_type]
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
fancy_type[ocaml_fancy_type] = {"ocaml": ocaml_type,
"fortran": fortran_type}
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
return dict(fancy_type)
import sys
def dict_config_file(config_file, dic_type):
d = defaultdict(dict)
list_key = ["doc", "ezfio_name", "ezfio_dir", "ezfio_default_value"]
provider_names = config_file.sections()
for provider_name in provider_names:
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
if config_file.get(provider_name, "type") not in dic_type:
2014-12-02 16:09:16 +01:00
print config_file.get(provider_name, "type"), "not avalaible. Choose in: "
print ", ".join([i for i in dic_type])
2014-12-02 15:13:34 +01:00
sys.exit(1)
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
for k in list_key:
d[provider_name][k] = config_file.get(provider_name, k)
type_raw = config_file.get(provider_name, "type")
d[provider_name]["ezfio_type"] = dic_type[type_raw]["fortran"]
d[provider_name]["ocaml_type"] = dic_type[type_raw]["ocaml"]
return dict(d)
def create_ezfio_string(dict_all, t):
a = {"config": "ezfio_type",
"default": "ezfio_default_value"}
info = a[t]
d = defaultdict(list)
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
for provider_name, dict_info in dict_all.items():
l_to_join = [" ", dict_info['ezfio_name'], dict_info[info], "\n"]
d[dict_info['ezfio_dir']].append(l_to_join)
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
# Only for fun,sry reader...
return [" ".join([k, "\n"] + [j for i in v for j in i])
for k, v in d.items()]
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
def create_ezfio_provider(dict_all):
2014-12-01 16:26:34 +01:00
from ezfio_with_default import EZFIO_Provider
lp = []
T = EZFIO_Provider()
2014-12-02 15:13:34 +01:00
for provider_name, dict_info in dict_all.items():
T.set_type(dict_info['ezfio_type'])
T.set_name(provider_name)
T.set_doc(dict_info['doc'])
T.set_ezfio_dir(dict_info['ezfio_dir'])
T.set_ezfio_name(dict_info['ezfio_name'])
T.set_default(dict_info['ezfio_default_value'])
2014-12-01 16:26:34 +01:00
2014-12-02 15:13:34 +01:00
T.set_output("output_%s" % dict_info['ezfio_dir'])
lp.append(str(T))
2014-12-01 16:26:34 +01:00
return lp
import os
2014-12-02 15:13:34 +01:00
2014-12-02 16:09:16 +01:00
def get_dict_cfg(pickle=False):
2014-12-02 15:13:34 +01:00
2014-12-02 16:09:16 +01:00
if pickle:
import pickle
path = os.environ.get('QPACKAGE_ROOT') + "/data/"
dict_cfg = pickle.load(open(path + "dict_cfg.p", "rb"))
else:
dict_cfg = {}
fancy_type = create_dic_fancy_type()
2014-12-01 16:26:34 +01:00
2014-12-02 16:09:16 +01:00
src = os.environ.get('QPACKAGE_ROOT') + "/src/"
2014-12-02 15:13:34 +01:00
2014-12-02 16:09:16 +01:00
dirs_name = [x[0] for x in os.walk(src, topdown=False)]
for path_config_folder in dirs_name:
2014-12-02 15:13:34 +01:00
2014-12-01 16:26:34 +01:00
try:
2014-12-02 16:09:16 +01:00
config = ConfigParser.ConfigParser()
2014-12-01 16:26:34 +01:00
config.readfp(open(path_config_folder + '/EZFIO.cfg'))
2014-12-02 15:13:34 +01:00
except Exception as inst:
2014-12-01 16:26:34 +01:00
pass
else:
2014-12-02 15:13:34 +01:00
dict_cfg.update(dict_config_file(config, fancy_type))
return dict_cfg
2014-12-02 16:09:16 +01:00
def get_ezfio_cmd(dict_cfg):
d = defaultdict(dict)
for k, v in dict_cfg.items():
d[k] = {"set": "set_" + v["ezfio_dir"] + "_" + k,
"get": "get_" + v["ezfio_dir"] + "_" + k}
return dict(d)
def save_dict_cfg(d):
import pickle
path = os.environ.get('QPACKAGE_ROOT') + "/data/"
pickle.dump(d, open(path + "dict_cfg.p", "wb"))
2014-12-02 15:13:34 +01:00
if __name__ == "__main__":
2014-12-02 16:09:16 +01:00
roger = get_dict_cfg()
save_dict_cfg(roger)
biloute = get_dict_cfg(True)
print biloute
2014-12-02 15:13:34 +01:00
2014-12-02 16:09:16 +01:00
for i in create_ezfio_string(biloute, "config"):
print i
2014-12-02 15:13:34 +01:00
2014-12-02 16:09:16 +01:00
for i in create_ezfio_string(biloute, "default"):
2014-12-02 15:13:34 +01:00
print i
2014-12-01 16:26:34 +01:00
2014-12-02 16:09:16 +01:00
for i in get_ezfio_cmd(biloute).items():
2014-12-02 15:13:34 +01:00
print i