utils.py 3.1 KB

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