exporter.py 2.3 KB

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