importer.py 3.1 KB

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