cli.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 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, config_parser=None):
  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. if not config_parser:
  50. self._historyfile = config.get_value("Readline", "history")
  51. else:
  52. self._historyfile = config_parser.get_value("Readline", "history")
  53. self.hasxsel = hasxsel
  54. self.config = config_parser
  55. try:
  56. enc = CryptoEngine.get()
  57. enc.callback = callback()
  58. self._db = db
  59. # this cascades down all the way to setting the database key
  60. self._db.open()
  61. except Exception as e: # pragma: no cover
  62. self.error(e)
  63. sys.exit(1)
  64. try:
  65. readline.read_history_file(self._historyfile)
  66. except IOError as e: # pragma: no cover
  67. pass
  68. self.prompt = "pwman> "