exporter.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. from xml.etree.cElementTree import Element, SubElement, dump, ElementTree
  21. except ImportError:
  22. from cElementTree import Element, SubElement, dump, ElementTree
  23. from pwman.data.nodes import Node
  24. from pwman.data.tags import Tag
  25. class Exporter:
  26. def types(self):
  27. return ["pwman3"]
  28. types = classmethod(types)
  29. def get(self, type):
  30. if type == "pwman3":
  31. return Pwman3Exporter()
  32. get = classmethod(get)
  33. def export_data(self, db, file, nodeids):
  34. pass
  35. class Pwman3Exporter(Exporter):
  36. def export_data(self, db, file, nodeids):
  37. nodeids = db.listnodes()
  38. nodes = db.getnodes(nodeids)
  39. root = Element("PwmanXmlList", version="3.0")
  40. for n in nodes:
  41. sub = SubElement(root, "Node")
  42. SubElement(sub, "username").text = n.get_username()
  43. SubElement(sub, "password").text = n.get_password()
  44. SubElement(sub, "url").text = n.get_url()
  45. SubElement(sub, "notes").text = n.get_notes()
  46. tagelement = SubElement(sub, "tags")
  47. for t in n.get_tags():
  48. SubElement(tagelement, "name").text = t.get_name()
  49. ElementTree(root).write(file)