nodes.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 import CryptoEngine
  23. class NewNode(object):
  24. def __init__(self, username="", password="", url="", notes="", tags=""):
  25. """Initialise everything to null."""
  26. self._id = 0
  27. self._username = username
  28. self._password = password
  29. self._url = url
  30. self._notes = notes
  31. self._tags = tags
  32. #def __setattr__(self, name, value):
  33. # if name in ['username', 'password', 'url', 'notes']:
  34. # enc = CryptoEngine.get()
  35. # object.__setattr__(self, name, enc.decrypt(value).strip())
  36. # if name in ['_username', '_password', '_url', '_notes']:
  37. # enc = CryptoEngine.get()
  38. # object.__setattr__(self, name, enc.encrypt(value).strip())
  39. # if name == '_tags':
  40. # object.__setattr__(self, name, [t for t in value])
  41. # elif name == '_id':
  42. # object.__setattr__(self, name, value)
  43. #def __getattribute__(self, name):
  44. # if name in ['username', 'password', 'url', 'notes']:
  45. # enc = CryptoEngine.get()
  46. # return enc.decrypt(eval('self._'+name).strip())
  47. # else:
  48. # return object.__getattribute__(self, name)
  49. def dump_edit_to_db(self):
  50. dump = ""
  51. dump += "username:"+self._username+"##"
  52. dump += "password:"+self._password+"##"
  53. dump += "url:"+self._url+"##"
  54. dump += "notes:"+self._notes+"##"
  55. dump += "tags:"
  56. tagsloc = ""
  57. for tag in self._tags:
  58. if isinstance(tag, str):
  59. tagsloc += "tag:"+tag.strip()+"**endtag**"
  60. else:
  61. tagsloc += "tag:"+tag._name+"**endtag**"
  62. dump += tagsloc
  63. dump = [dump]
  64. return dump
  65. def dump_to_db(self):
  66. enc = CryptoEngine.get()
  67. dump = ""
  68. dump += "username:"+enc.encrypt(self._username)+"##"
  69. dump += "password:"+enc.encrypt(self._password)+"##"
  70. dump += "url:"+enc.encrypt(self._url)+"##"
  71. dump += "notes:"+enc.encrypt(self._notes)+"##"
  72. dump += "tags:"
  73. tagsloc = ""
  74. for tag in self._tags:
  75. if isinstance(tag, str):
  76. tagsloc += "tag:"+tag.strip()+"**endtag**"
  77. else:
  78. tagsloc += "tag:"+tag._name+"**endtag**"
  79. dump += tagsloc
  80. dump = [dump]
  81. return dump
  82. @property
  83. def password(self):
  84. """Get the current password."""
  85. enc = CryptoEngine.get()
  86. return enc.decrypt(self._password).strip()
  87. @property
  88. def username(self):
  89. """Get the current username."""
  90. enc = CryptoEngine.get()
  91. return enc.decrypt(self._username).strip()
  92. @username.setter
  93. def username(self, value):
  94. """Set the username."""
  95. enc = CryptoEngine.get()
  96. self._username = enc.encrypt(value).strip()
  97. @password.setter
  98. def password(self, value):
  99. """Set the Notes."""
  100. enc = CryptoEngine.get()
  101. self._password = enc.encrypt(value).strip()
  102. @property
  103. def tags(self):
  104. enc = CryptoEngine.get()
  105. try:
  106. return [enc.decrypt(tag) for tag in filter(None, self._tags)]
  107. except Exception:
  108. return [tag for tag in filter(None, self._tags)]
  109. @tags.setter
  110. def tags(self, value):
  111. self._tags = [tag for tag in value]
  112. @property
  113. def url(self):
  114. """Get the current url."""
  115. enc = CryptoEngine.get()
  116. return enc.decrypt(self._url).strip()
  117. @url.setter
  118. def url(self, value):
  119. """Set the Notes."""
  120. enc = CryptoEngine.get()
  121. self._url = enc.encrypt(value).strip()
  122. @property
  123. def notes(self):
  124. """Get the current notes."""
  125. enc = CryptoEngine.get()
  126. return enc.decrypt(self._notes).strip()
  127. @notes.setter
  128. def notes(self, value):
  129. """Set the Notes."""
  130. enc = CryptoEngine.get()
  131. self._notes = enc.encrypt(value).strip()
  132. class Node(object):
  133. def __init__(self, username="", password="", url="",
  134. notes="", tags=[]):
  135. """Initialise everything to null."""
  136. self._id = 0
  137. enc = CryptoEngine.get()
  138. self._username = enc.encrypt(username)
  139. self._password = enc.encrypt(password)
  140. self._url = enc.encrypt(url)
  141. self._notes = enc.encrypt(notes)
  142. self._tags = []
  143. self.set_tags(tags)
  144. def get_tags(self):
  145. tags = []
  146. enc = CryptoEngine.get()
  147. for i in self._tags:
  148. tags.append(enc.decrypt(i))
  149. return tags
  150. def set_tags(self, tags):
  151. self._tags = []
  152. enc = CryptoEngine.get()
  153. for i in tags:
  154. self._tags.append(enc.encrypt(i))
  155. def get_id(self):
  156. return self._id
  157. def set_id(self, id):
  158. self._id = id
  159. def get_username(self):
  160. """Return the username."""
  161. enc = CryptoEngine.get()
  162. return enc.decrypt(self._username)
  163. def set_username(self, username):
  164. """Set the username."""
  165. enc = CryptoEngine.get()
  166. self._username = enc.encrypt(username)
  167. def get_password(self):
  168. """Return the password."""
  169. enc = CryptoEngine.get()
  170. return enc.decrypt(self._password)
  171. def set_password(self, password):
  172. """Set the password."""
  173. enc = CryptoEngine.get()
  174. self._password = enc.encrypt(password)
  175. def get_url(self):
  176. """Return the URL."""
  177. enc = CryptoEngine.get()
  178. return enc.decrypt(self._url)
  179. def set_url(self, url):
  180. """Set the URL."""
  181. enc = CryptoEngine.get()
  182. self._url = enc.encrypt(url)
  183. def get_notes(self):
  184. """Return the Notes."""
  185. enc = CryptoEngine.get()
  186. return enc.decrypt(self._notes)
  187. def set_notes(self, notes):
  188. """Set the Notes."""
  189. enc = CryptoEngine.get()
  190. self._notes = enc.encrypt(notes)