def _get_io_function(mode, io): """Returns io function with specified mode. This will be determined on the fly based on file name extension and available ipi/utils/io/backends/io_*.py backends. Args: mode: Which format has the file? e.g. "pdb", "xml" or "xyz" io: One of "print_path", "print", "read" or "iter" """ try: mode = mode[mode.find(".") + 1:] if mode in mode_map: mode = mode_map[mode] module = importlib.import_module("ipi.utils.io.backends.io_%s" % mode) except ImportError: print "Error: mode %s is not supported." % mode sys.exit() try: func = getattr(module, io_map[io] % mode) except KeyError: print "Error: io %s is not supported with mode %s." % (io, mode) sys.exit() return func
def _get_io_function(mode, io): """Returns io function with specified mode. This will be determined on the fly based on file name extension and available ipi/utils/io/backends/io_*.py backends. Args: mode: Which format has the file? e.g. "pdb", "xml" or "xyz" io: One of "print_path", "print", "read" or "iter" """ try: mode = mode[mode.find(".")+1:] if mode in mode_map: mode = mode_map[mode] module = importlib.import_module("ipi.utils.io.backends.io_%s" % mode) except ImportError: print "Error: mode %s is not supported." % mode sys.exit() try: func = getattr(module, io_map[io] % mode) except KeyError: print "Error: io %s is not supported with mode %s." % (io, mode) sys.exit() return func
def read_file(mode, filedesc, **kwargs): """Reads one frame from an open `mode`-style file. Args: filedesc: An open readable file object from a `mode` formatted file. All other args are passed directly to the responsible io function. Returns: A dictionary with 'atoms', 'cell' and 'comment'. """ return importlib.import_module("ipi.utils.io.backends.io_units").\ process_units(*_get_io_function(mode, "read")(filedesc=filedesc, **kwargs), output=kwargs["output"] if "output" in kwargs.keys() else "objects")