Beispiel #1
0
def import_program_module(prog, typ):
    """ import the module for a program by name

    :param prog: the program name
    :type prog: str
    :param typ: which type of module it is (writer or reader)
    :type typ: str
    """
    assert prog in pclass.values(par.Program)
    assert typ in pclass.values(par.Module)

    name = module_name(prog)
    module = importlib.import_module('elstruct.{:s}.{:s}'.format(typ, name))
    return module
Beispiel #2
0
def _intercept_scf_guess_option(scf_opts):
    guess_opts = []
    ret_scf_opts = []
    for opt in scf_opts:
        if (elstruct.option.is_valid(opt)
                and opt in pclass.values(elstruct.par.Option.Scf.Guess)):
            guess_opts.append(opt)
        else:
            ret_scf_opts.append(opt)
    return guess_opts, ret_scf_opts
Beispiel #3
0
def program_modules_with_function(function):
    """
        :param function: a function with the desired signature
        :type function: function
    """

    progs = []
    for prog in pclass.values(par.Program):
        if function in READER_MODULE_DCT[prog]:
            progs.append(prog)

    return progs
Beispiel #4
0
def call_module_function(prog, function, *args, **kwargs):
    """ call the module implementation of a given function

        :param prog: the program
        :type prog: str
        :param function_template: a function with the desired signature
        :type function_template: function
    """

    assert prog in pclass.values(par.Program)
    assert prog in program_modules_with_function(function)

    name = '_{}'.format(prog)
    module = importlib.import_module('elstruct.reader.{:s}'.format(name))
    reader = getattr(module, function)

    return reader(*args, **kwargs)
Beispiel #5
0
def program_modules_with_function(typ, function_template):
    """ list the programs implementing a given function

    :param typ: which type of module it is (writer or reader)
    :type typ: str
    :param function_template: a function with the desired signature
    :type function_template: function
    """
    progs = []
    for prog in pclass.values(par.Program):
        module = import_program_module(prog, typ)
        if hasattr(module, function_template.__name__):
            function = getattr(module, function_template.__name__)

            # make sure the signature matches the template
            assert _getargspec(function) == _getargspec(function_template)

            progs.append(prog)

    return tuple(sorted(progs))
Beispiel #6
0
def call_module_function(prog, function, *args, **kwargs):
    """ call the module implementation of a given function

        :param prog: the program
        :type prog: str
        :param function_template: a function with the desired signature
        :type function_template: function
    """
    def _rename_prog(prog):
        """ Rename a program if number does not match module name """
        if prog == 'molpro2021':
            prog = 'molpro2015'
        elif prog == 'gaussian16':
            prog = 'gaussian09'
        return prog

    assert prog in pclass.values(par.Program)
    assert prog in program_modules_with_function(function)

    name = f'_{_rename_prog(prog)}'
    module = importlib.import_module(f'elstruct.writer.{name:s}')
    writer = getattr(module, 'write_input')

    return writer(function, *args, **kwargs)