Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/libcst/metadata/file_path_provider.py: 60%
15 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:43 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:43 +0000
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.
6from pathlib import Path
7from typing import Collection, List, Mapping, Optional
9import libcst as cst
10from libcst.metadata.base_provider import BatchableMetadataProvider
13class FilePathProvider(BatchableMetadataProvider[Collection[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`.
20 Example usage:
22 .. code:: python
24 class CustomVisitor(CSTVisitor):
25 METADATA_DEPENDENCIES = [FilePathProvider]
27 path: pathlib.Path
29 def visit_Module(self, node: libcst.Module) -> None:
30 self.path = self.get_metadata(FilePathProvider, node)
32 .. code::
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')}
40 """
42 @classmethod
43 def gen_cache(
44 cls, root_path: Path, paths: List[str], timeout: Optional[int] = None
45 ) -> Mapping[str, Path]:
46 cache = {path: (root_path / path).resolve() for path in paths}
47 return cache
49 def __init__(self, cache: Path) -> None:
50 super().__init__(cache)
51 self.path: Path = cache
53 def visit_Module(self, node: cst.Module) -> Optional[bool]:
54 self.set_metadata(node, self.path)
55 return False