##// END OF EJS Templates
Allow 2033 to get literal $ in shell commands.
Thomas Kluyver -
Show More
@@ -107,3 +107,5 b' def test_dollar_formatter():'
107 107 nt.assert_equals(s, "12")
108 108 s = f.format("$n/{stuff[:5]}", **ns)
109 109 nt.assert_equals(s, "12/hello")
110 s = f.format("$n $$HOME", **ns)
111 nt.assert_equals(s, "12 $HOME")
@@ -663,20 +663,26 b' class DollarFormatter(FullEvalFormatter):'
663 663 In [4]: f.format('$a or {b}', a=1, b=2)
664 664 Out[4]: u'1 or 2'
665 665 """
666 _dollar_pattern = re.compile("(.*)\$([\w\.]+)")
666 _dollar_pattern = re.compile("(.*?)\$(\$?[\w\.]+)")
667 667 def parse(self, fmt_string):
668 668 for literal_txt, field_name, format_spec, conversion \
669 669 in Formatter.parse(self, fmt_string):
670 670
671 671 # Find $foo patterns in the literal text.
672 672 continue_from = 0
673 txt = ""
673 674 for m in self._dollar_pattern.finditer(literal_txt):
674 675 new_txt, new_field = m.group(1,2)
675 yield (new_txt, new_field, "", None)
676 # $$foo --> $foo
677 if new_field.startswith("$"):
678 txt += new_txt + new_field
679 else:
680 yield (txt + new_txt, new_field, "", None)
681 txt = ""
676 682 continue_from = m.end()
677 683
678 684 # Re-yield the {foo} style pattern
679 yield (literal_txt[continue_from:], field_name, format_spec, conversion)
685 yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion)
680 686
681 687
682 688 def columnize(items, separator=' ', displaywidth=80):
General Comments 0
You need to be logged in to leave comments. Login now