Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_dialog.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. import os
  27. import shutil
  28. from twisted.internet.defer import Deferred
  29. from twisted.internet.interfaces import IReactorProcess
  30. from twisted.internet.utils import getProcessOutput
  31. __all__ = ('get_input_from_dialog',)
  32. def get_input_from_dialog(reactor: IReactorProcess, title: str = 'Unlock key',
  33. text: str = 'Please enter passphrase to unlock key',
  34. hide_text: bool = True) -> Deferred:
  35. """
  36. Show a Gnome/GTK desktop dialog asking for a passphrase.
  37. This is using zenity, the GNOME port of dialog which allows you to display dialog boxes
  38. from the commandline and shell scripts. To install (on Linux):
  39. .. code:: console
  40. sudo apt update
  41. sudo install zenity
  42. See also:
  43. - https://gitlab.gnome.org/GNOME/zenity
  44. - https://wiki.ubuntuusers.de/Zenity/
  45. - https://bash.cyberciti.biz/guide/Zenity:_Shell_Scripting_with_Gnome
  46. :param reactor: Twisted reactor to use.
  47. :param title: Dialog window title to show.
  48. :param text: Dialog text to show above text entry field.
  49. :param hide_text: Hide entry field text.
  50. :return: A deferred that resolves with the string the user entered.
  51. """
  52. exe = shutil.which('zenity')
  53. if not exe:
  54. raise RuntimeError('get_input_from_dialog(): could not find zenity (install with "apt install zenity")')
  55. args = ['--entry', '--title', title, '--text', text]
  56. if hide_text:
  57. args.append('--hide-text')
  58. d = getProcessOutput(exe, args=args, env=os.environ, reactor=reactor)
  59. def _consume(output):
  60. passphrase = output.decode('utf8').strip()
  61. return passphrase
  62. d.addCallback(_consume)
  63. return d
  64. if __name__ == "__main__":
  65. from twisted.internet.task import react
  66. async def main(reactor):
  67. passphrase = await get_input_from_dialog(reactor)
  68. print(type(passphrase), len(passphrase), '"{}"'.format(passphrase))
  69. react(main)