cli.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_engine import CryptoEngine
  28. import sys
  29. import cmd
  30. from pwman.ui.baseui import BaseCommands
  31. try:
  32. import readline
  33. _readline_available = True
  34. except ImportError as e: # pragma: no cover
  35. _readline_available = False
  36. class PwmanCli(cmd.Cmd, BaseCommands):
  37. """
  38. Inherit from the BaseCommands and Aliases
  39. """
  40. undoc_header = "Aliases:"
  41. def __init__(self, db, hasxsel, callback, config_parser, **kwargs):
  42. """
  43. initialize CLI interface, set up the DB
  44. connecion, see if we have xsel ...
  45. """
  46. super(PwmanCli, self).__init__(**kwargs)
  47. self.intro = "%s %s (c) visit: %s" % (pwman.appname, pwman.version,
  48. pwman.website)
  49. self._historyfile = config_parser.get_value("Readline", "history")
  50. self.hasxsel = hasxsel
  51. self.config = config_parser
  52. try:
  53. enc = CryptoEngine.get()
  54. enc.callback = callback()
  55. self._db = db
  56. # this cascades down all the way to setting the database key
  57. self._db.open()
  58. except Exception as e: # pragma: no cover
  59. self.error(e)
  60. sys.exit(1)
  61. try:
  62. readline.read_history_file(self._historyfile)
  63. except IOError as e: # pragma: no cover
  64. pass
  65. self.prompt = "pwman> "