nodes.py 6.0 KB

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