cli.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, 2013, 2014, 2015 Oz Nahum <nahumoz@gmail.com>
  18. # ============================================================================
  19. # pylint: disable=I0011
  20. from __future__ import print_function
  21. import sys
  22. import cmd
  23. if sys.version_info.major > 2:
  24. raw_input = input
  25. try:
  26. import readline
  27. _readline_available = True
  28. except ImportError as e: # pragma: no cover
  29. import pyreadline as readline
  30. _readline_available = True
  31. from pwman.ui.baseui import BaseCommands
  32. from pwman import (get_conf_options, get_db_version, version, website, parser_options)
  33. from pwman.ui.tools import CLICallback
  34. from pwman.data import factory
  35. from pwman.exchange.importer import Importer
  36. from pwman.util.crypto_engine import CryptoEngine
  37. class PwmanCli(cmd.Cmd, BaseCommands):
  38. """
  39. Inherit from the BaseCommands and Aliases
  40. """
  41. undoc_header = "Aliases:"
  42. def __init__(self, db, hasxsel, callback, config_parser, **kwargs):
  43. """
  44. initialize CLI interface, set up the DB
  45. connecion, see if we have xsel ...
  46. """
  47. super(PwmanCli, self).__init__(**kwargs)
  48. self.intro = "%s %s (c) visit: %s" % ('pwman3', version, 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> "
  66. def get_ui_platform(platform): # pragma: no cover
  67. if 'darwin' in platform:
  68. from pwman.ui.mac import PwmanCliMac as PwmanCli
  69. OSX = True
  70. elif 'win' in platform:
  71. from pwman.ui.win import PwmanCliWin as PwmanCli
  72. OSX = False
  73. else:
  74. from pwman.ui.cli import PwmanCli
  75. OSX = False
  76. return PwmanCli, OSX
  77. def main():
  78. args = parser_options().parse_args()
  79. PwmanCli, OSX = get_ui_platform(sys.platform)
  80. xselpath, dbtype, config = get_conf_options(args, OSX)
  81. dburi = config.get_value('Database', 'dburi')
  82. print(dburi)
  83. dbver = get_db_version(config, args)
  84. CryptoEngine.get()
  85. db = factory.createdb(dburi, dbver)
  86. if args.file_delim:
  87. importer = Importer((args, config, db))
  88. importer.run()
  89. sys.exit(0)
  90. cli = PwmanCli(db, xselpath, CLICallback, config)
  91. try:
  92. cli.cmdloop()
  93. except KeyboardInterrupt as e:
  94. print(e)
  95. finally:
  96. config.save()