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
23 # Windows lacks all sorts of subprocess features that we need to kludge around
24 if sys
.platform
.startswith('win'):
25 from jhbuild
.utils
import subprocess_win32
26 sys
.modules
['subprocess'] = subprocess_win32
28 # Python < 2.4 lacks reversed() builtin
29 if not hasattr(__builtin__
, 'reversed'):
34 __builtin__
.reversed = reversed
36 # Python < 2.4 lacks string.Template class
38 if not hasattr(string
, 'Template'):
42 """Helper class for combining multiple mappings.
44 Used by .{safe_,}substitute() to combine the mapping and keyword
47 def __init__(self
, primary
, secondary
):
48 self
._primary
= primary
49 self
._secondary
= secondary
51 def __getitem__(self
, key
):
53 return self
._primary
[key
]
55 return self
._secondary
[key
]
58 class _TemplateMetaclass(type):
61 (?P<escaped>%(delim)s) | # Escape sequence of two delimiters
62 (?P<named>%(id)s) | # delimiter and a Python identifier
63 {(?P<braced>%(id)s)} | # delimiter and a braced identifier
64 (?P<invalid>) # Other ill-formed delimiter exprs
68 def __init__(cls
, name
, bases
, dct
):
69 super(_TemplateMetaclass
, cls
).__init
__(name
, bases
, dct
)
73 pattern
= _TemplateMetaclass
.pattern
% {
74 'delim' : _re
.escape(cls
.delimiter
),
77 cls
.pattern
= _re
.compile(pattern
, _re
.IGNORECASE | _re
.VERBOSE
)
81 """A string class for supporting $-substitutions."""
82 __metaclass__
= _TemplateMetaclass
86 idpattern
= r
'[_a-z][_a-z0-9]*'
88 def __init__(self
, template
):
89 self
.template
= template
91 # Search for $$, $identifier, ${identifier}, and any bare $'s
93 def _invalid(self
, mo
):
94 i
= mo
.start('invalid')
95 lines
= self
.template
[:i
].splitlines(True)
100 colno
= i
- len(''.join(lines
[:-1]))
102 raise ValueError(_('Invalid placeholder in string: line %d, col %d') %
105 def substitute(self
, *args
, **kws
):
107 raise TypeError(_('Too many positional arguments'))
111 mapping
= _multimap(kws
, args
[0])
114 # Helper function for .sub()
116 # Check the most common path first.
117 named
= mo
.group('named') or mo
.group('braced')
118 if named
is not None:
120 # We use this idiom instead of str() because the latter will
121 # fail if val is a Unicode containing non-ASCII characters.
123 if mo
.group('escaped') is not None:
124 return self
.delimiter
125 if mo
.group('invalid') is not None:
127 raise ValueError(_('Unrecognized named group in pattern'),
129 return self
.pattern
.sub(convert
, self
.template
)
131 def safe_substitute(self
, *args
, **kws
):
133 raise TypeError(_('Too many positional arguments'))
137 mapping
= _multimap(kws
, args
[0])
140 # Helper function for .sub()
142 named
= mo
.group('named')
143 if named
is not None:
145 # We use this idiom instead of str() because the latter
146 # will fail if val is a Unicode containing non-ASCII
147 return '%s' % mapping
[named
]
149 return self
.delimiter
+ named
150 braced
= mo
.group('braced')
151 if braced
is not None:
153 return '%s' % mapping
[braced
]
155 return self
.delimiter
+ '{' + braced
+ '}'
156 if mo
.group('escaped') is not None:
157 return self
.delimiter
158 if mo
.group('invalid') is not None:
159 return self
.delimiter
160 raise ValueError(_('Unrecognized named group in pattern'),
162 return self
.pattern
.sub(convert
, self
.template
)
164 string
.Template
= Template
166 # Python < 2.4 lacks subprocess module
170 from jhbuild
.cut_n_paste
import subprocess
171 sys
.modules
['subprocess'] = subprocess