Skip to content

Snapshot Module

datajoint_file_validator.snapshot.Snapshot = List[Dict[str, Any]] module-attribute

datajoint_file_validator.snapshot.S3URI = str module-attribute

datajoint_file_validator.snapshot.PathLike = Union[str, Path, S3URI] module-attribute

datajoint_file_validator.snapshot.create_snapshot(path)

Generate a snapshot of a file or directory at local path. Converts the list of dataclasses to a Snapshot.

Source code in datajoint_file_validator/snapshot.py
 95
 96
 97
 98
 99
100
101
def create_snapshot(path: str) -> Snapshot:
    """
    Generate a snapshot of a file or directory at local `path`.
    Converts the list of dataclasses to a Snapshot.
    """
    files = _snapshot_to_cls(path)
    return [f.asdict() for f in files]

datajoint_file_validator.snapshot.FileMetadata dataclass

Metadata for a file.

Source code in datajoint_file_validator/snapshot.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@dataclass
class FileMetadata:
    """
    Metadata for a file.
    """

    name: str
    path: str = field(init=False)
    abs_path: str
    rel_path: str
    extension: str
    size: int
    type: str
    last_modified: str
    mtime_ns: int
    ctime_ns: int
    atime_ns: int
    _path: Optional[Path] = field(default=None, repr=False)

    def __post_init__(self):
        self.path = self.rel_path

    @staticmethod
    def to_iso_8601(time_ns: int):
        time_ = datetime.fromtimestamp(time_ns / 1e9)
        return time_.replace(tzinfo=pytz.UTC).isoformat()

    @classmethod
    def from_path(cls, path: Path, root: Path) -> "FileMetadata":
        """Return a FileMetadata object from a Path object."""
        is_file = path.is_file()
        rel_path = str(path.relative_to(root))
        abs_path = str(path)
        # Add trailing slash to directories
        if not is_file and not rel_path.endswith("/"):
            rel_path += "/"
            abs_path += "/"

        return cls(
            name=path.name,
            rel_path=rel_path,
            abs_path=abs_path,
            size=path.stat().st_size,
            type="file" if is_file else "directory",
            last_modified=cls.to_iso_8601(path.stat().st_mtime_ns),
            extension=path.suffix,
            mtime_ns=path.stat().st_mtime_ns,
            ctime_ns=path.stat().st_ctime_ns,
            atime_ns=path.stat().st_atime_ns,
            _path=path if config.enable_path_handle else None,
        )

    def __repr__(self):
        return f"{self.__class__.__name__}(path={self.path!r}, type={self.type!r})"

    @staticmethod
    def _dict_factory(x):
        exclude_fields = ("_path",)
        return {k: v for (k, v) in x if ((v is not None) and (k not in exclude_fields))}

    def asdict(self):
        return asdict(self, dict_factory=self._dict_factory)

from_path(path, root) classmethod

Return a FileMetadata object from a Path object.

Source code in datajoint_file_validator/snapshot.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@classmethod
def from_path(cls, path: Path, root: Path) -> "FileMetadata":
    """Return a FileMetadata object from a Path object."""
    is_file = path.is_file()
    rel_path = str(path.relative_to(root))
    abs_path = str(path)
    # Add trailing slash to directories
    if not is_file and not rel_path.endswith("/"):
        rel_path += "/"
        abs_path += "/"

    return cls(
        name=path.name,
        rel_path=rel_path,
        abs_path=abs_path,
        size=path.stat().st_size,
        type="file" if is_file else "directory",
        last_modified=cls.to_iso_8601(path.stat().st_mtime_ns),
        extension=path.suffix,
        mtime_ns=path.stat().st_mtime_ns,
        ctime_ns=path.stat().st_ctime_ns,
        atime_ns=path.stat().st_atime_ns,
        _path=path if config.enable_path_handle else None,
    )

to_iso_8601(time_ns) staticmethod

Source code in datajoint_file_validator/snapshot.py
33
34
35
36
@staticmethod
def to_iso_8601(time_ns: int):
    time_ = datetime.fromtimestamp(time_ns / 1e9)
    return time_.replace(tzinfo=pytz.UTC).isoformat()

asdict()

Source code in datajoint_file_validator/snapshot.py
71
72
def asdict(self):
    return asdict(self, dict_factory=self._dict_factory)