Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 13392:777cef34a890
dispatch: support for $ escaping in shell-alias definition
Sigils in shell-alias can be escaped by doubling them.
author | Roman Sokolov <sokolov.r.v@gmail.com> |
---|---|
date | Fri, 11 Feb 2011 03:32:40 +0300 |
parents | f1fa8f481c7c |
children | 14f3795a5ed7 |
comparison
equal
deleted
inserted
replaced
13391:d00bbff8600e | 13392:777cef34a890 |
---|---|
1500 for i in iterable: | 1500 for i in iterable: |
1501 if not i: | 1501 if not i: |
1502 return False | 1502 return False |
1503 return True | 1503 return True |
1504 | 1504 |
1505 def interpolate(prefix, mapping, s, fn=None): | 1505 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
1506 """Return the result of interpolating items in the mapping into string s. | 1506 """Return the result of interpolating items in the mapping into string s. |
1507 | 1507 |
1508 prefix is a single character string, or a two character string with | 1508 prefix is a single character string, or a two character string with |
1509 a backslash as the first character if the prefix needs to be escaped in | 1509 a backslash as the first character if the prefix needs to be escaped in |
1510 a regular expression. | 1510 a regular expression. |
1511 | 1511 |
1512 fn is an optional function that will be applied to the replacement text | 1512 fn is an optional function that will be applied to the replacement text |
1513 just before replacement. | 1513 just before replacement. |
1514 | |
1515 escape_prefix is an optional flag that allows using doubled prefix for | |
1516 its escaping. | |
1514 """ | 1517 """ |
1515 fn = fn or (lambda s: s) | 1518 fn = fn or (lambda s: s) |
1516 r = re.compile(r'%s(%s)' % (prefix, '|'.join(mapping.keys()))) | 1519 patterns = '|'.join(mapping.keys()) |
1520 if escape_prefix: | |
1521 patterns += '|' + prefix | |
1522 if len(prefix) > 1: | |
1523 prefix_char = prefix[1:] | |
1524 else: | |
1525 prefix_char = prefix | |
1526 mapping[prefix_char] = prefix_char | |
1527 r = re.compile(r'%s(%s)' % (prefix, patterns)) | |
1517 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) | 1528 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
1518 | 1529 |
1519 def getport(port): | 1530 def getport(port): |
1520 """Return the port for a given network service. | 1531 """Return the port for a given network service. |
1521 | 1532 |