MCPcopy Create free account
hub / github.com/webpy/webpy / _interpolate

Function _interpolate

web/db.py:1493–1560  ·  view source on GitHub ↗

Takes a format string and returns a list of 2-tuples of the form (boolean, string) where boolean says whether string should be evaled or not. from (public domain, Ka-Ping Yee)

(format)

Source from the content-addressed store, hash-verified

1491
1492
1493def _interpolate(format):
1494 """
1495 Takes a format string and returns a list of 2-tuples of the form
1496 (boolean, string) where boolean says whether string should be evaled
1497 or not.
1498
1499 from <http://lfw.org/python/Itpl.py> (public domain, Ka-Ping Yee)
1500 """
1501
1502 def matchorfail(text, pos):
1503 match = tokenprog.match(text, pos)
1504 if match is None:
1505 raise _ItplError(text, pos)
1506 return match, match.end()
1507
1508 namechars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
1509 chunks = []
1510 pos = 0
1511
1512 while 1:
1513 dollar = format.find("$", pos)
1514 if dollar < 0:
1515 break
1516 nextchar = format[dollar + 1]
1517
1518 if nextchar == "{":
1519 chunks.append((0, format[pos:dollar]))
1520 pos, level = dollar + 2, 1
1521 while level:
1522 match, pos = matchorfail(format, pos)
1523 tstart, tend = match.regs[3]
1524 token = format[tstart:tend]
1525 if token == "{":
1526 level = level + 1
1527 elif token == "}":
1528 level = level - 1
1529 chunks.append((1, format[dollar + 2 : pos - 1]))
1530
1531 elif nextchar in namechars:
1532 chunks.append((0, format[pos:dollar]))
1533 match, pos = matchorfail(format, dollar + 1)
1534 while pos < len(format):
1535 if (
1536 format[pos] == "."
1537 and pos + 1 < len(format)
1538 and format[pos + 1] in namechars
1539 ):
1540 match, pos = matchorfail(format, pos + 1)
1541 elif format[pos] in "([":
1542 pos, level = pos + 1, 1
1543 while level:
1544 match, pos = matchorfail(format, pos)
1545 tstart, tend = match.regs[3]
1546 token = format[tstart:tend]
1547 if token[0] in "([":
1548 level = level + 1
1549 elif token[0] in ")]":
1550 level = level - 1

Callers

nothing calls this directly

Calls 2

matchorfailFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected