dbdiff.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. """
  3. # Simple password manager
  4. # Copyright (c) 2011-2023 Michael Büsch <m@bues.ch>
  5. # Licensed under the GNU/GPL version 2 or later.
  6. """
  7. from libpwman.database import *
  8. import difflib
  9. __all__ = [
  10. "PWManDatabaseDiff",
  11. ]
  12. class PWManDatabaseDiff:
  13. def __init__(self, db, oldDb):
  14. self.__db = db
  15. self.__oldDb = oldDb
  16. def getUnifiedDiff(self, contextLines=3):
  17. diff = difflib.unified_diff(
  18. a=self.__oldDb.dumpEntries(showTotpKey=True).splitlines(),
  19. b=self.__db.dumpEntries(showTotpKey=True).splitlines(),
  20. fromfile=str(self.__oldDb.getFilename()),
  21. tofile=str(self.__db.getFilename()),
  22. n=contextLines,
  23. lineterm="")
  24. return "\n".join(diff)
  25. def getContextDiff(self, contextLines=3):
  26. diff = difflib.context_diff(
  27. a=self.__oldDb.dumpEntries(showTotpKey=True).splitlines(),
  28. b=self.__db.dumpEntries(showTotpKey=True).splitlines(),
  29. fromfile=str(self.__oldDb.getFilename()),
  30. tofile=str(self.__db.getFilename()),
  31. n=contextLines,
  32. lineterm="")
  33. return "\n".join(diff)
  34. def getNdiffDiff(self):
  35. diff = difflib.ndiff(
  36. a=self.__oldDb.dumpEntries(showTotpKey=True).splitlines(),
  37. b=self.__db.dumpEntries(showTotpKey=True).splitlines(),
  38. linejunk=None,
  39. charjunk=None)
  40. return "\n".join(diff)
  41. def getHtmlDiff(self, contextLines=3):
  42. htmldiff = difflib.HtmlDiff(
  43. linejunk=None,
  44. charjunk=None)
  45. diff = htmldiff.make_file(
  46. fromlines=self.__oldDb.dumpEntries(showTotpKey=True).splitlines(),
  47. tolines=self.__db.dumpEntries(showTotpKey=True).splitlines(),
  48. fromdesc=str(self.__oldDb.getFilename()),
  49. todesc=str(self.__db.getFilename()),
  50. context=True,
  51. numlines=contextLines)
  52. return "".join(diff)