naf.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import time
  2. from .utils import typed_dataclass, field, gen_uuid
  3. @typed_dataclass
  4. class NAF:
  5. network_id: str = field(default_factory=gen_uuid)
  6. owner_id: str | None = None
  7. creator_id: str | None = None
  8. last_owner_time: float = field(default_factory=time.time)
  9. template: str | None = None
  10. persistent: bool = False
  11. parent: str | None = None
  12. components: list = field(default_factory=list)
  13. is_first_sync: bool = True
  14. def to_obj(self):
  15. self.last_owner_time = time.time()
  16. data = {
  17. "networkId": self.network_id,
  18. "owner": self.owner_id or self.creator_id,
  19. "creator": self.creator_id or self.owner_id,
  20. "lastOwnerTime": self.last_owner_time,
  21. "template": self.template,
  22. "persistent": self.persistent,
  23. "parent": self.parent,
  24. "components": self.components,
  25. "isFirstSync": self.is_first_sync,
  26. }
  27. self.is_first_sync = False
  28. return data
  29. @classmethod
  30. def from_obj(cls, data):
  31. obj = cls(
  32. network_id=data["networkId"],
  33. owner_id=data["owner"],
  34. creator_id=data["creator"],
  35. parent=data["parent"],
  36. template=data["template"],
  37. persistent=data["persistent"],
  38. )
  39. obj.last_owner_time = data["lastOwnerTime"]
  40. obj.components = data["components"]
  41. obj.is_first_sync = data["isFirstSync"]
  42. return obj