nodes.py 6.4 KB

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