importer.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. try:
  23. import xml.etree.cElementTree as ET
  24. except ImportError:
  25. import cElementTree as ET
  26. from pwman.data.nodes import Node
  27. from pwman.data.tags import Tag
  28. class Importer:
  29. def types(self):
  30. return ["pwman3", "pwman1"]
  31. types = classmethod(types)
  32. def get(self, type):
  33. if type == "pwman3":
  34. return Pwman3Importer()
  35. if type == "pwman1":
  36. return Pwman1Importer()
  37. get = classmethod(get)
  38. def import_data(self, db, file):
  39. pass
  40. class Pwman1Importer(Importer):
  41. def __init__(self):
  42. self.tagstack = []
  43. self.nodes = []
  44. def parse_list(self, db, list):
  45. lists = list.findall("PwList")
  46. for l in lists:
  47. name = l.get("name").lower().replace(' ', '')
  48. self.tagstack.append(name)
  49. self.parse_list(db, l)
  50. self.tagstack.pop()
  51. items = list.findall("PwItem")
  52. tags = []
  53. for t in self.tagstack:
  54. tags.append(Tag(t))
  55. for i in items:
  56. username = i.findtext("user")
  57. password = i.findtext("passwd")
  58. url = i.findtext("host")
  59. notes = "%s | %s" % (i.findtext("name"), i.findtext("launch"))
  60. n = Node(username, password, url, notes)
  61. n.set_tags(tags)
  62. self.nodes.append(n)
  63. def import_data(self, db, file):
  64. tree = ET.parse(file)
  65. root = tree.getroot()
  66. list = root.find("PwList")
  67. self.parse_list(db, list)
  68. db.addnodes(self.nodes)
  69. class Pwman3Importer(Importer):
  70. def import_data(self, db, file):
  71. tree = ET.parse(file)
  72. root = tree.getroot()
  73. nodes = root.findall("Node")
  74. nodesarray = []
  75. for n in nodes:
  76. username = n.findtext("username")
  77. password = n.findtext("password")
  78. url = n.findtext("url")
  79. notes = n.findtext("notes")
  80. node = Node(username, password, url, notes)
  81. tagnames = n.find("tags").findall("name")
  82. tags = []
  83. for t in tagnames:
  84. tags.append(Tag(t.text))
  85. node.set_tags(tags)
  86. nodesarray.append(node)
  87. db.addnodes(nodesarray)