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 |