File: message.py

package info (click to toggle)
epoptes 23.01-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,704 kB
  • sloc: python: 2,461; sh: 477; makefile: 11
file content (77 lines) | stat: -rwxr-xr-x 2,402 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python3
# This file is part of Epoptes, https://epoptes.org
# Copyright 2012-2018 the Epoptes team, see AUTHORS.
# SPDX-License-Identifier: GPL-3.0-or-later
"""
Display a simple window with a message.
"""
import os
import sys

from _common import gettext as _
from gi.repository import Gtk


class MessageWindow(Gtk.Window):
    """Display a simple window with a message."""
    def __init__(self, text, title="Epoptes", markup=True,
                 icon_name="dialog-information"):
        super().__init__(title=title, icon_name=icon_name)
        self.set_position(Gtk.WindowPosition.CENTER)

        grid = Gtk.Grid(column_spacing=10, row_spacing=10, margin=10)
        self.add(grid)

        image = Gtk.Image.new_from_icon_name(icon_name, Gtk.IconSize.DIALOG)
        grid.add(image)

        # Always load the plain text first in case the markup parsing fails
        label = Gtk.Label(
            label=text, selectable=True, hexpand=True, vexpand=True,
            halign=Gtk.Align.START, valign=Gtk.Align.START)
        if markup:
            label.set_markup(text)
        grid.add(label)

        button = Gtk.Button.new_from_stock(Gtk.STOCK_CLOSE)
        button.set_hexpand(False)
        button.set_halign(Gtk.Align.END)
        button.connect("clicked", Gtk.main_quit)
        grid.attach(button, 1, 1, 2, 1)
        self.set_focus_child(button)

        accelgroup = Gtk.AccelGroup()
        key, modifier = Gtk.accelerator_parse('Escape')
        accelgroup.connect(
            key, modifier, Gtk.AccelFlags.VISIBLE, Gtk.main_quit)
        self.add_accel_group(accelgroup)


def main():
    """Run the module from the command line."""
    if len(sys.argv) <= 1 or len(sys.argv) > 5:
        print(_("Usage: {} text [title] [markup] [icon_name]").format(
            os.path.basename(__file__)), file=sys.stderr)
        exit(1)
    text = sys.argv[1]
    if len(sys.argv) > 2 and sys.argv[2]:
        title = sys.argv[2]
    else:
        title = "Epoptes"
    if len(sys.argv) > 3 and sys.argv[3]:
        markup = sys.argv[3].lower() == "true"
    else:
        markup = True
    if len(sys.argv) > 4:
        icon_name = sys.argv[4]
    else:
        icon_name = "dialog-information"

    window = MessageWindow(text, title, markup, icon_name)
    window.connect("destroy", Gtk.main_quit)
    window.show_all()
    Gtk.main()


if __name__ == '__main__':
    main()