Ejemplo n.º 1
0
        else:
            super().__init__(hex=raw)

    def __repr__(self) -> str:
        return f"<EntryID {self.hex}>"

    @classmethod
    def new(cls: Type[EntryIDTypeVar]) -> EntryIDTypeVar:
        return cls(uuid4())


EntryIDField = fields.uuid_based_field_factory(EntryID)


class EntryName(str):
    __slots__ = ()

    def __new__(cls, raw: str) -> "EntryName":
        raw = normalize("NFC", raw)
        # Stick to UNIX filesystem philosophy:
        # - no `.` or `..` name
        # - no `/` or null byte in the name
        # - max 255 bytes long name
        if (not 0 < len(raw.encode("utf8")) < 256 or raw == "." or raw == ".."
                or "/" in raw or "\x00" in raw):
            raise ValueError("Invalid entry name")
        return super(EntryName, cls).__new__(cls, raw)


EntryNameField = fields.str_based_field_factory(EntryName)
Ejemplo n.º 2
0
        return f"<DeviceID {super().__repr__()}>"

    @property
    def user_id(self) -> UserID:
        return UserID(self.split("@")[0])

    @property
    def device_name(self) -> DeviceName:
        return DeviceName(self.split("@")[1])

    @classmethod
    def new(cls) -> "DeviceID":
        return cls(f"{uuid4().hex}@{uuid4().hex}")


OrganizationIDField = fields.str_based_field_factory(OrganizationID)
UserIDField = fields.str_based_field_factory(UserID)
DeviceNameField = fields.str_based_field_factory(DeviceName)
DeviceIDField = fields.str_based_field_factory(DeviceID)


class HumanHandle(namedtuple("HumanHandle", "email label")):
    __slots__ = ()

    def __init__(self, email: str, label: str):
        # TODO: how to check the email  easily ?
        if not isinstance(email, str) or not 0 < _bytes_size(email) < 255:
            raise ValueError("Invalid email address")

        if not isinstance(label, str) or not 0 < _bytes_size(label) < 255:
            raise ValueError("Invalid label")