cli.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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-2016 Oz N Tiram <oz.tiram@gmail.com>
  18. # ============================================================================
  19. # pylint: disable=I0011
  20. import cmd
  21. import sys
  22. if sys.version_info.major > 2:
  23. raw_input = input
  24. try:
  25. import readline
  26. _readline_available = True
  27. except ImportError as e: # pragma: no cover
  28. import pyreadline as readline
  29. _readline_available = True
  30. from pwman.ui.baseui import BaseCommands
  31. from pwman import (get_conf_options, get_db_version, version, website,
  32. parser_options, has_cryptography, calculate_client_info,
  33. is_latest_version)
  34. from pwman.ui.tools import CLICallback
  35. from pwman.data import factory
  36. from pwman.exchange.importer import Importer
  37. from pwman.util.crypto_engine import CryptoEngine
  38. class PwmanCli(cmd.Cmd, BaseCommands):
  39. """
  40. Inherit from the BaseCommands and Aliases
  41. """
  42. undoc_header = "Aliases:"
  43. def __init__(self, db, hasxsel, callback, config_parser, **kwargs):
  44. """
  45. initialize CLI interface, set up the DB
  46. connecion, see if we have xsel ...
  47. """
  48. super(PwmanCli, self).__init__(**kwargs)
  49. self.intro = "%s %s (c) visit: %s" % ('pwman3', version, website)
  50. self._historyfile = config_parser.get_value("Readline", "history")
  51. self.hasxsel = hasxsel
  52. self.config = config_parser
  53. try:
  54. enc = CryptoEngine.get()
  55. enc.callback = callback()
  56. self._db = db
  57. # this cascades down all the way to setting the database key
  58. self._db.open()
  59. except Exception as e: # pragma: no cover
  60. self.error(e)
  61. sys.exit(1)
  62. try:
  63. readline.read_history_file(self._historyfile)
  64. except IOError as e: # pragma: no cover
  65. pass
  66. self.prompt = "pwman> "
  67. def get_ui_platform(platform): # pragma: no cover
  68. if 'darwin' in platform:
  69. from pwman.ui.mac import PwmanCliMac as PwmanCli
  70. OSX = True
  71. elif 'win' in platform:
  72. from pwman.ui.win import PwmanCliWin as PwmanCli
  73. OSX = False
  74. else:
  75. from pwman.ui.cli import PwmanCli
  76. OSX = False
  77. return PwmanCli, OSX
  78. def main():
  79. args = parser_options().parse_args()
  80. PwmanCli, OSX = get_ui_platform(sys.platform)
  81. xselpath, dbtype, config = get_conf_options(args, OSX)
  82. dburi = config.get_value('Database', 'dburi')
  83. if config.get_value('Updater',
  84. 'supress_version_check').lower() != 'yes':
  85. client_info = config.get_value('Updater', 'client_info')
  86. if not client_info:
  87. client_info = calculate_client_info()
  88. config.set_value('Updater', 'client_info', client_info)
  89. _, latest = is_latest_version(version, client_info)
  90. if not latest:
  91. print("A newer version of Pwman3 was release, you should consider updating") # noqa
  92. if not has_cryptography:
  93. import colorama
  94. if config.get_value('Crypto', 'supress_warning').lower() != 'yes':
  95. print("{}WARNING: You are not using PyCrypto!!!\n"
  96. "WARNING: You should install PyCrypto for better security and " # noqa
  97. "perfomance\nWARNING: You can supress this warning by editing " # noqa
  98. "pwman config file.{}".format(colorama.Fore.RED,
  99. colorama.Style.RESET_ALL))
  100. print(dburi)
  101. dbver = get_db_version(config, args)
  102. timeout = int(config.get_value('Global', 'lock_timeout'))
  103. CryptoEngine.get(timeout)
  104. db = factory.createdb(dburi, dbver)
  105. if args.file_delim:
  106. importer = Importer((args, config, db))
  107. importer.run()
  108. sys.exit(0)
  109. cli = PwmanCli(db, xselpath, CLICallback, config)
  110. try:
  111. cli.cmdloop()
  112. except KeyboardInterrupt as e:
  113. print(e)
  114. finally:
  115. config.save()