Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/traitlets/utils/__init__.py: 22%

32 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1from __future__ import annotations 

2 

3import os 

4import pathlib 

5from typing import Sequence 

6 

7 

8# vestigal things from IPython_genutils. 

9def cast_unicode(s: str | bytes, encoding: str = "utf-8") -> str: 

10 if isinstance(s, bytes): 

11 return s.decode(encoding, "replace") 

12 return s 

13 

14 

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

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

17 

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

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

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

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

22 

23 filefind('myfile.txt') 

24 

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

26 

27 filefind('~/myfile.txt') 

28 

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

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

31 

32 Parameters 

33 ---------- 

34 filename : str 

35 The filename to look for. 

36 path_dirs : str, None or sequence of str 

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

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

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

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

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

42 

43 Returns 

44 ------- 

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

46 """ 

47 

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

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

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

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

52 return filename 

53 

54 if path_dirs is None: 

55 path_dirs = ("",) 

56 elif isinstance(path_dirs, str): 

57 path_dirs = (path_dirs,) 

58 elif isinstance(path_dirs, pathlib.Path): 

59 path_dirs = (str(path_dirs),) 

60 

61 for path in path_dirs: 

62 if path == ".": 

63 path = os.getcwd() 

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

65 if os.path.isfile(testname): 

66 return os.path.abspath(testname) 

67 

68 raise OSError(f"File {filename!r} does not exist in any of the search paths: {path_dirs!r}") 

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