Debian Codegraph @

files/usr/bin/xlet-about-dialog

raw
1 #!/usr/bin/python3
2
3 import argparse
4 import sys
5 import os
6 import json
7 import gettext
8 import datetime
9 import gi
10 gi.require_version('Gtk', '3.0')
11 from gi.repository import Gtk, GdkPixbuf, GLib
12
13 gettext.install('cinnamon', '/usr/share/locale')
14
15 home = os.path.expanduser('~')
16
17
18 class AboutDialog(Gtk.AboutDialog):
19 def __init__(self, metadata, stype):
20 super().__init__()
21 self.metadata = metadata
22 self.stype = stype
23
24 self.set_icon()
25
26 if 'version' in metadata:
27 version = self.metadata['version']
28 if 'last-edited' in metadata:
29 timestamp = datetime.datetime.fromtimestamp(self.metadata['last-edited']).isoformat(' ')
30 self.props.version = _("Version %s (%s)") % (version, timestamp)
31 else:
32 self.props.version = _("Version %s") % version
33 elif 'last-edited' in metadata:
34 timestamp = datetime.datetime.fromtimestamp(int(self.metadata['last-edited'])).isoformat(' ')
35 self.props.version = _("Updated on %s") % timestamp
36
37 comments = self._(self.metadata.get('description', ''))
38
39 if 'comments' in self.metadata:
40 comments += '\n\n' + self._(self.metadata['comments'])
41 self.props.comments = comments
42
43 s_id = self.get_spices_id()
44 if s_id:
45 self.props.website = f'https://cinnamon-spices.linuxmint.com/{self.stype}/view/{s_id}'
46 self.props.website_label = _("More info")
47
48 if 'contributors' in self.metadata:
49 self.props.authors = self.metadata['contributors'].split(',')
50
51 xlet_name = self._(self.metadata.get('name'))
52 self.props.program_name = f"{xlet_name} ({self.metadata.get('uuid')})"
53
54 self.connect('response', self.close)
55 self.set_title(_("About %s") % self.props.program_name)
56 self.show()
57
58 def close(self, *args):
59 Gtk.main_quit()
60
61 def set_icon(self):
62 # Use the generic type icon
63 self.set_logo_icon_name(f"cs-{self.metadata['type']}")
64 # Override with metadata if set..
65 if 'icon' in self.metadata:
66 self.set_logo_icon_name(self.metadata['icon'])
67 # Override with icon.png if present
68 icon_path = os.path.join(self.metadata['path'], 'icon.png')
69 if os.path.exists(icon_path):
70 icon = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_path, 48, 48)
71 self.set_logo(icon)
72 self.set_default_icon_name(f"cs-{self.metadata['type']}")
73
74 def _(self, msg):
75 gettext.bindtextdomain(self.metadata.get('uuid'), home + '/.local/share/locale')
76 translation = gettext.dgettext(self.metadata.get('uuid'), msg)
77 # try with cinnamon domain if the xlet domain doesn't work
78 if translation == msg:
79 return _(msg)
80
81 return translation
82
83 def get_spices_id(self):
84 try:
85 cache_path = os.path.join(GLib.get_user_cache_dir(), 'cinnamon', 'spices', self.stype[0:-1], 'index.json')
86 if os.path.exists(cache_path):
87 with open(cache_path, 'r', encoding='utf-8') as cache_file:
88 index_cache = json.load(cache_file)
89 if self.metadata['uuid'] in index_cache:
90 return index_cache[self.metadata['uuid']]['spices-id']
91 return None
92 except Exception as e:
93 print('Failed to load/parse index.json')
94 print(e)
95 return None
96
97
98 if __name__ == "__main__":
99 parser = argparse.ArgumentParser()
100 parser.description = 'Arguments for xlet-about-dialog'
101 parser.add_argument('xlet_type', type=str, help=_('the type of the Spice'),
102 choices=['applets', 'desklets', 'extensions',
103 'actions', 'themes'])
104 parser.add_argument('uuid', type=str, metavar='UUID', nargs=1,
105 help=_('the UUID of the Spice'))
106 _args = parser.parse_args()
107
108 xlet_type = _args.xlet_type
109 uuid = _args.uuid[0]
110
111 suffix = f'share/nemo/actions/{uuid}' if xlet_type == 'actions' else f'share/cinnamon/{xlet_type}/{uuid}'
112 prefixes = [os.path.join(home, '.local'), '/usr']
113
114 path = None
115 for prefix in prefixes:
116 path = os.path.join(prefix, suffix)
117 if os.path.exists(path):
118 break
119
120 if path is None:
121 print(_('Unable to locate %s %s') % (xlet_type, uuid))
122 sys.exit(1)
123
124 try:
125 with open(os.path.join(path, 'metadata.json'), encoding='utf-8') as meta:
126 _metadata = json.load(meta)
127
128 _metadata['path'] = path
129 _metadata['type'] = xlet_type
130
131 AboutDialog(_metadata, xlet_type)
132
133 Gtk.main()
134 except FileNotFoundError:
135 pass