cli.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.ocli import Aliases, BaseCommands
  32. try:
  33. import readline
  34. _readline_available = True
  35. except ImportError, e: # pragma: no cover
  36. _readline_available = False
  37. def get_pass_conf():
  38. numerics = config.get_value("Generator", "numerics").lower() == 'true'
  39. # TODO: allow custom leetifying through the config
  40. leetify = config.get_value("Generator", "leetify").lower() == 'true'
  41. special_chars = config.get_value("Generator", "special_chars"
  42. ).lower() == 'true'
  43. return numerics, leetify, special_chars
  44. class PwmanCliNew(Aliases, BaseCommands):
  45. """
  46. Inherit from the BaseCommands and Aliases
  47. """
  48. def __init__(self, db, hasxsel, callback):
  49. """
  50. initialize CLI interface, set up the DB
  51. connecion, see if we have xsel ...
  52. """
  53. cmd.Cmd.__init__(self)
  54. self.intro = "%s %s (c) visit: %s" % (pwman.appname, pwman.version,
  55. pwman.website)
  56. self._historyfile = config.get_value("Readline", "history")
  57. self.hasxsel = hasxsel
  58. try:
  59. enc = CryptoEngine.get()
  60. enc._callback = callback()
  61. self._db = db
  62. # this cascades down all the way to setting the database key
  63. self._db.open()
  64. except Exception, e: # pragma: no cover
  65. self.error(e)
  66. sys.exit(1)
  67. try:
  68. readline.read_history_file(self._historyfile)
  69. except IOError, e: # pragma: no cover
  70. pass
  71. self.prompt = "pwman> "