importer.py 3.1 KB

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