-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding=utf-8 -*- | ||
r""" | ||
""" | ||
import typing as t | ||
from os import PathLike | ||
from pathlib import Path | ||
|
||
|
||
__all__ = ['FileIndex'] | ||
|
||
|
||
class FileIndex: | ||
fp: Path | ||
root: Path = None | ||
_index: t.List[Path] | ||
|
||
def __init__(self, fp: PathLike): | ||
self.fp = Path(fp) | ||
self.root = self.fp.parent | ||
self._index = [] | ||
|
||
def load(self) -> None: | ||
r""" loads the index from disk """ | ||
with open(self.fp, "r") as f: | ||
self._index.clear() | ||
for line in f: | ||
line = line.strip() | ||
if not line or line.startswith("#"): continue | ||
self._index.append(self.root / line) | ||
|
||
def save(self) -> None: | ||
r""" saves the index to disk """ | ||
with open(self.fp, "w") as f: | ||
for fp in self._index: | ||
f.write(f"{fp.relative_to(self.root)!s}") | ||
|
||
def add_file(self, file: PathLike): | ||
r""" add a file to the index (memory only) """ | ||
self._index.append(self.root / file) | ||
|
||
def unlink(self, missing_ok: bool=False) -> None: | ||
r""" unlinks the file-index itself """ | ||
self.fp.unlink(missing_ok=missing_ok) | ||
|
||
def unlink_indexed(self) -> None: | ||
r""" unlinks all indexed files """ | ||
for fp in self._index: | ||
fp.unlink(missing_ok=True) |