Files
OpenRA/packaging/linux/gtk-dialog.py
Unrud 678f249c63 Scripts: Disable markup in error messages
Error messages are displayed using the following methods:

* **zenity** parses pango markup and replaces escaped characters
* **kdialog** replaces (some) escaped characters
* **gtk-dialog.py** replaces `\n`
* **printf** interprets format strings and replaces escaped characters
* **echo** just displays the text

The error messages themself contain escaped characters and paths from variables.

This PR unifies the behavior by:

* Use **printf** to format error messages and replace escaped characters
* Setting `--no-markup` for **zenity** to disable pango markup and escaped characters
* Remove `\n` replacement from **gtk-dialog.py**.
* Use plain **echo** instead of **printf**
2022-07-10 18:55:25 +02:00

49 lines
1.7 KiB
Python

#!/usr/bin/python
"""A simple GTK3 graphical dialog helper that can be used as a fallback if zenity is not available
Compatible with python 2 or 3 with the gi bindings.
Three modes are available:
test: accepts no arguments, returns 0 if the python dependencies are available, or 1 if not
error: show a gtk-3 error dialog
accepts arguments --title and --text
question: show a gtk-3 question dialog
accepts arguments --title and --text
returns 0 on Yes, or 1 on No
"""
import sys
try:
import argparse
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
except ImportError:
sys.exit(1)
class Error():
def __init__(self, title, text):
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, title)
dialog.format_secondary_text(text)
dialog.run()
dialog.destroy()
class Question():
def __init__(self, title, text):
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, title)
dialog.format_secondary_text(text)
response = dialog.run()
dialog.destroy()
sys.exit(0 if response == Gtk.ResponseType.YES else 1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('type', choices=['error', 'question', 'test'])
parser.add_argument('--title', type=str, required=False, default='')
parser.add_argument('--text', type=str, required=False, default='')
args = parser.parse_args()
if args.type == 'question':
Question(args.title, args.text)
elif args.type == 'error':
Error(args.title, args.text)