nodes.py 7.4 KB

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