def read_zeolite(binary_filepath: str, parent_zeolite: PerfectZeolite, json_filepath: Optional[str] = None) -> Zeolite: if json_filepath: with open(json_filepath, 'r') as f: attr_dict = json.load(f) additions_map = attr_dict['additions_maps'] else: additions_map = None z = Zeolite(read(binary_filepath)) z.register_with_parent(parent_zeolite, additions_map) return z
def read_zeolites(file_path: str, str_ext: str = '.traj', zipped: bool = True, glob_cmd: Optional[str] = None) -> Dict[str, PerfectZeolite]: """ Read the zeolites from a .zeo file or folder or folder :param file_path: path to .zeo file with or without .zeo extension or path to unzipped zeo folder :type file_path: str :param str_ext: type of files in .zeo zip file :type str_ext: str :param glob_cmd: regular expression to select from folder structure :type glob_cmd: str :return: Dictionary of Zeotypes loaded from the files :rtype: Dict[str, PerfectZeolite] """ if zipped: if '.' not in file_path: file_path = file_path + '.zeo' folder_path = unpack_zeo_file(file_path) else: folder_path = file_path zeotype_dict = {} folder_list = glob.glob(os.path.join(folder_path, '*/')) # read parent zeolite try: parent_folder = [ folder for folder in folder_list if Path(folder).stem == 'parent' ][0] except IndexError as e: raise FileExistsError( 'parent folder not found in specified directory') from e # get parent zeolite binary file # use regex binary command if glob_cmd: binary_glob_cmd = glob_cmd else: binary_glob_cmd = '*' + str_ext binary_filepath = glob.glob(os.path.join(parent_folder, binary_glob_cmd))[0] json_filepath = glob.glob(os.path.join(parent_folder, '*.json'))[0] index_mapper_filepath = glob.glob( os.path.join(folder_path, 'index_mapper.json'))[0] parent_zeolite = read_parent_zeolite(binary_filepath, json_filepath, index_mapper_filepath) zeotype_dict['parent'] = parent_zeolite for folder in folder_list: if folder == parent_folder: continue name = Path(folder).stem json_path = os.path.join(folder, name + '.json') binary_path = os.path.join(folder, name + str_ext) with open(json_path, 'r') as f: my_zeotype = Zeolite(read(binary_path)) attr_dict = json.load(f) my_zeotype.name = name additions_map = attr_dict['additions_map'] my_zeotype.register_with_parent(parent_zeolite, additions_map) zeotype_dict[name] = my_zeotype if zipped: delete_folder(folder_path) return zeotype_dict