1"""
2utils:
3- provides utility wrappers to run asynchronous functions in a blocking environment.
4- vendor functions from ipython_genutils that should be retired at some point.
5"""
6from __future__ import annotations
7
8import os
9from typing import Sequence
10
11from jupyter_core.utils import ensure_async, run_sync # noqa: F401 # noqa: F401
12
13from .session import utcnow # noqa
14
15
16def _filefind(filename: str, path_dirs: str | Sequence[str] | None = None) -> str:
17 """Find a file by looking through a sequence of paths.
18
19 This iterates through a sequence of paths looking for a file and returns
20 the full, absolute path of the first occurrence of the file. If no set of
21 path dirs is given, the filename is tested as is, after running through
22 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
23
24 filefind('myfile.txt')
25
26 will find the file in the current working dir, but::
27
28 filefind('~/myfile.txt')
29
30 Will find the file in the users home directory. This function does not
31 automatically try any paths, such as the cwd or the user's home directory.
32
33 Parameters
34 ----------
35 filename : str
36 The filename to look for.
37 path_dirs : str, None or sequence of str
38 The sequence of paths to look for the file in. If None, the filename
39 need to be absolute or be in the cwd. If a string, the string is
40 put into a sequence and the searched. If a sequence, walk through
41 each element and join with ``filename``, calling :func:`expandvars`
42 and :func:`expanduser` before testing for existence.
43
44 Returns
45 -------
46 Raises :exc:`IOError` or returns absolute path to file.
47 """
48
49 # If paths are quoted, abspath gets confused, strip them...
50 filename = filename.strip('"').strip("'")
51 # If the input is an absolute path, just check it exists
52 if os.path.isabs(filename) and os.path.isfile(filename):
53 return filename
54
55 if path_dirs is None:
56 path_dirs = ("",)
57 elif isinstance(path_dirs, str):
58 path_dirs = (path_dirs,)
59
60 for path in path_dirs:
61 if path == ".":
62 path = os.getcwd() # noqa
63 testname = _expand_path(os.path.join(path, filename))
64 if os.path.isfile(testname):
65 return os.path.abspath(testname)
66 msg = f"File {filename!r} does not exist in any of the search paths: {path_dirs!r}"
67 raise OSError(msg)
68
69
70def _expand_path(s: str) -> str:
71 """Expand $VARS and ~names in a string, like a shell
72
73 :Examples:
74
75 In [2]: os.environ['FOO']='test'
76
77 In [3]: expand_path('variable FOO is $FOO')
78 Out[3]: 'variable FOO is test'
79 """
80 # This is a pretty subtle hack. When expand user is given a UNC path
81 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
82 # the $ to get (\\server\share\%username%). I think it considered $
83 # alone an empty var. But, we need the $ to remains there (it indicates
84 # a hidden share).
85 if os.name == "nt":
86 s = s.replace("$\\", "IPYTHON_TEMP")
87 s = os.path.expandvars(os.path.expanduser(s))
88 if os.name == "nt":
89 s = s.replace("IPYTHON_TEMP", "$\\")
90 return s