cli.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. # pylint: disable=I0011
  22. """
  23. Define the CLI interface for pwman3 and the helper functions
  24. """
  25. from __future__ import print_function
  26. import pwman
  27. from pwman.util.crypto import CryptoEngine
  28. import pwman.util.config as config
  29. import sys
  30. import cmd
  31. from pwman.ui.base import Aliases, BaseCommands
  32. try:
  33. import readline
  34. _readline_available = True
  35. except ImportError as e: # pragma: no cover
  36. _readline_available = False
  37. class PwmanCliNew(cmd.Cmd, Aliases, BaseCommands):
  38. """
  39. Inherit from the BaseCommands and Aliases
  40. """
  41. def __init__(self, db, hasxsel, callback):
  42. """
  43. initialize CLI interface, set up the DB
  44. connecion, see if we have xsel ...
  45. """
  46. cmd.Cmd.__init__(self)
  47. self.intro = "%s %s (c) visit: %s" % (pwman.appname, pwman.version,
  48. pwman.website)
  49. self._historyfile = config.get_value("Readline", "history")
  50. self.hasxsel = hasxsel
  51. try:
  52. enc = CryptoEngine.get()
  53. enc.callback = callback()
  54. self._db = db
  55. # this cascades down all the way to setting the database key
  56. self._db.open()
  57. except Exception as e: # pragma: no cover
  58. self.error(e)
  59. sys.exit(1)
  60. try:
  61. readline.read_history_file(self._historyfile)
  62. except IOError as e: # pragma: no cover
  63. pass
  64. self.prompt = "pwman> "