forked from linuxmint/cinnamon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_cs_module_desktop_files.py
executable file
·125 lines (104 loc) · 4.38 KB
/
generate_cs_module_desktop_files.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
import os
import glob
import polib
import sys
from gi.repository import GLib
try:
sys.path.append('files/usr/lib/cinnamon-settings/modules')
sys.path.append('files/usr/lib/cinnamon-settings/bin')
mod_files = glob.glob('files/usr/lib/cinnamon-settings/modules/*.py')
mod_files.sort()
if len(mod_files) is 0:
raise Exception("No settings modules found!!")
for i in range(len(mod_files)):
mod_files[i] = mod_files[i].split('/')[5]
mod_files[i] = mod_files[i].split('.')[0]
if mod_files[i][0:3] != "cs_":
raise Exception("Settings modules must have a prefix of 'cs_' !!")
modules = map(__import__, mod_files)
except Exception, detail:
print detail
sys.exit(1)
DESKTOP_GROUP = "Desktop Entry"
DESKTOP_KEY_NAME = "Name"
DESKTOP_KEY_COMMENT = "Comment"
DESKTOP_KEY_EXEC = "Exec"
DESKTOP_KEY_ICON = "Icon"
DESKTOP_KEY_CATEGORIES = "Categories"
DESKTOP_KEY_TYPE = "Type"
DESKTOP_KEY_ONLY_SHOW_IN = "OnlyShowIn"
DESKTOP_TYPE_APPLICATION = "Application"
class KeyFile:
def __init__(self, mod):
self.kf_name = "cinnamon-settings-%s.desktop" % (mod.name)
self.kf = GLib.KeyFile()
self.kf.set_string(DESKTOP_GROUP, DESKTOP_KEY_NAME, mod.sidePage.name)
try:
self.kf.set_string(DESKTOP_GROUP, DESKTOP_KEY_COMMENT, mod.comment)
except:
pass
self.kf.set_string(DESKTOP_GROUP, DESKTOP_KEY_ICON, mod.sidePage.icon)
self.kf.set_string(DESKTOP_GROUP, DESKTOP_KEY_EXEC, "cinnamon-settings %s" % (mod.name))
self.kf.set_string(DESKTOP_GROUP, DESKTOP_KEY_TYPE, DESKTOP_TYPE_APPLICATION)
self.kf.set_string_list(DESKTOP_GROUP, DESKTOP_KEY_ONLY_SHOW_IN, ("X-Cinnamon",))
if mod.category in ("admin"):
self.kf.set_string_list(DESKTOP_GROUP, DESKTOP_KEY_CATEGORIES, ("Settings","System"))
else:
self.kf.set_string_list(DESKTOP_GROUP, DESKTOP_KEY_CATEGORIES, ("Settings",))
class Main:
def __init__(self):
self.keyfiles = []
for i in range(len(modules)):
try:
mod = modules[i].Module(None)
keyfile = KeyFile(mod)
self.keyfiles.append(keyfile)
except:
print "Failed to load module %s" % modules[i]
import traceback
traceback.print_exc()
self.mo_files = {}
if len(self.keyfiles) > 0:
for root, subFolders, files in os.walk("/usr/share/cinnamon/locale"):
for file in files:
if file == "cinnamon.mo":
path, junk = os.path.split(root)
path, locale = os.path.split(path)
self.mo_files[locale] = polib.mofile(os.path.join(root, file))
if len(self.mo_files) > 0:
for locale in self.mo_files.keys():
for entry in self.mo_files[locale]:
self.check_name(locale, entry)
for locale in self.mo_files.keys():
for entry in self.mo_files[locale]:
self.check_comment(locale, entry)
for kf in self.keyfiles:
action_path = os.path.join("files", "usr", "share", "applications", kf.kf_name)
outstring, length = kf.kf.to_data()
if os.path.exists(action_path):
os.remove(action_path)
outfile = open(action_path, 'w')
outfile.write(outstring)
outfile.close()
print "Cinnamon settings desktop file generation complete."
def check_name(self, locale, entry):
if entry.msgstr != '':
for kf in self.keyfiles:
try:
name = kf.kf.get_string(DESKTOP_GROUP, DESKTOP_KEY_NAME)
if name == entry.msgid:
kf.kf.set_locale_string(DESKTOP_GROUP, DESKTOP_KEY_NAME, locale, entry.msgstr)
except GLib.GError:
pass
def check_comment(self, locale, entry):
if entry.msgstr != '':
for kf in self.keyfiles:
try:
name = kf.kf.get_string(DESKTOP_GROUP, DESKTOP_KEY_COMMENT)
if name == entry.msgid:
kf.kf.set_locale_string(DESKTOP_GROUP, DESKTOP_KEY_COMMENT, locale, entry.msgstr)
except GLib.GError:
pass
if __name__ == "__main__":
Main()