def SQSDatabase(path, name_constraint=''): """ Convienence function to create a TinyDB for the SQS database found at `path`. Parameters ---------- path : path-like of the folder containing the SQS database. name_constraint : Any name constraint to add into the recursive glob. Not case sensitive. Exact substring. Returns ------- TinyDB Database of abstract SQS. """ db = TinyDB(storage=MemoryStorage) dataset_filenames = recursive_glob(path, '*.json') dataset_filenames = [ fname for fname in dataset_filenames if name_constraint.upper() in fname.upper() ] for fname in dataset_filenames: with open(fname) as file_: try: db.insert(json.load(file_)) except ValueError as e: raise ValueError('JSON Error in {}: {}'.format(fname, e)) return db
def get_config_file(config_folder=".", queue_script="vaspjob.pbs"): """ Get the file name of the config file in config_folder and its sub-folders Parameter config_folder: str (path like) The folder containing the config file or containing config file in its sub-folder queue_script: str (filename like) The filename of the queue script Return config_file: dict The dict of the filename(key) and corresponding path(value). If the config file is not in config_folder, store None. If more than one file (the same filename), it will store the uppest file [Ref. get_shortest_path] """ config_file = {} required_file = ["db.json", "my_launchpad.yaml"] option_file = ["FW_config.yaml", "my_fworker.yaml", "my_qadapter.yaml", queue_script] files = required_file + option_file for file in files: file_list = recursive_glob(config_folder, file) if len(file_list) == 0: if file in required_file: raise FileNotFoundError("{} file is required for configuration of dfttk.".format(file)) else: warnings.warn("{} file does not exist, the default setting will be used.".format(file)) config_file[file] = None else: config_file[file] = get_abspath(get_shortest_path(file_list)) return config_file
def get_structure_file(STR_FOLDER=".", RECURSIVE=False, MATCH_PATTERN="*"): """ Get all file names in STR_FOLDER path [and its sub-folder (RECURSIVE)] with condition (MATCH_PATTEN) Parameter STR_FOLDER: str (path-like) The path containing files, Default: . RECURSIVE: bool Including the sub-folder (True) or not (Default: False) MATCH_PATTERN: str The match pattern for the file name Return STR_FILES: list[str (filename-like)] The list of all the filenames matching the conditions """ ## Get the file name of structure file STR_FOLDER = get_abspath(STR_FOLDER) if not os.path.exists(STR_FOLDER): raise FileNotFoundError( "The specified folder/file '{}' does not exist.".format( STR_FOLDER)) if os.path.isfile(STR_FOLDER): STR_FILES = [STR_FOLDER] else: if RECURSIVE: STR_FILES = recursive_glob(STR_FOLDER, MATCH_PATTERN) else: STR_FILES = glob.glob(os.path.join(STR_FOLDER, MATCH_PATTERN)) for file_i in STR_FILES: if os.path.isdir(file_i): STR_FILES.remove(file_i) return STR_FILES