nodes.py 5.7 KB

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