user.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. from .apikey import apikey_generator
  2. from .exception import config_exception
  3. from .exception import not_ready_exception
  4. from .exception import validator_exception
  5. from .validator import nick_validator
  6. from .validator import apikey_validator
  7. from .validator import password_validator
  8. class user:
  9. """
  10. This represents user in the system. It would be stored in the config
  11. file. Creating new must be done by builders, which checks content with
  12. validators. User class is immutable.
  13. """
  14. def __init__(self, nick: str, password: str, apikey: str) -> None:
  15. """
  16. This create new user, and require all parameters of it.
  17. Parameters:
  18. nick (str): Nick of the user
  19. password (str): Password of the user
  20. apikey (str): ApiKey of the user
  21. """
  22. self.__nick = nick
  23. self.__password = password
  24. self.__apikey = apikey
  25. self.__result = None
  26. @property
  27. def nick(self) -> str:
  28. """ It returns nick of the user. """
  29. return self.__nick
  30. @property
  31. def password(self) -> str:
  32. """ It returns password of the user. """
  33. return self.__password
  34. @property
  35. def apikey(self) -> str:
  36. """ It returns apikey of the user. """
  37. return self.__apikey
  38. def __str__(self) -> str:
  39. """
  40. This create dump of the user.
  41. Returns:
  42. (str): Dump of the user
  43. """
  44. target = "User:\n"
  45. target = target + "Nick: \"" + str(self.nick) + "\"\n"
  46. target = target + "Password: \"" + str(self.password) + "\"\n"
  47. target = target + "ApiKey: \"" + str(self.apikey) + "\"\n"
  48. return target
  49. class user_factory:
  50. """
  51. This class is responsible for generating new users. It make avairable
  52. creating user step by step, and it is responsible for generating ApiKey.
  53. """
  54. def __init__(self, target: user | None = None) -> None:
  55. """
  56. This create new user factory. When user had been provided, then it is
  57. used to initialize nick, ApiKey and password of the factory.
  58. Parameters
  59. target (user | None): User to initialize factory (default = None)
  60. """
  61. self.__nick = None
  62. self.__password = None
  63. self.__apikey = None
  64. self.__result = None
  65. if target is not None:
  66. self.__nick = target.nick
  67. self.__password = target.password
  68. self.__apikey = target.apikey
  69. @property
  70. def apikey(self) -> str:
  71. """ This return factory ApiKey. """
  72. if self.__apikey is None:
  73. self.__apikey = apikey_generator()
  74. return self.__apikey
  75. @property
  76. def nick(self) -> str | None:
  77. """ This return nick of the factory. """
  78. return self.__nick
  79. @property
  80. def password(self) -> str | None:
  81. """ This return password of the factory. """
  82. return self.__password
  83. @nick.setter
  84. def nick(self, target: str) -> None:
  85. """ This set new nick, and validate it. """
  86. if nick_validator(target).invalid:
  87. raise validator_exception("user.nick")
  88. self.__nick = target
  89. @password.setter
  90. def password(self, target: str) -> None:
  91. """ This set new password, and validate it. """
  92. if password_validator(target).invalid:
  93. raise validator_exception("user.password")
  94. self.__password = target
  95. @property
  96. def ready(self) -> bool:
  97. """ This check that factory is ready or not. """
  98. if self.__nick is None:
  99. return False
  100. if self.__password is None:
  101. return False
  102. return True
  103. @property
  104. def result(self) -> user:
  105. """ This create new user. Factory must be ready, to do it. """
  106. if not self.ready:
  107. raise not_ready_exception(self)
  108. if self.__result is not None:
  109. return self.__result
  110. self.__result = user(
  111. self.nick,
  112. self.password,
  113. self.apikey
  114. )
  115. return self.__result
  116. class user_builder:
  117. """
  118. This create new user builder. User builder is responsble for loading users
  119. from dicts, which would be provided from config file.
  120. """
  121. def __init__(self, target: dict):
  122. """
  123. This create new user builder.
  124. Parameters:
  125. target (dict): Target dict to load user from
  126. """
  127. self.__target = target
  128. self.__result = None
  129. @property
  130. def nick(self) -> str:
  131. """ This return nick from dict. """
  132. if not "nick" in self.__target:
  133. return ""
  134. return self.__target["nick"]
  135. @property
  136. def password(self) -> str:
  137. """ This return password from dict. """
  138. if not "password" in self.__target:
  139. return ""
  140. return self.__target["password"]
  141. @property
  142. def apikey(self) -> str:
  143. """ This return ApiKey from dict. """
  144. if not "apikey" in self.__target:
  145. return ""
  146. return self.__target["apikey"]
  147. def check(self) -> None | str:
  148. """
  149. This validate all properties, nick, password and ApiKey. All must
  150. be valid, if any is not valid or is not provided, then it return
  151. error. When all is valid, return None, when anything is wrong,
  152. then return it as string.
  153. Returns:
  154. (None): When all is valid
  155. (str): Content of the error
  156. """
  157. if not "nick" in self.__target:
  158. return "User not contain nick."
  159. if nick_validator(self.nick).invalid:
  160. return "User nick \"" + self.nick + "\" is invalid."
  161. if not "password" in self.__target:
  162. return self.nick + " not contain password."
  163. if password_validator(self.password).invalid:
  164. return self.nick + " has invalid password."
  165. if not "apikey" in self.__target:
  166. return self.nick + " not contain apikey."
  167. if apikey_validator(self.apikey).invalid:
  168. return self.nick + " has invalid apikey."
  169. return None
  170. @property
  171. def result(self) -> user:
  172. """ Ready user form dict. """
  173. if self.__result is not None:
  174. return self.__result
  175. check = self.check()
  176. if check is not None:
  177. raise config_exception("User config contain error. " + check)
  178. self.__result = user(
  179. self.nick,
  180. self.password,
  181. self.apikey
  182. )
  183. return self.__result
  184. class user_exporter:
  185. """
  186. This export user to dict, which could be safe into config file.
  187. """
  188. def __init__(self, target: user) -> None:
  189. """
  190. This initialize new exporter.
  191. Parameters:
  192. target (user): Target user to export
  193. """
  194. self.__target = target
  195. @property
  196. def target(self) -> user:
  197. """ Target user to export. """
  198. return self.__target
  199. @property
  200. def result(self) -> dict:
  201. """ Exported user as dict. """
  202. return {
  203. "nick": self.target.nick,
  204. "password": self.target.password,
  205. "apikey": self.target.apikey
  206. }