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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#!/usr/bin/python
# -*- coding: latin1 -*-
### Copyright (C) 2007-09 Damon Lynch <damonlynch@gmail.com>
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import sys
import gc
import distutils.version
import gtk.gdk as gdk
import config
import locale
import gettext
class Configi18n:
""" Setup translation
Adapated from code example of Mark Mruss http://www.learningpython.com.
Unlike his example, this code uses a local locale directory only if the environment
variable LOCALEDIR has been set to some or other value.
"""
# Do not put this code block in __init__, because it needs to be run only once
# if the evironment value 'LOCAELDIR' is set, then use this as the source of translation data
# otherwise, rely on the system-wide data
locale_path = os.environ.get('LOCALEDIR', None)
# Init the list of languages to support
langs = []
#Check the default locale
lc, encoding = locale.getdefaultlocale()
if (lc):
#If we have a default, it's the first in the list
langs = [lc]
# Now let's get all of the supported languages on the system
language = os.environ.get('LANGUAGE', None)
if (language):
# langage comes back something like en_CA:en_US:en_GB:en
langs += language.split(":")
# add on to the back of the list the translations that we know that we have, our defaults
langs += ["en_US"]
# Now langs is a list of all of the languages that we are going
# to try to use. First we check the default, then what the system
# told us, and finally the 'known' list
gettext.bindtextdomain(config.APP_NAME, locale_path)
gettext.textdomain(config.APP_NAME)
# Get the language to use
lang = gettext.translation(config.APP_NAME, locale_path, languages=langs, fallback = True)
# Install the language, map _() (which we marked our
# strings to translate with) to self.lang.gettext() which will
# translate them.
_ = lang.gettext
def pythonifyVersion(v):
""" makes version number a version number in distutils sense"""
return distutils.version.StrictVersion(v.replace( '~',''))
def getFullProgramName():
""" return the full name of the process running """
return os.path.basename(sys.argv[0])
def getProgramName():
""" return the name of the process running, removing the .py extension if it exists """
programName = getFullProgramName()
if programName.find('.py') > 0:
programName = programName[:programName.find('.py')]
return programName
def splitDirectories(directories):
""" split directories specified in string into a list """
if directories.find(',') > 0:
d = directories.split(',')
else:
d = directories.split()
directories = []
for i in d:
directories.append(i.strip())
return directories
def getFullPath(path):
""" make path relative to home directory if not an absolute path """
if os.path.isabs(path):
return path
else:
return os.path.join(os.path.expanduser('~'), path)
def escape(s):
"""
Replace special characters by SGML entities.
"""
entities = ("&&", "<<", ">>")
for e in entities:
s = s.replace(e[0], e[1:])
return s
def formatSizeForUser(bytes, zeroString="", withDecimals=True, kbOnly=False):
"""Format an int containing the number of bytes into a string suitable for
printing out to the user. zeroString is the string to use if bytes == 0.
source: https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/util.py?rev=3993
"""
if bytes > (1 << 30) and not kbOnly:
value = (bytes / (1024.0 * 1024.0 * 1024.0))
if withDecimals:
format = "%1.1fGB"
else:
format = "%dGB"
elif bytes > (1 << 20) and not kbOnly:
value = (bytes / (1024.0 * 1024.0))
if withDecimals:
format = "%1.1fMB"
else:
format = "%dMB"
elif bytes > (1 << 10):
value = (bytes / 1024.0)
if withDecimals:
format = "%1.1fKB"
else:
format = "%dKB"
elif bytes > 1:
value = bytes
if withDecimals:
format = "%1.1fB"
else:
format = "%dB"
else:
return zeroString
return format % value
def scale2pixbuf(width_max, height_max, pixbuf, return_size=False):
"""
Scale to width_max and height_max.
Keep aspect ratio.
Code adapted from gthumpy, by guettli
"""
width_orig = float(pixbuf.get_width())
height_orig = float(pixbuf.get_height())
if (width_orig / width_max) > (height_orig / height_max):
height = int((height_orig / width_orig) * width_max)
width = width_max
else:
width = int((width_orig / height_orig) * height_max)
height=height_max
pixbuf = pixbuf.scale_simple(width, height, gdk.INTERP_BILINEAR)
gc.collect() # Tell Python to clean up the memory
if return_size:
return pixbuf, width_orig, height_orig
return pixbuf
if __name__ == '__main__':
i = Configi18n()
_ = i._
print _("hello world")
|