nodes.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. #============================================================================
  17. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  18. #============================================================================
  19. #============================================================================
  20. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  21. #============================================================================
  22. from pwman.util.crypto_engine import CryptoEngine
  23. class NewNode(object):
  24. def __str__(self): # pragma: no cover
  25. enc = CryptoEngine.get()
  26. try:
  27. tags = ', '.join([enc.decrypt(tag).strip() for tag in filter(None,
  28. self._tags)])
  29. except Exception:
  30. tags = ', '.join([tag.strip() for tag in filter(None, self._tags)])
  31. user = enc.decrypt(self._username).strip()
  32. url = enc.decrypt(self._url).strip()
  33. return '{0}@{1}\t{2}'.format(user, url,
  34. tags)
  35. def dump_edit_to_db(self):
  36. dump = u""
  37. try:
  38. dump += u"username:"+self._username.decode()+u"##"
  39. except AttributeError:
  40. dump += u"username:"+self._username+u"##"
  41. try:
  42. dump += u"password:"+self._password.decode()+u"##"
  43. except AttributeError:
  44. dump += u"password:"+self._password+u"##"
  45. try:
  46. dump += u"url:"+self._url.decode()+u"##"
  47. except AttributeError:
  48. dump += u"url:"+self._url+u"##"
  49. try:
  50. dump += u"notes:"+self._notes.decode()+u"##"
  51. except AttributeError:
  52. dump += u"notes:"+self._notes+u"##"
  53. dump += u"tags:"
  54. tagsloc = u""
  55. for tag in self._tags:
  56. if isinstance(tag, str):
  57. tagsloc += u"tag:"+tag.strip()+u"**endtag**"
  58. else:
  59. tagsloc += u'tag:'+tag._name.decode()+u'**endtag**'
  60. dump += tagsloc
  61. dump = [dump]
  62. return dump
  63. @property
  64. def password(self):
  65. """Get the current password."""
  66. enc = CryptoEngine.get()
  67. return enc.decrypt(self._password).strip()
  68. @property
  69. def username(self):
  70. """Get the current username."""
  71. enc = CryptoEngine.get()
  72. return enc.decrypt(self._username).strip()
  73. @username.setter
  74. def username(self, value):
  75. """Set the username."""
  76. enc = CryptoEngine.get()
  77. self._username = enc.encrypt(value).strip()
  78. @password.setter
  79. def password(self, value):
  80. """Set the Notes."""
  81. enc = CryptoEngine.get()
  82. self._password = enc.encrypt(value).strip()
  83. @property
  84. def tags(self):
  85. enc = CryptoEngine.get()
  86. try:
  87. return [enc.decrypt(tag) for tag in filter(None, self._tags)]
  88. except Exception:
  89. return [tag for tag in filter(None, self._tags)]
  90. @tags.setter
  91. def tags(self, value):
  92. self._tags = [tag for tag in value]
  93. @property
  94. def url(self):
  95. """Get the current url."""
  96. enc = CryptoEngine.get()
  97. return enc.decrypt(self._url).strip()
  98. @url.setter
  99. def url(self, value):
  100. """Set the Notes."""
  101. enc = CryptoEngine.get()
  102. self._url = enc.encrypt(value).strip()
  103. @property
  104. def notes(self):
  105. """Get the current notes."""
  106. enc = CryptoEngine.get()
  107. return enc.decrypt(self._notes).strip()
  108. @notes.setter
  109. def notes(self, value):
  110. """Set the Notes."""
  111. enc = CryptoEngine.get()
  112. self._notes = enc.encrypt(value).strip()
  113. class Node(object):
  114. def __init__(self, clear_text=True, **kwargs):
  115. if clear_text:
  116. enc = CryptoEngine.get()
  117. self._username = enc.encrypt(kwargs.get('username')).strip()
  118. self._password = enc.encrypt(kwargs.get('password')).strip()
  119. self._url = enc.encrypt(kwargs.get('url')).strip()
  120. self._notes = enc.encrypt(kwargs.get('notes')).strip()
  121. self._tags = [enc.encrypt(t).strip() for t in kwargs.get('tags')]
  122. @classmethod
  123. def from_encrypted_entries(cls, username, password, url, notes, tags):
  124. """
  125. We use this alternatively, to create a node instance when reading
  126. the encrypted entities from the database
  127. """
  128. node = Node(clear_text=False)
  129. node._username = username.strip()
  130. node._password = password.strip()
  131. node._url = url.strip()
  132. node._notes = notes.strip()
  133. node._tags = [t.strip() for t in tags]
  134. return node
  135. def __repr__(self):
  136. """we use this method to write node to the database"""
  137. res = u''
  138. for item in self.__dir__:
  139. res += self.__dir__[item]
  140. return res
  141. def __iter__(self):
  142. for item in ['_username', '_password',
  143. '_url', '_notes']:
  144. yield getattr(self, item)
  145. yield self._tags
  146. @property
  147. def password(self):
  148. """Get the current password."""
  149. enc = CryptoEngine.get()
  150. return enc.decrypt(self._password).strip()
  151. @property
  152. def username(self):
  153. """Get the current username."""
  154. enc = CryptoEngine.get()
  155. return enc.decrypt(self._username).strip()
  156. @username.setter
  157. def username(self, value):
  158. """Set the username."""
  159. enc = CryptoEngine.get()
  160. self._username = enc.encrypt(value).strip()
  161. @password.setter
  162. def password(self, value):
  163. """Set the Notes."""
  164. enc = CryptoEngine.get()
  165. self._password = enc.encrypt(value).strip()
  166. @property
  167. def tags(self):
  168. enc = CryptoEngine.get()
  169. try:
  170. return [enc.decrypt(tag) for tag in filter(None, self._tags)]
  171. except Exception:
  172. return [tag for tag in filter(None, self._tags)]
  173. @tags.setter
  174. def tags(self, value):
  175. self._tags = [tag for tag in value]
  176. @property
  177. def url(self):
  178. """Get the current url."""
  179. enc = CryptoEngine.get()
  180. return enc.decrypt(self._url).strip()
  181. @url.setter
  182. def url(self, value):
  183. """Set the Notes."""
  184. enc = CryptoEngine.get()
  185. self._url = enc.encrypt(value).strip()
  186. @property
  187. def notes(self):
  188. """Get the current notes."""
  189. enc = CryptoEngine.get()
  190. return enc.decrypt(self._notes).strip()
  191. @notes.setter
  192. def notes(self, value):
  193. """Set the Notes."""
  194. enc = CryptoEngine.get()
  195. self._notes = enc.encrypt(value).strip()