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
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
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
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)
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))
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)