nodes.py 6.9 KB

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