def __read_directory(usr_path, file_type): """ Universal read directory. Given a path and a type, it will do the appropriate read actions :param str usr_path: Path to directory :param str file_type: .xls, .xlsx, .txt, .lpd :return none: """ # no path provided. start gui browse if not usr_path: # got dir path usr_path, src_files = get_src_or_dst("read", "directory") # Check if this is a valid directory path valid_path = path_type(usr_path, "directory") # If dir path is valid if valid_path: # List all files of target type in dir files_found = [] # Extra case for xlsx excel files if file_type == ".xls": files_found += list_files(".xlsx", usr_path) files_found += list_files(file_type, usr_path) # notify how many files were found print("Found: {} {} file(s)".format( len(files_found), FILE_TYPE_MAP[file_type]["file_type"])) # Loop for each file found for file_path in files_found: # Call read lipd for each file found __read_file(file_path, file_type) else: print("Directory path is not valid: {}".format(usr_path)) return
def __universal_read(file_path, file_type): """ Use a file path to create file metadata and load a file in the appropriate way, according to the provided file type. :param str file_path: Path to file :param str file_type: One of approved file types: xls, xlsx, txt, lpd :return none: """ global files, cwd, settings try: # check that we are using the correct function to load this file type. (i.e. readNoaa for a .txt file) correct_ext = load_fn_matches_ext(file_path, file_type) # Check that this path references a file valid_path = path_type(file_path, "file") # is the path a file? if valid_path and correct_ext: # get file metadata for one file file_meta = collect_metadata_file(file_path) # append to global files, then load in D if file_type == ".lpd": # add meta to global file meta files[".lpd"].append(file_meta) # append to global files elif file_type in [".xls", ".xlsx"]: print("reading: {}".format( print_filename(file_meta["full_path"]))) files[".xls"].append(file_meta) # append to global files elif file_type == ".txt": print("reading: {}".format( print_filename(file_meta["full_path"]))) files[".txt"].append(file_meta) # we want to move around with the files we load # change dir into the dir of the target file cwd = file_meta["dir"] if cwd: os.chdir(cwd) except Exception as e: pass # Placeholder to catch errors so we can always chdir back to cwd os.chdir(cwd) return
def __write_lipd(dat, usr_path): """ Write LiPD data to file, provided an output directory and dataset name. :param dict dat: Metadata :param str usr_path: Destination path :param str dsn: Dataset name of one specific file to write :return none: """ global settings # no path provided. start gui browse if not usr_path: # got dir path usr_path, _ignore = get_src_or_dst("write", "directory") # Check if this is a valid directory path valid_path = path_type(usr_path, "directory") # If dir path is valid if valid_path: # Filename is given, write out one file if "paleoData" in dat: try: if settings["verbose"]: print("writing: {}".format(dat["dataSetName"])) lipd_write(dat, usr_path) except KeyError as ke: print("Error: Unable to write file: unknown, {}".format(ke)) except Exception as e: print("Error: Unable to write file: {}, {}".format( dat["dataSetName"], e)) # Filename is not given, write out whole library else: if dat: for name, lipd_dat in dat.items(): try: if settings["verbose"]: print("writing: {}".format(name)) lipd_write(lipd_dat, usr_path) except Exception as e: print("Error: Unable to write file: {}, {}".format( name, e)) return