nodes.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. return [enc.decrypt(tag).strip() for tag in filter(None,self._tags)]
  106. @tags.setter
  107. def tags(self, value):
  108. self._tags = [tag for tag in value]
  109. @property
  110. def url(self):
  111. """Get the current url."""
  112. enc = CryptoEngine.get()
  113. return enc.decrypt(self._url).strip()
  114. @url.setter
  115. def url(self, value):
  116. """Set the Notes."""
  117. enc = CryptoEngine.get()
  118. self._url = enc.encrypt(value).strip()
  119. @property
  120. def notes(self):
  121. """Get the current notes."""
  122. enc = CryptoEngine.get()
  123. return enc.decrypt(self._notes).strip()
  124. @notes.setter
  125. def notes(self, value):
  126. """Set the Notes."""
  127. enc = CryptoEngine.get()
  128. self._notes = enc.encrypt(value).strip()
  129. class Node(object):
  130. def __init__(self, username="", password="", url="",
  131. notes="", tags=[]):
  132. """Initialise everything to null."""
  133. self._id = 0
  134. enc = CryptoEngine.get()
  135. self._username = enc.encrypt(username)
  136. self._password = enc.encrypt(password)
  137. self._url = enc.encrypt(url)
  138. self._notes = enc.encrypt(notes)
  139. self._tags = []
  140. self.set_tags(tags)
  141. def get_tags(self):
  142. tags = []
  143. enc = CryptoEngine.get()
  144. for i in self._tags:
  145. tags.append(enc.decrypt(i))
  146. return tags
  147. def set_tags(self, tags):
  148. self._tags = []
  149. enc = CryptoEngine.get()
  150. for i in tags:
  151. self._tags.append(enc.encrypt(i))
  152. def get_id(self):
  153. return self._id
  154. def set_id(self, id):
  155. self._id = id
  156. def get_username(self):
  157. """Return the username."""
  158. enc = CryptoEngine.get()
  159. return enc.decrypt(self._username)
  160. def set_username(self, username):
  161. """Set the username."""
  162. enc = CryptoEngine.get()
  163. self._username = enc.encrypt(username)
  164. def get_password(self):
  165. """Return the password."""
  166. enc = CryptoEngine.get()
  167. return enc.decrypt(self._password)
  168. def set_password(self, password):
  169. """Set the password."""
  170. enc = CryptoEngine.get()
  171. self._password = enc.encrypt(password)
  172. def get_url(self):
  173. """Return the URL."""
  174. enc = CryptoEngine.get()
  175. return enc.decrypt(self._url)
  176. def set_url(self, url):
  177. """Set the URL."""
  178. enc = CryptoEngine.get()
  179. self._url = enc.encrypt(url)
  180. def get_notes(self):
  181. """Return the Notes."""
  182. enc = CryptoEngine.get()
  183. return enc.decrypt(self._notes)
  184. def set_notes(self, notes):
  185. """Set the Notes."""
  186. enc = CryptoEngine.get()
  187. self._notes = enc.encrypt(notes)