user.py 7.5 KB

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