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
|
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#
# Author : Eliovir
# Creation Date : 2013-03-17 13:40:49CEST
# Last Revision : $Date: $
# Revision : $Rev: $
"""
Generator of .desktop files, for Elatom.
"""
__revision__ = "$Rev: $"
__author__ = "Eliovir"
import gettext
import configparser
import glob
import locale
import os
import sys
###########
# #
# Gettext #
# #
###########
def set_locale(current_locale=None):
if current_locale is None:
current_locale, encoding = locale.getdefaultlocale()
# Hack to get the locale directory
basepath = os.path.abspath(os.path.dirname(sys.argv[0]))
localedir = os.path.join(basepath, "locale")
domain = "elatom" # the translation file is elatom.mo
# Set up Python's gettext
if current_locale != None:
mytranslation = gettext.translation(domain, localedir,
[current_locale], fallback=True)
mytranslation.install()
n_ = mytranslation.ngettext
else:
print("Can not setup gettext for %s. Exiting." % locale)
sys.exit(1)
class Desktop:
_path = None
_inifile = None
_section = "Desktop Entry"
def __init__(self, path):
self._inifile = configparser.ConfigParser()
self._inifile.optionxform = str
self._path = path
if os.path.exists(self._path):
file_handler = open(self._path, 'r+', encoding='utf-8')
self._inifile.readfp(file_handler)
if not self._inifile.has_section(self._section):
self._inifile.add_section(self._section)
def generate(self, locales):
"""
Set the content of the .desktop file.
@type locales: list
@param locales: locale identifiers
"""
working_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
values = {
# http://www.freedesktop.org/wiki/Specifications/menu-spec
"Categories": "Network;FileTransfer;Feed;",
"Comment": "Download automatically your podcasts",
"Exec": os.path.join(working_dir, "elatom.py"),
"GenericName": "Podcasts downloader",
"Hidden": "false",
"Icon": "elatom",
"MimeType": "application/rss+xml;application/atom+xml",
"Name": "Elatom",
"NoDisplay": "false",
"StartupNotify": "false",
"Terminal": "false",
"Type": "Application",
"Version": "1.0"
}
for locale in locales:
set_locale(locale)
# TRANSLATORS: Value of "Comment" in the freedesktop.org .desktop file.
translated_comment = _("Download automatically your podcasts")
if not translated_comment == values["Comment"]:
values["Comment[" + locale + "]"] = translated_comment
# TRANSLATORS: Value of "GenericName" in the freedesktop.org .desktop file.
translated_genericname = _("Podcasts downloader")
if not translated_genericname == values["GenericName"]:
values["GenericName[" + locale + "]"] = translated_genericname
keys = list(values.keys())
keys.sort()
for key in keys:
self._inifile.set(self._section, key, values[key])
def save(self):
"""
Save the .desktop file.
"""
with open(self._path, 'w', encoding='utf-8') as inifile:
self._inifile.write(inifile, space_around_delimiters=False)
if __name__ == "__main__":
set_locale()
d = Desktop('elatom.desktop')
locales = [os.path.basename(fi)[:-3]
for fi in glob.glob('locale/*.po')]
d.generate(locales)
d.save()
|