win.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """"
  2. # ============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. # ============================================================================
  18. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  19. # ============================================================================
  20. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  21. # ============================================================================
  22. """
  23. from __future__ import print_function
  24. from pwman.ui.cli import PwmanCliNew
  25. from pwman.data.nodes import NewNode
  26. from pwman.ui import tools
  27. import time
  28. import pwman.util.config as config
  29. import ast
  30. from pwman.util.crypto import zerome
  31. from colorama import Fore
  32. try:
  33. import msvcrt
  34. except ImportError:
  35. pass
  36. class PwmanCliWinNew(PwmanCliNew):
  37. """
  38. windows ui class
  39. """
  40. def do_new(self, args):
  41. """
  42. can override default config settings the following way:
  43. Pwman3 0.2.1 (c) visit: http://github.com/pwman3/pwman3
  44. pwman> n {'leetify':False, 'numerics':True, 'special_chars':True}
  45. Password (Blank to generate):
  46. """
  47. errmsg = """could not parse config override, please input some"""\
  48. + """ kind of dictionary, e.g.: n {'leetify':False, """\
  49. + """'numerics':True, 'special_chars':True}"""
  50. try:
  51. username = self.get_username()
  52. if args:
  53. try:
  54. args = ast.literal_eval(args)
  55. except Exception:
  56. raise Exception(errmsg)
  57. if not isinstance(args, dict):
  58. raise Exception(errmsg)
  59. password = self.get_password(1, **args)
  60. else:
  61. numerics = config.get_value(
  62. "Generator", "numerics").lower() == 'true'
  63. # TODO: allow custom leetifying through the config
  64. leetify = config.get_value(
  65. "Generator", "leetify").lower() == 'true'
  66. special_chars = config.get_value(
  67. "Generator", "special_chars").lower() == 'true'
  68. password = self.get_password(0,
  69. numerics=numerics,
  70. symbols=leetify,
  71. special_signs=special_chars)
  72. url = self.get_url()
  73. notes = self.get_notes()
  74. node = NewNode(username, password, url, notes)
  75. tags = self.get_tags()
  76. node.tags = tags
  77. self._db.addnodes([node])
  78. print("Password ID: %d" % (node._id))
  79. # when done with node erase it
  80. zerome(password)
  81. except Exception as e:
  82. self.error(e)
  83. def print_node(self, node):
  84. width = str(tools._defaultwidth)
  85. print("Node {}.".format(node._id))
  86. print("{} {}".format(tools.typeset("Username:", Fore.RED).ljust(width),
  87. node.username))
  88. print ("{} {}".format(tools.typeset("Password:", Fore.RED).ljust(width),
  89. node.password))
  90. print("{} {}".format(tools.typeset("Url:", Fore.RED).ljust(width), node.url))
  91. print("{} {}".format(tools.typeset("Notes:", Fore.RED).ljust(width), node.notes))
  92. print("{}".format(tools.typeset("Tags: ", Fore.RED)), end=" ")
  93. for t in node.tags:
  94. print(t.name)
  95. def heardEnterWin():
  96. c = msvcrt.kbhit()
  97. if c == 1:
  98. ret = msvcrt.getch()
  99. if ret is not None:
  100. return True
  101. return False
  102. def waituntil_enter(somepredicate, timeout, period=0.25):
  103. mustend = time.time() + timeout
  104. while time.time() < mustend:
  105. cond = somepredicate()
  106. if cond:
  107. break
  108. time.sleep(period)
  109. self.do_cls('')
  110. flushtimeout = int(config.get_value("Global", "cls_timeout"))
  111. if flushtimeout > 0:
  112. print("Press any key to flush screen (autoflash "
  113. "in %d sec.)" % flushtimeout)
  114. waituntil_enter(heardEnterWin, flushtimeout)