#
# BusyB, an automated build utility.
#
# Copyright (C) 1997-2005
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# Parts of this file were derived from the posixpath.py which is distributed
# with python.
#
# Class to expand $(var) in a string given
# a table.
#
import locale
import re
class Expander:
def __init__(self, table, marker='$'):
self.varprog = None
self.table = table
self.marker = marker
self.varprog = re.compile('\\' + marker + "\(([a-zA-z0-9_]+)\)" )
def expand(self,text):
"""Expand shell variables of form $(var) Unknown variables
are left unchanged."""
if self.marker not in text:
return text
i = 0
while 1:
m = self.varprog.search(text, i)
if not m:
break
i, j = m.span(0)
name = m.group(1)
if name[:1] == '{' and name[-1:] == '}':
name = name[1:-1]
if self.table.has_key(name):
tail = text[j:]
text = text[:i] + self.table[name]
i = len( text)
text = text + tail
else:
i = j
return text
|