cli.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.data.nodes import NewNode
  28. from pwman.data.tags import TagNew as TagN
  29. from pwman.util.crypto import CryptoEngine
  30. from pwman.util.crypto import zerome
  31. import pwman.util.config as config
  32. import sys
  33. import cmd
  34. import time
  35. import select as uselect
  36. import ast
  37. from pwman.ui import tools
  38. from pwman.ui.tools import CMDLoop
  39. from pwman.ui.tools import CliMenuItem
  40. from pwman.ui.ocli import PwmanCliOld, Aliases, BaseCommands
  41. from colorama import Fore
  42. import getpass
  43. try:
  44. import readline
  45. _readline_available = True
  46. except ImportError, e: # pragma: no cover
  47. _readline_available = False
  48. def get_pass_conf():
  49. numerics = config.get_value("Generator", "numerics").lower() == 'true'
  50. # TODO: allow custom leetifying through the config
  51. leetify = config.get_value("Generator", "leetify").lower() == 'true'
  52. special_chars = config.get_value("Generator", "special_chars"
  53. ).lower() == 'true'
  54. return numerics, leetify, special_chars
  55. class PwmanCliNew(Aliases, BaseCommands):
  56. """
  57. Inherit from the BaseCommands and Aliases
  58. """
  59. def __init__(self, db, hasxsel, callback):
  60. """
  61. initialize CLI interface, set up the DB
  62. connecion, see if we have xsel ...
  63. """
  64. cmd.Cmd.__init__(self)
  65. self.intro = "%s %s (c) visit: %s" % (pwman.appname, pwman.version,
  66. pwman.website)
  67. self._historyfile = config.get_value("Readline", "history")
  68. self.hasxsel = hasxsel
  69. try:
  70. enc = CryptoEngine.get()
  71. enc._callback = callback()
  72. self._db = db
  73. self._db.open()
  74. except Exception, e: # pragma: no cover
  75. self.error(e)
  76. sys.exit(1)
  77. try:
  78. readline.read_history_file(self._historyfile)
  79. except IOError, e: # pragma: no cover
  80. pass
  81. self.prompt = "pwman> "