"""Utilities for dealing with paths."""from__future__importannotationsimportcontextlibimportosimportsocketfrompathlibimportPath
[docs]defget_uri(dir_name:str|Path)->str:""" Return the URI path for a directory. This allows files hosted on different file servers to have distinct locations. Parameters ---------- dir_name : str or Path A directory name. Returns ------- str Full URI path, e.g., "fileserver.host.com:/full/path/of/dir_name". """fullpath=Path(dir_name).absolute()hostname=socket.gethostname()withcontextlib.suppress(socket.gaierror,socket.herror):hostname=socket.gethostbyaddr(hostname)[0]returnf"{hostname}:{fullpath}"
[docs]defstrip_hostname(uri_path:str|Path)->str:""" Strop the hostname from a URI path. For example, "fileserver.host.com:/full/path/of/dir_name" will be transformed to "/full/path/of/dir_name". Parameters ---------- uri_path : str or Path A URI path. Returns ------- str The path without the hostname information. """dir_name=str(uri_path)if":"indir_name:dir_name=dir_name.split(":",1)[1]returndir_name
[docs]deffind_recent_logfile(dir_name:Path|str,logfile_extensions:str|list[str])->str:""" Find the most recent logfile in a given directory. Parameters ---------- dir_name The path to the directory to search logfile_extensions The extension (or list of possible extensions) of the logfile to search for. For an exact match only, put in the full file name. Returns ------- logfile The path to the most recent logfile with the desired extension """mod_time=0.0logfile=Noneifisinstance(logfile_extensions,str):logfile_extensions=[logfile_extensions]forfinos.listdir(dir_name):f_path=os.path.join(dir_name,f)forextinlogfile_extensions:ifextinfandos.path.getmtime(f_path)>mod_time:mod_time=os.path.getmtime(f_path)logfile=os.path.abspath(f_path)returnlogfile