Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/jupyter_client/utils.py: 16%
25 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
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"""
6import os
8from jupyter_core.utils import ensure_async, run_sync # noqa: F401 # noqa: F401
11def _filefind(filename, path_dirs=None):
12 """Find a file by looking through a sequence of paths.
14 This iterates through a sequence of paths looking for a file and returns
15 the full, absolute path of the first occurence of the file. If no set of
16 path dirs is given, the filename is tested as is, after running through
17 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
19 filefind('myfile.txt')
21 will find the file in the current working dir, but::
23 filefind('~/myfile.txt')
25 Will find the file in the users home directory. This function does not
26 automatically try any paths, such as the cwd or the user's home directory.
28 Parameters
29 ----------
30 filename : str
31 The filename to look for.
32 path_dirs : str, None or sequence of str
33 The sequence of paths to look for the file in. If None, the filename
34 need to be absolute or be in the cwd. If a string, the string is
35 put into a sequence and the searched. If a sequence, walk through
36 each element and join with ``filename``, calling :func:`expandvars`
37 and :func:`expanduser` before testing for existence.
39 Returns
40 -------
41 Raises :exc:`IOError` or returns absolute path to file.
42 """
44 # If paths are quoted, abspath gets confused, strip them...
45 filename = filename.strip('"').strip("'")
46 # If the input is an absolute path, just check it exists
47 if os.path.isabs(filename) and os.path.isfile(filename):
48 return filename
50 if path_dirs is None:
51 path_dirs = ("",)
52 elif isinstance(path_dirs, str):
53 path_dirs = (path_dirs,)
55 for path in path_dirs:
56 if path == ".":
57 path = os.getcwd() # noqa
58 testname = _expand_path(os.path.join(path, filename))
59 if os.path.isfile(testname):
60 return os.path.abspath(testname)
61 msg = f"File {filename!r} does not exist in any of the search paths: {path_dirs!r}"
62 raise OSError(msg)
65def _expand_path(s):
66 """Expand $VARS and ~names in a string, like a shell
68 :Examples:
70 In [2]: os.environ['FOO']='test'
72 In [3]: expand_path('variable FOO is $FOO')
73 Out[3]: 'variable FOO is test'
74 """
75 # This is a pretty subtle hack. When expand user is given a UNC path
76 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
77 # the $ to get (\\server\share\%username%). I think it considered $
78 # alone an empty var. But, we need the $ to remains there (it indicates
79 # a hidden share).
80 if os.name == "nt":
81 s = s.replace("$\\", "IPYTHON_TEMP")
82 s = os.path.expandvars(os.path.expanduser(s))
83 if os.name == "nt":
84 s = s.replace("IPYTHON_TEMP", "$\\")
85 return s