nodes.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. enc = CryptoEngine.get()
  56. dump = ""
  57. dump += "username:"+self._username+"##"
  58. dump += "password:"+self._password+"##"
  59. dump += "url:"+self._url+"##"
  60. dump += "notes:"+self._notes+"##"
  61. dump += "tags:"
  62. tagsloc = ""
  63. for tag in self._tags:
  64. if isinstance(tag, str):
  65. tagsloc += "tag:"+tag.strip()+"**endtag**"
  66. else:
  67. tagsloc += "tag:"+tag.get_name()+"**endtag**"
  68. dump += tagsloc
  69. dump = [dump]
  70. return dump
  71. def dump_to_db(self):
  72. enc = CryptoEngine.get()
  73. dump = ""
  74. dump += "username:"+enc.encrypt(self._username)+"##"
  75. dump += "password:"+enc.encrypt(self._password)+"##"
  76. dump += "url:"+enc.encrypt(self._url)+"##"
  77. dump += "notes:"+enc.encrypt(self._notes)+"##"
  78. dump += "tags:"
  79. tagsloc = ""
  80. for tag in self._tags:
  81. if isinstance(tag, str):
  82. tagsloc += "tag:"+tag.strip()+"**endtag**"
  83. else:
  84. tagsloc += "tag:"+tag.get_name()+"**endtag**"
  85. dump += tagsloc
  86. dump = [dump]
  87. return dump
  88. def set_tags(self, tags):
  89. """
  90. this method expects a list of tag instances.
  91. hence feed it with them.
  92. fixed! the method parse_node_string in
  93. SQLiteDatabase returns a dictionary,
  94. but a also, Tags instances..."""
  95. self._tags = []
  96. if tags:
  97. self._tags = [t for t in tags]
  98. def set_id(self, id):
  99. self._id = id
  100. def set_password(self, password):
  101. """Set the password."""
  102. enc = CryptoEngine.get()
  103. self._password = enc.encrypt(password).strip()
  104. def set_url(self, url):
  105. """Set the URL."""
  106. enc = CryptoEngine.get()
  107. self._url = enc.encrypt(url)
  108. def set_notes(self, notes):
  109. """Set the Notes."""
  110. enc = CryptoEngine.get()
  111. self._notes = enc.encrypt(notes)
  112. class Node(object):
  113. def __init__(self, username="", password="", url="",
  114. notes="", tags=[]):
  115. """Initialise everything to null."""
  116. self._id = 0
  117. enc = CryptoEngine.get()
  118. self._username = enc.encrypt(username)
  119. self._password = enc.encrypt(password)
  120. self._url = enc.encrypt(url)
  121. self._notes = enc.encrypt(notes)
  122. self._tags = []
  123. self.set_tags(tags)
  124. def get_tags(self):
  125. tags = []
  126. enc = CryptoEngine.get()
  127. for i in self._tags:
  128. tags.append(enc.decrypt(i))
  129. return tags
  130. def set_tags(self, tags):
  131. self._tags = []
  132. enc = CryptoEngine.get()
  133. for i in tags:
  134. self._tags.append(enc.encrypt(i))
  135. def get_id(self):
  136. return self._id
  137. def set_id(self, id):
  138. self._id = id
  139. def get_username(self):
  140. """Return the username."""
  141. enc = CryptoEngine.get()
  142. return enc.decrypt(self._username)
  143. def set_username(self, username):
  144. """Set the username."""
  145. enc = CryptoEngine.get()
  146. self._username = enc.encrypt(username)
  147. def get_password(self):
  148. """Return the password."""
  149. enc = CryptoEngine.get()
  150. return enc.decrypt(self._password)
  151. def set_password(self, password):
  152. """Set the password."""
  153. enc = CryptoEngine.get()
  154. self._password = enc.encrypt(password)
  155. def get_url(self):
  156. """Return the URL."""
  157. enc = CryptoEngine.get()
  158. return enc.decrypt(self._url)
  159. def set_url(self, url):
  160. """Set the URL."""
  161. enc = CryptoEngine.get()
  162. self._url = enc.encrypt(url)
  163. def get_notes(self):
  164. """Return the Notes."""
  165. enc = CryptoEngine.get()
  166. return enc.decrypt(self._notes)
  167. def set_notes(self, notes):
  168. """Set the Notes."""
  169. enc = CryptoEngine.get()
  170. self._notes = enc.encrypt(notes)