A collection of [File][mkdocs.structure.files.File] objects.
| 60 | |
| 61 | |
| 62 | class Files: |
| 63 | """A collection of [File][mkdocs.structure.files.File] objects.""" |
| 64 | |
| 65 | def __init__(self, files: Iterable[File]) -> None: |
| 66 | self._src_uris = {f.src_uri: f for f in files} |
| 67 | |
| 68 | def __iter__(self) -> Iterator[File]: |
| 69 | """Iterate over the files within.""" |
| 70 | return iter(self._src_uris.values()) |
| 71 | |
| 72 | def __len__(self) -> int: |
| 73 | """The number of files within.""" |
| 74 | return len(self._src_uris) |
| 75 | |
| 76 | def __contains__(self, path: str) -> bool: |
| 77 | """Soft-deprecated, prefer `get_file_from_path(path) is not None`.""" |
| 78 | return PurePath(path).as_posix() in self._src_uris |
| 79 | |
| 80 | @property |
| 81 | def src_paths(self) -> dict[str, File]: |
| 82 | """Soft-deprecated, prefer `src_uris`.""" |
| 83 | return {file.src_path: file for file in self} |
| 84 | |
| 85 | @property |
| 86 | def src_uris(self) -> Mapping[str, File]: |
| 87 | """ |
| 88 | A mapping containing every file, with the keys being their |
| 89 | [`src_uri`][mkdocs.structure.files.File.src_uri]. |
| 90 | """ |
| 91 | return self._src_uris |
| 92 | |
| 93 | def get_file_from_path(self, path: str) -> File | None: |
| 94 | """Return a File instance with File.src_uri equal to path.""" |
| 95 | return self._src_uris.get(PurePath(path).as_posix()) |
| 96 | |
| 97 | def append(self, file: File) -> None: |
| 98 | """Add file to the Files collection.""" |
| 99 | if file.src_uri in self._src_uris: |
| 100 | warnings.warn( |
| 101 | "To replace an existing file, call `remove` before `append`.", DeprecationWarning |
| 102 | ) |
| 103 | del self._src_uris[file.src_uri] |
| 104 | self._src_uris[file.src_uri] = file |
| 105 | |
| 106 | def remove(self, file: File) -> None: |
| 107 | """Remove file from Files collection.""" |
| 108 | try: |
| 109 | del self._src_uris[file.src_uri] |
| 110 | except KeyError: |
| 111 | raise ValueError(f'{file.src_uri!r} not in collection') |
| 112 | |
| 113 | def copy_static_files( |
| 114 | self, |
| 115 | dirty: bool = False, |
| 116 | *, |
| 117 | inclusion: Callable[[InclusionLevel], bool] = InclusionLevel.is_included, |
| 118 | ) -> None: |
| 119 | """Copy static files from source to destination.""" |
no outgoing calls
searching dependent graphs…