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 pwman.ui.cli import PwmanCliNew
  24. from pwman.data.nodes import NewNode
  25. from pwman.ui import tools
  26. import time
  27. import pwman.util.config as config
  28. import ast
  29. from pwman.util.crypto import zerome
  30. from colorama import Fore
  31. try:
  32. import msvcrt
  33. except ImportError:
  34. pass
  35. class PwmanCliWinNew(PwmanCliNew):
  36. """
  37. windows ui class
  38. """
  39. def do_new(self, args):
  40. """
  41. can override default config settings the following way:
  42. Pwman3 0.2.1 (c) visit: http://github.com/pwman3/pwman3
  43. pwman> n {'leetify':False, 'numerics':True, 'special_chars':True}
  44. Password (Blank to generate):
  45. """
  46. errmsg = """could not parse config override, please input some"""\
  47. + """ kind of dictionary, e.g.: n {'leetify':False, """\
  48. + """'numerics':True, 'special_chars':True}"""
  49. try:
  50. username = self.get_username()
  51. if args:
  52. try:
  53. args = ast.literal_eval(args)
  54. except Exception:
  55. raise Exception(errmsg)
  56. if not isinstance(args, dict):
  57. raise Exception(errmsg)
  58. password = self.get_password(1, **args)
  59. else:
  60. numerics = config.get_value(
  61. "Generator", "numerics").lower() == 'true'
  62. # TODO: allow custom leetifying through the config
  63. leetify = config.get_value(
  64. "Generator", "leetify").lower() == 'true'
  65. special_chars = config.get_value(
  66. "Generator", "special_chars").lower() == 'true'
  67. password = self.get_password(0,
  68. numerics=numerics,
  69. symbols=leetify,
  70. special_signs=special_chars)
  71. url = self.get_url()
  72. notes = self.get_notes()
  73. node = NewNode(username, password, url, notes)
  74. tags = self.get_tags()
  75. node.tags = tags
  76. self._db.addnodes([node])
  77. print "Password ID: %d" % (node._id)
  78. # when done with node erase it
  79. zerome(password)
  80. except Exception as e:
  81. self.error(e)
  82. def print_node(self, node):
  83. width = str(tools._defaultwidth)
  84. print "Node %d." % (node.get_id())
  85. print ("%" + width+"s %s") % (tools.typeset("Username:", Fore.RED),
  86. node.get_username())
  87. print ("%"+width+"s %s") % (tools.typeset("Password:", Fore.RED),
  88. node.get_password())
  89. print ("%"+width+"s %s") % (tools.typeset("Url:", Fore.RED),
  90. node.get_url())
  91. print ("%"+width+"s %s") % (tools.typeset("Notes:", Fore.RED),
  92. node.get_notes())
  93. print tools.typeset("Tags: ", Fore.RED),
  94. for t in node.get_tags():
  95. print " %s \n" % t.get_name(),
  96. def heardEnterWin():
  97. c = msvcrt.kbhit()
  98. if c == 1:
  99. ret = msvcrt.getch()
  100. if ret is not None:
  101. return True
  102. return False
  103. def waituntil_enter(somepredicate, timeout, period=0.25):
  104. mustend = time.time() + timeout
  105. while time.time() < mustend:
  106. cond = somepredicate()
  107. if cond:
  108. break
  109. time.sleep(period)
  110. self.do_cls('')
  111. flushtimeout = int(config.get_value("Global", "cls_timeout"))
  112. if flushtimeout > 0:
  113. print "Press any key to flush screen (autoflash "\
  114. + "in %d sec.)" % flushtimeout
  115. waituntil_enter(heardEnterWin, flushtimeout)