Пример #1
0
def parse_param_file(params_list, param_file):
    """read all the parameters in the prob_param_files and add valid
    parameters to the params list.  This returns the parameter list.

    """

    namespace = "problem"

    try:
        f = open(param_file, "r")
    except FileNotFoundError:
        sys.exit(f"write_probdata.py: ERROR: file {param_file} does not exist")

    line = get_next_line(f)

    err = 0

    while line and not err:

        # this splits the line into separate fields.  A field is a
        # single word or a pair in parentheses like "(a, b)"
        fields = re.findall(
            r'[\w\"\+\./\-]+|\([\w+\./\-]+\s*,\s*[\w\+\.\-]+\)', line)

        if len(fields) < 3:
            print(
                "write_probdata.py: ERROR: missing one or more fields in parameter definition."
            )
            err = 1
            continue

        name = fields[0]
        dtype = fields[1]
        default = fields[2]

        current_param = rp.Param(name,
                                 dtype,
                                 default,
                                 namespace=namespace,
                                 in_fortran=1)

        # optional field: in namelist
        try:
            in_namelist_in = fields[3]
            if in_namelist_in in ["y", "Y"]:
                in_namelist = True
            else:
                in_namelist = False

        except IndexError:
            in_namelist = False

        # optional field: size
        try:
            size = fields[4]
        except IndexError:
            size = 1

        current_param.in_namelist = in_namelist
        current_param.size = size

        # check to see if this parameter is defined in the current
        # list if we delete the old one and take the new one (we
        # assume that later files automatically have higher
        # priority)
        p_names = [p.name for p in params_list]
        try:
            idx = p_names.index(current_param.name)
        except ValueError:
            pass
        else:
            params_list.pop(idx)

        if not err == 1:
            params_list.append(current_param)

        line = get_next_line(f)

    return err
Пример #2
0
def parse_params(infile, out_directory):

    params = []

    namespace = None

    try:
        f = open(infile)
    except IOError:
        sys.exit("error opening the input file")

    for line in f:
        if line[0] == "#":
            continue

        if line.strip() == "":
            continue

        if line[0] == "@":
            # this is a command
            cmd, value = line.split(":")
            if cmd == "@namespace":
                fields = value.split()
                namespace = fields[0]

            else:
                sys.exit("invalid command")

            continue

        # this splits the line into separate fields.  A field is a
        # single word or a pair in parentheses like "(a, b)"
        fields = re.findall(r'[\w\"\+\.-]+|\([\w+\.-]+\s*,\s*[\w\+\.-]+\)',
                            line)

        name = fields[0]
        if name[0] == "(":
            name, cpp_var_name = re.findall(r"\w+", name)
        else:
            cpp_var_name = name

        dtype = fields[1].lower()

        default = fields[2]
        if default[0] == "(":
            default, debug_default = re.findall(r"\w+", default)
        else:
            debug_default = None

        try:
            in_fortran_string = fields[3]
        except IndexError:
            in_fortran = 0
        else:
            if in_fortran_string.lower().strip() == "y":
                in_fortran = 1
            else:
                in_fortran = 0

        try:
            ifdef = fields[4]
        except IndexError:
            ifdef = None

        if namespace is None:
            sys.exit("namespace not set")

        params.append(
            rp.Param(name,
                     dtype,
                     default,
                     cpp_var_name=cpp_var_name,
                     namespace=namespace,
                     debug_default=debug_default,
                     in_fortran=in_fortran,
                     ifdef=ifdef))

    # output

    # find all the namespaces
    namespaces = {q.namespace for q in params}

    for nm in namespaces:

        params_nm = [q for q in params if q.namespace == nm]
        ifdefs = {q.ifdef for q in params_nm}

        # write name_declares.H
        try:
            cd = open(f"{out_directory}/{nm}_declares.H", "w")
        except IOError:
            sys.exit(f"unable to open {nm}_declares.H for writing")

        cd.write(CWARNING)
        cd.write(f"#ifndef _{nm.upper()}_DECLARES_H_\n")
        cd.write(f"#define _{nm.upper()}_DECLARES_H_\n")

        for ifdef in ifdefs:
            if ifdef is None:
                for p in [q for q in params_nm if q.ifdef is None]:
                    cd.write(p.get_declare_string())
            else:
                cd.write(f"#ifdef {ifdef}\n")
                for p in [q for q in params_nm if q.ifdef == ifdef]:
                    cd.write(p.get_declare_string())
                cd.write("#endif\n")

        cd.write("#endif\n")
        cd.close()

        # write name_params.H
        try:
            cp = open(f"{out_directory}/{nm}_params.H", "w")
        except IOError:
            sys.exit(f"unable to open {nm}_params.H for writing")

        cp.write(CWARNING)
        cp.write(f"#ifndef _{nm.upper()}_PARAMS_H_\n")
        cp.write(f"#define _{nm.upper()}_PARAMS_H_\n")

        cp.write("\n")
        cp.write(f"namespace {nm} {{\n")

        for ifdef in ifdefs:
            if ifdef is None:
                for p in [q for q in params_nm if q.ifdef is None]:
                    cp.write(p.get_decl_string())
            else:
                cp.write(f"#ifdef {ifdef}\n")
                for p in [q for q in params_nm if q.ifdef == ifdef]:
                    cp.write(p.get_decl_string())
                cp.write("#endif\n")
        cp.write("}\n\n")
        cp.write("#endif\n")
        cp.close()

        # write castro_queries.H
        try:
            cq = open(f"{out_directory}/{nm}_queries.H", "w")
        except IOError:
            sys.exit(f"unable to open {nm}_queries.H for writing")

        cq.write(CWARNING)

        for ifdef in ifdefs:
            if ifdef is None:
                for p in [q for q in params_nm if q.ifdef is None]:
                    cq.write(p.get_default_string())
                    cq.write(p.get_query_string("C++"))
                    cq.write("\n")
            else:
                cq.write(f"#ifdef {ifdef}\n")
                for p in [q for q in params_nm if q.ifdef == ifdef]:
                    cq.write(p.get_default_string())
                    cq.write(p.get_query_string("C++"))
                    cq.write("\n")
                cq.write("#endif\n")
            cq.write("\n")
        cq.close()

        # write the job info tests
        try:
            jo = open(f"{out_directory}/{nm}_job_info_tests.H", "w")
        except IOError:
            sys.exit(f"unable to open {nm}_job_info_tests.H")

        for ifdef in ifdefs:
            if ifdef is None:
                for p in [q for q in params_nm if q.ifdef is None]:
                    jo.write(p.get_job_info_test())
            else:
                jo.write(f"#ifdef {ifdef}\n")
                for p in [q for q in params_nm if q.ifdef == ifdef]:
                    jo.write(p.get_job_info_test())
                jo.write("#endif\n")

        jo.close()