1# Copyright (c) Meta Platforms, Inc. and affiliates.
2#
3# This source code is licensed under the MIT license found in the
4# LICENSE file in the root directory of this source tree.
5
6from pathlib import Path
7from typing import Any, List, Mapping, Optional
8
9import libcst as cst
10from libcst.metadata.base_provider import BatchableMetadataProvider
11
12
13class FilePathProvider(BatchableMetadataProvider[Path]):
14 """
15 Provides the path to the current file on disk as metadata for the root
16 :class:`~libcst.Module` node. Requires a :class:`~libcst.metadata.FullRepoManager`.
17 The returned path will always be resolved to an absolute path using
18 :func:`pathlib.Path.resolve`.
19
20 Example usage:
21
22 .. code:: python
23
24 class CustomVisitor(CSTVisitor):
25 METADATA_DEPENDENCIES = [FilePathProvider]
26
27 path: pathlib.Path
28
29 def visit_Module(self, node: libcst.Module) -> None:
30 self.path = self.get_metadata(FilePathProvider, node)
31
32 .. code::
33
34 >>> mgr = FullRepoManager(".", {"libcst/_types.py"}, {FilePathProvider})
35 >>> wrapper = mgr.get_metadata_wrapper_for_path("libcst/_types.py")
36 >>> fqnames = wrapper.resolve(FilePathProvider)
37 >>> {type(k): v for k, v in wrapper.resolve(FilePathProvider).items()}
38 {<class 'libcst._nodes.module.Module'>: PosixPath('/home/user/libcst/_types.py')}
39
40 """
41
42 @classmethod
43 def gen_cache(
44 cls, root_path: Path, paths: List[str], **kwargs: Any
45 ) -> Mapping[str, Path]:
46 cache = {path: (root_path / path).resolve() for path in paths}
47 return cache
48
49 def __init__(self, cache: Path) -> None:
50 super().__init__(cache)
51 self.path: Path = cache
52
53 def visit_Module(self, node: cst.Module) -> Optional[bool]:
54 self.set_metadata(node, self.path)
55 return False