Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/jupyter_client/utils.py: 28%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

29 statements  

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""" 

6 

7from __future__ import annotations 

8 

9import os 

10from collections.abc import Sequence 

11 

12from jupyter_core.utils import ensure_async, run_sync # noqa: F401 # noqa: F401 

13 

14from .session import utcnow # noqa 

15 

16 

17def _filefind(filename: str, path_dirs: str | Sequence[str] | None = None) -> str: 

18 """Find a file by looking through a sequence of paths. 

19 

20 This iterates through a sequence of paths looking for a file and returns 

21 the full, absolute path of the first occurrence of the file. If no set of 

22 path dirs is given, the filename is tested as is, after running through 

23 :func:`expandvars` and :func:`expanduser`. Thus a simple call:: 

24 

25 filefind('myfile.txt') 

26 

27 will find the file in the current working dir, but:: 

28 

29 filefind('~/myfile.txt') 

30 

31 Will find the file in the users home directory. This function does not 

32 automatically try any paths, such as the cwd or the user's home directory. 

33 

34 Parameters 

35 ---------- 

36 filename : str 

37 The filename to look for. 

38 path_dirs : str, None or sequence of str 

39 The sequence of paths to look for the file in. If None, the filename 

40 need to be absolute or be in the cwd. If a string, the string is 

41 put into a sequence and the searched. If a sequence, walk through 

42 each element and join with ``filename``, calling :func:`expandvars` 

43 and :func:`expanduser` before testing for existence. 

44 

45 Returns 

46 ------- 

47 Raises :exc:`IOError` or returns absolute path to file. 

48 """ 

49 

50 # If paths are quoted, abspath gets confused, strip them... 

51 filename = filename.strip('"').strip("'") 

52 # If the input is an absolute path, just check it exists 

53 if os.path.isabs(filename) and os.path.isfile(filename): 

54 return filename 

55 

56 if path_dirs is None: 

57 path_dirs = ("",) 

58 elif isinstance(path_dirs, str): 

59 path_dirs = (path_dirs,) 

60 

61 for path in path_dirs: 

62 if path == ".": 

63 path = os.getcwd() # noqa 

64 testname = _expand_path(os.path.join(path, filename)) 

65 if os.path.isfile(testname): 

66 return os.path.abspath(testname) 

67 msg = f"File {filename!r} does not exist in any of the search paths: {path_dirs!r}" 

68 raise OSError(msg) 

69 

70 

71def _expand_path(s: str) -> str: 

72 """Expand $VARS and ~names in a string, like a shell 

73 

74 :Examples: 

75 

76 In [2]: os.environ['FOO']='test' 

77 

78 In [3]: expand_path('variable FOO is $FOO') 

79 Out[3]: 'variable FOO is test' 

80 """ 

81 # This is a pretty subtle hack. When expand user is given a UNC path 

82 # on Windows (\\server\share$\%username%), os.path.expandvars, removes 

83 # the $ to get (\\server\share\%username%). I think it considered $ 

84 # alone an empty var. But, we need the $ to remains there (it indicates 

85 # a hidden share). 

86 if os.name == "nt": 

87 s = s.replace("$\\", "IPYTHON_TEMP") 

88 s = os.path.expandvars(os.path.expanduser(s)) 

89 if os.name == "nt": 

90 s = s.replace("IPYTHON_TEMP", "$\\") 

91 return s