nodes.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if isinstance(tag, bytes):
  59. try:
  60. tagsloc += u"tag:"+tag.decode()+u"**endtag**"
  61. except:
  62. tagsloc += u"tag:"+tag+u"**endtag**"
  63. dump += tagsloc
  64. dump = [dump]
  65. return dump
  66. @property
  67. def password(self):
  68. """Get the current password."""
  69. enc = CryptoEngine.get()
  70. return enc.decrypt(self._password).strip()
  71. @property
  72. def username(self):
  73. """Get the current username."""
  74. enc = CryptoEngine.get()
  75. return enc.decrypt(self._username).strip()
  76. @username.setter
  77. def username(self, value):
  78. """Set the username."""
  79. enc = CryptoEngine.get()
  80. self._username = enc.encrypt(value).strip()
  81. @password.setter
  82. def password(self, value):
  83. """Set the Notes."""
  84. enc = CryptoEngine.get()
  85. self._password = enc.encrypt(value).strip()
  86. @property
  87. def tags(self):
  88. enc = CryptoEngine.get()
  89. try:
  90. return [enc.decrypt(tag) for tag in filter(None, self._tags)]
  91. except Exception:
  92. return [tag for tag in filter(None, self._tags)]
  93. @tags.setter
  94. def tags(self, value):
  95. self._tags = [tag for tag in value]
  96. @property
  97. def url(self):
  98. """Get the current url."""
  99. enc = CryptoEngine.get()
  100. return enc.decrypt(self._url).strip()
  101. @url.setter
  102. def url(self, value):
  103. """Set the Notes."""
  104. enc = CryptoEngine.get()
  105. self._url = enc.encrypt(value).strip()
  106. @property
  107. def notes(self):
  108. """Get the current notes."""
  109. enc = CryptoEngine.get()
  110. return enc.decrypt(self._notes).strip()
  111. @notes.setter
  112. def notes(self, value):
  113. """Set the Notes."""
  114. enc = CryptoEngine.get()
  115. self._notes = enc.encrypt(value).strip()
  116. class Node(object): # pragma: no cover
  117. def __init__(self, username="", password="", url="",
  118. notes="", tags=[]):
  119. """Initialise everything to null."""
  120. self._id = 0
  121. enc = CryptoEngine.get()
  122. self._username = enc.encrypt(username)
  123. self._password = enc.encrypt(password)
  124. self._url = enc.encrypt(url)
  125. self._notes = enc.encrypt(notes)
  126. self._tags = []
  127. self.set_tags(tags)
  128. def get_tags(self):
  129. tags = []
  130. enc = CryptoEngine.get()
  131. for i in self._tags:
  132. tags.append(enc.decrypt(i))
  133. return tags
  134. def set_tags(self, tags):
  135. self._tags = []
  136. enc = CryptoEngine.get()
  137. for i in tags:
  138. self._tags.append(enc.encrypt(i))
  139. def get_id(self):
  140. return self._id
  141. def set_id(self, id):
  142. self._id = id
  143. def get_username(self):
  144. """Return the username."""
  145. enc = CryptoEngine.get()
  146. return enc.decrypt(self._username)
  147. def set_username(self, username):
  148. """Set the username."""
  149. enc = CryptoEngine.get()
  150. self._username = enc.encrypt(username)
  151. def get_password(self):
  152. """Return the password."""
  153. enc = CryptoEngine.get()
  154. return enc.decrypt(self._password)
  155. def set_password(self, password):
  156. """Set the password."""
  157. enc = CryptoEngine.get()
  158. self._password = enc.encrypt(password)
  159. def get_url(self):
  160. """Return the URL."""
  161. enc = CryptoEngine.get()
  162. return enc.decrypt(self._url)
  163. def set_url(self, url):
  164. """Set the URL."""
  165. enc = CryptoEngine.get()
  166. self._url = enc.encrypt(url)
  167. def get_notes(self):
  168. """Return the Notes."""
  169. enc = CryptoEngine.get()
  170. return enc.decrypt(self._notes)
  171. def set_notes(self, notes):
  172. """Set the Notes."""
  173. enc = CryptoEngine.get()
  174. self._notes = enc.encrypt(notes)