1 # jhbuild - a build script for GNOME 1.x and 2.x
2 # Copyright (C) 2001-2006 James Henstridge
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # extras not found in old versions of Python
20 from __future__
import generators
24 # Python < 2.2.1 lacks True and False constants
26 if not hasattr(__builtin__
, 'True'):
27 __builtin__
.True = (1 == 1)
28 __builtin__
.False = (1 != 1)
30 # Python < 2.3 lacks enumerate() builtin
31 if not hasattr(__builtin__
, 'enumerate'):
32 def enumerate(iterable
):
37 __builtin__
.enumerate = enumerate
39 # Python < 2.3 lacks optparse module
43 from jhbuild
.cut_n_paste
import optparse
44 sys
.modules
['optparse'] = optparse
46 # Python < 2.3 lacks locale.getpreferredencoding() function
48 if not hasattr(locale
, 'getpreferredencoding'):
52 # Fall back to parsing environment variables :-(
53 def getpreferredencoding(do_setlocale
= True):
54 """Return the charset that the user is likely using,
55 by looking at environment variables."""
56 return locale
.getdefaultlocale()[1]
58 def getpreferredencoding(do_setlocale
= True):
59 """Return the charset that the user is likely using,
60 according to the system configuration."""
62 oldloc
= locale
.setlocale(locale
.LC_CTYPE
)
63 locale
.setlocale(locale
.LC_CTYPE
, "")
64 result
= locale
.nl_langinfo(locale
.CODESET
)
65 locale
.setlocale(locale
.LC_CTYPE
, oldloc
)
68 return locale
.nl_langinfo(locale
.CODESET
)
70 # Python < 2.4 lacks reversed() builtin
71 if not hasattr(__builtin__
, 'reversed'):
76 __builtin__
.reversed = reversed
78 # Python < 2.4 lacks string.Template class
80 if not hasattr(string
, 'Template'):
84 """Helper class for combining multiple mappings.
86 Used by .{safe_,}substitute() to combine the mapping and keyword
89 def __init__(self
, primary
, secondary
):
90 self
._primary
= primary
91 self
._secondary
= secondary
93 def __getitem__(self
, key
):
95 return self
._primary
[key
]
97 return self
._secondary
[key
]
100 class _TemplateMetaclass(type):
103 (?P<escaped>%(delim)s) | # Escape sequence of two delimiters
104 (?P<named>%(id)s) | # delimiter and a Python identifier
105 {(?P<braced>%(id)s)} | # delimiter and a braced identifier
106 (?P<invalid>) # Other ill-formed delimiter exprs
110 def __init__(cls
, name
, bases
, dct
):
111 super(_TemplateMetaclass
, cls
).__init
__(name
, bases
, dct
)
113 pattern
= cls
.pattern
115 pattern
= _TemplateMetaclass
.pattern
% {
116 'delim' : _re
.escape(cls
.delimiter
),
117 'id' : cls
.idpattern
,
119 cls
.pattern
= _re
.compile(pattern
, _re
.IGNORECASE | _re
.VERBOSE
)
123 """A string class for supporting $-substitutions."""
124 __metaclass__
= _TemplateMetaclass
125 __module__
= 'string'
128 idpattern
= r
'[_a-z][_a-z0-9]*'
130 def __init__(self
, template
):
131 self
.template
= template
133 # Search for $$, $identifier, ${identifier}, and any bare $'s
135 def _invalid(self
, mo
):
136 i
= mo
.start('invalid')
137 lines
= self
.template
[:i
].splitlines(True)
142 colno
= i
- len(''.join(lines
[:-1]))
144 raise ValueError(_('Invalid placeholder in string: line %d, col %d') %
147 def substitute(self
, *args
, **kws
):
149 raise TypeError(_('Too many positional arguments'))
153 mapping
= _multimap(kws
, args
[0])
156 # Helper function for .sub()
158 # Check the most common path first.
159 named
= mo
.group('named') or mo
.group('braced')
160 if named
is not None:
162 # We use this idiom instead of str() because the latter will
163 # fail if val is a Unicode containing non-ASCII characters.
165 if mo
.group('escaped') is not None:
166 return self
.delimiter
167 if mo
.group('invalid') is not None:
169 raise ValueError(_('Unrecognized named group in pattern'),
171 return self
.pattern
.sub(convert
, self
.template
)
173 def safe_substitute(self
, *args
, **kws
):
175 raise TypeError(_('Too many positional arguments'))
179 mapping
= _multimap(kws
, args
[0])
182 # Helper function for .sub()
184 named
= mo
.group('named')
185 if named
is not None:
187 # We use this idiom instead of str() because the latter
188 # will fail if val is a Unicode containing non-ASCII
189 return '%s' % mapping
[named
]
191 return self
.delimiter
+ named
192 braced
= mo
.group('braced')
193 if braced
is not None:
195 return '%s' % mapping
[braced
]
197 return self
.delimiter
+ '{' + braced
+ '}'
198 if mo
.group('escaped') is not None:
199 return self
.delimiter
200 if mo
.group('invalid') is not None:
201 return self
.delimiter
202 raise ValueError(_('Unrecognized named group in pattern'),
204 return self
.pattern
.sub(convert
, self
.template
)
206 string
.Template
= Template
208 # Python < 2.4 lacks subprocess module
212 from jhbuild
.cut_n_paste
import subprocess
213 sys
.modules
['subprocess'] = subprocess