mdrs-client-python/mdrsclient/models/folder.py

78 lines
2.1 KiB
Python

from typing import Final, NamedTuple
from pydantic.dataclasses import dataclass
from mdrsclient.models.file import File
from mdrsclient.models.utils import iso8601_to_user_friendly
class FolderAccessLevelItem(NamedTuple):
id: int
key: str
label: str
class FolderAccessLevel:
ACCESS_LEVELS: Final[list[FolderAccessLevelItem]] = [
FolderAccessLevelItem(-1, "storage", "Storage"),
FolderAccessLevelItem(0, "private", "Private"),
FolderAccessLevelItem(1, "cbs_open", "CBS Open"),
FolderAccessLevelItem(2, "pw_open", "PW Open"),
FolderAccessLevelItem(3, "public", "Public"),
]
@staticmethod
def key2id(key: str) -> int | None:
acl = next((x for x in FolderAccessLevel.ACCESS_LEVELS if x.key == key), None)
return acl.id if acl is not None else None
@staticmethod
def id2label(id: int) -> str | None:
acl = next((x for x in FolderAccessLevel.ACCESS_LEVELS if x.id == id), None)
return acl.label if acl is not None else None
@dataclass(frozen=True)
class FolderSimple:
id: str
pid: str | None
name: str
access_level: int
lock: bool
lab_id: int
description: str
created_at: str
updated_at: str
restrict_opened_at: str | None
@property
def access_level_name(self) -> str:
label = FolderAccessLevel.id2label(self.access_level)
return label if label is not None else ""
@property
def lock_name(self) -> str:
return "locked" if self.lock else "unlocked"
@property
def created_at_name(self) -> str:
return iso8601_to_user_friendly(self.created_at)
@property
def updated_at_name(self) -> str:
return iso8601_to_user_friendly(self.updated_at)
@dataclass(frozen=True)
class Folder(FolderSimple):
metadata: list[dict]
sub_folders: list[FolderSimple]
files: list[File]
path: str
def find_sub_folder(self, name: str) -> FolderSimple | None:
return next((x for x in self.sub_folders if x.name == name), None)
def find_file(self, name: str) -> File | None:
return next((x for x in self.files if x.name == name), None)